-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathfactory.go
144 lines (118 loc) · 5.01 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package sqlserverreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver"
import (
"database/sql"
"errors"
"fmt"
"time"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/receiver"
"go.opentelemetry.io/collector/scraper"
"go.opentelemetry.io/collector/scraper/scraperhelper"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/sqlquery"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver/internal/metadata"
)
var errConfigNotSQLServer = errors.New("config was not a sqlserver receiver config")
// NewFactory creates a factory for SQL Server receiver.
func NewFactory() receiver.Factory {
return receiver.NewFactory(
metadata.Type,
createDefaultConfig,
receiver.WithMetrics(createMetricsReceiver, metadata.MetricsStability))
}
func createDefaultConfig() component.Config {
cfg := scraperhelper.NewDefaultControllerConfig()
cfg.CollectionInterval = 10 * time.Second
return &Config{
ControllerConfig: cfg,
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
}
}
func setupQueries(cfg *Config) []string {
var queries []string
if isDatabaseIOQueryEnabled(&cfg.MetricsBuilderConfig.Metrics) {
queries = append(queries, getSQLServerDatabaseIOQuery(cfg.InstanceName))
}
if cfg.MetricsBuilderConfig.Metrics.SqlserverBatchRequestRate.Enabled ||
cfg.MetricsBuilderConfig.Metrics.SqlserverPageBufferCacheHitRatio.Enabled ||
cfg.MetricsBuilderConfig.Metrics.SqlserverResourcePoolDiskThrottledReadRate.Enabled ||
cfg.MetricsBuilderConfig.Metrics.SqlserverResourcePoolDiskThrottledWriteRate.Enabled ||
cfg.MetricsBuilderConfig.Metrics.SqlserverLockWaitRate.Enabled ||
cfg.MetricsBuilderConfig.Metrics.SqlserverProcessesBlocked.Enabled ||
cfg.MetricsBuilderConfig.Metrics.SqlserverBatchSQLRecompilationRate.Enabled ||
cfg.MetricsBuilderConfig.Metrics.SqlserverBatchSQLCompilationRate.Enabled ||
cfg.MetricsBuilderConfig.Metrics.SqlserverUserConnectionCount.Enabled {
queries = append(queries, getSQLServerPerformanceCounterQuery(cfg.InstanceName))
}
if cfg.MetricsBuilderConfig.Metrics.SqlserverDatabaseCount.Enabled {
queries = append(queries, getSQLServerPropertiesQuery(cfg.InstanceName))
}
return queries
}
func directDBConnectionEnabled(config *Config) bool {
return config.Server != "" &&
config.Username != "" &&
string(config.Password) != ""
}
// Assumes config has all information necessary to directly connect to the database
func getDBConnectionString(config *Config) string {
return fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", config.Server, config.Username, string(config.Password), config.Port)
}
// SQL Server scraper creation is split out into a separate method for the sake of testing.
func setupSQLServerScrapers(params receiver.Settings, cfg *Config) []*sqlServerScraperHelper {
if !directDBConnectionEnabled(cfg) {
params.Logger.Info("No direct connection will be made to the SQL Server: Configuration doesn't include some options.")
return nil
}
queries := setupQueries(cfg)
if len(queries) == 0 {
params.Logger.Info("No direct connection will be made to the SQL Server: No metrics are enabled requiring it.")
return nil
}
// TODO: Test if this needs to be re-defined for each scraper
// This should be tested when there is more than one query being made.
dbProviderFunc := func() (*sql.DB, error) {
return sql.Open("sqlserver", getDBConnectionString(cfg))
}
var scrapers []*sqlServerScraperHelper
for i, query := range queries {
id := component.NewIDWithName(metadata.Type, fmt.Sprintf("query-%d: %s", i, query))
sqlServerScraper := newSQLServerScraper(id, query,
cfg.InstanceName,
cfg.ControllerConfig,
params.Logger,
sqlquery.TelemetryConfig{},
dbProviderFunc,
sqlquery.NewDbClient,
metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, params))
scrapers = append(scrapers, sqlServerScraper)
}
return scrapers
}
// Note: This method will fail silently if there is no work to do. This is an acceptable use case
// as this receiver can still get information on Windows from performance counters without a direct
// connection. Messages will be logged at the INFO level in such cases.
func setupScrapers(params receiver.Settings, cfg *Config) ([]scraperhelper.ControllerOption, error) {
sqlServerScrapers := setupSQLServerScrapers(params, cfg)
var opts []scraperhelper.ControllerOption
for _, sqlScraper := range sqlServerScrapers {
s, err := scraper.NewMetrics(sqlScraper.ScrapeMetrics,
scraper.WithStart(sqlScraper.Start),
scraper.WithShutdown(sqlScraper.Shutdown))
if err != nil {
return nil, err
}
opt := scraperhelper.AddScraper(metadata.Type, s)
opts = append(opts, opt)
}
return opts, nil
}
func isDatabaseIOQueryEnabled(metrics *metadata.MetricsConfig) bool {
if metrics.SqlserverDatabaseLatency.Enabled ||
metrics.SqlserverDatabaseOperations.Enabled ||
metrics.SqlserverDatabaseIo.Enabled {
return true
}
return false
}