Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/mongodb_exporter/mongodb_exporter.go
5391 views
1
package mongodb_exporter //nolint:golint
2
3
import (
4
"fmt"
5
"net/url"
6
7
"github.com/go-kit/log"
8
"github.com/percona/mongodb_exporter/exporter"
9
config_util "github.com/prometheus/common/config"
10
11
"github.com/grafana/agent/pkg/integrations"
12
integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"
13
"github.com/grafana/agent/pkg/integrations/v2/metricsutils"
14
)
15
16
// Config controls mongodb_exporter
17
type Config struct {
18
// MongoDB connection URI. example:mongodb://user:[email protected]:27017/admin?ssl=true"
19
URI config_util.Secret `yaml:"mongodb_uri"`
20
}
21
22
// UnmarshalYAML implements yaml.Unmarshaler for Config
23
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
24
type plain Config
25
return unmarshal((*plain)(c))
26
}
27
28
// Name returns the name of the integration that this config represents.
29
func (c *Config) Name() string {
30
return "mongodb_exporter"
31
}
32
33
// InstanceKey returns the address:port of the mongodb server being queried.
34
func (c *Config) InstanceKey(_ string) (string, error) {
35
u, err := url.Parse(string(c.URI))
36
if err != nil {
37
return "", fmt.Errorf("could not parse url: %w", err)
38
}
39
return u.Host, nil
40
}
41
42
// NewIntegration creates a new mongodb_exporter
43
func (c *Config) NewIntegration(logger log.Logger) (integrations.Integration, error) {
44
return New(logger, c)
45
}
46
47
func init() {
48
integrations.RegisterIntegration(&Config{})
49
integrations_v2.RegisterLegacy(&Config{}, integrations_v2.TypeMultiplex, metricsutils.NewNamedShim("mongodb"))
50
}
51
52
// New creates a new mongodb_exporter integration.
53
func New(logger log.Logger, c *Config) (integrations.Integration, error) {
54
logrusLogger := integrations.NewLogger(logger)
55
56
exp := exporter.New(&exporter.Opts{
57
URI: string(c.URI),
58
Logger: logrusLogger,
59
DisableDefaultRegistry: true,
60
61
// NOTE(rfratto): CompatibleMode configures the exporter to use old metric
62
// names from mongodb_exporter <v0.20.0. Many existing dashboards rely on
63
// the old names, so we hard-code it to true now. We may wish to make this
64
// configurable in the future.
65
CompatibleMode: true,
66
CollectAll: true,
67
DirectConnect: true,
68
})
69
70
return integrations.NewHandlerIntegration(c.Name(), exp.Handler()), nil
71
}
72
73