Skip to content

Commit 9b65fca

Browse files
committed
Refactor sending repo host pgbackrest logs to additional volume.
1 parent 1023c02 commit 9b65fca

File tree

5 files changed

+130
-231
lines changed

5 files changed

+130
-231
lines changed

internal/collector/helpers_test.go

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,9 @@
55
package collector
66

77
import (
8-
corev1 "k8s.io/api/core/v1"
9-
"k8s.io/apimachinery/pkg/api/resource"
10-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11-
12-
"github.com/crunchydata/postgres-operator/internal/initialize"
138
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
149
)
1510

16-
var (
17-
// TODO (testing): With the new RELATED_IMAGES defaulting behavior, tests could be refactored
18-
// to reference those environment variables instead of hard coded image values
19-
CrunchyPostgresHAImage = "registry.developers.crunchydata.com/crunchydata/crunchy-postgres:ubi8-13.6-1"
20-
CrunchyPGBackRestImage = "registry.developers.crunchydata.com/crunchydata/crunchy-pgbackrest:ubi8-2.38-0"
21-
CrunchyPGBouncerImage = "registry.developers.crunchydata.com/crunchydata/crunchy-pgbouncer:ubi8-1.16-2"
22-
)
23-
2411
func testInstrumentationSpec() *v1beta1.InstrumentationSpec {
2512
spec := v1beta1.InstrumentationSpec{
2613
Config: &v1beta1.InstrumentationConfigSpec{
@@ -43,58 +30,3 @@ func testInstrumentationSpec() *v1beta1.InstrumentationSpec {
4330

4431
return spec.DeepCopy()
4532
}
46-
47-
// Copied from postgrescluster package
48-
func testVolumeClaimSpec() v1beta1.VolumeClaimSpec {
49-
// Defines a volume claim spec that can be used to create instances
50-
return v1beta1.VolumeClaimSpec{
51-
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
52-
Resources: corev1.VolumeResourceRequirements{
53-
Requests: map[corev1.ResourceName]resource.Quantity{
54-
corev1.ResourceStorage: resource.MustParse("1Gi"),
55-
},
56-
},
57-
}
58-
}
59-
60-
// Copied from postgrescluster package (and then edited)
61-
func testCluster() *v1beta1.PostgresCluster {
62-
// Defines a base cluster spec that can be used by tests to generate a
63-
// cluster with an expected number of instances
64-
cluster := v1beta1.PostgresCluster{
65-
ObjectMeta: metav1.ObjectMeta{
66-
Name: "hippo",
67-
},
68-
Spec: v1beta1.PostgresClusterSpec{
69-
PostgresVersion: 13,
70-
Image: CrunchyPostgresHAImage,
71-
ImagePullSecrets: []corev1.LocalObjectReference{{
72-
Name: "myImagePullSecret"},
73-
},
74-
InstanceSets: []v1beta1.PostgresInstanceSetSpec{{
75-
Name: "instance1",
76-
Replicas: initialize.Int32(1),
77-
DataVolumeClaimSpec: testVolumeClaimSpec(),
78-
}},
79-
Backups: v1beta1.Backups{
80-
PGBackRest: v1beta1.PGBackRestArchive{
81-
Image: CrunchyPGBackRestImage,
82-
Repos: []v1beta1.PGBackRestRepo{{
83-
Name: "repo1",
84-
Volume: &v1beta1.RepoPVC{
85-
VolumeClaimSpec: testVolumeClaimSpec(),
86-
},
87-
}},
88-
RepoHost: &v1beta1.PGBackRestRepoHost{},
89-
},
90-
},
91-
Proxy: &v1beta1.PostgresProxySpec{
92-
PGBouncer: &v1beta1.PGBouncerPodSpec{
93-
Image: CrunchyPGBouncerImage,
94-
},
95-
},
96-
Instrumentation: testInstrumentationSpec(),
97-
},
98-
}
99-
return cluster.DeepCopy()
100-
}

internal/collector/pgbackrest.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"context"
99
_ "embed"
1010
"encoding/json"
11-
"fmt"
1211
"slices"
1312

1413
"github.com/crunchydata/postgres-operator/internal/naming"
@@ -23,28 +22,13 @@ var pgBackRestLogsTransforms json.RawMessage
2322

2423
func NewConfigForPgBackrestRepoHostPod(
2524
ctx context.Context,
26-
cluster *v1beta1.PostgresCluster,
25+
spec *v1beta1.InstrumentationSpec,
2726
repos []v1beta1.PGBackRestRepo,
27+
directory string,
2828
) *Config {
29-
config := NewConfig(cluster.Spec.Instrumentation)
29+
config := NewConfig(spec)
3030

31-
if OpenTelemetryLogsEnabled(ctx, cluster) {
32-
33-
var directory string
34-
for _, repo := range repos {
35-
if repo.Volume != nil {
36-
// If the user has set a log path in the spec, use it.
37-
// Otherwise, default to /pgbackrest/repo#/log
38-
if cluster.Spec.Backups.PGBackRest.RepoHost != nil &&
39-
cluster.Spec.Backups.PGBackRest.RepoHost.Log != nil &&
40-
cluster.Spec.Backups.PGBackRest.RepoHost.Log.Path != "" {
41-
directory = cluster.Spec.Backups.PGBackRest.RepoHost.Log.Path
42-
} else {
43-
directory = fmt.Sprintf(naming.PGBackRestRepoLogPath, repo.Name)
44-
}
45-
break
46-
}
47-
}
31+
if OpenTelemetryLogsEnabled(ctx, spec) {
4832

4933
// We should only enter this function if a PVC is assigned for a dedicated repohost
5034
// but if we don't have one, exit early.
@@ -107,9 +91,8 @@ func NewConfigForPgBackrestRepoHostPod(
10791
// If there are exporters to be added to the logs pipelines defined in
10892
// the spec, add them to the pipeline. Otherwise, add the DebugExporter.
10993
exporters := []ComponentID{DebugExporter}
110-
if cluster.Spec.Instrumentation != nil && cluster.Spec.Instrumentation.Logs != nil &&
111-
cluster.Spec.Instrumentation.Logs.Exporters != nil {
112-
exporters = slices.Clone(cluster.Spec.Instrumentation.Logs.Exporters)
94+
if spec != nil && spec.Logs != nil && spec.Logs.Exporters != nil {
95+
exporters = slices.Clone(spec.Logs.Exporters)
11396
}
11497

11598
config.Pipelines["logs/pgbackrest"] = Pipeline{

internal/collector/pgbackrest_test.go

Lines changed: 8 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ func TestNewConfigForPgBackrestRepoHostPod(t *testing.T) {
3030
}
3131
var instrumentation *v1beta1.InstrumentationSpec
3232
require.UnmarshalInto(t, &instrumentation, `{}`)
33-
cluster := testCluster()
34-
cluster.Spec.Instrumentation = instrumentation
35-
config := NewConfigForPgBackrestRepoHostPod(ctx, cluster, repos)
33+
config := NewConfigForPgBackrestRepoHostPod(ctx, instrumentation, repos, "/test/directory")
3634

3735
result, err := config.ToYAML()
3836
assert.NilError(t, err)
@@ -44,7 +42,7 @@ exporters:
4442
extensions:
4543
file_storage/pgbackrest_logs:
4644
create_directory: false
47-
directory: /pgbackrest/repo1/log/receiver
45+
directory: /test/directory/receiver
4846
fsync: true
4947
processors:
5048
batch/1s:
@@ -102,8 +100,8 @@ processors:
102100
receivers:
103101
filelog/pgbackrest_log:
104102
include:
105-
- /pgbackrest/repo1/log/*.log
106-
- /pgbackrest/repo1/log/*.log.1
103+
- /test/directory/*.log
104+
- /test/directory/*.log.1
107105
multiline:
108106
line_start_pattern: ^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}|^-{19}
109107
storage: file_storage/pgbackrest_logs
@@ -137,8 +135,7 @@ service:
137135
Volume: new(v1beta1.RepoPVC),
138136
},
139137
}
140-
cluster := testCluster()
141-
config := NewConfigForPgBackrestRepoHostPod(ctx, cluster, repos)
138+
config := NewConfigForPgBackrestRepoHostPod(ctx, testInstrumentationSpec(), repos, "/another/directory")
142139

143140
result, err := config.ToYAML()
144141
assert.NilError(t, err)
@@ -154,7 +151,7 @@ exporters:
154151
extensions:
155152
file_storage/pgbackrest_logs:
156153
create_directory: false
157-
directory: /pgbackrest/repo1/log/receiver
154+
directory: /another/directory/receiver
158155
fsync: true
159156
processors:
160157
batch/1s:
@@ -212,121 +209,8 @@ processors:
212209
receivers:
213210
filelog/pgbackrest_log:
214211
include:
215-
- /pgbackrest/repo1/log/*.log
216-
- /pgbackrest/repo1/log/*.log.1
217-
multiline:
218-
line_start_pattern: ^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}|^-{19}
219-
storage: file_storage/pgbackrest_logs
220-
service:
221-
extensions:
222-
- file_storage/pgbackrest_logs
223-
pipelines:
224-
logs/pgbackrest:
225-
exporters:
226-
- googlecloud
227-
processors:
228-
- resource/pgbackrest
229-
- transform/pgbackrest_logs
230-
- resourcedetection
231-
- batch/logs
232-
- groupbyattrs/compact
233-
receivers:
234-
- filelog/pgbackrest_log
235-
`)
236-
})
237-
238-
t.Run("LogPathDefined", func(t *testing.T) {
239-
gate := feature.NewGate()
240-
assert.NilError(t, gate.SetFromMap(map[string]bool{
241-
feature.OpenTelemetryLogs: true,
242-
}))
243-
ctx := feature.NewContext(context.Background(), gate)
244-
repos := []v1beta1.PGBackRestRepo{
245-
{
246-
Name: "repo1",
247-
Volume: new(v1beta1.RepoPVC),
248-
},
249-
}
250-
cluster := testCluster()
251-
cluster.Spec.Backups.PGBackRest.RepoHost.Log = &v1beta1.LoggingConfiguration{
252-
Path: "/test/path",
253-
}
254-
config := NewConfigForPgBackrestRepoHostPod(ctx, cluster, repos)
255-
256-
result, err := config.ToYAML()
257-
assert.NilError(t, err)
258-
assert.DeepEqual(t, result, `# Generated by postgres-operator. DO NOT EDIT.
259-
# Your changes will not be saved.
260-
exporters:
261-
debug:
262-
verbosity: detailed
263-
googlecloud:
264-
log:
265-
default_log_name: opentelemetry.io/collector-exported-log
266-
project: google-project-name
267-
extensions:
268-
file_storage/pgbackrest_logs:
269-
create_directory: false
270-
directory: /test/path/receiver
271-
fsync: true
272-
processors:
273-
batch/1s:
274-
timeout: 1s
275-
batch/200ms:
276-
timeout: 200ms
277-
batch/logs:
278-
send_batch_size: 8192
279-
timeout: 200ms
280-
groupbyattrs/compact: {}
281-
resource/pgbackrest:
282-
attributes:
283-
- action: insert
284-
key: k8s.container.name
285-
value: pgbackrest
286-
- action: insert
287-
key: k8s.namespace.name
288-
value: ${env:K8S_POD_NAMESPACE}
289-
- action: insert
290-
key: k8s.pod.name
291-
value: ${env:K8S_POD_NAME}
292-
- action: insert
293-
key: process.executable.name
294-
value: pgbackrest
295-
resourcedetection:
296-
detectors: []
297-
override: false
298-
timeout: 30s
299-
transform/pgbackrest_logs:
300-
log_statements:
301-
- statements:
302-
- set(instrumentation_scope.name, "pgbackrest")
303-
- set(instrumentation_scope.schema_url, "https://opentelemetry.io/schemas/1.29.0")
304-
- 'merge_maps(log.cache, ExtractPatterns(log.body, "^(?<timestamp>\\d{4}-\\d{2}-\\d{2}
305-
\\d{2}:\\d{2}:\\d{2}\\.\\d{3}) (?<process_id>P\\d{2,3})\\s*(?<error_severity>\\S*):
306-
(?<message>(?s).*)$"), "insert") where Len(log.body) > 0'
307-
- set(log.severity_text, log.cache["error_severity"]) where IsString(log.cache["error_severity"])
308-
- set(log.severity_number, SEVERITY_NUMBER_TRACE) where log.severity_text ==
309-
"TRACE"
310-
- set(log.severity_number, SEVERITY_NUMBER_DEBUG) where log.severity_text ==
311-
"DEBUG"
312-
- set(log.severity_number, SEVERITY_NUMBER_DEBUG2) where log.severity_text ==
313-
"DETAIL"
314-
- set(log.severity_number, SEVERITY_NUMBER_INFO) where log.severity_text ==
315-
"INFO"
316-
- set(log.severity_number, SEVERITY_NUMBER_WARN) where log.severity_text ==
317-
"WARN"
318-
- set(log.severity_number, SEVERITY_NUMBER_ERROR) where log.severity_text ==
319-
"ERROR"
320-
- set(log.time, Time(log.cache["timestamp"], "%Y-%m-%d %H:%M:%S.%L")) where
321-
IsString(log.cache["timestamp"])
322-
- set(log.attributes["process.pid"], log.cache["process_id"])
323-
- set(log.attributes["log.record.original"], log.body)
324-
- set(log.body, log.cache["message"])
325-
receivers:
326-
filelog/pgbackrest_log:
327-
include:
328-
- /test/path/*.log
329-
- /test/path/*.log.1
212+
- /another/directory/*.log
213+
- /another/directory/*.log.1
330214
multiline:
331215
line_start_pattern: ^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}|^-{19}
332216
storage: file_storage/pgbackrest_logs

internal/pgbackrest/config.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,33 @@ func CreatePGBackRestConfigMapIntent(ctx context.Context, postgresCluster *v1bet
133133
).String()
134134

135135
if collector.OpenTelemetryLogsOrMetricsEnabled(ctx, postgresCluster) {
136+
// Get pgbackrest log path for repo host pod
137+
var pgBackRestLogPath string
138+
for _, repo := range postgresCluster.Spec.Backups.PGBackRest.Repos {
139+
if repo.Volume != nil {
140+
// If the user has set a log path in the spec, use it.
141+
// Otherwise, default to /pgbackrest/repo#/log
142+
if postgresCluster.Spec.Backups.PGBackRest.RepoHost != nil &&
143+
postgresCluster.Spec.Backups.PGBackRest.RepoHost.Log != nil &&
144+
postgresCluster.Spec.Backups.PGBackRest.RepoHost.Log.Path != "" {
145+
pgBackRestLogPath = postgresCluster.Spec.Backups.PGBackRest.RepoHost.Log.Path
146+
} else {
147+
pgBackRestLogPath = fmt.Sprintf(naming.PGBackRestRepoLogPath, repo.Name)
148+
}
149+
break
150+
}
151+
}
136152

137153
err = collector.AddToConfigMap(ctx, collector.NewConfigForPgBackrestRepoHostPod(
138154
ctx,
139-
postgresCluster,
155+
postgresCluster.Spec.Instrumentation,
140156
postgresCluster.Spec.Backups.PGBackRest.Repos,
157+
pgBackRestLogPath,
141158
), cm)
142159

143160
// If OTel logging is enabled, add logrotate config for the RepoHost
144161
if err == nil &&
145162
collector.OpenTelemetryLogsEnabled(ctx, postgresCluster) {
146-
var pgBackRestLogPath string
147-
for _, repo := range postgresCluster.Spec.Backups.PGBackRest.Repos {
148-
if repo.Volume != nil {
149-
// If the user has set a log path in the spec, use it.
150-
// Otherwise, default to /pgbackrest/repo#/log
151-
if postgresCluster.Spec.Backups.PGBackRest.RepoHost != nil &&
152-
postgresCluster.Spec.Backups.PGBackRest.RepoHost.Log != nil &&
153-
postgresCluster.Spec.Backups.PGBackRest.RepoHost.Log.Path != "" {
154-
pgBackRestLogPath = postgresCluster.Spec.Backups.PGBackRest.RepoHost.Log.Path
155-
} else {
156-
pgBackRestLogPath = fmt.Sprintf(naming.PGBackRestRepoLogPath, repo.Name)
157-
}
158-
break
159-
}
160-
}
161163

162164
collector.AddLogrotateConfigs(ctx, postgresCluster.Spec.Instrumentation, cm, []collector.LogrotateConfig{{
163165
LogFiles: []string{pgBackRestLogPath + "/*.log"},

0 commit comments

Comments
 (0)