Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/integrations/mssql/sql_exporter.go
5304 views
1
package mssql
2
3
import (
4
"errors"
5
"fmt"
6
"net/url"
7
"time"
8
9
"github.com/go-kit/log"
10
"github.com/prometheus/client_golang/prometheus"
11
config_util "github.com/prometheus/common/config"
12
13
"github.com/burningalchemist/sql_exporter"
14
"github.com/burningalchemist/sql_exporter/config"
15
"github.com/grafana/agent/pkg/integrations"
16
integrations_v2 "github.com/grafana/agent/pkg/integrations/v2"
17
"github.com/grafana/agent/pkg/integrations/v2/metricsutils"
18
"github.com/prometheus/common/model"
19
)
20
21
// DefaultConfig is the default config for the mssql integration
22
var DefaultConfig = Config{
23
MaxIdleConnections: 3,
24
MaxOpenConnections: 3,
25
Timeout: 10 * time.Second,
26
}
27
28
// Config is the configuration for the mssql integration
29
type Config struct {
30
ConnectionString config_util.Secret `yaml:"connection_string,omitempty"`
31
MaxIdleConnections int `yaml:"max_idle_connections,omitempty"`
32
MaxOpenConnections int `yaml:"max_open_connections,omitempty"`
33
Timeout time.Duration `yaml:"timeout,omitempty"`
34
}
35
36
func (c Config) validate() error {
37
if c.ConnectionString == "" {
38
return errors.New("the connection_string parameter is required")
39
}
40
41
url, err := url.Parse(string(c.ConnectionString))
42
if err != nil {
43
return fmt.Errorf("failed to parse connection_string: %w", err)
44
}
45
46
if url.Scheme != "sqlserver" {
47
return errors.New("scheme of provided connection_string URL must be sqlserver")
48
}
49
50
if c.MaxOpenConnections < 1 {
51
return errors.New("max_connections must be at least 1")
52
}
53
54
if c.MaxIdleConnections < 1 {
55
return errors.New("max_idle_connection must be at least 1")
56
}
57
58
if c.Timeout <= 0 {
59
return errors.New("timeout must be positive")
60
}
61
62
return nil
63
}
64
65
// Identifier returns a string that identifies the integration.
66
func (c *Config) InstanceKey(agentKey string) (string, error) {
67
url, err := url.Parse(string(c.ConnectionString))
68
if err != nil {
69
return "", fmt.Errorf("failed to parse connection string URL: %w", err)
70
}
71
72
return url.Host, nil
73
}
74
75
// UnmarshalYAML implements yaml.Unmarshaler for Config
76
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
77
*c = DefaultConfig
78
79
type plain Config
80
return unmarshal((*plain)(c))
81
}
82
83
// Name returns the name of the integration this config is for.
84
func (c *Config) Name() string {
85
return "mssql"
86
}
87
88
func init() {
89
integrations.RegisterIntegration(&Config{})
90
integrations_v2.RegisterLegacy(&Config{}, integrations_v2.TypeMultiplex, metricsutils.NewNamedShim("mssql"))
91
}
92
93
// NewIntegration creates a new integration from the config.
94
func (c *Config) NewIntegration(l log.Logger) (integrations.Integration, error) {
95
if err := c.validate(); err != nil {
96
return nil, fmt.Errorf("failed to validate config: %w", err)
97
}
98
99
t, err := sql_exporter.NewTarget(
100
"mssqlintegration",
101
"",
102
string(c.ConnectionString),
103
[]*config.CollectorConfig{
104
&collectorConfig,
105
},
106
prometheus.Labels{},
107
&config.GlobalConfig{
108
ScrapeTimeout: model.Duration(c.Timeout),
109
TimeoutOffset: model.Duration(500 * time.Millisecond),
110
MaxConns: c.MaxOpenConnections,
111
MaxIdleConns: c.MaxIdleConnections,
112
},
113
)
114
115
if err != nil {
116
return nil, fmt.Errorf("failed to create mssql target: %w", err)
117
}
118
119
col := newTargetCollectorAdapter(t, l)
120
121
return integrations.NewCollectorIntegration(
122
c.Name(),
123
integrations.WithCollectors(col),
124
), nil
125
}
126
127