Skip to content

Commit 02249f8

Browse files
committed
rotate postgres logs WIP
1 parent 9977db2 commit 02249f8

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

internal/collector/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
_ "embed"
1010
"fmt"
1111
"math"
12+
"strings"
1213

1314
corev1 "k8s.io/api/core/v1"
1415
"k8s.io/apimachinery/pkg/util/sets"
@@ -118,7 +119,7 @@ func NewConfig(spec *v1beta1.InstrumentationSpec) *Config {
118119
// AddLogrotateConfig generates a logrotate configuration and adds it to the
119120
// provided configmap
120121
func AddLogrotateConfig(ctx context.Context, spec *v1beta1.InstrumentationSpec,
121-
outInstanceConfigMap *corev1.ConfigMap, logFilePath, postrotateScript string,
122+
outInstanceConfigMap *corev1.ConfigMap, logFilesToRotate []string, postrotateScript string,
122123
) error {
123124
var err error
124125
var retentionPeriod *v1beta1.Duration
@@ -138,22 +139,22 @@ func AddLogrotateConfig(ctx context.Context, spec *v1beta1.InstrumentationSpec,
138139
}
139140
}
140141

141-
outInstanceConfigMap.Data["logrotate.conf"] = generateLogrotateConfig(logFilePath,
142+
outInstanceConfigMap.Data["logrotate.conf"] = generateLogrotateConfig(logFilesToRotate,
142143
retentionPeriod, postrotateScript)
143144

144145
return err
145146
}
146147

147148
// generateLogrotateConfig generates a configuration string for logrotate based
148149
// on the provided full log file path, retention period, and postrotate script
149-
func generateLogrotateConfig(logFilePath string, retentionPeriod *v1beta1.Duration,
150+
func generateLogrotateConfig(logFilesToRotate []string, retentionPeriod *v1beta1.Duration,
150151
postrotateScript string,
151152
) string {
152153
number, interval := parseDurationForLogrotate(retentionPeriod)
153154

154155
return fmt.Sprintf(
155156
logrotateConfigFormatString,
156-
logFilePath,
157+
strings.Join(logFilesToRotate, " "),
157158
number,
158159
interval,
159160
postrotateScript,

internal/collector/postgres.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func NewConfigForPostgresPod(ctx context.Context,
3737
//go:embed "generated/postgres_logs_transforms.json"
3838
var postgresLogsTransforms json.RawMessage
3939

40+
// PostgresPostRotateScript is the script that is run after postgres's log
41+
// files have been rotated.
42+
const PostgresPostRotateScript = "pkill -USR1 --exact --parent=1 postgres"
43+
4044
// postgresCSVNames returns the names of fields in the CSV logs for version.
4145
func postgresCSVNames(version int) string {
4246
// JSON is the preferred format, so use those names.
@@ -100,11 +104,11 @@ func EnablePostgresLogging(
100104
// probably requires another process that deletes the oldest files.
101105
//
102106
// The ".log" suffix is replaced by ".json" for JSON log files.
103-
outParameters.Mandatory.Add("log_filename", "postgresql-%a.log")
107+
outParameters.Mandatory.Add("log_filename", "postgresql")
104108
outParameters.Mandatory.Add("log_file_mode", "0660")
105-
outParameters.Mandatory.Add("log_rotation_age", "1d")
109+
outParameters.Mandatory.Add("log_rotation_age", "0")
106110
outParameters.Mandatory.Add("log_rotation_size", "0")
107-
outParameters.Mandatory.Add("log_truncate_on_rotation", "on")
111+
outParameters.Mandatory.Add("log_truncate_on_rotation", "off")
108112

109113
// Log in a timezone that the OpenTelemetry Collector will understand.
110114
outParameters.Mandatory.Add("log_timezone", "UTC")
@@ -124,7 +128,7 @@ func EnablePostgresLogging(
124128
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/receiver/filelogreceiver#readme
125129
outConfig.Receivers["filelog/postgres_csvlog"] = map[string]any{
126130
// Read the CSV files and keep track of what has been processed.
127-
"include": []string{directory + "/*.csv"},
131+
"include": []string{directory + "/*.csv", directory + "/*.csv.1"},
128132
"storage": "file_storage/postgres_logs",
129133

130134
// Postgres does not escape newlines in its CSV log format. Search for
@@ -155,7 +159,7 @@ func EnablePostgresLogging(
155159
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/receiver/filelogreceiver#readme
156160
outConfig.Receivers["filelog/postgres_jsonlog"] = map[string]any{
157161
// Read the JSON files and keep track of what has been processed.
158-
"include": []string{directory + "/*.json"},
162+
"include": []string{directory + "/*.json", directory + "/*.json.1"},
159163
"storage": "file_storage/postgres_logs",
160164

161165
// Differentiate these from the CSV ones above.

internal/collector/postgres_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ receivers:
190190
filelog/postgres_csvlog:
191191
include:
192192
- /pgdata/logs/postgres/*.csv
193+
- /pgdata/logs/postgres/*.csv.1
193194
multiline:
194195
line_start_pattern: ^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d.\d{3} UTC,(?:"[_\D](?:[^"]|"")*")?,(?:"[_\D](?:[^"]|"")*")?,\d*,(?:"(?:[^"]|"")+")?,[0-9a-f]+[.][0-9a-f]+,\d+,
195196
operators:
@@ -206,6 +207,7 @@ receivers:
206207
filelog/postgres_jsonlog:
207208
include:
208209
- /pgdata/logs/postgres/*.json
210+
- /pgdata/logs/postgres/*.json.1
209211
operators:
210212
- from: body
211213
to: body.original
@@ -422,6 +424,7 @@ receivers:
422424
filelog/postgres_csvlog:
423425
include:
424426
- /pgdata/logs/postgres/*.csv
427+
- /pgdata/logs/postgres/*.csv.1
425428
multiline:
426429
line_start_pattern: ^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d.\d{3} UTC,(?:"[_\D](?:[^"]|"")*")?,(?:"[_\D](?:[^"]|"")*")?,\d*,(?:"(?:[^"]|"")+")?,[0-9a-f]+[.][0-9a-f]+,\d+,
427430
operators:
@@ -438,6 +441,7 @@ receivers:
438441
filelog/postgres_jsonlog:
439442
include:
440443
- /pgdata/logs/postgres/*.json
444+
- /pgdata/logs/postgres/*.json.1
441445
operators:
442446
- from: body
443447
to: body.original

internal/controller/postgrescluster/instance.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,7 @@ func (r *Reconciler) reconcileInstance(
12061206
// TODO: Setting the includeLogrotate argument to false for now. This
12071207
// should be changed when we implement log rotation for postgres
12081208
collector.AddToPod(ctx, cluster.Spec.Instrumentation, cluster.Spec.ImagePullPolicy, instanceConfigMap, &instance.Spec.Template.Spec,
1209-
[]corev1.VolumeMount{postgres.DataVolumeMount()}, "", false)
1209+
[]corev1.VolumeMount{postgres.DataVolumeMount()}, "", true)
12101210
}
12111211

12121212
if err == nil &&
@@ -1427,6 +1427,15 @@ func (r *Reconciler) reconcileInstanceConfigMap(
14271427
if err == nil && (feature.Enabled(ctx, feature.OpenTelemetryLogs) || feature.Enabled(ctx, feature.OpenTelemetryMetrics)) {
14281428
err = collector.AddToConfigMap(ctx, otelConfig, instanceConfigMap)
14291429
}
1430+
// If OTel logging is enabled, add logrotate config
1431+
if err == nil && otelConfig != nil && feature.Enabled(ctx, feature.OpenTelemetryLogs) {
1432+
logFilesToRotate := []string{
1433+
postgres.LogDirectory() + "/postgresql.json",
1434+
postgres.LogDirectory() + "/postgresql.csv",
1435+
}
1436+
err = collector.AddLogrotateConfig(ctx, cluster.Spec.Instrumentation, instanceConfigMap,
1437+
logFilesToRotate, collector.PostgresPostRotateScript)
1438+
}
14301439
if err == nil {
14311440
err = patroni.InstanceConfigMap(ctx, cluster, spec, instanceConfigMap)
14321441
}

internal/controller/postgrescluster/pgbouncer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (r *Reconciler) reconcilePGBouncerConfigMap(
106106
// If OTel logging is enabled, add logrotate config
107107
if err == nil && otelConfig != nil && feature.Enabled(ctx, feature.OpenTelemetryLogs) {
108108
err = collector.AddLogrotateConfig(ctx, cluster.Spec.Instrumentation, configmap,
109-
naming.PGBouncerFullLogPath, collector.PGBouncerPostRotateScript)
109+
[]string{naming.PGBouncerFullLogPath}, collector.PGBouncerPostRotateScript)
110110
}
111111
if err == nil {
112112
err = errors.WithStack(r.apply(ctx, configmap))

0 commit comments

Comments
 (0)