Instrument async handlers

- instruments async handler for report and for queueing async
requests
- make MustRegister only ever run once to prevent sync issues

Signed-off-by: Alex Ellis (VMware) <alexellis2@gmail.com>
This commit is contained in:
Alex Ellis (VMware)
2019-01-05 10:17:18 +00:00
committed by Alex Ellis
parent 5a1bdcdb91
commit fca32a0e79
5 changed files with 31 additions and 11 deletions

View File

@ -37,6 +37,7 @@ func NewExporter(options MetricOptions, credentials *auth.BasicAuthCredentials)
// Describe is to describe the metrics for Prometheus
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.metricOptions.GatewayFunctionInvocation.Describe(ch)
e.metricOptions.GatewayFunctionsHistogram.Describe(ch)
e.metricOptions.ServiceReplicasGauge.Describe(ch)

View File

@ -40,24 +40,27 @@ func Test_Describe_DescribesThePrometheusMetrics(t *testing.T) {
go exporter.Describe(ch)
d := (<-ch)
d := <-ch
expectedGatewayFunctionInvocationDesc := `Desc{fqName: "gateway_function_invocation_total", help: "Individual function metrics", constLabels: {}, variableLabels: [function_name code]}`
actualGatewayFunctionInvocationDesc := d.String()
if expectedGatewayFunctionInvocationDesc != actualGatewayFunctionInvocationDesc {
t.Errorf("Want %s, got: %s", expectedGatewayFunctionInvocationDesc, actualGatewayFunctionInvocationDesc)
}
d = (<-ch)
d = <-ch
expectedGatewayFunctionsHistogramDesc := `Desc{fqName: "gateway_functions_seconds", help: "Function time taken", constLabels: {}, variableLabels: [function_name]}`
actualGatewayFunctionsHistogramDesc := d.String()
if expectedGatewayFunctionsHistogramDesc != actualGatewayFunctionsHistogramDesc {
t.Errorf("Want %s, got: %s", expectedGatewayFunctionsHistogramDesc, actualGatewayFunctionsHistogramDesc)
}
d = (<-ch)
d = <-ch
expectedServiceReplicasGaugeDesc := `Desc{fqName: "gateway_service_count", help: "Docker service replicas", constLabels: {}, variableLabels: [function_name]}`
actualServiceReplicasGaugeDesc := d.String()
if expectedServiceReplicasGaugeDesc != actualServiceReplicasGaugeDesc {
t.Errorf("Want %s, got: %s", expectedServiceReplicasGaugeDesc, actualServiceReplicasGaugeDesc)
}
}
func Test_Collect_CollectsTheNumberOfReplicasOfAService(t *testing.T) {

View File

@ -5,6 +5,7 @@ package metrics
import (
"net/http"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
@ -24,6 +25,16 @@ type ServiceMetricOptions struct {
Counter *prometheus.CounterVec
}
// Synchronize to make sure MustRegister only called once
var once = sync.Once{}
// RegisterExporter registers with Prometheus for tracking
func RegisterExporter(exporter *Exporter) {
once.Do(func() {
prometheus.MustRegister(exporter)
})
}
// PrometheusHandler Bootstraps prometheus for metrics collection
func PrometheusHandler() http.Handler {
return promhttp.Handler()
@ -84,8 +95,3 @@ func BuildMetricsOptions() MetricOptions {
return metricsOptions
}
// RegisterExporter registers with Prometheus for tracking
func RegisterExporter(exporter *Exporter) {
prometheus.MustRegister(exporter)
}