Skip to content

Commit a1f8383

Browse files
committed
Rotate logs whether retentionPeriod is set or not. Default to 1 day when not set.
Move PGBouncerPostRotateScript to collector package. Round daily retention count up to the nearest day.
1 parent 80a0fd5 commit a1f8383

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

internal/collector/config.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,29 @@ func NewConfig(spec *v1beta1.InstrumentationSpec) *Config {
119119
// provided configmap
120120
func AddLogrotateConfig(ctx context.Context, spec *v1beta1.InstrumentationSpec,
121121
outInstanceConfigMap *corev1.ConfigMap, logFilePath, postrotateScript string,
122-
) {
123-
var logrotateConfig string
122+
) error {
123+
var err error
124+
var retentionPeriod *v1beta1.Duration
125+
124126
if outInstanceConfigMap.Data == nil {
125127
outInstanceConfigMap.Data = make(map[string]string)
126128
}
127129

130+
// If retentionPeriod is set in the spec, use that value; otherwise, we want
131+
// to use a reasonably short duration. Defaulting to 1 day.
128132
if spec != nil && spec.Logs != nil && spec.Logs.RetentionPeriod != nil {
129-
logrotateConfig = generateLogrotateConfig(logFilePath, spec.Logs.RetentionPeriod,
130-
postrotateScript)
133+
retentionPeriod = spec.Logs.RetentionPeriod
134+
} else {
135+
retentionPeriod, err = v1beta1.NewDuration("1d")
136+
if err != nil {
137+
return err
138+
}
131139
}
132140

133-
outInstanceConfigMap.Data["logrotate.conf"] = logrotateConfig
141+
outInstanceConfigMap.Data["logrotate.conf"] = generateLogrotateConfig(logFilePath,
142+
retentionPeriod, postrotateScript)
143+
144+
return err
134145
}
135146

136147
// generateLogrotateConfig generates a configuration string for logrotate based
@@ -150,11 +161,14 @@ func generateLogrotateConfig(logFilePath string, retentionPeriod *v1beta1.Durati
150161
}
151162

152163
// parseDurationForLogrotate takes a retention period and returns the rotate
153-
// number and interval string that should be used in the logrotate config
164+
// number and interval string that should be used in the logrotate config.
165+
// If the retentionPeriod is less than 24 hours, the function will return the
166+
// number of hours and "hourly"; otherwise, we will round up to the nearest day
167+
// and return the day count and "daily"
154168
func parseDurationForLogrotate(retentionPeriod *v1beta1.Duration) (int, string) {
155169
hours := math.Round(retentionPeriod.AsDuration().Hours())
156170
if hours < 24 {
157171
return int(hours), "hourly"
158172
}
159-
return int(math.Round(hours / 24)), "daily"
173+
return int(math.Ceil(hours / 24)), "daily"
160174
}

internal/collector/config_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ service:
6464
})
6565
}
6666

67-
// TODO: write this test after rebasing on new retention API changes.
6867
func TestGenerateLogrotateConfig(t *testing.T) {
6968
for _, tt := range []struct {
7069
logFilePath string
@@ -151,7 +150,7 @@ func TestParseDurationForLogrotate(t *testing.T) {
151150
},
152151
{
153152
retentionPeriod: "35hour",
154-
number: 1,
153+
number: 2,
155154
interval: "daily",
156155
},
157156
{

internal/collector/pgbouncer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import (
2222
//go:embed "generated/pgbouncer_metrics_queries.json"
2323
var pgBouncerMetricsQueries json.RawMessage
2424

25+
// PGBouncerPostRotateScript is the script that is run after pgBouncer's log
26+
// files have been rotated. The pgbouncer process is sent a sighup signal.
27+
const PGBouncerPostRotateScript = "pkill -HUP --exact pgbouncer"
28+
2529
// NewConfigForPgBouncerPod creates a config for the OTel collector container
2630
// that runs as a sidecar in the pgBouncer Pod
2731
func NewConfigForPgBouncerPod(

internal/controller/postgrescluster/pgbouncer.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,10 @@ func (r *Reconciler) reconcilePGBouncerConfigMap(
103103
(feature.Enabled(ctx, feature.OpenTelemetryLogs) || feature.Enabled(ctx, feature.OpenTelemetryMetrics)) {
104104
err = collector.AddToConfigMap(ctx, otelConfig, configmap)
105105
}
106-
// If OTel logging is enabled and retentionPeriod is set, add logrotate config
107-
if err == nil && otelConfig != nil && feature.Enabled(ctx, feature.OpenTelemetryLogs) &&
108-
cluster.Spec.Instrumentation != nil && cluster.Spec.Instrumentation.Logs != nil &&
109-
cluster.Spec.Instrumentation.Logs.RetentionPeriod != nil {
110-
collector.AddLogrotateConfig(ctx, cluster.Spec.Instrumentation, configmap,
111-
naming.PGBouncerFullLogPath, naming.PGBouncerPostRotateScript)
106+
// If OTel logging is enabled, add logrotate config
107+
if err == nil && otelConfig != nil && feature.Enabled(ctx, feature.OpenTelemetryLogs) {
108+
err = collector.AddLogrotateConfig(ctx, cluster.Spec.Instrumentation, configmap,
109+
naming.PGBouncerFullLogPath, collector.PGBouncerPostRotateScript)
112110
}
113111
if err == nil {
114112
err = errors.WithStack(r.apply(ctx, configmap))

internal/naming/names.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,6 @@ const (
161161
// PGbouncerFullLogPath is the full path to the pgbouncer log file
162162
PGBouncerFullLogPath = PGBouncerLogPath + "/pgbouncer.log"
163163

164-
// PGBouncerPostRotateScript is the script that is run after pgBouncer's log
165-
// files have been rotated. The pgbouncer process is sent a sighup signal.
166-
// TODO: Should this go here or in controller/postgrescluster/pgbouncer.go?
167-
// Or somewhere in the internal/pgbouncer folder, for that matter?
168-
PGBouncerPostRotateScript = "pkill -HUP --exact pgbouncer"
169-
170164
// suffix used with postgrescluster name for associated configmap.
171165
// for instance, if the cluster is named 'mycluster', the
172166
// configmap will be named 'mycluster-pgbackrest-config'

0 commit comments

Comments
 (0)