Debian Codegraph @

bridges/prometheus/config.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package prometheus // import "go.opentelemetry.io/contrib/bridges/prometheus"
5
6 import (
7 "github.com/prometheus/client_golang/prometheus"
8 )
9
10 // config contains options for the producer.
11 type config struct {
12 gatherers []prometheus.Gatherer
13 }
14
15 // newConfig creates a validated config configured with options.
16 func newConfig(opts ...Option) config {
17 cfg := config{}
18 for _, opt := range opts {
19 cfg = opt.apply(cfg)
20 }
21
22 if len(cfg.gatherers) == 0 {
23 cfg.gatherers = []prometheus.Gatherer{prometheus.DefaultGatherer}
24 }
25
26 return cfg
27 }
28
29 // Option sets producer option values.
30 type Option interface {
31 apply(config) config
32 }
33
34 type optionFunc func(config) config
35
36 func (fn optionFunc) apply(cfg config) config {
37 return fn(cfg)
38 }
39
40 // WithGatherer configures which prometheus Gatherer the Bridge will gather
41 // from. If no registerer is used the prometheus DefaultGatherer is used.
42 func WithGatherer(gatherer prometheus.Gatherer) Option {
43 return optionFunc(func(cfg config) config {
44 cfg.gatherers = append(cfg.gatherers, gatherer)
45 return cfg
46 })
47 }