Skip to content

Commit d6800dd

Browse files
committed
OTel: Add tests for metrics exporters.
1 parent b0451f1 commit d6800dd

11 files changed

+655
-26
lines changed

internal/collector/helpers_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ func testInstrumentationSpec() *v1beta1.InstrumentationSpec {
2323
Logs: &v1beta1.InstrumentationLogsSpec{
2424
Exporters: []string{"googlecloud"},
2525
},
26+
Metrics: &v1beta1.InstrumentationMetricsSpec{
27+
Exporters: []string{"googlecloud"},
28+
},
2629
}
2730

2831
return spec.DeepCopy()

internal/collector/patroni_test.go

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
func TestEnablePatroniLogging(t *testing.T) {
19-
t.Run("NilInstrumentationSpec", func(t *testing.T) {
19+
t.Run("EmptyInstrumentationSpec", func(t *testing.T) {
2020
gate := feature.NewGate()
2121
assert.NilError(t, gate.SetFromMap(map[string]bool{
2222
feature.OpenTelemetryLogs: true,
@@ -26,9 +26,7 @@ func TestEnablePatroniLogging(t *testing.T) {
2626
config := NewConfig(nil)
2727
cluster := new(v1beta1.PostgresCluster)
2828
require.UnmarshalInto(t, &cluster.Spec, `{
29-
instrumentation: {
30-
logs: { retentionPeriod: 5h },
31-
},
29+
instrumentation: {}
3230
}`)
3331

3432
EnablePatroniLogging(ctx, cluster, config)
@@ -216,3 +214,137 @@ service:
216214
`)
217215
})
218216
}
217+
218+
func TestEnablePatroniMetrics(t *testing.T) {
219+
t.Run("EmptyInstrumentationSpec", func(t *testing.T) {
220+
gate := feature.NewGate()
221+
assert.NilError(t, gate.SetFromMap(map[string]bool{
222+
feature.OpenTelemetryMetrics: true,
223+
}))
224+
ctx := feature.NewContext(context.Background(), gate)
225+
226+
config := NewConfig(nil)
227+
cluster := new(v1beta1.PostgresCluster)
228+
require.UnmarshalInto(t, &cluster.Spec, `{
229+
instrumentation: {}
230+
}`)
231+
232+
EnablePatroniMetrics(ctx, cluster, config)
233+
234+
result, err := config.ToYAML()
235+
assert.NilError(t, err)
236+
assert.DeepEqual(t, result, `# Generated by postgres-operator. DO NOT EDIT.
237+
# Your changes will not be saved.
238+
exporters:
239+
debug:
240+
verbosity: detailed
241+
prometheus/cpk-monitoring:
242+
endpoint: 0.0.0.0:9187
243+
extensions: {}
244+
processors:
245+
batch/1s:
246+
timeout: 1s
247+
batch/200ms:
248+
timeout: 200ms
249+
batch/logs:
250+
send_batch_size: 8192
251+
timeout: 200ms
252+
groupbyattrs/compact: {}
253+
resourcedetection:
254+
detectors: []
255+
override: false
256+
timeout: 30s
257+
receivers:
258+
prometheus/cpk-monitoring:
259+
config:
260+
scrape_configs:
261+
- job_name: patroni
262+
scheme: https
263+
scrape_interval: 10s
264+
static_configs:
265+
- targets:
266+
- 0.0.0.0:8008
267+
tls_config:
268+
insecure_skip_verify: true
269+
service:
270+
extensions: []
271+
pipelines:
272+
metrics/patroni:
273+
exporters:
274+
- prometheus/cpk-monitoring
275+
processors:
276+
- batch/200ms
277+
- groupbyattrs/compact
278+
receivers:
279+
- prometheus/cpk-monitoring
280+
`)
281+
})
282+
283+
t.Run("InstrumentationSpecDefined", func(t *testing.T) {
284+
gate := feature.NewGate()
285+
assert.NilError(t, gate.SetFromMap(map[string]bool{
286+
feature.OpenTelemetryMetrics: true,
287+
}))
288+
ctx := feature.NewContext(context.Background(), gate)
289+
290+
cluster := new(v1beta1.PostgresCluster)
291+
cluster.Spec.Instrumentation = testInstrumentationSpec()
292+
config := NewConfig(cluster.Spec.Instrumentation)
293+
294+
EnablePatroniMetrics(ctx, cluster, config)
295+
296+
result, err := config.ToYAML()
297+
assert.NilError(t, err)
298+
assert.DeepEqual(t, result, `# Generated by postgres-operator. DO NOT EDIT.
299+
# Your changes will not be saved.
300+
exporters:
301+
debug:
302+
verbosity: detailed
303+
googlecloud:
304+
log:
305+
default_log_name: opentelemetry.io/collector-exported-log
306+
project: google-project-name
307+
prometheus/cpk-monitoring:
308+
endpoint: 0.0.0.0:9187
309+
extensions: {}
310+
processors:
311+
batch/1s:
312+
timeout: 1s
313+
batch/200ms:
314+
timeout: 200ms
315+
batch/logs:
316+
send_batch_size: 8192
317+
timeout: 200ms
318+
groupbyattrs/compact: {}
319+
resourcedetection:
320+
detectors: []
321+
override: false
322+
timeout: 30s
323+
receivers:
324+
prometheus/cpk-monitoring:
325+
config:
326+
scrape_configs:
327+
- job_name: patroni
328+
scheme: https
329+
scrape_interval: 10s
330+
static_configs:
331+
- targets:
332+
- 0.0.0.0:8008
333+
tls_config:
334+
insecure_skip_verify: true
335+
service:
336+
extensions: []
337+
pipelines:
338+
metrics/patroni:
339+
exporters:
340+
- prometheus/cpk-monitoring
341+
- googlecloud
342+
processors:
343+
- batch/200ms
344+
- groupbyattrs/compact
345+
receivers:
346+
- prometheus/cpk-monitoring
347+
`)
348+
349+
})
350+
}

internal/collector/pgadmin_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
func TestEnablePgAdminLogging(t *testing.T) {
23-
t.Run("NilInstrumentationSpec", func(t *testing.T) {
23+
t.Run("EmptyInstrumentationSpec", func(t *testing.T) {
2424
gate := feature.NewGate()
2525
assert.NilError(t, gate.SetFromMap(map[string]bool{
2626
feature.OpenTelemetryLogs: true,
@@ -31,9 +31,7 @@ func TestEnablePgAdminLogging(t *testing.T) {
3131
configmap := new(corev1.ConfigMap)
3232
initialize.Map(&configmap.Data)
3333
var instrumentation *v1beta1.InstrumentationSpec
34-
require.UnmarshalInto(t, &instrumentation, `{
35-
logs: { retentionPeriod: 12h },
36-
}`)
34+
require.UnmarshalInto(t, &instrumentation, `{}`)
3735
err := collector.EnablePgAdminLogging(ctx, instrumentation, configmap)
3836
assert.NilError(t, err)
3937

internal/collector/pgbackrest_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
func TestNewConfigForPgBackrestRepoHostPod(t *testing.T) {
19-
t.Run("NilInstrumentationSpec", func(t *testing.T) {
19+
t.Run("EmptyInstrumentationSpec", func(t *testing.T) {
2020
gate := feature.NewGate()
2121
assert.NilError(t, gate.SetFromMap(map[string]bool{
2222
feature.OpenTelemetryLogs: true,
@@ -29,9 +29,7 @@ func TestNewConfigForPgBackrestRepoHostPod(t *testing.T) {
2929
},
3030
}
3131
var instrumentation *v1beta1.InstrumentationSpec
32-
require.UnmarshalInto(t, &instrumentation, `{
33-
logs: { retentionPeriod: 12h },
34-
}`)
32+
require.UnmarshalInto(t, &instrumentation, `{}`)
3533

3634
config := NewConfigForPgBackrestRepoHostPod(ctx, instrumentation, repos)
3735

0 commit comments

Comments
 (0)