Path: blob/main/pkg/integrations/mssql/sql_exporter.go
5304 views
package mssql12import (3"errors"4"fmt"5"net/url"6"time"78"github.com/go-kit/log"9"github.com/prometheus/client_golang/prometheus"10config_util "github.com/prometheus/common/config"1112"github.com/burningalchemist/sql_exporter"13"github.com/burningalchemist/sql_exporter/config"14"github.com/grafana/agent/pkg/integrations"15integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"16"github.com/grafana/agent/pkg/integrations/v2/metricsutils"17"github.com/prometheus/common/model"18)1920// DefaultConfig is the default config for the mssql integration21var DefaultConfig = Config{22MaxIdleConnections: 3,23MaxOpenConnections: 3,24Timeout: 10 * time.Second,25}2627// Config is the configuration for the mssql integration28type Config struct {29ConnectionString config_util.Secret `yaml:"connection_string,omitempty"`30MaxIdleConnections int `yaml:"max_idle_connections,omitempty"`31MaxOpenConnections int `yaml:"max_open_connections,omitempty"`32Timeout time.Duration `yaml:"timeout,omitempty"`33}3435func (c Config) validate() error {36if c.ConnectionString == "" {37return errors.New("the connection_string parameter is required")38}3940url, err := url.Parse(string(c.ConnectionString))41if err != nil {42return fmt.Errorf("failed to parse connection_string: %w", err)43}4445if url.Scheme != "sqlserver" {46return errors.New("scheme of provided connection_string URL must be sqlserver")47}4849if c.MaxOpenConnections < 1 {50return errors.New("max_connections must be at least 1")51}5253if c.MaxIdleConnections < 1 {54return errors.New("max_idle_connection must be at least 1")55}5657if c.Timeout <= 0 {58return errors.New("timeout must be positive")59}6061return nil62}6364// Identifier returns a string that identifies the integration.65func (c *Config) InstanceKey(agentKey string) (string, error) {66url, err := url.Parse(string(c.ConnectionString))67if err != nil {68return "", fmt.Errorf("failed to parse connection string URL: %w", err)69}7071return url.Host, nil72}7374// UnmarshalYAML implements yaml.Unmarshaler for Config75func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {76*c = DefaultConfig7778type plain Config79return unmarshal((*plain)(c))80}8182// Name returns the name of the integration this config is for.83func (c *Config) Name() string {84return "mssql"85}8687func init() {88integrations.RegisterIntegration(&Config{})89integrations_v2.RegisterLegacy(&Config{}, integrations_v2.TypeMultiplex, metricsutils.NewNamedShim("mssql"))90}9192// NewIntegration creates a new integration from the config.93func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) {94if err := c.validate(); err != nil {95return nil, fmt.Errorf("failed to validate config: %w", err)96}9798t, err := sql_exporter.NewTarget(99"mssqlintegration",100"",101string(c.ConnectionString),102[]*config.CollectorConfig{103&collectorConfig,104},105prometheus.Labels{},106&config.GlobalConfig{107ScrapeTimeout: model.Duration(c.Timeout),108TimeoutOffset: model.Duration(500 * time.Millisecond),109MaxConns: c.MaxOpenConnections,110MaxIdleConns: c.MaxIdleConnections,111},112)113114if err != nil {115return nil, fmt.Errorf("failed to create mssql target: %w", err)116}117118col := newTargetCollectorAdapter(t, l)119120return integrations.NewCollectorIntegration(121c.Name(),122integrations.WithCollectors(col),123), nil124}125126127