Path: blob/main/pkg/integrations/mongodb_exporter/mongodb_exporter.go
5391 views
package mongodb_exporter //nolint:golint12import (3"fmt"4"net/url"56"github.com/go-kit/log"7"github.com/percona/mongodb_exporter/exporter"8config_util "github.com/prometheus/common/config"910"github.com/grafana/agent/pkg/integrations"11integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"12"github.com/grafana/agent/pkg/integrations/v2/metricsutils"13)1415// Config controls mongodb_exporter16type Config struct {17// MongoDB connection URI. example:mongodb://user:[email protected]:27017/admin?ssl=true"18URI config_util.Secret `yaml:"mongodb_uri"`19}2021// UnmarshalYAML implements yaml.Unmarshaler for Config22func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {23type plain Config24return unmarshal((*plain)(c))25}2627// Name returns the name of the integration that this config represents.28func (c *Config) Name() string {29return "mongodb_exporter"30}3132// InstanceKey returns the address:port of the mongodb server being queried.33func (c *Config) InstanceKey(_ string) (string, error) {34u, err := url.Parse(string(c.URI))35if err != nil {36return "", fmt.Errorf("could not parse url: %w", err)37}38return u.Host, nil39}4041// NewIntegration creates a new mongodb_exporter42func (c *Config) NewIntegration(logger log.Logger) (integrations.Integration, error) {43return New(logger, c)44}4546func init() {47integrations.RegisterIntegration(&Config{})48integrations_v2.RegisterLegacy(&Config{}, integrations_v2.TypeMultiplex, metricsutils.NewNamedShim("mongodb"))49}5051// New creates a new mongodb_exporter integration.52func New(logger log.Logger, c *Config) (integrations.Integration, error) {53logrusLogger := integrations.NewLogger(logger)5455exp := exporter.New(&exporter.Opts{56URI: string(c.URI),57Logger: logrusLogger,58DisableDefaultRegistry: true,5960// NOTE(rfratto): CompatibleMode configures the exporter to use old metric61// names from mongodb_exporter <v0.20.0. Many existing dashboards rely on62// the old names, so we hard-code it to true now. We may wish to make this63// configurable in the future.64CompatibleMode: true,65CollectAll: true,66DirectConnect: true,67})6869return integrations.NewHandlerIntegration(c.Name(), exp.Handler()), nil70}717273