Path: blob/main/pkg/integrations/v2/vmware_exporter/vmware_exporter.go
5371 views
package vmware_exporter12import (3"fmt"4"net/url"5"time"67"github.com/go-kit/log"8"github.com/grafana/agent/pkg/integrations/v2"9"github.com/grafana/agent/pkg/integrations/v2/common"10"github.com/grafana/agent/pkg/integrations/v2/metricsutils"11"github.com/grafana/vmware_exporter/vsphere"12config_util "github.com/prometheus/common/config"13)1415func init() {16integrations.Register(&Config{}, integrations.TypeMultiplex)17}1819// DefaultConfig holds non-zero default options for hte Config when it is20// unmarshaled from YAML.21var DefaultConfig = Config{22ChunkSize: 256,23CollectConcurrency: 8,24ObjectDiscoveryInterval: 0,25EnableExporterMetrics: true,26}2728// Config configures the vmware_exporter integration.29type Config struct {30ChunkSize int `yaml:"request_chunk_size,omitempty"`31CollectConcurrency int `yaml:"collect_concurrency,omitempty"`32VSphereURL string `yaml:"vsphere_url,omitempty"`33VSphereUser string `yaml:"vsphere_user,omitempty"`34VSpherePass config_util.Secret `yaml:"vsphere_password,omitempty"`35ObjectDiscoveryInterval time.Duration `yaml:"discovery_interval,omitempty"`36EnableExporterMetrics bool `yaml:"enable_exporter_metrics,omitempty"`37Common common.MetricsConfig `yaml:",inline"`38}3940var _ integrations.Config = (*Config)(nil)4142// UnmarshalYAML implements the Unmarshaler interface.43func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {44*c = DefaultConfig45type plain Config46return unmarshal((*plain)(c))47}4849// Name returns the name of this integration.50func (c *Config) Name() string {51return "vsphere"52}5354// ApplyDefaults applies the integration's default configuration.55func (c *Config) ApplyDefaults(g integrations.Globals) error {56c.Common.ApplyDefaults(g.SubsystemOpts.Metrics.Autoscrape)57return nil58}5960// Identifier returns a string that identifies the instance of the integration.61func (c *Config) Identifier(g integrations.Globals) (string, error) {62if c.Common.InstanceKey != nil {63return *c.Common.InstanceKey, nil64}6566u, err := url.Parse(c.VSphereURL)67if err != nil {68return "", err69}70return fmt.Sprintf("%s:%s", u.Hostname(), u.Port()), nil71}7273// NewIntegration constructs a new instance of this integration.74func (c *Config) NewIntegration(log log.Logger, g integrations.Globals) (integrations.Integration, error) {75vsphereURL, err := url.Parse(c.VSphereURL)76if err != nil {77return nil, err78}79vsphereURL.User = url.UserPassword(c.VSphereUser, string(c.VSpherePass))8081exporterConfig := vsphere.Config{82ChunkSize: c.ChunkSize,83CollectConcurrency: c.CollectConcurrency,84VSphereURL: vsphereURL,85ObjectDiscoveryInterval: c.ObjectDiscoveryInterval,86EnableExporterMetrics: c.EnableExporterMetrics,87}88exporter, err := vsphere.NewExporter(log, &exporterConfig)89if err != nil {90return nil, err91}9293return metricsutils.NewMetricsHandlerIntegration(94log, c, c.Common, g, exporter,95)96}979899