mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
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 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)
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user