Skip to content

Instantly share code, notes, and snippets.

// Metrics handler collects status metrics.
func StatusMetrics() gin.HandlerFunc {
return func(c *gin.Context) {
statusCode := c.Writer.Status()
metrics.GetOrRegisterMeter("call."+getHandlerName(c)+".Status."+strconv.Itoa(statusCode), nil).Mark(1)
}
}
router := gin.New()
router.Use(StatusMetrics()) // now we will collect metric for number of reples for a status
if logFile != nil {
router.Use(gin.LoggerWithWriter(logFile))
router.Use(gin.RecoveryWithWriter(logFile))
} else {
router.Use(gin.Logger())
router.Use(gin.Recovery())
}
router.Use(Metrics()) // now we will publish metrics for each API call
// create router and register basic handlers
router := gin.New()
if logFile != nil {
router.Use(gin.LoggerWithWriter(logFile))
router.Use(gin.RecoveryWithWriter(logFile))
} else {
router.Use(gin.Logger())
router.Use(gin.Recovery())
}
router.Use(Metrics()) // now we will publish metrics for each API call
@rkhmelyuk
rkhmelyuk / metrics_handler.go
Last active February 18, 2018 02:51
Metrics handler for Gin
// Metrics handler collects metrics about registered handlers:
// latency, requests count, request and response size etc.
func Metrics() gin.HandlerFunc {
return func(c *gin.Context) {
path := getHandlerName(c)
timerMetric := metrics.GetOrRegisterTimer("call."+path+".Latency", nil)
start := time.Now()
c.Next()
timerMetric.UpdateSince(start)
// create router and register basic handlers
router := gin.New()
if logFile != nil {
router.Use(gin.LoggerWithWriter(logFile))
router.Use(gin.RecoveryWithWriter(logFile))
} else {
router.Use(gin.Logger())
router.Use(gin.Recovery())
}
// create router and register basic handlers
router := gin.New()
if logFile != nil {
router.Use(gin.LoggerWithWriter(logFile))
router.Use(gin.RecoveryWithWriter(logFile))
} else {
router.Use(gin.Logger())
router.Use(gin.Recovery())
}
@rkhmelyuk
rkhmelyuk / publish_graphite_metrics.go
Created February 18, 2018 02:31
publish graphite metrics
// publish metrics every minute to graphite at 'graphite.mycompany.com:2003'
// 'application_name' is a prefix for this application metrics
addr, _ := net.ResolveTCPAddr("tcp", "graphite.mycompany.com:2003")
go graphite.Graphite(metrics.DefaultRegistry, 1*time.Minute, "application_name", addr)
final AggregatedLogData logData = new AggregatedLogData();
logData.setLevel(AggregatedLogLevel.FULL);
boolean error = false;
try {
// .. execute code here
} catch (Exception e) {
logData.collectException("Failed to process rules", e);
error = true;
} finally {
if (error) {
final AggregatedLogData logData = new AggregatedLogData();
logData.setLevel(AggregatedLogLevel.FULL);
try {
// .. execute code here
} catch (Exception e) {
log.error("Failed to process rules", e);
} finally {
log.info(logData.toJsonString());
}
...
2017-01-01 02:23:01 [Req#123] [INFO] [TrafficLightIsGreenRule] trafficLightState=green
...
2017-01-01 02:23:01 [Req#123] [INFO] [TrafficLightIsRedRule] trafficLightState=green
...