-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathscraper_windows_test.go
189 lines (156 loc) · 6.02 KB
/
scraper_windows_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
//go:build windows
package sqlserverreceiver
import (
"context"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver/internal/metadata"
)
// mockPerfCounterWatcher is an autogenerated mock type for the PerfCounterWatcher type
type mockPerfCounterWatcher struct {
mock.Mock
}
// Close provides a mock function with given fields:
func (_m *mockPerfCounterWatcher) Close() error {
ret := _m.Called()
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
return r0
}
// Path provides a mock function with given fields:
func (_m *mockPerfCounterWatcher) Path() string {
ret := _m.Called()
var r0 string
if rf, ok := ret.Get(0).(func() string); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(string)
}
return r0
}
// ScrapeData provides a mock function with given fields:
func (_m *mockPerfCounterWatcher) ScrapeData() ([]winperfcounters.CounterValue, error) {
ret := _m.Called()
var r0 []winperfcounters.CounterValue
if rf, ok := ret.Get(0).(func() []winperfcounters.CounterValue); ok {
r0 = rf()
} else if ret.Get(0) != nil {
r0 = ret.Get(0).([]winperfcounters.CounterValue)
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
func (_m *mockPerfCounterWatcher) Reset() error {
return nil
}
func TestSqlServerScraper(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
logger, obsLogs := observer.New(zap.WarnLevel)
settings := receivertest.NewNopSettings(metadata.Type)
settings.Logger = zap.New(logger)
s := newSQLServerPCScraper(settings, cfg)
assert.NoError(t, s.start(context.Background(), nil))
assert.Empty(t, s.watcherRecorders)
assert.Equal(t, 21, obsLogs.Len())
assert.Equal(t, 21, obsLogs.FilterMessageSnippet("failed to create perf counter with path \\SQLServer:").Len())
assert.Equal(t, 21, obsLogs.FilterMessageSnippet("The specified object was not found on the computer.").Len())
assert.Equal(t, 1, obsLogs.FilterMessageSnippet("\\SQLServer:General Statistics\\").Len())
assert.Equal(t, 3, obsLogs.FilterMessageSnippet("\\SQLServer:SQL Statistics\\").Len())
assert.Equal(t, 2, obsLogs.FilterMessageSnippet("\\SQLServer:Locks(_Total)\\").Len())
assert.Equal(t, 6, obsLogs.FilterMessageSnippet("\\SQLServer:Buffer Manager\\").Len())
assert.Equal(t, 1, obsLogs.FilterMessageSnippet("\\SQLServer:Access Methods(_Total)\\").Len())
assert.Equal(t, 8, obsLogs.FilterMessageSnippet("\\SQLServer:Databases(*)\\").Len())
metrics, err := s.scrape(context.Background())
require.NoError(t, err)
assert.Equal(t, 0, metrics.ResourceMetrics().Len())
err = s.shutdown(context.Background())
require.NoError(t, err)
}
var (
goldenScrapePath = filepath.Join("testdata", "golden_scrape.yaml")
goldenNamedInstanceScrapePath = filepath.Join("testdata", "golden_named_instance_scrape.yaml")
dbInstance = "db-instance"
)
func TestScrape(t *testing.T) {
t.Run("default", func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
settings := receivertest.NewNopSettings(metadata.Type)
scraper := newSQLServerPCScraper(settings, cfg)
for i, rec := range perfCounterRecorders {
perfCounterWatcher := &mockPerfCounterWatcher{}
perfCounterWatcher.On("ScrapeData").Return([]winperfcounters.CounterValue{{InstanceName: dbInstance, Value: float64(i)}}, nil)
for _, recorder := range rec.recorders {
scraper.watcherRecorders = append(scraper.watcherRecorders, watcherRecorder{
watcher: perfCounterWatcher,
recorder: recorder,
})
}
}
scrapeData, err := scraper.scrape(context.Background())
require.NoError(t, err)
expectedMetrics, err := golden.ReadMetrics(goldenScrapePath)
require.NoError(t, err)
require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, scrapeData,
pmetrictest.IgnoreMetricDataPointsOrder(), pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp()))
})
t.Run("named", func(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig().(*Config)
cfg.MetricsBuilderConfig = metadata.MetricsBuilderConfig{
Metrics: metadata.DefaultMetricsConfig(),
ResourceAttributes: metadata.ResourceAttributesConfig{
SqlserverDatabaseName: metadata.ResourceAttributeConfig{
Enabled: true,
},
SqlserverInstanceName: metadata.ResourceAttributeConfig{
Enabled: true,
},
SqlserverComputerName: metadata.ResourceAttributeConfig{
Enabled: true,
},
},
}
cfg.ComputerName = "CustomServer"
cfg.InstanceName = "CustomInstance"
settings := receivertest.NewNopSettings(metadata.Type)
scraper := newSQLServerPCScraper(settings, cfg)
for i, rec := range perfCounterRecorders {
perfCounterWatcher := &mockPerfCounterWatcher{}
perfCounterWatcher.On("ScrapeData").Return([]winperfcounters.CounterValue{{InstanceName: dbInstance, Value: float64(i)}}, nil)
for _, recorder := range rec.recorders {
scraper.watcherRecorders = append(scraper.watcherRecorders, watcherRecorder{
watcher: perfCounterWatcher,
recorder: recorder,
})
}
}
scrapeData, err := scraper.scrape(context.Background())
require.NoError(t, err)
expectedMetrics, err := golden.ReadMetrics(goldenNamedInstanceScrapePath)
require.NoError(t, err)
require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, scrapeData,
pmetrictest.IgnoreMetricDataPointsOrder(), pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp()))
})
}