Skip to content

Commit c4e73d7

Browse files
authored
Merge pull request #20 from raffis/v1
allow custom metrics path #19
2 parents c05769b + 482bfa7 commit c4e73d7

File tree

16 files changed

+90
-27
lines changed

16 files changed

+90
-27
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# 1.0.0-beta7
2+
**Date**: Mon Jan 18 21:34:20 CET 2020
3+
4+
## Bugfixes
5+
* Fixes missing Path in ServiceMonitor helm chart
6+
7+
## Features
8+
* Support changeable metrics path #19
9+
10+
111
# 1.0.0-beta6
212
**Date**: Thu Nov 12 22:35:21 CET 2020
313

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ mongodb://CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry@loc
6565
```
6666

6767
## Access metrics
68-
The metrics are exposed at `/metrics`.
68+
The metrics are by default exposed at `/metrics`.
6969

7070
```
7171
curl localhost:9412/metrics
@@ -85,6 +85,7 @@ You may also use env variables to configure the exporter:
8585
| MDBEXPORTER_LOG_LEVEL | Log level |
8686
| MDBEXPORTER_LOG_ENCODING | Log format |
8787
| MDBEXPORTER_BIND | Bind address for the HTTP server |
88+
| MDBEXPORTER_METRICSPATH | Change the metrics path (/metrics) |
8889

8990
Note if you have multiple MongoDB servers you can inject an env variable for each instead using `MDBEXPORTER_MONGODB_URI`:
9091

chart/prometheus-mongodb-query-exporter/Chart.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
apiVersion: v2
2-
appVersion: 1.0.0-beta6
3-
description: A Prometheus exporter for MongoDB custom query/aggregations
2+
appVersion: 1.0.0-beta7
3+
description: A Prometheus exporter for MongoDB custom aggregations
44
home: https://github.com/raffis/mongodb-query-exporter
55
keywords:
66
- exporter
@@ -12,4 +12,4 @@ keywords:
1212
name: prometheus-mongodb-query-exporter
1313
sources:
1414
- https://github.com/raffis/mongodb-query-exporter
15-
version: 1.0.3
15+
version: 1.1.3

