mirror of
https://github.com/openfaas/faas.git
synced 2025-06-11 09:46:48 +00:00
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:
parent
5a1bdcdb91
commit
fca32a0e79
@ -37,6 +37,7 @@ func NewExporter(options MetricOptions, credentials *auth.BasicAuthCredentials)
|
|||||||
|
|
||||||
// Describe is to describe the metrics for Prometheus
|
// Describe is to describe the metrics for Prometheus
|
||||||
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
|
||||||
|
|
||||||
e.metricOptions.GatewayFunctionInvocation.Describe(ch)
|
e.metricOptions.GatewayFunctionInvocation.Describe(ch)
|
||||||
e.metricOptions.GatewayFunctionsHistogram.Describe(ch)
|
e.metricOptions.GatewayFunctionsHistogram.Describe(ch)
|
||||||
e.metricOptions.ServiceReplicasGauge.Describe(ch)
|
e.metricOptions.ServiceReplicasGauge.Describe(ch)
|
||||||
|
@ -40,24 +40,27 @@ func Test_Describe_DescribesThePrometheusMetrics(t *testing.T) {
|
|||||||
|
|
||||||
go exporter.Describe(ch)
|
go exporter.Describe(ch)
|
||||||
|
|
||||||
d := (<-ch)
|
d := <-ch
|
||||||
expectedGatewayFunctionInvocationDesc := `Desc{fqName: "gateway_function_invocation_total", help: "Individual function metrics", constLabels: {}, variableLabels: [function_name code]}`
|
expectedGatewayFunctionInvocationDesc := `Desc{fqName: "gateway_function_invocation_total", help: "Individual function metrics", constLabels: {}, variableLabels: [function_name code]}`
|
||||||
actualGatewayFunctionInvocationDesc := d.String()
|
actualGatewayFunctionInvocationDesc := d.String()
|
||||||
if expectedGatewayFunctionInvocationDesc != actualGatewayFunctionInvocationDesc {
|
if expectedGatewayFunctionInvocationDesc != actualGatewayFunctionInvocationDesc {
|
||||||
t.Errorf("Want %s, got: %s", 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]}`
|
expectedGatewayFunctionsHistogramDesc := `Desc{fqName: "gateway_functions_seconds", help: "Function time taken", constLabels: {}, variableLabels: [function_name]}`
|
||||||
actualGatewayFunctionsHistogramDesc := d.String()
|
actualGatewayFunctionsHistogramDesc := d.String()
|
||||||
if expectedGatewayFunctionsHistogramDesc != actualGatewayFunctionsHistogramDesc {
|
if expectedGatewayFunctionsHistogramDesc != actualGatewayFunctionsHistogramDesc {
|
||||||
t.Errorf("Want %s, got: %s", 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]}`
|
expectedServiceReplicasGaugeDesc := `Desc{fqName: "gateway_service_count", help: "Docker service replicas", constLabels: {}, variableLabels: [function_name]}`
|
||||||
actualServiceReplicasGaugeDesc := d.String()
|
actualServiceReplicasGaugeDesc := d.String()
|
||||||
if expectedServiceReplicasGaugeDesc != actualServiceReplicasGaugeDesc {
|
if expectedServiceReplicasGaugeDesc != actualServiceReplicasGaugeDesc {
|
||||||
t.Errorf("Want %s, got: %s", expectedServiceReplicasGaugeDesc, actualServiceReplicasGaugeDesc)
|
t.Errorf("Want %s, got: %s", expectedServiceReplicasGaugeDesc, actualServiceReplicasGaugeDesc)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Collect_CollectsTheNumberOfReplicasOfAService(t *testing.T) {
|
func Test_Collect_CollectsTheNumberOfReplicasOfAService(t *testing.T) {
|
||||||
|
@ -5,6 +5,7 @@ package metrics
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
@ -24,6 +25,16 @@ type ServiceMetricOptions struct {
|
|||||||
Counter *prometheus.CounterVec
|
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
|
// PrometheusHandler Bootstraps prometheus for metrics collection
|
||||||
func PrometheusHandler() http.Handler {
|
func PrometheusHandler() http.Handler {
|
||||||
return promhttp.Handler()
|
return promhttp.Handler()
|
||||||
@ -84,8 +95,3 @@ func BuildMetricsOptions() MetricOptions {
|
|||||||
|
|
||||||
return metricsOptions
|
return metricsOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterExporter registers with Prometheus for tracking
|
|
||||||
func RegisterExporter(exporter *Exporter) {
|
|
||||||
prometheus.MustRegister(exporter)
|
|
||||||
}
|
|
||||||
|
@ -96,7 +96,10 @@ func main() {
|
|||||||
faasHandlers.SecretHandler = handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers, urlResolver, nilURLTransformer)
|
faasHandlers.SecretHandler = handlers.MakeForwardingProxyHandler(reverseProxy, forwardingNotifiers, urlResolver, nilURLTransformer)
|
||||||
|
|
||||||
alertHandler := plugin.NewExternalServiceQuery(*config.FunctionsProviderURL, credentials)
|
alertHandler := plugin.NewExternalServiceQuery(*config.FunctionsProviderURL, credentials)
|
||||||
faasHandlers.Alert = handlers.MakeNotifierWrapper(handlers.MakeAlertHandler(alertHandler), forwardingNotifiers)
|
faasHandlers.Alert = handlers.MakeNotifierWrapper(
|
||||||
|
handlers.MakeAlertHandler(alertHandler),
|
||||||
|
forwardingNotifiers,
|
||||||
|
)
|
||||||
|
|
||||||
if config.UseNATS() {
|
if config.UseNATS() {
|
||||||
log.Println("Async enabled: Using NATS Streaming.")
|
log.Println("Async enabled: Using NATS Streaming.")
|
||||||
@ -105,8 +108,15 @@ func main() {
|
|||||||
log.Fatalln(queueErr)
|
log.Fatalln(queueErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
faasHandlers.QueuedProxy = handlers.MakeCallIDMiddleware(handlers.MakeQueuedProxy(metricsOptions, true, natsQueue, functionURLTransformer))
|
faasHandlers.QueuedProxy = handlers.MakeNotifierWrapper(
|
||||||
faasHandlers.AsyncReport = handlers.MakeAsyncReport(metricsOptions)
|
handlers.MakeCallIDMiddleware(handlers.MakeQueuedProxy(metricsOptions, true, natsQueue, functionURLTransformer)),
|
||||||
|
forwardingNotifiers,
|
||||||
|
)
|
||||||
|
|
||||||
|
faasHandlers.AsyncReport = handlers.MakeNotifierWrapper(
|
||||||
|
handlers.MakeAsyncReport(metricsOptions),
|
||||||
|
forwardingNotifiers,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
prometheusQuery := metrics.NewPrometheusQuery(config.PrometheusHost, config.PrometheusPort, &http.Client{})
|
prometheusQuery := metrics.NewPrometheusQuery(config.PrometheusHost, config.PrometheusPort, &http.Client{})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user