Use default namespace in metrics exporter

Signed-off-by: Vivek Singh <vivekkmr45@yahoo.in>
This commit is contained in:
Vivek Singh 2020-03-22 23:23:50 +05:30 committed by Alex Ellis
parent f7b02b47f8
commit c0ba41ce33
4 changed files with 16 additions and 14 deletions

View File

@ -51,7 +51,7 @@ type PrometheusFunctionNotifier struct {
func (p PrometheusFunctionNotifier) Notify(method string, URL string, originalURL string, statusCode int, event string, duration time.Duration) { func (p PrometheusFunctionNotifier) Notify(method string, URL string, originalURL string, statusCode int, event string, duration time.Duration) {
serviceName := getServiceName(originalURL) serviceName := getServiceName(originalURL)
if len(p.FunctionNamespace) > 0 { if len(p.FunctionNamespace) > 0 {
if index := strings.Index(serviceName, "."); index == -1 { if !strings.Contains(serviceName, ".") {
serviceName = fmt.Sprintf("%s.%s", serviceName, p.FunctionNamespace) serviceName = fmt.Sprintf("%s.%s", serviceName, p.FunctionNamespace)
} }
} }

View File

@ -65,7 +65,7 @@ func main() {
fmt.Println(metadataQuery) fmt.Println(metadataQuery)
metricsOptions := metrics.BuildMetricsOptions() metricsOptions := metrics.BuildMetricsOptions()
exporter := metrics.NewExporter(metricsOptions, credentials) exporter := metrics.NewExporter(metricsOptions, credentials, config.Namespace)
exporter.StartServiceWatcher(*config.FunctionsProviderURL, metricsOptions, "func", servicePollInterval) exporter.StartServiceWatcher(*config.FunctionsProviderURL, metricsOptions, "func", servicePollInterval)
metrics.RegisterExporter(exporter) metrics.RegisterExporter(exporter)

View File

@ -23,17 +23,19 @@ import (
// Exporter is a prometheus exporter // Exporter is a prometheus exporter
type Exporter struct { type Exporter struct {
metricOptions MetricOptions metricOptions MetricOptions
services []types.FunctionStatus services []types.FunctionStatus
credentials *auth.BasicAuthCredentials credentials *auth.BasicAuthCredentials
FunctionNamespace string
} }
// NewExporter creates a new exporter for the OpenFaaS gateway metrics // 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{ return &Exporter{
metricOptions: options, metricOptions: options,
services: []types.FunctionStatus{}, services: []types.FunctionStatus{},
credentials: credentials, credentials: credentials,
FunctionNamespace: namespace,
} }
} }
@ -91,8 +93,7 @@ func (e *Exporter) StartServiceWatcher(endpointURL url.URL, metricsOptions Metri
} }
if len(namespaces) == 0 { if len(namespaces) == 0 {
emptyNamespace := "" services, err := e.getFunctions(endpointURL, e.FunctionNamespace)
services, err := e.getFunctions(endpointURL, emptyNamespace)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
continue continue
@ -170,8 +171,9 @@ func (e *Exporter) getFunctions(endpointURL url.URL, namespace string) ([]types.
func (e *Exporter) getNamespaces(endpointURL url.URL) ([]string, error) { func (e *Exporter) getNamespaces(endpointURL url.URL) ([]string, error) {
namespaces := []string{} 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 { if e.credentials != nil {
get.SetBasicAuth(e.credentials.User, e.credentials.Password) get.SetBasicAuth(e.credentials.User, e.credentials.Password)
} }

View File

@ -33,7 +33,7 @@ func readGauge(g prometheus.Metric) metricResult {
func Test_Describe_DescribesThePrometheusMetrics(t *testing.T) { func Test_Describe_DescribesThePrometheusMetrics(t *testing.T) {
metricsOptions := BuildMetricsOptions() metricsOptions := BuildMetricsOptions()
exporter := NewExporter(metricsOptions, nil) exporter := NewExporter(metricsOptions, nil, "openfaas-fn")
ch := make(chan *prometheus.Desc) ch := make(chan *prometheus.Desc)
// defer close(ch) // defer close(ch)
@ -65,7 +65,7 @@ func Test_Describe_DescribesThePrometheusMetrics(t *testing.T) {
func Test_Collect_CollectsTheNumberOfReplicasOfAService(t *testing.T) { func Test_Collect_CollectsTheNumberOfReplicasOfAService(t *testing.T) {
metricsOptions := BuildMetricsOptions() metricsOptions := BuildMetricsOptions()
exporter := NewExporter(metricsOptions, nil) exporter := NewExporter(metricsOptions, nil, "openfaas-fn")
expectedService := types.FunctionStatus{ expectedService := types.FunctionStatus{
Name: "function_with_two_replica", Name: "function_with_two_replica",