chart/prometheus-mongodb-query-exporter/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ populate the `podAnnotations` as below:
2929
podAnnotations:
3030
prometheus.io/scrape: "true"
3131
prometheus.io/port: "metrics"
32+
prometheus.io/path: "/metrics"
3233
```
3334
3435
## Configuration

chart/prometheus-mongodb-query-exporter/templates/NOTES.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ Verify the application is working by running these commands:
22
{{if contains "NodePort" .Values.service.type }}
33
NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
44
NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ include "prometheus-mongodb-query-exporter.fullname" . }})
5-
curl http://$NODE_IP:$NODE_PORT/metrics
5+
curl http://$NODE_IP:$NODE_PORT{{ .Values.metricsPath }}
66
{{- else if contains "LoadBalancer" .Values.service.type }}
77
# NOTE: It may take a few minutes for the LoadBalancer IP to be available.
88
SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "prometheus-mongodb-query-exporter.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
9-
curl http://$SERVICE_IP:{{ .Values.service.port }}/metrics
9+
curl http://$SERVICE_IP:{{ .Values.service.port }}{{ .Values.metricsPath }}
1010
{{- else if contains "ClusterIP" .Values.service.type }}
1111
kubectl port-forward service/{{ include "prometheus-mongodb-query-exporter.fullname" . }} {{ .Values.service.port }}
12-
curl http://127.0.0.1:{{ .Values.service.port }}/metrics
12+
curl http://127.0.0.1:{{ .Values.service.port }}{{ .Values.metricsPath }}
1313
{{- end }}

chart/prometheus-mongodb-query-exporter/templates/deployment.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ spec:
5656
imagePullPolicy: {{ .Values.image.pullPolicy }}
5757
args:
5858
- --bind={{ printf ":%s" .Values.port }}
59+
- --path={{ .Values.metricsPath }}
5960
{{- if .Values.extraArgs }}
6061
{{- toYaml .Values.extraArgs | nindent 8 }}
6162
{{- end }}

chart/prometheus-mongodb-query-exporter/templates/servicemonitor.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ metadata:
1919
spec:
2020
endpoints:
2121
- port: metrics
22+
path: {{ .Values.metricsPath }}
2223
interval: {{ .Values.serviceMonitor.interval }}
2324
scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }}
2425
{{- if .Values.serviceMonitor.metricRelabelings }}

chart/prometheus-mongodb-query-exporter/templates/tests/test-connection.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ spec:
1414
- name: wget
1515
image: busybox
1616
command: ['wget']
17-
args: ['-qO-', '{{ include "prometheus-mongodb-query-exporter.fullname" . }}:{{ .Values.service.port }}/metrics']
17+
args: ['-qO-', '{{ include "prometheus-mongodb-query-exporter.fullname" . }}:{{ .Values.service.port }}{{ .Values.metricsPath }}']
1818
restartPolicy: Never

chart/prometheus-mongodb-query-exporter/values.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fullnameOverride: ""
99
image:
1010
pullPolicy: IfNotPresent
1111
repository: raffis/mongodb-query-exporter
12-
tag: v1.0.0-beta6
12+
tag: v1.0.0-beta7
1313

1414
imagePullSecrets: []
1515

@@ -87,6 +87,9 @@ podAnnotations: {}
8787

8888
port: "9412"
8989

90+
# Change the metrics path
91+
metricsPath: /metrics
92+
9093
priorityClassName: ""
9194

9295
readinessProbe:

cmd/root.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"fmt"
45
"net/http"
56
"os"
67
"os/user"
@@ -23,6 +24,7 @@ var (
2324
logEncoding string
2425
bind string
2526
uri string
27+
metricsPath string
2628
queryTimeout int
2729
rootCmd = &cobra.Command{
2830
Use: "mongodb_query_exporter",
@@ -64,23 +66,25 @@ var (
6466

6567
prometheus.MustRegister(c)
6668
c.StartCacheInvalidator()
67-
serve(prometheus.DefaultGatherer, conf.GetBindAddr())
69+
serve(prometheus.DefaultGatherer, conf)
6870
},
6971
}
7072
)
7173

72-
// Run executes a blocking http server. Starts the http listener with the /metrics endpoint
73-
// and parses all configured metrics passed by config
74-
func serve(reg prometheus.Gatherer, addr string) {
75-
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
76-
http.Error(w, "Use the /metrics endpoint", http.StatusOK)
77-
})
74+
// Run executes a blocking http server. Starts the http listener with the metrics and healthz endpoints.
75+
func serve(reg prometheus.Gatherer, conf config.Config) {
76+
if conf.GetMetricsPath() != "/" {
77+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
78+
http.Error(w, fmt.Sprintf("Use the %s endpoint", conf.GetMetricsPath()), http.StatusOK)
79+
})
80+
}
81+
7882
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { http.Error(w, "OK", http.StatusOK) })
79-
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
83+
http.HandleFunc(conf.GetMetricsPath(), func(w http.ResponseWriter, r *http.Request) {
8084
promhttp.HandlerFor(reg, promhttp.HandlerOpts{}).ServeHTTP(w, r)
8185
})
8286

83-
err := http.ListenAndServe(addr, nil)
87+
err := http.ListenAndServe(conf.GetBindAddr(), nil)
8488

8589
// If the port is already in use or another fatal error panic
8690
if err != nil {
@@ -103,17 +107,20 @@ func init() {
103107
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "info", "Define the log level (default is info) [debug,info,warning,error]")
104108
rootCmd.PersistentFlags().StringVarP(&logEncoding, "log-encoding", "e", "json", "Define the log format (default is json) [json,console]")
105109
rootCmd.PersistentFlags().StringVarP(&bind, "bind", "b", ":9412", "Address to bind http server (default is :9412)")
110+
rootCmd.PersistentFlags().StringVarP(&metricsPath, "path", "p", "/metrics", "Metric path (default is /metrics)")
106111
rootCmd.PersistentFlags().IntVarP(&queryTimeout, "query-timeout", "t", 10, "Timeout for MongoDB queries")
107112
viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("log-level"))
108113
viper.BindPFlag("log.encoding", rootCmd.PersistentFlags().Lookup("log-encoding"))
109114
viper.BindPFlag("bind", rootCmd.PersistentFlags().Lookup("bind"))
115+
viper.BindPFlag("metricsPath", rootCmd.PersistentFlags().Lookup("path"))
110116
viper.BindPFlag("mongodb.uri", rootCmd.PersistentFlags().Lookup("uri"))
111117
viper.BindPFlag("mongodb.queryTimeout", rootCmd.PersistentFlags().Lookup("query-timeout"))
112118
viper.BindEnv("mongodb.uri", "MDBEXPORTER_MONGODB_URI")
113119
viper.BindEnv("global.queryTimeout", "MDBEXPORTER_MONGODB_QUERY_TIMEOUT")
114120
viper.BindEnv("log.level", "MDBEXPORTER_LOG_LEVEL")
115121
viper.BindEnv("log.encoding", "MDBEXPORTER_LOG_ENCODING")
116122
viper.BindEnv("bind", "MDBEXPORTER_BIND")
123+
viper.BindEnv("metricsPath", "MDBEXPORTER_METRICSPATH")
117124
}
118125

119126
func initConfig() {

0 commit comments

Comments
 (0)