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
Next Next commit
Parse PgBouncer logs using the OTel Collector
Issue: PGO-2056
  • Loading branch information
dsessler7 authored and cbandy committed Feb 7, 2025
commit 2e59c1bf9f438c6c2c0e91685afb06a86ad78fdd
110 changes: 110 additions & 0 deletions internal/collector/pgbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"slices"

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

Expand All @@ -33,11 +34,120 @@ func NewConfigForPgBouncerPod(

config := NewConfig()

EnablePgBouncerLogging(ctx, cluster, config)
EnablePgBouncerMetrics(ctx, config, sqlQueryUsername)

return config
}

// EnablePgBouncerLogging adds necessary configuration to the collector config to collect
// logs from pgBouncer when the OpenTelemetryLogging feature flag is enabled.
func EnablePgBouncerLogging(ctx context.Context,
inCluster *v1beta1.PostgresCluster,
outConfig *Config) {
if feature.Enabled(ctx, feature.OpenTelemetryLogs) {
directory := naming.PGBouncerLogPath

// 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.
//
// 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,
"fsync": true,
}

// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/receiver/filelogreceiver#readme
outConfig.Receivers["filelog/pgbouncer_log"] = map[string]any{
// Read the log files and keep track of what has been processed.
"include": []string{directory + "/*.log"},
"storage": "file_storage/pgbouncer_logs",
}

// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/processor/resourceprocessor#readme
outConfig.Processors["resource/pgbouncer"] = map[string]any{
"attributes": []map[string]any{
// Container and Namespace names need no escaping because they are DNS labels.
// Pod names need no escaping because they are DNS subdomains.
//
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names
// https://github.com/open-telemetry/semantic-conventions/blob/v1.29.0/docs/resource/k8s.md
{"action": "insert", "key": "k8s.container.name", "value": naming.ContainerPGBouncer},
{"action": "insert", "key": "k8s.namespace.name", "value": "${env:K8S_POD_NAMESPACE}"},
{"action": "insert", "key": "k8s.pod.name", "value": "${env:K8S_POD_NAME}"},
},
}

// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/processor/transformprocessor#readme
outConfig.Processors["transform/pgbouncer_logs"] = map[string]any{
"log_statements": []map[string]any{{
"context": "log",
"statements": []string{
// Set instrumentation scope
`set(instrumentation_scope.name, "pgbouncer")`,

// Extract timestamp, pid, log level, and message and store in cache.
`merge_maps(cache, ExtractPatterns(body, ` +
`"^(?<timestamp>\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3} [A-Z]{3}) ` +
`\\[(?<pid>\\d+)\\] (?<log_level>[A-Z]+) (?<msg>.*$)"), "insert")`,

// https://opentelemetry.io/docs/specs/otel/logs/data-model/#field-severitytext
`set(severity_text, cache["log_level"])`,

// Map pgBouncer (libusual) "logging levels" to OpenTelemetry severity levels.
//
// https://github.com/libusual/libusual/blob/master/usual/logging.c
// https://opentelemetry.io/docs/specs/otel/logs/data-model/#field-severitynumber
// https://opentelemetry.io/docs/specs/otel/logs/data-model-appendix/#appendix-b-severitynumber-example-mappings
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/pkg/ottl/contexts/ottllog#enums
`set(severity_number, SEVERITY_NUMBER_DEBUG) where severity_text == "NOISE" or severity_text == "DEBUG"`,
`set(severity_number, SEVERITY_NUMBER_INFO) where severity_text == "LOG"`,
`set(severity_number, SEVERITY_NUMBER_WARN) where severity_text == "WARNING"`,
`set(severity_number, SEVERITY_NUMBER_ERROR) where severity_text == "ERROR"`,
`set(severity_number, SEVERITY_NUMBER_FATAL) where severity_text == "FATAL"`,

// Parse the timestamp.
// The format is neither RFC 3339 nor ISO 8601:
//
// The date and time are separated by a single space U+0020,
// followed by a dot U+002E, milliseconds, another space U+0020,
// then a timezone abbreviation.
//
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/pkg/stanza/docs/types/timestamp.md
`set(time, Time(cache["timestamp"], "%F %T.%L %Z"))`,

// Keep the unparsed log record in a standard attribute, and replace
// the log record body with the message field.
//
// https://github.com/open-telemetry/semantic-conventions/blob/v1.29.0/docs/general/logs.md
`set(attributes["log.record.original"], body)`,

// Set pid as attribute
`set(attributes["process.pid"], cache["pid"])`,

// Set the log message to body.
`set(body, cache["msg"])`,
},
}},
}

outConfig.Pipelines["logs/pgbouncer"] = Pipeline{
Extensions: []ComponentID{"file_storage/pgbouncer_logs"},
Receivers: []ComponentID{"filelog/pgbouncer_log"},
Processors: []ComponentID{
"resource/pgbouncer",
"transform/pgbouncer_logs",
SubSecondBatchProcessor,
CompactingProcessor,
},
Exporters: []ComponentID{DebugExporter},
}
}
}

