Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we want to require the instrumentation spec, then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, just like you said at another point: this is all feature gated, but turning on a feature gate doesn't necessarily mean you want X feature for all your clusters.

!(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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just re-ordering and grouping


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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got tired of looping through repos to get this name, so I had this func return it since I needed this name right after this func was called. I could've done that work outside this func or create a util, maybe I'll consider that tomorrow.

}

// 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