Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9f9c439
Adds a New Condition for PVC Resize Errors
andrewlecuyer Feb 7, 2025
a942197
Initial configuration for an OpenTelemetry Collector
cbandy Dec 23, 2024
3ea8f17
Add an OTel Collector with Patroni metrics
dsessler7 Jan 6, 2025
c3a98fb
Add PgBouncer metrics
dsessler7 Jan 14, 2025
9fcef77
Parse Postgres and pgAudit logs using the OTel Collector
benjaminjb Jan 22, 2025
08ab9a4
Parse Patroni logs
dsessler7 Jan 22, 2025
2e59c1b
Parse PgBouncer logs using the OTel Collector
dsessler7 Jan 29, 2025
96e1ffb
Scrape pgAdmin logs using the OTel collector
tony-landreth Jan 29, 2025
ee9bf60
Add pgBackRest repohost log collector
benjaminjb Feb 1, 2025
836572d
Validate and strip/minify Collector SQL files
cbandy Feb 7, 2025
f2a80ac
Change pgbackrest init for running containers
benjaminjb Feb 7, 2025
0dcb1be
Bump controller-gen to v0.17.2
cbandy Feb 10, 2025
fbb4f32
Change PostgresIdentifier to a type alias
cbandy Jan 3, 2025
7089149
Add k8s attributes to patroni logs. Add CompactingProcessor to patron…
dsessler7 Feb 7, 2025
8e37a1f
Create initial API for OTel instrumentation. Allow users to configure…
dsessler7 Feb 9, 2025
38fc33a
Add instrumentation_scope.name and log.record.original attributes to …
dsessler7 Feb 9, 2025
3602c70
Add configurable collector (#4092)
benjaminjb Feb 12, 2025
f7e9625
Add shared functions for quoting shell words
cbandy Nov 4, 2024
d4483cc
Add a function for setting permission on directories
cbandy Feb 10, 2025
e6ea78b
Store pgAdmin log file positions in the logs directory
cbandy Feb 6, 2025
951fa40
Ensure Postgres and Patroni log directories are writable
cbandy Feb 10, 2025
88130ca
Ensure pgBackRest log directories are writable
cbandy Feb 11, 2025
8dbe427
Add a field specifying when to delete log files
cbandy Feb 14, 2025
1797f8f
Rotate PgBouncer logs using specified retention
dsessler7 Feb 11, 2025
8b87822
Document a Kubernetes bug with the duration format
cbandy Feb 18, 2025
85636a8
Add an API struct representing a single Secret value
cbandy Jan 15, 2025
ef1eae0
Allow more control over the arguments to pg_upgrade
cbandy Dec 9, 2024
510ddf4
Validate pg_upgrade versions at the API server
cbandy Feb 19, 2025
e4dfdf2
Add a validated field for Postgres parameters
cbandy Dec 20, 2024
e884806
Otel pgMonitor metrics (#4096)
tony-landreth Feb 21, 2025
00c9068
Add reload logic to collector container start script.
dsessler7 Feb 19, 2025
19a28f7
Add a test helper that unmarshals JSON and YAML
cbandy Feb 26, 2025
9977db2
If the OpenTelemetryLogs feature gate is set, tell patroni to log to …
dsessler7 Feb 26, 2025
bfd4160
Add resources from API to OTEL sidecar (#4104)
benjaminjb Feb 26, 2025
6ba9057
Change PostgresCluster.spec.config to a pointer
cbandy Feb 26, 2025
2a2fe9b
Calculate Postgres parameters in the controller
cbandy Feb 26, 2025
9018342
Rotate postgres logs according to retentionPeriod in spec.
dsessler7 Feb 20, 2025
d04885c
Clone embedded metrics variable to avoid continuous appending.
dsessler7 Feb 28, 2025
00a93f6
Add a script to help with bumping dependencies
cbandy Feb 28, 2025
6dbbf9b
Bump golang.org/x/crypto and golang.org/x/oauth2
cbandy Feb 28, 2025
b50bae9
Rotate pgbackrest (#4108)
benjaminjb Mar 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Rotate pgbackrest (CrunchyData#4108)
* Rotate pgbackrest logs in repohost and Postgres pod
* Dir creation in collector startup

Issues: [PGO-2171]
  • Loading branch information
benjaminjb authored Mar 1, 2025
commit b50bae9782aa202015feacbd3afedaa434fb630f
23 changes: 19 additions & 4 deletions internal/collector/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ package collector
import (
"context"
"fmt"
"path"

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

"github.com/crunchydata/postgres-operator/internal/config"
"github.com/crunchydata/postgres-operator/internal/feature"
"github.com/crunchydata/postgres-operator/internal/initialize"
"github.com/crunchydata/postgres-operator/internal/naming"
"github.com/crunchydata/postgres-operator/internal/shell"
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)

Expand Down Expand Up @@ -44,9 +46,12 @@ func AddToPod(
outPod *corev1.PodSpec,
volumeMounts []corev1.VolumeMount,
sqlQueryPassword string,
logDirectories []string,
includeLogrotate bool,
) {
if !(feature.Enabled(ctx, feature.OpenTelemetryLogs) || feature.Enabled(ctx, feature.OpenTelemetryMetrics)) {
if spec == nil ||
!(feature.Enabled(ctx, feature.OpenTelemetryLogs) ||
feature.Enabled(ctx, feature.OpenTelemetryMetrics)) {
return
}

Expand Down Expand Up @@ -84,7 +89,7 @@ func AddToPod(
Name: naming.ContainerCollector,
Image: config.CollectorContainerImage(spec),
ImagePullPolicy: pullPolicy,
Command: startCommand(includeLogrotate),
Command: startCommand(logDirectories, includeLogrotate),
Env: []corev1.EnvVar{
{
Name: "K8S_POD_NAMESPACE",
Expand Down Expand Up @@ -146,13 +151,23 @@ func AddToPod(
}

// startCommand generates the command script used by the collector container
func startCommand(includeLogrotate bool) []string {
func startCommand(logDirectories []string, includeLogrotate bool) []string {
var mkdirScript string
if len(logDirectories) != 0 {
for _, logDir := range logDirectories {
mkdirScript = mkdirScript + `
` + shell.MakeDirectories(0o775, logDir,
path.Join(logDir, "receiver"))
}
}

var logrotateCommand string
if includeLogrotate {
logrotateCommand = `logrotate -s /tmp/logrotate.status /etc/logrotate.d/logrotate.conf`
}

var startScript = fmt.Sprintf(`
%s
OTEL_PIDFILE=/tmp/otel.pid

start_otel_collector() {
Expand All @@ -175,7 +190,7 @@ while read -r -t 5 -u "${fd}" ||:; do
start_otel_collector
fi
done
`, configDirectory, logrotateCommand)
`, mkdirScript, configDirectory, logrotateCommand)

wrapper := `monitor() {` + startScript + `}; export directory="$1"; export -f monitor; exec -a "$0" bash -ceu monitor`

Expand Down
3 changes: 1 addition & 2 deletions internal/collector/pgbackrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ func NewConfigForPgBackrestRepoHostPod(

// Keep track of what log records and files have been processed.
// Use a subdirectory of the logs directory to stay within the same failure domain.
// TODO(log-rotation): Create this directory during Collector startup.
config.Extensions["file_storage/pgbackrest_logs"] = map[string]any{
"directory": directory + "/receiver",
"create_directory": true,
"create_directory": false,
"fsync": true,
}

Expand Down
4 changes: 2 additions & 2 deletions internal/collector/pgbackrest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exporters:
verbosity: detailed
extensions:
file_storage/pgbackrest_logs:
create_directory: true
create_directory: false
directory: /pgbackrest/repo1/log/receiver
fsync: true
processors:
Expand Down Expand Up @@ -131,7 +131,7 @@ exporters:
project: google-project-name
extensions:
file_storage/pgbackrest_logs:
create_directory: true
create_directory: false
directory: /pgbackrest/repo1/log/receiver
fsync: true
processors:
Expand Down
3 changes: 1 addition & 2 deletions internal/collector/pgbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ func EnablePgBouncerLogging(ctx context.Context,

// Keep track of what log records and files have been processed.
// Use a subdirectory of the logs directory to stay within the same failure domain.
// TODO(log-rotation): Create this directory during Collector startup.
//
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/extension/storage/filestorage#readme
outConfig.Extensions["file_storage/pgbouncer_logs"] = map[string]any{
"directory": directory + "/receiver",
"create_directory": true,
"create_directory": false,
"fsync": true,
}

Expand Down
4 changes: 2 additions & 2 deletions internal/collector/pgbouncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exporters:
verbosity: detailed
extensions:
file_storage/pgbouncer_logs:
create_directory: true
create_directory: false
directory: /tmp/receiver
fsync: true
processors:
Expand Down Expand Up @@ -124,7 +124,7 @@ exporters:
project: google-project-name
extensions:
file_storage/pgbouncer_logs:
create_directory: true
create_directory: false
directory: /tmp/receiver
fsync: true
processors:
Expand Down
8 changes: 5 additions & 3 deletions internal/collector/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ func NewConfigForPostgresPod(ctx context.Context,
) *Config {
config := NewConfig(inCluster.Spec.Instrumentation)

// Metrics
EnablePostgresMetrics(ctx, inCluster, config)
EnablePatroniMetrics(ctx, inCluster, config)
EnablePatroniLogging(ctx, inCluster, config)

// Logging
EnablePostgresLogging(ctx, inCluster, config, outParameters)
EnablePatroniLogging(ctx, inCluster, config)

return config
}
Expand Down Expand Up @@ -229,10 +232,9 @@ func EnablePostgresLogging(
}

// pgBackRest pipeline
// TODO(log-rotation): Create this directory during Collector startup.
outConfig.Extensions["file_storage/pgbackrest_logs"] = map[string]any{
"directory": naming.PGBackRestPGDataLogPath + "/receiver",
"create_directory": true,
"create_directory": false,
"fsync": true,
}

Expand Down
4 changes: 2 additions & 2 deletions internal/collector/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ exporters:
verbosity: detailed
extensions:
file_storage/pgbackrest_logs:
create_directory: true
create_directory: false
directory: /pgdata/pgbackrest/log/receiver
fsync: true
file_storage/postgres_logs:
Expand Down Expand Up @@ -272,7 +272,7 @@ exporters:
project: google-project-name
extensions:
file_storage/pgbackrest_logs:
create_directory: true
create_directory: false
directory: /pgdata/pgbackrest/log/receiver
fsync: true
file_storage/postgres_logs:
Expand Down
22 changes: 20 additions & 2 deletions internal/controller/postgrescluster/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1219,8 +1219,10 @@ func (r *Reconciler) reconcileInstance(
}

// For now, we are not using logrotate to rotate postgres or patroni logs
// but we are using it for pgbackrest logs in the postgres pod
collector.AddToPod(ctx, cluster.Spec.Instrumentation, cluster.Spec.ImagePullPolicy, instanceConfigMap, &instance.Spec.Template.Spec,
[]corev1.VolumeMount{postgres.DataVolumeMount()}, pgPassword, false)
[]corev1.VolumeMount{postgres.DataVolumeMount()}, pgPassword,
[]string{naming.PGBackRestPGDataLogPath}, true)
}

// Add postgres-exporter to the instance Pod spec
Expand Down Expand Up @@ -1425,8 +1427,24 @@ func (r *Reconciler) reconcileInstanceConfigMap(
})

// If OTel logging or metrics is enabled, add collector config
if err == nil && (feature.Enabled(ctx, feature.OpenTelemetryLogs) || feature.Enabled(ctx, feature.OpenTelemetryMetrics)) {
if err == nil &&
(feature.Enabled(ctx, feature.OpenTelemetryLogs) ||
feature.Enabled(ctx, feature.OpenTelemetryMetrics)) {
err = collector.AddToConfigMap(ctx, otelConfig, instanceConfigMap)

// Add pgbackrest logrotate if OpenTelemetryLogs is enabled and
// local volumes are available
if err == nil &&
feature.Enabled(ctx, feature.OpenTelemetryLogs) &&
pgbackrest.RepoHostVolumeDefined(cluster) &&
cluster.Spec.Instrumentation != nil {

collector.AddLogrotateConfigs(ctx, cluster.Spec.Instrumentation,
instanceConfigMap,
[]collector.LogrotateConfig{{
LogFiles: []string{naming.PGBackRestPGDataLogPath + "/*.log"},
}})
}
}
if err == nil {
err = patroni.InstanceConfigMap(ctx, cluster, spec, instanceConfigMap)
Expand Down
9 changes: 4 additions & 5 deletions internal/controller/postgrescluster/pgbackrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,18 +688,17 @@ func (r *Reconciler) generateRepoHostIntent(ctx context.Context, postgresCluster

if pgbackrest.RepoHostVolumeDefined(postgresCluster) {
// add the init container to make the pgBackRest repo volume log directory
pgbackrest.MakePGBackrestLogDir(&repo.Spec.Template, postgresCluster)
pgBackRestLogPath := pgbackrest.MakePGBackrestLogDir(&repo.Spec.Template, postgresCluster)

containersToAdd := []string{naming.PGBackRestRepoContainerName}

// If OpenTelemetryLogs is enabled, we want to add the collector to the pod
// and also add the RepoVolumes to the container.
if feature.Enabled(ctx, feature.OpenTelemetryLogs) {
// TODO: Setting the includeLogrotate argument to false for now. This
// should be changed when we implement log rotation for pgbackrest
if postgresCluster.Spec.Instrumentation != nil && feature.Enabled(ctx, feature.OpenTelemetryLogs) {
collector.AddToPod(ctx, postgresCluster.Spec.Instrumentation, postgresCluster.Spec.ImagePullPolicy,
&corev1.ConfigMap{ObjectMeta: naming.PGBackRestConfig(postgresCluster)},
&repo.Spec.Template.Spec, []corev1.VolumeMount{}, "", false)
&repo.Spec.Template.Spec, []corev1.VolumeMount{}, "",
[]string{pgBackRestLogPath}, true)

containersToAdd = append(containersToAdd, naming.ContainerCollector)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/postgrescluster/pgbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func (r *Reconciler) reconcilePGBouncerConfigMap(
}
// If OTel logging or metrics is enabled, add collector config
if otelConfig != nil &&
(feature.Enabled(ctx, feature.OpenTelemetryLogs) || feature.Enabled(ctx, feature.OpenTelemetryMetrics)) {
(feature.Enabled(ctx, feature.OpenTelemetryLogs) ||
feature.Enabled(ctx, feature.OpenTelemetryMetrics)) {
err = collector.AddToConfigMap(ctx, otelConfig, configmap)
}
// If OTel logging is enabled, add logrotate config
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/standalone_pgadmin/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func statefulset(
}

collector.AddToPod(ctx, pgadmin.Spec.Instrumentation, pgadmin.Spec.ImagePullPolicy,
configmap, &sts.Spec.Template.Spec, volumeMounts, "", false)
configmap, &sts.Spec.Template.Spec, volumeMounts, "", []string{}, false)
}

return sts
Expand Down
36 changes: 30 additions & 6 deletions internal/pgbackrest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/crunchydata/postgres-operator/internal/collector"
"github.com/crunchydata/postgres-operator/internal/config"
"github.com/crunchydata/postgres-operator/internal/feature"
"github.com/crunchydata/postgres-operator/internal/initialize"
"github.com/crunchydata/postgres-operator/internal/naming"
"github.com/crunchydata/postgres-operator/internal/postgres"
Expand Down Expand Up @@ -129,11 +130,32 @@ func CreatePGBackRestConfigMapIntent(ctx context.Context, postgresCluster *v1bet
postgresCluster.Spec.Backups.PGBackRest.Global,
).String()

err = collector.AddToConfigMap(ctx, collector.NewConfigForPgBackrestRepoHostPod(
ctx,
postgresCluster.Spec.Instrumentation,
postgresCluster.Spec.Backups.PGBackRest.Repos,
), cm)
if RepoHostVolumeDefined(postgresCluster) &&
(feature.Enabled(ctx, feature.OpenTelemetryLogs) ||
feature.Enabled(ctx, feature.OpenTelemetryMetrics)) {
err = collector.AddToConfigMap(ctx, collector.NewConfigForPgBackrestRepoHostPod(
ctx,
postgresCluster.Spec.Instrumentation,
postgresCluster.Spec.Backups.PGBackRest.Repos,
), cm)

// If OTel logging is enabled, add logrotate config for the RepoHost
if err == nil &&
postgresCluster.Spec.Instrumentation != nil &&
feature.Enabled(ctx, feature.OpenTelemetryLogs) {
var pgBackRestLogPath string
for _, repo := range postgresCluster.Spec.Backups.PGBackRest.Repos {
if repo.Volume != nil {
pgBackRestLogPath = fmt.Sprintf(naming.PGBackRestRepoLogPath, repo.Name)
break
}
}

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

cm.Data[ConfigHashKey] = configHash
Expand All @@ -144,7 +166,7 @@ func CreatePGBackRestConfigMapIntent(ctx context.Context, postgresCluster *v1bet
// MakePGBackrestLogDir creates the pgBackRest default log path directory used when a
// dedicated repo host is configured.
func MakePGBackrestLogDir(template *corev1.PodTemplateSpec,
cluster *v1beta1.PostgresCluster) {
cluster *v1beta1.PostgresCluster) string {

var pgBackRestLogPath string
for _, repo := range cluster.Spec.Backups.PGBackRest.Repos {
Expand Down Expand Up @@ -172,6 +194,8 @@ func MakePGBackrestLogDir(template *corev1.PodTemplateSpec,
}
}
template.Spec.InitContainers = append(template.Spec.InitContainers, container)

return pgBackRestLogPath
}

// RestoreCommand returns the command for performing a pgBackRest restore. In addition to calling
Expand Down
2 changes: 1 addition & 1 deletion internal/pgbouncer/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func Pod(

if feature.Enabled(ctx, feature.OpenTelemetryLogs) || feature.Enabled(ctx, feature.OpenTelemetryMetrics) {
collector.AddToPod(ctx, inCluster.Spec.Instrumentation, inCluster.Spec.ImagePullPolicy, inConfigMap,
outPod, []corev1.VolumeMount{configVolumeMount}, string(inSecret.Data["pgbouncer-password"]),
outPod, []corev1.VolumeMount{configVolumeMount}, string(inSecret.Data["pgbouncer-password"]), []string{naming.PGBouncerLogPath},
true)
}
}
Expand Down