From c0ba41ce331de250a0d3b16b48d604d39e8e05ba Mon Sep 17 00:00:00 2001 From: Vivek Singh Date: Sun, 22 Mar 2020 23:23:50 +0530 Subject: [PATCH] Use default namespace in metrics exporter Signed-off-by: Vivek Singh --- gateway/handlers/notifiers.go | 2 +- gateway/main.go | 2 +- gateway/metrics/exporter.go | 22 ++++++++++++---------- gateway/metrics/exporter_test.go | 4 ++-- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/gateway/handlers/notifiers.go b/gateway/handlers/notifiers.go index f7a2c956..10fa2349 100644 --- a/gateway/handlers/notifiers.go +++ b/gateway/handlers/notifiers.go @@ -51,7 +51,7 @@ type PrometheusFunctionNotifier struct { func (p PrometheusFunctionNotifier) Notify(method string, URL string, originalURL string, statusCode int, event string, duration time.Duration) { serviceName := getServiceName(originalURL) if len(p.FunctionNamespace) > 0 { - if index := strings.Index(serviceName, "."); index == -1 { + if !strings.Contains(serviceName, ".") { serviceName = fmt.Sprintf("%s.%s", serviceName, p.FunctionNamespace) } } diff --git a/gateway/main.go b/gateway/main.go index 5f971684..19477285 100644 --- a/gateway/main.go +++ b/gateway/main.go @@ -65,7 +65,7 @@ func main() { fmt.Println(metadataQuery) metricsOptions := metrics.BuildMetricsOptions() - exporter := metrics.NewExporter(metricsOptions, credentials) + exporter := metrics.NewExporter(metricsOptions, credentials, config.Namespace) exporter.StartServiceWatcher(*config.FunctionsProviderURL, metricsOptions, "func", servicePollInterval) metrics.RegisterExporter(exporter) diff --git a/gateway/metrics/exporter.go b/gateway/metrics/exporter.go index e16c4392..c3e5c0a3 100644 --- a/gateway/metrics/exporter.go +++ b/gateway/metrics/exporter.go @@ -23,17 +23,19 @@ import ( // Exporter is a prometheus exporter type Exporter struct { - metricOptions MetricOptions - services []types.FunctionStatus - credentials *auth.BasicAuthCredentials + metricOptions MetricOptions + services []types.FunctionStatus + credentials *auth.BasicAuthCredentials + FunctionNamespace string } // NewExporter creates a new exporter for the OpenFaaS gateway metrics -func NewExporter(options MetricOptions, credentials *auth.BasicAuthCredentials) *Exporter { +func NewExporter(options MetricOptions, credentials *auth.BasicAuthCredentials, namespace string) *Exporter { return &Exporter{ - metricOptions: options, - services: []types.FunctionStatus{}, - credentials: credentials, + metricOptions: options, + services: []types.FunctionStatus{}, + credentials: credentials, + FunctionNamespace: namespace, } } @@ -91,8 +93,7 @@ func (e *Exporter) StartServiceWatcher(endpointURL url.URL, metricsOptions Metri } if len(namespaces) == 0 { - emptyNamespace := "" - services, err := e.getFunctions(endpointURL, emptyNamespace) + services, err := e.getFunctions(endpointURL, e.FunctionNamespace) if err != nil { log.Println(err) continue @@ -170,8 +171,9 @@ func (e *Exporter) getFunctions(endpointURL url.URL, namespace string) ([]types. func (e *Exporter) getNamespaces(endpointURL url.URL) ([]string, error) { namespaces := []string{} + endpointURL.Path = path.Join(endpointURL.Path, "system/namespaces") - get, _ := http.NewRequest(http.MethodGet, endpointURL.String()+"system/namespaces", nil) + get, _ := http.NewRequest(http.MethodGet, endpointURL.String(), nil) if e.credentials != nil { get.SetBasicAuth(e.credentials.User, e.credentials.Password) } diff --git a/gateway/metrics/exporter_test.go b/gateway/metrics/exporter_test.go index 93cd0390..069b5b8a 100644 --- a/gateway/metrics/exporter_test.go +++ b/gateway/metrics/exporter_test.go @@ -33,7 +33,7 @@ func readGauge(g prometheus.Metric) metricResult { func Test_Describe_DescribesThePrometheusMetrics(t *testing.T) { metricsOptions := BuildMetricsOptions() - exporter := NewExporter(metricsOptions, nil) + exporter := NewExporter(metricsOptions, nil, "openfaas-fn") ch := make(chan *prometheus.Desc) // defer close(ch) @@ -65,7 +65,7 @@ func Test_Describe_DescribesThePrometheusMetrics(t *testing.T) { func Test_Collect_CollectsTheNumberOfReplicasOfAService(t *testing.T) { metricsOptions := BuildMetricsOptions() - exporter := NewExporter(metricsOptions, nil) + exporter := NewExporter(metricsOptions, nil, "openfaas-fn") expectedService := types.FunctionStatus{ Name: "function_with_two_replica",