Skip to content

Commit 345d90f

Browse files
authored
Add util func for adding collector logic (#4128)
Check feature gates and check spec In our original execution, we had a mix of logic to enable OTel: some logic required just the feature gate, some logic required the feature gates AND the instrumentation spec. This PR regularizes the logic: every check require both gates and spec to indicate the user wants instrumentation; specific checks for logs/metrics within larger checks can be left as is. Note: This PR also removes the instrumentation check from ExporterEnabled. We may want to re-add logic like that and be clear about which takes precedence.
1 parent d1c228d commit 345d90f

29 files changed

+157
-90
lines changed

internal/collector/instance.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ func AddToPod(
5050
includeLogrotate bool,
5151
thisPodServesMetrics bool,
5252
) {
53-
if spec == nil ||
54-
!(feature.Enabled(ctx, feature.OpenTelemetryLogs) ||
55-
feature.Enabled(ctx, feature.OpenTelemetryMetrics)) {
53+
if !OpenTelemetryLogsOrMetricsEnabled(ctx, spec) {
5654
return
5755
}
5856

internal/collector/naming.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const PGBouncerMetrics = "metrics/pgbouncer"
1515
const PostgresMetrics = "metrics/postgres"
1616
const PatroniMetrics = "metrics/patroni"
1717
const ResourceDetectionProcessor = "resourcedetection"
18+
const MonitoringUser = "ccp_monitoring"
1819

1920
const SqlQuery = "sqlquery"
2021

internal/collector/patroni.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"slices"
1010
"strconv"
1111

12-
"github.com/crunchydata/postgres-operator/internal/feature"
1312
"github.com/crunchydata/postgres-operator/internal/naming"
1413
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1514
)
@@ -23,7 +22,7 @@ func EnablePatroniLogging(ctx context.Context,
2322
spec = inCluster.Spec.Instrumentation.Logs
2423
}
2524

26-
if feature.Enabled(ctx, feature.OpenTelemetryLogs) {
25+
if OpenTelemetryLogsEnabled(ctx, inCluster) {
2726
directory := naming.PatroniPGDataLogPath
2827

2928
// Keep track of what log records and files have been processed.
@@ -134,7 +133,7 @@ func EnablePatroniMetrics(ctx context.Context,
134133
inCluster *v1beta1.PostgresCluster,
135134
outConfig *Config,
136135
) {
137-
if feature.Enabled(ctx, feature.OpenTelemetryMetrics) {
136+
if OpenTelemetryMetricsEnabled(ctx, inCluster) {
138137
// Add Prometheus exporter
139138
outConfig.Exporters[Prometheus] = map[string]any{
140139
"endpoint": "0.0.0.0:" + strconv.Itoa(PrometheusPort),

internal/collector/patroni_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"gotest.tools/v3/assert"
1212

1313
"github.com/crunchydata/postgres-operator/internal/feature"
14+
"github.com/crunchydata/postgres-operator/internal/testing/require"
1415
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1516
)
1617

@@ -23,8 +24,14 @@ func TestEnablePatroniLogging(t *testing.T) {
2324
ctx := feature.NewContext(context.Background(), gate)
2425

2526
config := NewConfig(nil)
27+
cluster := new(v1beta1.PostgresCluster)
28+
require.UnmarshalInto(t, &cluster.Spec, `{
29+
instrumentation: {
30+
logs: { retentionPeriod: 5h },
31+
},
32+
}`)
2633

27-
EnablePatroniLogging(ctx, new(v1beta1.PostgresCluster), config)
34+
EnablePatroniLogging(ctx, cluster, config)
2835

2936
result, err := config.ToYAML()
3037
assert.NilError(t, err)

internal/collector/pgadmin.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import (
1010

1111
corev1 "k8s.io/api/core/v1"
1212

13-
"github.com/crunchydata/postgres-operator/internal/feature"
1413
"github.com/crunchydata/postgres-operator/internal/naming"
1514
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1615
)
1716

1817
func EnablePgAdminLogging(ctx context.Context, spec *v1beta1.InstrumentationSpec,
1918
configmap *corev1.ConfigMap,
2019
) error {
21-
if !feature.Enabled(ctx, feature.OpenTelemetryLogs) {
20+
if !OpenTelemetryLogsEnabled(ctx, spec) {
2221
return nil
2322
}
23+
2424
otelConfig := NewConfig(spec)
2525

2626
otelConfig.Extensions["file_storage/pgadmin_data_logs"] = map[string]any{
@@ -125,5 +125,6 @@ func EnablePgAdminLogging(ctx context.Context, spec *v1beta1.InstrumentationSpec
125125
if err == nil {
126126
configmap.Data["collector.yaml"] = otelYAML
127127
}
128+
128129
return err
129130
}

internal/collector/pgadmin_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
corev1 "k8s.io/api/core/v1"
1313

1414
"github.com/crunchydata/postgres-operator/internal/collector"
15-
pgadmin "github.com/crunchydata/postgres-operator/internal/controller/standalone_pgadmin"
1615
"github.com/crunchydata/postgres-operator/internal/feature"
1716
"github.com/crunchydata/postgres-operator/internal/initialize"
1817
"github.com/crunchydata/postgres-operator/internal/testing/cmp"
@@ -31,7 +30,11 @@ func TestEnablePgAdminLogging(t *testing.T) {
3130

3231
configmap := new(corev1.ConfigMap)
3332
initialize.Map(&configmap.Data)
34-
err := collector.EnablePgAdminLogging(ctx, nil, configmap)
33+
var instrumentation *v1beta1.InstrumentationSpec
34+
require.UnmarshalInto(t, &instrumentation, `{
35+
logs: { retentionPeriod: 12h },
36+
}`)
37+
err := collector.EnablePgAdminLogging(ctx, instrumentation, configmap)
3538
assert.NilError(t, err)
3639

3740
assert.Assert(t, cmp.MarshalMatches(configmap.Data, `
@@ -44,7 +47,7 @@ collector.yaml: |
4447
extensions:
4548
file_storage/pgadmin_data_logs:
4649
create_directory: false
47-
directory: `+pgadmin.LogDirectoryAbsolutePath+`/receiver
50+
directory: /var/lib/pgadmin/logs/receiver
4851
fsync: true
4952
processors:
5053
batch/1s:
@@ -90,11 +93,11 @@ collector.yaml: |
9093
receivers:
9194
filelog/gunicorn:
9295
include:
93-
- `+pgadmin.GunicornLogFileAbsolutePath+`
96+
- /var/lib/pgadmin/logs/gunicorn.log
9497
storage: file_storage/pgadmin_data_logs
9598
filelog/pgadmin:
9699
include:
97-
- `+pgadmin.LogFileAbsolutePath+`
100+
- /var/lib/pgadmin/logs/pgadmin.log
98101
storage: file_storage/pgadmin_data_logs
99102
service:
100103
extensions:
@@ -165,7 +168,7 @@ collector.yaml: |
165168
extensions:
166169
file_storage/pgadmin_data_logs:
167170
create_directory: false
168-
directory: `+pgadmin.LogDirectoryAbsolutePath+`/receiver
171+
directory: /var/lib/pgadmin/logs/receiver
169172
fsync: true
170173
processors:
171174
batch/1s:
@@ -211,11 +214,11 @@ collector.yaml: |
211214
receivers:
212215
filelog/gunicorn:
213216
include:
214-
- `+pgadmin.GunicornLogFileAbsolutePath+`
217+
- /var/lib/pgadmin/logs/gunicorn.log
215218
storage: file_storage/pgadmin_data_logs
216219
filelog/pgadmin:
217220
include:
218-
- `+pgadmin.LogFileAbsolutePath+`
221+
- /var/lib/pgadmin/logs/pgadmin.log
219222
storage: file_storage/pgadmin_data_logs
220223
service:
221224
extensions:

internal/collector/pgbackrest.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"fmt"
1212
"slices"
1313

14-
"github.com/crunchydata/postgres-operator/internal/feature"
1514
"github.com/crunchydata/postgres-operator/internal/naming"
1615
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1716
)
@@ -29,7 +28,7 @@ func NewConfigForPgBackrestRepoHostPod(
2928
) *Config {
3029
config := NewConfig(spec)
3130

32-
if feature.Enabled(ctx, feature.OpenTelemetryLogs) {
31+
if OpenTelemetryLogsEnabled(ctx, spec) {
3332

3433
var directory string
3534
for _, repo := range repos {

internal/collector/pgbackrest_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"gotest.tools/v3/assert"
1212

1313
"github.com/crunchydata/postgres-operator/internal/feature"
14+
"github.com/crunchydata/postgres-operator/internal/testing/require"
1415
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1516
)
1617

@@ -27,8 +28,12 @@ func TestNewConfigForPgBackrestRepoHostPod(t *testing.T) {
2728
Volume: new(v1beta1.RepoPVC),
2829
},
2930
}
31+
var instrumentation *v1beta1.InstrumentationSpec
32+
require.UnmarshalInto(t, &instrumentation, `{
33+
logs: { retentionPeriod: 12h },
34+
}`)
3035

31-
config := NewConfigForPgBackrestRepoHostPod(ctx, nil, repos)
36+
config := NewConfigForPgBackrestRepoHostPod(ctx, instrumentation, repos)
3237

3338
result, err := config.ToYAML()
3439
assert.NilError(t, err)

internal/collector/pgbouncer.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"slices"
1313
"strconv"
1414

15-
"github.com/crunchydata/postgres-operator/internal/feature"
1615
"github.com/crunchydata/postgres-operator/internal/naming"
1716
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1817
)
@@ -40,7 +39,7 @@ func NewConfigForPgBouncerPod(
4039
config := NewConfig(cluster.Spec.Instrumentation)
4140

4241
EnablePgBouncerLogging(ctx, cluster, config)
43-
EnablePgBouncerMetrics(ctx, config, sqlQueryUsername)
42+
EnablePgBouncerMetrics(ctx, cluster, config, sqlQueryUsername)
4443

4544
return config
4645
}
@@ -56,7 +55,7 @@ func EnablePgBouncerLogging(ctx context.Context,
5655
spec = inCluster.Spec.Instrumentation.Logs
5756
}
5857

59-
if feature.Enabled(ctx, feature.OpenTelemetryLogs) {
58+
if OpenTelemetryLogsEnabled(ctx, inCluster) {
6059
directory := naming.PGBouncerLogPath
6160

6261
// Keep track of what log records and files have been processed.
@@ -171,8 +170,10 @@ func EnablePgBouncerLogging(ctx context.Context,
171170

172171
// EnablePgBouncerMetrics adds necessary configuration to the collector config to scrape
173172
// metrics from pgBouncer when the OpenTelemetryMetrics feature flag is enabled.
174-
func EnablePgBouncerMetrics(ctx context.Context, config *Config, sqlQueryUsername string) {
175-
if feature.Enabled(ctx, feature.OpenTelemetryMetrics) {
173+
func EnablePgBouncerMetrics(ctx context.Context, inCluster *v1beta1.PostgresCluster,
174+
config *Config, sqlQueryUsername string) {
175+
176+
if OpenTelemetryMetricsEnabled(ctx, inCluster) {
176177
// Add Prometheus exporter
177178
config.Exporters[Prometheus] = map[string]any{
178179
"endpoint": "0.0.0.0:" + strconv.Itoa(PrometheusPort),

internal/collector/pgbouncer_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"gotest.tools/v3/assert"
1212

1313
"github.com/crunchydata/postgres-operator/internal/feature"
14+
"github.com/crunchydata/postgres-operator/internal/testing/require"
1415
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1516
)
1617

@@ -23,8 +24,13 @@ func TestEnablePgBouncerLogging(t *testing.T) {
2324
ctx := feature.NewContext(context.Background(), gate)
2425

2526
config := NewConfig(nil)
26-
27-
EnablePgBouncerLogging(ctx, new(v1beta1.PostgresCluster), config)
27+
cluster := new(v1beta1.PostgresCluster)
28+
require.UnmarshalInto(t, &cluster.Spec, `{
29+
instrumentation: {
30+
logs: { retentionPeriod: 5h },
31+
},
32+
}`)
33+
EnablePgBouncerLogging(ctx, cluster, config)
2834

2935
result, err := config.ToYAML()
3036
assert.NilError(t, err)

internal/collector/postgres.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1717

18-
"github.com/crunchydata/postgres-operator/internal/feature"
1918
"github.com/crunchydata/postgres-operator/internal/naming"
2019
"github.com/crunchydata/postgres-operator/internal/postgres"
2120
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
@@ -86,7 +85,7 @@ func EnablePostgresLogging(
8685
spec = inCluster.Spec.Instrumentation.Logs
8786
}
8887

89-
if inCluster != nil && feature.Enabled(ctx, feature.OpenTelemetryLogs) {
88+
if OpenTelemetryLogsEnabled(ctx, inCluster) {
9089
directory := postgres.LogDirectory()
9190
version := inCluster.Spec.PostgresVersion
9291

internal/collector/postgres_metrics.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ import (
1212
"slices"
1313
"strconv"
1414

15-
"github.com/crunchydata/postgres-operator/internal/feature"
1615
"github.com/crunchydata/postgres-operator/internal/logging"
17-
"github.com/crunchydata/postgres-operator/internal/pgmonitor"
1816
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1917
)
2018

@@ -59,7 +57,7 @@ type metric struct {
5957
}
6058

6159
func EnablePostgresMetrics(ctx context.Context, inCluster *v1beta1.PostgresCluster, config *Config) {
62-
if feature.Enabled(ctx, feature.OpenTelemetryMetrics) {
60+
if OpenTelemetryMetricsEnabled(ctx, inCluster) {
6361
log := logging.FromContext(ctx)
6462
var err error
6563

@@ -131,7 +129,7 @@ func EnablePostgresMetrics(ctx context.Context, inCluster *v1beta1.PostgresClust
131129
"driver": "postgres",
132130
"datasource": fmt.Sprintf(
133131
`host=localhost dbname=postgres port=5432 user=%s password=${env:PGPASSWORD}`,
134-
pgmonitor.MonitoringUser),
132+
MonitoringUser),
135133
"collection_interval": "5s",
136134
// Give Postgres time to finish setup.
137135
"initial_delay": "10s",
@@ -142,7 +140,7 @@ func EnablePostgresMetrics(ctx context.Context, inCluster *v1beta1.PostgresClust
142140
"driver": "postgres",
143141
"datasource": fmt.Sprintf(
144142
`host=localhost dbname=postgres port=5432 user=%s password=${env:PGPASSWORD}`,
145-
pgmonitor.MonitoringUser),
143+
MonitoringUser),
146144
"collection_interval": "300s",
147145
// Give Postgres time to finish setup.
148146
"initial_delay": "10s",
@@ -172,7 +170,7 @@ func EnablePostgresMetrics(ctx context.Context, inCluster *v1beta1.PostgresClust
172170
"driver": "postgres",
173171
"datasource": fmt.Sprintf(
174172
`host=localhost dbname=postgres port=5432 user=%s password=${env:PGPASSWORD}`,
175-
pgmonitor.MonitoringUser),
173+
MonitoringUser),
176174
"collection_interval": querySet.CollectionInterval,
177175
// Give Postgres time to finish setup.
178176
"initial_delay": "10s",

internal/collector/postgres_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/crunchydata/postgres-operator/internal/feature"
1414
"github.com/crunchydata/postgres-operator/internal/postgres"
15+
"github.com/crunchydata/postgres-operator/internal/testing/require"
1516
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
1617
)
1718

@@ -25,6 +26,11 @@ func TestEnablePostgresLogging(t *testing.T) {
2526

2627
cluster := new(v1beta1.PostgresCluster)
2728
cluster.Spec.PostgresVersion = 99
29+
require.UnmarshalInto(t, &cluster.Spec, `{
30+
instrumentation: {
31+
logs: { retentionPeriod: 5h },
32+
},
33+
}`)
2834

2935
config := NewConfig(nil)
3036
params := postgres.NewParameterSet()

0 commit comments

Comments
 (0)