-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
scraper_test.go
189 lines (153 loc) · 6.29 KB
/
scraper_test.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package sqlserverreceiver
import (
"context"
"encoding/json"
"errors"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/receiver/receivertest"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/sqlquery"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
)
func enableAllScraperMetrics(cfg *Config) {
// Some of these metrics are enabled by default, but it's still helpful to include
// in the case of using a config that may have previously disabled a metric.
cfg.MetricsBuilderConfig.Metrics.SqlserverBatchRequestRate.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverBatchSQLCompilationRate.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverBatchSQLRecompilationRate.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverDatabaseCount.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverDatabaseIo.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverDatabaseLatency.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverDatabaseOperations.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverLockWaitRate.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverPageBufferCacheHitRatio.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverProcessesBlocked.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverResourcePoolDiskThrottledReadRate.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverResourcePoolDiskThrottledWriteRate.Enabled = true
cfg.MetricsBuilderConfig.Metrics.SqlserverUserConnectionCount.Enabled = true
}
func TestEmptyScrape(t *testing.T) {
cfg := createDefaultConfig().(*Config)
cfg.Username = "sa"
cfg.Password = "password"
cfg.Port = 1433
cfg.Server = "0.0.0.0"
cfg.MetricsBuilderConfig.ResourceAttributes.SqlserverInstanceName.Enabled = true
assert.NoError(t, cfg.Validate())
// Ensure there aren't any scrapers when all metrics are disabled.
// Disable all metrics manually that are enabled by default
cfg.MetricsBuilderConfig.Metrics.SqlserverBatchRequestRate.Enabled = false
cfg.MetricsBuilderConfig.Metrics.SqlserverPageBufferCacheHitRatio.Enabled = false
cfg.MetricsBuilderConfig.Metrics.SqlserverLockWaitRate.Enabled = false
cfg.MetricsBuilderConfig.Metrics.SqlserverBatchSQLRecompilationRate.Enabled = false
cfg.MetricsBuilderConfig.Metrics.SqlserverBatchSQLCompilationRate.Enabled = false
cfg.MetricsBuilderConfig.Metrics.SqlserverUserConnectionCount.Enabled = false
scrapers := setupSQLServerScrapers(receivertest.NewNopSettings(), cfg)
assert.Empty(t, scrapers)
}
func TestSuccessfulScrape(t *testing.T) {
cfg := createDefaultConfig().(*Config)
cfg.Username = "sa"
cfg.Password = "password"
cfg.Port = 1433
cfg.Server = "0.0.0.0"
cfg.MetricsBuilderConfig.ResourceAttributes.SqlserverInstanceName.Enabled = true
assert.NoError(t, cfg.Validate())
enableAllScraperMetrics(cfg)
scrapers := setupSQLServerScrapers(receivertest.NewNopSettings(), cfg)
assert.NotEmpty(t, scrapers)
for _, scraper := range scrapers {
err := scraper.Start(context.Background(), componenttest.NewNopHost())
assert.NoError(t, err)
defer assert.NoError(t, scraper.Shutdown(context.Background()))
scraper.client = mockClient{
instanceName: scraper.instanceName,
SQL: scraper.sqlQuery,
}
actualMetrics, err := scraper.Scrape(context.Background())
assert.NoError(t, err)
var expectedFile string
switch scraper.sqlQuery {
case getSQLServerDatabaseIOQuery(scraper.instanceName):
expectedFile = filepath.Join("testdata", "expectedDatabaseIO.yaml")
case getSQLServerPerformanceCounterQuery(scraper.instanceName):
expectedFile = filepath.Join("testdata", "expectedPerfCounters.yaml")
case getSQLServerPropertiesQuery(scraper.instanceName):
expectedFile = filepath.Join("testdata", "expectedProperties.yaml")
}
// Uncomment line below to re-generate expected metrics.
// golden.WriteMetrics(t, expectedFile, actualMetrics)
expectedMetrics, err := golden.ReadMetrics(expectedFile)
assert.NoError(t, err)
assert.NoError(t, pmetrictest.CompareMetrics(actualMetrics, expectedMetrics,
pmetrictest.IgnoreMetricDataPointsOrder(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreResourceMetricsOrder()))
}
}
func TestScrapeInvalidQuery(t *testing.T) {
cfg := createDefaultConfig().(*Config)
cfg.Username = "sa"
cfg.Password = "password"
cfg.Port = 1433
cfg.Server = "0.0.0.0"
cfg.MetricsBuilderConfig.ResourceAttributes.SqlserverInstanceName.Enabled = true
assert.NoError(t, cfg.Validate())
enableAllScraperMetrics(cfg)
scrapers := setupSQLServerScrapers(receivertest.NewNopSettings(), cfg)
assert.NotNil(t, scrapers)
for _, scraper := range scrapers {
err := scraper.Start(context.Background(), componenttest.NewNopHost())
assert.NoError(t, err)
defer assert.NoError(t, scraper.Shutdown(context.Background()))
scraper.client = mockClient{
instanceName: scraper.instanceName,
SQL: "Invalid SQL query",
}
actualMetrics, err := scraper.Scrape(context.Background())
assert.Error(t, err)
assert.Empty(t, actualMetrics)
}
}
var _ sqlquery.DbClient = (*mockClient)(nil)
type mockClient struct {
SQL string
instanceName string
}
func readFile(fname string) ([]sqlquery.StringMap, error) {
file, err := os.ReadFile(filepath.Join("testdata", fname))
if err != nil {
return nil, err
}
var metrics []sqlquery.StringMap
err = json.Unmarshal(file, &metrics)
if err != nil {
return nil, err
}
return metrics, nil
}
func (mc mockClient) QueryRows(context.Context, ...any) ([]sqlquery.StringMap, error) {
var queryResults []sqlquery.StringMap
var err error
switch mc.SQL {
case getSQLServerDatabaseIOQuery(mc.instanceName):
queryResults, err = readFile("database_io_scraped_data.txt")
case getSQLServerPerformanceCounterQuery(mc.instanceName):
queryResults, err = readFile("perfCounterQueryData.txt")
case getSQLServerPropertiesQuery(mc.instanceName):
queryResults, err = readFile("propertyQueryData.txt")
default:
return nil, errors.New("No valid query found")
}
if err != nil {
return nil, err
}
return queryResults, nil
}