Merge pull request #88 from iron-io/log-metrics

Log metrics
This commit is contained in:
Travis Reeder
2016-09-06 18:33:28 -07:00
committed by GitHub
2 changed files with 43 additions and 0 deletions

27
api/runner/metrics.go Normal file
View File

@@ -0,0 +1,27 @@
package runner
import (
"time"
"github.com/Sirupsen/logrus"
titancommon "github.com/iron-io/titan/common"
"golang.org/x/net/context"
)
func LogMetric(ctx context.Context, name string, metricType string, value interface{}) {
log := titancommon.Logger(ctx)
log.WithFields(logrus.Fields{
"metric": name, "type": metricType, "value": value}).Info()
}
func LogMetricGauge(ctx context.Context, name string, value int) {
LogMetric(ctx, name, "gauge", value)
}
func LogMetricCount(ctx context.Context, name string, value int) {
LogMetric(ctx, name, "count", value)
}
func LogMetricTime(ctx context.Context, name string, time time.Duration) {
LogMetric(ctx, name, "time", time)
}

View File

@@ -105,6 +105,13 @@ func handleRunner(c *gin.Context) {
log.WithField("routes", routes).Debug("Got routes from datastore")
for _, el := range routes {
log = log.WithFields(logrus.Fields{
"app": appName, "route": el.Path, "image": el.Image, "request_id": reqID})
// Request count metric
metricBaseName := "server.handleRunner." + appName + "."
runner.LogMetricCount(ctx, (metricBaseName + "requests"), 1)
if params, match := matchRoute(el.Path, route); match {
var stdout bytes.Buffer // TODO: should limit the size of this, error if gets too big. akin to: https://golang.org/pkg/io/#LimitReader
@@ -147,6 +154,7 @@ func handleRunner(c *gin.Context) {
Env: envVars,
}
metricStart := time.Now()
if result, err := Api.Runner.Run(c, cfg); err != nil {
log.WithError(err).Error(models.ErrRunnerRunRoute)
c.JSON(http.StatusInternalServerError, simpleError(models.ErrRunnerRunRoute))
@@ -157,11 +165,19 @@ func handleRunner(c *gin.Context) {
if result.Status() == "success" {
c.Data(http.StatusOK, "", stdout.Bytes())
runner.LogMetricCount(ctx, (metricBaseName + "succeeded"), 1)
} else {
// log.WithFields(logrus.Fields{"app": appName, "route": el, "req_id": reqID}).Debug(stderr.String())
// Error count metric
runner.LogMetricCount(ctx, (metricBaseName + "error"), 1)
c.AbortWithStatus(http.StatusInternalServerError)
}
}
// Execution time metric
metricElapsed := time.Since(metricStart)
runner.LogMetricTime(ctx, (metricBaseName + "time"), metricElapsed)
return
}
}