From 49afdf5f3527073b39f3471b0be6f44a7ecdbcba Mon Sep 17 00:00:00 2001 From: Henrique Chehad Date: Tue, 6 Sep 2016 16:41:11 -0300 Subject: [PATCH] created metrics func --- api/runner/metrics.go | 27 +++++++++++++++++++++++++++ api/server/runner.go | 14 +++++--------- 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 api/runner/metrics.go diff --git a/api/runner/metrics.go b/api/runner/metrics.go new file mode 100644 index 000000000..7e0a52828 --- /dev/null +++ b/api/runner/metrics.go @@ -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 LogMetricGauge(ctx context.Context, name string, value int) { + log := titancommon.Logger(ctx) + log.WithFields(logrus.Fields{ + "metric": name, "type": "count", "value": value}).Info() +} + +func LogMetricCount(ctx context.Context, name string, value int) { + log := titancommon.Logger(ctx) + log.WithFields(logrus.Fields{ + "metric": name, "type": "count", "value": value}).Info() +} + +func LogMetricTime(ctx context.Context, name string, time time.Duration) { + log := titancommon.Logger(ctx) + log.WithFields(logrus.Fields{ + "metric": name, "type": "time", "value": time}).Info() +} diff --git a/api/server/runner.go b/api/server/runner.go index 5032b0427..5f9ff5b73 100644 --- a/api/server/runner.go +++ b/api/server/runner.go @@ -105,13 +105,12 @@ func handleRunner(c *gin.Context) { log.WithField("routes", routes).Debug("Got routes from datastore") for _, el := range routes { - metricBaseName := "server.handleRunner." + appName + "." log = log.WithFields(logrus.Fields{ "app": appName, "route": el.Path, "image": el.Image, "request_id": reqID}) // Request count metric - log.WithFields(logrus.Fields{ - "metric": (metricBaseName + "requests"), "type": "count", "value": 1}).Info() + metricBaseName := "server.handleRunner." + appName + "." + runner.LogMetricCount(ctx, (metricBaseName + "requests"), 1) if params, match := matchRoute(el.Path, route); match { @@ -166,22 +165,19 @@ func handleRunner(c *gin.Context) { if result.Status() == "success" { c.Data(http.StatusOK, "", stdout.Bytes()) - log.WithFields(logrus.Fields{ - "metric": (metricBaseName + "succeeded"), "type": "count", "value": 1}).Info() + runner.LogMetricCount(ctx, (metricBaseName + "succeeded"), 1) } else { // log.WithFields(logrus.Fields{"app": appName, "route": el, "req_id": reqID}).Debug(stderr.String()) // Error count metric - log.WithFields(logrus.Fields{ - "metric": (metricBaseName + "error"), "type": "count", "value": 1}).Info() + runner.LogMetricCount(ctx, (metricBaseName + "error"), 1) c.AbortWithStatus(http.StatusInternalServerError) } } // Execution time metric metricElapsed := time.Since(metricStart) - log.WithFields(logrus.Fields{ - "metric": (metricBaseName + "time"), "type": "time", "value": metricElapsed}).Info() + runner.LogMetricTime(ctx, (metricBaseName + "time"), metricElapsed) return } }