Skip to content

Commit 46ee186

Browse files
authored
Prefer OTEL to exporter if both enabled (#4137)
If both Exporter and OTel Metrics are enabled, CPK prefers OTel.
1 parent b0697a2 commit 46ee186

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

internal/pgmonitor/util.go

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"os"
1010

11+
"github.com/crunchydata/postgres-operator/internal/collector"
1112
"github.com/crunchydata/postgres-operator/internal/logging"
1213
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1314
)
@@ -27,6 +28,11 @@ func GetQueriesConfigDir(ctx context.Context) string {
2728

2829
// ExporterEnabled returns true if the monitoring exporter is enabled
2930
func ExporterEnabled(ctx context.Context, cluster *v1beta1.PostgresCluster) bool {
31+
// If OpenTelemetry metrics are enabled for this cluster, that takes precedence
32+
// over the postgres_exporter metrics.
33+
if collector.OpenTelemetryMetricsEnabled(ctx, cluster) {
34+
return false
35+
}
3036
if cluster.Spec.Monitoring == nil {
3137
return false
3238
}

internal/pgmonitor/util_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010

1111
"gotest.tools/v3/assert"
1212

13+
"github.com/crunchydata/postgres-operator/internal/feature"
14+
"github.com/crunchydata/postgres-operator/internal/testing/require"
1315
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1416
)
1517

@@ -26,4 +28,19 @@ func TestExporterEnabled(t *testing.T) {
2628

2729
cluster.Spec.Monitoring.PGMonitor.Exporter = &v1beta1.ExporterSpec{}
2830
assert.Assert(t, ExporterEnabled(ctx, cluster))
31+
32+
// Enabling the OpenTelemetryMetrics is not sufficient to disable the exporter
33+
gate := feature.NewGate()
34+
assert.NilError(t, gate.SetFromMap(map[string]bool{
35+
feature.OpenTelemetryMetrics: true,
36+
}))
37+
ctx = feature.NewContext(ctx, gate)
38+
assert.Assert(t, ExporterEnabled(ctx, cluster))
39+
40+
require.UnmarshalInto(t, &cluster.Spec, `{
41+
instrumentation: {
42+
logs: { retentionPeriod: 5h },
43+
},
44+
}`)
45+
assert.Assert(t, !ExporterEnabled(ctx, cluster))
2946
}

0 commit comments

Comments
 (0)