mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
created metrics func
This commit is contained in:
27
api/runner/metrics.go
Normal file
27
api/runner/metrics.go
Normal 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 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()
|
||||||
|
}
|
||||||
@@ -105,13 +105,12 @@ func handleRunner(c *gin.Context) {
|
|||||||
|
|
||||||
log.WithField("routes", routes).Debug("Got routes from datastore")
|
log.WithField("routes", routes).Debug("Got routes from datastore")
|
||||||
for _, el := range routes {
|
for _, el := range routes {
|
||||||
metricBaseName := "server.handleRunner." + appName + "."
|
|
||||||
log = log.WithFields(logrus.Fields{
|
log = log.WithFields(logrus.Fields{
|
||||||
"app": appName, "route": el.Path, "image": el.Image, "request_id": reqID})
|
"app": appName, "route": el.Path, "image": el.Image, "request_id": reqID})
|
||||||
|
|
||||||
// Request count metric
|
// Request count metric
|
||||||
log.WithFields(logrus.Fields{
|
metricBaseName := "server.handleRunner." + appName + "."
|
||||||
"metric": (metricBaseName + "requests"), "type": "count", "value": 1}).Info()
|
runner.LogMetricCount(ctx, (metricBaseName + "requests"), 1)
|
||||||
|
|
||||||
if params, match := matchRoute(el.Path, route); match {
|
if params, match := matchRoute(el.Path, route); match {
|
||||||
|
|
||||||
@@ -166,22 +165,19 @@ func handleRunner(c *gin.Context) {
|
|||||||
|
|
||||||
if result.Status() == "success" {
|
if result.Status() == "success" {
|
||||||
c.Data(http.StatusOK, "", stdout.Bytes())
|
c.Data(http.StatusOK, "", stdout.Bytes())
|
||||||
log.WithFields(logrus.Fields{
|
runner.LogMetricCount(ctx, (metricBaseName + "succeeded"), 1)
|
||||||
"metric": (metricBaseName + "succeeded"), "type": "count", "value": 1}).Info()
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// log.WithFields(logrus.Fields{"app": appName, "route": el, "req_id": reqID}).Debug(stderr.String())
|
// log.WithFields(logrus.Fields{"app": appName, "route": el, "req_id": reqID}).Debug(stderr.String())
|
||||||
// Error count metric
|
// Error count metric
|
||||||
log.WithFields(logrus.Fields{
|
runner.LogMetricCount(ctx, (metricBaseName + "error"), 1)
|
||||||
"metric": (metricBaseName + "error"), "type": "count", "value": 1}).Info()
|
|
||||||
|
|
||||||
c.AbortWithStatus(http.StatusInternalServerError)
|
c.AbortWithStatus(http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Execution time metric
|
// Execution time metric
|
||||||
metricElapsed := time.Since(metricStart)
|
metricElapsed := time.Since(metricStart)
|
||||||
log.WithFields(logrus.Fields{
|
runner.LogMetricTime(ctx, (metricBaseName + "time"), metricElapsed)
|
||||||
"metric": (metricBaseName + "time"), "type": "time", "value": metricElapsed}).Info()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user