// EnablePgBouncerMetrics adds necessary configuration to the collector config to scrape
// metrics from pgBouncer when the OpenTelemetryMetrics feature flag is enabled.
func EnablePgBouncerMetrics(ctx context.Context, config *Config, sqlQueryUsername string) {
if feature.Enabled(ctx, feature.OpenTelemetryMetrics) {
// Add Prometheus exporter
Expand Down
98 changes: 98 additions & 0 deletions internal/collector/pgbouncer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2024 - 2025 Crunchy Data Solutions, Inc.
//
// SPDX-License-Identifier: Apache-2.0

package collector

import (
"context"
"testing"

"gotest.tools/v3/assert"

"github.com/crunchydata/postgres-operator/internal/feature"
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)

func TestEnablePgBouncerLogging(t *testing.T) {
t.Run("Enabled", func(t *testing.T) {
gate := feature.NewGate()
assert.NilError(t, gate.SetFromMap(map[string]bool{
feature.OpenTelemetryLogs: true,
}))
ctx := feature.NewContext(context.Background(), gate)

config := NewConfig()

EnablePgBouncerLogging(ctx, new(v1beta1.PostgresCluster), config)

result, err := config.ToYAML()
assert.NilError(t, err)
assert.DeepEqual(t, result, `# Generated by postgres-operator. DO NOT EDIT.
# Your changes will not be saved.
exporters:
debug:
verbosity: detailed
extensions:
file_storage/pgbouncer_logs:
create_directory: true
directory: /tmp/receiver
fsync: true
processors:
batch/1s:
timeout: 1s
batch/200ms:
timeout: 200ms
groupbyattrs/compact: {}
resource/pgbouncer:
attributes:
- action: insert
key: k8s.container.name
value: pgbouncer
- action: insert
key: k8s.namespace.name
value: ${env:K8S_POD_NAMESPACE}
- action: insert
key: k8s.pod.name
value: ${env:K8S_POD_NAME}
transform/pgbouncer_logs:
log_statements:
- context: log
statements:
- set(instrumentation_scope.name, "pgbouncer")
- merge_maps(cache, ExtractPatterns(body, "^(?<timestamp>\\d{4}-\\d{2}-\\d{2}
\\d{2}:\\d{2}:\\d{2}\\.\\d{3} [A-Z]{3}) \\[(?<pid>\\d+)\\] (?<log_level>[A-Z]+)
(?<msg>.*$)"), "insert")
- set(severity_text, cache["log_level"])
- set(severity_number, SEVERITY_NUMBER_DEBUG) where severity_text == "NOISE"
or severity_text == "DEBUG"
- set(severity_number, SEVERITY_NUMBER_INFO) where severity_text == "LOG"
- set(severity_number, SEVERITY_NUMBER_WARN) where severity_text == "WARNING"
- set(severity_number, SEVERITY_NUMBER_ERROR) where severity_text == "ERROR"
- set(severity_number, SEVERITY_NUMBER_FATAL) where severity_text == "FATAL"
- set(time, Time(cache["timestamp"], "%F %T.%L %Z"))
- set(attributes["log.record.original"], body)
- set(attributes["process.pid"], cache["pid"])
- set(body, cache["msg"])
receivers:
filelog/pgbouncer_log:
include:
- /tmp/*.log
storage: file_storage/pgbouncer_logs
service:
extensions:
- file_storage/pgbouncer_logs
pipelines:
logs/pgbouncer:
exporters:
- debug
processors:
- resource/pgbouncer
- transform/pgbouncer_logs
- batch/200ms
- groupbyattrs/compact
receivers:
- filelog/pgbouncer_log
`)
})
}
3 changes: 3 additions & 0 deletions internal/controller/postgrescluster/pgbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ func (r *Reconciler) generatePGBouncerDeployment(
pgbouncer.Pod(ctx, cluster, configmap, primaryCertificate, secret, &deploy.Spec.Template.Spec)
}

// Add tmp directory and volume for log files
addTMPEmptyDir(&deploy.Spec.Template)

return deploy, true, err
}

Expand Down
3 changes: 3 additions & 0 deletions internal/naming/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ const (
// dedicated repo host, if configured.
PGBackRestRepoLogPath = "/pgbackrest/%s/log"

// PGBouncerLogPath is the pgBouncer default log path configuration
PGBouncerLogPath = "/tmp"

// suffix used with postgrescluster name for associated configmap.
// for instance, if the cluster is named 'mycluster', the
// configmap will be named 'mycluster-pgbackrest-config'
Expand Down
7 changes: 6 additions & 1 deletion internal/pgbouncer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ func clusterINI(ctx context.Context, cluster *v1beta1.PostgresCluster) string {
"unix_socket_dir": "",
}

// When OTel metrics are enabled, allow pgbouncer's postgres user
// If OpenTelemetryLogs feature is enabled, enable logging to file
if feature.Enabled(ctx, feature.OpenTelemetryLogs) {
global["logfile"] = naming.PGBouncerLogPath + "/pgbouncer.log"
}

// When OTel metrics are enabled, allow pgBouncer's postgres user
// to run read-only console queries on pgBouncer's virtual db
if feature.Enabled(ctx, feature.OpenTelemetryMetrics) {
global["stats_users"] = PostgresqlUser
Expand Down