Skip to content

Commit 5d63058

Browse files
committed
add disable-exporter-metrics flag
1 parent 90a01a2 commit 5d63058

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

exporter/bufferLoadMetric.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ type bufferLoadMetric struct {
4343
tick *time.Ticker
4444
log logrus.FieldLogger
4545
lineLimitSet bool
46+
registry prometheus.Registerer
4647
}
4748

48-
func NewBufferLoadMetric(log logrus.FieldLogger, lineLimitSet bool) *bufferLoadMetric {
49+
func NewBufferLoadMetric(log logrus.FieldLogger, lineLimitSet bool, registry prometheus.Registerer) *bufferLoadMetric {
4950
m := &bufferLoadMetric{
5051
mutex: sync.NewCond(&sync.Mutex{}),
5152
log: log,
5253
lineLimitSet: lineLimitSet,
54+
registry: registry,
5355
}
5456
return m
5557
}
@@ -66,7 +68,7 @@ func (m *bufferLoadMetric) start(ticker *time.Ticker, tickProcessed chan struct{
6668
Name: "grok_exporter_line_buffer_load",
6769
Help: "Number of lines that are read from the logfile and waiting to be processed.",
6870
}, []string{"value", "interval"})
69-
prometheus.MustRegister(m.bufferLoad)
71+
m.registry.MustRegister(m.bufferLoad)
7072
m.bufferLoad.With(minLabel).Set(0)
7173
m.bufferLoad.With(maxLabel).Set(0)
7274
go func() {

exporter/bufferLoadMetric_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
package exporter
1616

1717
import (
18+
"github.com/prometheus/client_golang/prometheus"
1819
"github.com/sirupsen/logrus"
1920
"testing"
2021
"time"
2122
)
2223

2324
func TestBufferLoadMetric(t *testing.T) {
24-
m := NewBufferLoadMetric(logrus.New(), false)
25+
m := NewBufferLoadMetric(logrus.New(), false, prometheus.NewRegistry())
2526
c := make(chan time.Time)
2627
tick := &time.Ticker{
2728
C: c,
@@ -159,7 +160,7 @@ func expectValues(t *testing.T, m *bufferLoadMetric, min15s, min30s, min45s, min
159160
}
160161

161162
func TestResetBufferLoadMetrics(t *testing.T) {
162-
m := NewBufferLoadMetric(logrus.New(), false)
163+
m := NewBufferLoadMetric(logrus.New(), false, prometheus.NewRegistry())
163164
c := make(chan time.Time)
164165
tick := &time.Ticker{
165166
C: c,

grok_exporter.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ import (
3232
)
3333

3434
var (
35-
printVersion = flag.Bool("version", false, "Print the grok_exporter version.")
36-
configPath = flag.String("config", "", "Path to the config file. Try '-config ./example/config.yml' to get started.")
37-
showConfig = flag.Bool("showconfig", false, "Print the current configuration to the console. Example: 'grok_exporter -showconfig -config ./example/config.yml'")
35+
printVersion = flag.Bool("version", false, "Print the grok_exporter version.")
36+
configPath = flag.String("config", "", "Path to the config file. Try '-config ./example/config.yml' to get started.")
37+
showConfig = flag.Bool("showconfig", false, "Print the current configuration to the console. Example: 'grok_exporter -showconfig -config ./example/config.yml'")
38+
disableExporterMetrics = flag.Bool("disable-exporter-metrics", false, "If this flag is set, the metrics about the exporter itself (go_*, process_*, promhttp_*) will be excluded from /metrics")
3839
)
3940

4041
var (
@@ -67,23 +68,29 @@ func main() {
6768
fmt.Printf("%v\n", cfg)
6869
return
6970
}
71+
registry := prometheus.NewRegistry()
72+
if !*disableExporterMetrics {
73+
// init like the default registry, see client_golang/prometheus/registry.go init()
74+
registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
75+
registry.MustRegister(prometheus.NewGoCollector())
76+
}
7077
patterns, err := initPatterns(cfg)
7178
exitOnError(err)
7279
metrics, err := createMetrics(cfg, patterns)
7380
exitOnError(err)
7481
for _, m := range metrics {
75-
prometheus.MustRegister(m.Collector())
82+
registry.MustRegister(m.Collector())
7683
}
77-
nLinesTotal, nMatchesByMetric, procTimeMicrosecondsByMetric, nErrorsByMetric := initSelfMonitoring(metrics)
84+
nLinesTotal, nMatchesByMetric, procTimeMicrosecondsByMetric, nErrorsByMetric := initSelfMonitoring(metrics, registry)
7885

79-
tail, err := startTailer(cfg)
86+
tail, err := startTailer(cfg, registry)
8087
exitOnError(err)
8188

8289
// gather up the handlers with which to start the webserver
8390
httpHandlers := []exporter.HttpServerPathHandler{}
8491
httpHandlers = append(httpHandlers, exporter.HttpServerPathHandler{
8592
Path: cfg.Server.Path,
86-
Handler: promhttp.Handler()})
93+
Handler: promhttp.InstrumentMetricHandler(registry, promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))})
8794
if cfg.Input.Type == "webhook" {
8895
httpHandlers = append(httpHandlers, exporter.HttpServerPathHandler{
8996
Path: cfg.Input.WebhookPath,
@@ -250,7 +257,7 @@ func createMetrics(cfg *v3.Config, patterns *exporter.Patterns) ([]exporter.Metr
250257
return result, nil
251258
}
252259

253-
func initSelfMonitoring(metrics []exporter.Metric) (*prometheus.CounterVec, *prometheus.CounterVec, *prometheus.CounterVec, *prometheus.CounterVec) {
260+
func initSelfMonitoring(metrics []exporter.Metric, registry prometheus.Registerer) (*prometheus.CounterVec, *prometheus.CounterVec, *prometheus.CounterVec, *prometheus.CounterVec) {
254261
buildInfo := prometheus.NewGaugeVec(prometheus.GaugeOpts{
255262
Name: "grok_exporter_build_info",
256263
Help: "A metric with a constant '1' value labeled by version, builddate, branch, revision, goversion, and platform on which grok_exporter was built.",
@@ -272,11 +279,11 @@ func initSelfMonitoring(metrics []exporter.Metric) (*prometheus.CounterVec, *pro
272279
Help: "Number of errors for each metric. If this is > 0 there is an error in the configuration file. Check grok_exporter's console output.",
273280
}, []string{"metric"})
274281

275-
prometheus.MustRegister(buildInfo)
276-
prometheus.MustRegister(nLinesTotal)
277-
prometheus.MustRegister(nMatchesByMetric)
278-
prometheus.MustRegister(procTimeMicrosecondsByMetric)
279-
prometheus.MustRegister(nErrorsByMetric)
282+
registry.MustRegister(buildInfo)
283+
registry.MustRegister(nLinesTotal)
284+
registry.MustRegister(nMatchesByMetric)
285+
registry.MustRegister(procTimeMicrosecondsByMetric)
286+
registry.MustRegister(nErrorsByMetric)
280287

281288
buildInfo.WithLabelValues(exporter.Version, exporter.BuildDate, exporter.Branch, exporter.Revision, exporter.GoVersion, exporter.Platform).Set(1)
282289
// Initializing a value with zero makes the label appear. Otherwise the label is not shown until the first value is observed.
@@ -310,7 +317,7 @@ func startServer(cfg v3.ServerConfig, httpHandlers []exporter.HttpServerPathHand
310317
return serverErrors
311318
}
312319

313-
func startTailer(cfg *v3.Config) (fswatcher.FileTailer, error) {
320+
func startTailer(cfg *v3.Config, registry prometheus.Registerer) (fswatcher.FileTailer, error) {
314321
var (
315322
tail fswatcher.FileTailer
316323
err error
@@ -337,6 +344,6 @@ func startTailer(cfg *v3.Config) (fswatcher.FileTailer, error) {
337344
default:
338345
return nil, fmt.Errorf("Config error: Input type '%v' unknown.", cfg.Input.Type)
339346
}
340-
bufferLoadMetric := exporter.NewBufferLoadMetric(logger, cfg.Input.MaxLinesInBuffer > 0)
347+
bufferLoadMetric := exporter.NewBufferLoadMetric(logger, cfg.Input.MaxLinesInBuffer > 0, registry)
341348
return tailer.BufferedTailerWithMetrics(tail, bufferLoadMetric, logger, cfg.Input.MaxLinesInBuffer), nil
342349
}

0 commit comments

Comments
 (0)