Change basic stats to use opentracing rather than Prometheus API (#671)

* Change basic stats to use opentracing rather than Prometheus API directly

* Just ran gofmt

* Extract opentracing access for metrics to common/metrics.go

* Replace quotes strings with constants where possible
This commit is contained in:
Nigel Deakin
2018-01-11 17:34:51 +00:00
committed by GitHub
parent ba0aa3b1a9
commit ac2bfd3462
5 changed files with 361 additions and 118 deletions

View File

@@ -1,9 +1,9 @@
package agent
import (
"context"
"github.com/fnproject/fn/api/common"
"sync"
"github.com/prometheus/client_golang/prometheus"
)
// TODO this should expose:
@@ -30,8 +30,9 @@ type functionStats struct {
failed uint64
}
// Stats hold the statistics for all functions combined
// and the statistics for each individual function
type Stats struct {
// statistics for all functions combined
Queue uint64
Running uint64
Complete uint64
@@ -40,7 +41,7 @@ type Stats struct {
FunctionStatsMap map[string]*FunctionStats
}
// statistics for an individual function
// FunctionStats holds the statistics for an individual function
type FunctionStats struct {
Queue uint64
Running uint64
@@ -48,52 +49,6 @@ type FunctionStats struct {
Failed uint64
}
var (
fnCalls = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "fn_api_calls",
Help: "Function calls by app and path",
},
[](string){"app", "path"},
)
fnQueued = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fn_api_queued",
Help: "Queued requests by app and path",
},
[](string){"app", "path"},
)
fnRunning = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "fn_api_running",
Help: "Running requests by app and path",
},
[](string){"app", "path"},
)
fnCompleted = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "fn_api_completed",
Help: "Completed requests by app and path",
},
[](string){"app", "path"},
)
fnFailed = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "fn_api_failed",
Help: "Failed requests by path",
},
[](string){"app", "path"},
)
)
func init() {
prometheus.MustRegister(fnCalls)
prometheus.MustRegister(fnQueued)
prometheus.MustRegister(fnRunning)
prometheus.MustRegister(fnFailed)
prometheus.MustRegister(fnCompleted)
}
func (s *stats) getStatsForFunction(path string) *functionStats {
if s.functionStatsMap == nil {
s.functionStatsMap = make(map[string]*functionStats)
@@ -107,80 +62,81 @@ func (s *stats) getStatsForFunction(path string) *functionStats {
return thisFunctionStats
}
func (s *stats) Enqueue(app string, path string) {
func (s *stats) Enqueue(ctx context.Context, app string, path string) {
s.mu.Lock()
s.queue++
s.getStatsForFunction(path).queue++
fnQueued.WithLabelValues(app, path).Inc()
fnCalls.WithLabelValues(app, path).Inc()
common.IncrementGauge(ctx, queuedMetricName)
common.IncrementCounter(ctx, callsMetricName)
s.mu.Unlock()
}
// Call when a function has been queued but cannot be started because of an error
func (s *stats) Dequeue(app string, path string) {
func (s *stats) Dequeue(ctx context.Context, app string, path string) {
s.mu.Lock()
s.queue--
s.getStatsForFunction(path).queue--
fnQueued.WithLabelValues(app, path).Dec()
common.DecrementGauge(ctx, queuedMetricName)
s.mu.Unlock()
}
func (s *stats) DequeueAndStart(app string, path string) {
func (s *stats) DequeueAndStart(ctx context.Context, app string, path string) {
s.mu.Lock()
s.queue--
s.getStatsForFunction(path).queue--
fnQueued.WithLabelValues(app, path).Dec()
common.DecrementGauge(ctx, queuedMetricName)
s.running++
s.getStatsForFunction(path).running++
fnRunning.WithLabelValues(app, path).Inc()
common.IncrementGauge(ctx, runningSuffix)
s.mu.Unlock()
}
func (s *stats) Complete(app string, path string) {
func (s *stats) Complete(ctx context.Context, app string, path string) {
s.mu.Lock()
s.running--
s.getStatsForFunction(path).running--
fnRunning.WithLabelValues(app, path).Dec()
common.DecrementGauge(ctx, runningSuffix)
s.complete++
s.getStatsForFunction(path).complete++
fnCompleted.WithLabelValues(app, path).Inc()
common.IncrementCounter(ctx, completedMetricName)
s.mu.Unlock()
}
func (s *stats) Failed(app string, path string) {
func (s *stats) Failed(ctx context.Context, app string, path string) {
s.mu.Lock()
s.running--
s.getStatsForFunction(path).running--
fnRunning.WithLabelValues(app, path).Dec()
common.DecrementGauge(ctx, runningSuffix)
s.failed++
s.getStatsForFunction(path).failed++
fnFailed.WithLabelValues(app, path).Inc()
common.IncrementCounter(ctx, failedMetricName)
s.mu.Unlock()
}
func (s *stats) DequeueAndFail(app string, path string) {
func (s *stats) DequeueAndFail(ctx context.Context, app string, path string) {
s.mu.Lock()
s.queue--
s.getStatsForFunction(path).queue--
fnQueued.WithLabelValues(app, path).Dec()
common.DecrementGauge(ctx, queuedMetricName)
s.failed++
s.getStatsForFunction(path).failed++
fnFailed.WithLabelValues(app, path).Inc()
common.IncrementCounter(ctx, failedMetricName)
s.mu.Unlock()
}
@@ -200,3 +156,11 @@ func (s *stats) Stats() Stats {
s.mu.Unlock()
return stats
}
const (
queuedMetricName = "queued"
callsMetricName = "calls"
runningSuffix = "running"
completedMetricName = "completed"
failedMetricName = "failed"
)