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 Postgres and pgAudit logs using the OTel Collector
Postgres can log in two structured formats: CSV and JSON since Postgres 15.
The two formats are very similar semantically, so this parses them in a
shared OTTL transform processor.

Co-authored-by: Chris Bandy <[email protected]>
Issue: PGO-2033
Issue: PGO-2065
  • Loading branch information
benjaminjb and cbandy committed Feb 7, 2025
commit 9fcef77271ac1005603a4d161c27c24eb5414099
1 change: 1 addition & 0 deletions internal/collector/generated/postgres_logs_transforms.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 21 additions & 7 deletions internal/collector/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func AddToPod(
volumeMounts []corev1.VolumeMount,
sqlQueryPassword string,
) {
if !feature.Enabled(ctx, feature.OpenTelemetryMetrics) {
if !(feature.Enabled(ctx, feature.OpenTelemetryLogs) || feature.Enabled(ctx, feature.OpenTelemetryMetrics)) {
return
}

Expand All @@ -67,10 +67,22 @@ func AddToPod(
container := corev1.Container{
Name: naming.ContainerCollector,

Image: "ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector-contrib:0.116.1",
Image: "ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector-contrib:0.117.0",
ImagePullPolicy: inCluster.Spec.ImagePullPolicy,
Command: []string{"/otelcol-contrib", "--config", "/etc/otel-collector/config.yaml"},
Env: []corev1.EnvVar{
{
Name: "K8S_POD_NAMESPACE",
ValueFrom: &corev1.EnvVarSource{FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
}},
},
{
Name: "K8S_POD_NAME",
ValueFrom: &corev1.EnvVarSource{FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
}},
},
{
Name: "PGPASSWORD",
Value: sqlQueryPassword,
Expand All @@ -81,11 +93,13 @@ func AddToPod(
VolumeMounts: append(volumeMounts, configVolumeMount),
}

container.Ports = []corev1.ContainerPort{{
ContainerPort: int32(8889),
Name: "otel-metrics",
Protocol: corev1.ProtocolTCP,
}}
if feature.Enabled(ctx, feature.OpenTelemetryMetrics) {
container.Ports = []corev1.ContainerPort{{
ContainerPort: int32(8889),
Name: "otel-metrics",
Protocol: corev1.ProtocolTCP,
}}
}

outPod.Containers = append(outPod.Containers, container)
outPod.Volumes = append(outPod.Volumes, configVolume)
Expand Down
188 changes: 187 additions & 1 deletion internal/collector/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,200 @@ package collector

import (
"context"
_ "embed"
"encoding/json"
"fmt"
"slices"

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

func NewConfigForPostgresPod(ctx context.Context, inCluster *v1beta1.PostgresCluster) *Config {
func NewConfigForPostgresPod(ctx context.Context,
inCluster *v1beta1.PostgresCluster,
outParameters *postgres.Parameters,
) *Config {
config := NewConfig()

EnablePatroniMetrics(ctx, inCluster, config)
EnablePostgresLogging(ctx, inCluster, config, outParameters)

return config
}

// The contents of "postgres_logs_transforms.yaml" as JSON.
// See: https://pkg.go.dev/embed
//
//go:embed "generated/postgres_logs_transforms.json"
var postgresLogsTransforms json.RawMessage

// postgresCSVNames returns the names of fields in the CSV logs for version.
func postgresCSVNames(version int) string {
// JSON is the preferred format, so use those names.
// https://www.postgresql.org/docs/current/runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-JSONLOG

// https://www.postgresql.org/docs/8.3/runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-CSVLOG
names := `timestamp,user,dbname,pid` +
`,connection_from` + // NOTE: this contains the JSON "remote_host" and "remote_port" values
`,session_id,line_num,ps,session_start,vxid,txid` +
`,error_severity,state_code,message,detail,hint` +
`,internal_query,internal_position,context,statement,cursor_position` +
`,location` // NOTE: this contains the JSON "func_name", "file_name", and "file_line_num" values

// https://www.postgresql.org/docs/9.0/runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-CSVLOG
if version >= 9 {
names += `,application_name`
}

// https://www.postgresql.org/docs/13/runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-CSVLOG
if version >= 13 {
names += `,backend_type`
}

// https://www.postgresql.org/docs/14/runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-CSVLOG
if version >= 14 {
names += `,leader_pid,query_id`
}

return names
}

func EnablePostgresLogging(
ctx context.Context,
inCluster *v1beta1.PostgresCluster,
outConfig *Config,
outParameters *postgres.Parameters,
) {
if feature.Enabled(ctx, feature.OpenTelemetryLogs) {
directory := postgres.LogDirectory()

// https://www.postgresql.org/docs/current/runtime-config-logging.html
outParameters.Mandatory.Add("logging_collector", "on")
outParameters.Mandatory.Add("log_directory", directory)

// PostgreSQL v8.3 adds support for CSV logging, and
// PostgreSQL v15 adds support for JSON logging. The latter is preferred
// because newlines are escaped as "\n", U+005C + U+006E.
if inCluster.Spec.PostgresVersion < 15 {
outParameters.Mandatory.Add("log_destination", "csvlog")
} else {
outParameters.Mandatory.Add("log_destination", "jsonlog")
}

// Keep seven days of logs named for the day of the week;
// this has been the default produced by `initdb` for some time now.
// NOTE: The automated portions of log_filename are *entirely* based
// on time. There is no spelling that is guaranteed to be unique or
// monotonically increasing.
//
// TODO(logs): Limit the size/bytes of logs without losing messages;
// probably requires another process that deletes the oldest files.
//
// The ".log" suffix is replaced by ".json" for JSON log files.
outParameters.Mandatory.Add("log_filename", "postgresql-%a.log")
outParameters.Mandatory.Add("log_file_mode", "0660")
outParameters.Mandatory.Add("log_rotation_age", "1d")
outParameters.Mandatory.Add("log_rotation_size", "0")
outParameters.Mandatory.Add("log_truncate_on_rotation", "on")

// Log in a timezone that the OpenTelemetry Collector will understand.
outParameters.Mandatory.Add("log_timezone", "UTC")

// 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/postgres_logs"] = map[string]any{
"directory": directory + "/receiver",
"create_directory": true,
"fsync": true,
}

// TODO(postgres-14): We can stop parsing CSV logs when 14 is EOL.
// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/receiver/filelogreceiver#readme
outConfig.Receivers["filelog/postgres_csvlog"] = map[string]any{
// Read the CSV files and keep track of what has been processed.
"include": []string{directory + "/*.csv"},
"storage": "file_storage/postgres_logs",

// Postgres does not escape newlines in its CSV log format. Search for
// the beginning of every record, starting with an unquoted timestamp.
// The 2nd through 5th fields are optional, so match through to the 7th field.
// This should do a decent job of not matching the middle of some SQL statement.
//
// The number of fields has changed over the years, but the first few
// are always formatted the same way.
//
// NOTE: This regexp is invoked in multi-line mode. https://go.dev/s/re2syntax
"multiline": map[string]string{
"line_start_pattern": `^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d.\d{3} UTC` + // 1st: timestamp
`,(?:"[_\D](?:[^"]|"")*")?` + // 2nd: user name
`,(?:"[_\D](?:[^"]|"")*")?` + // 3rd: database name
`,\d*,(?:"(?:[^"]|"")+")?` + // 4–5th: process id, connection
`,[0-9a-f]+[.][0-9a-f]+,\d+,`, // 6–7th: session id, session line
},

// Differentiate these from the JSON ones below.
"operators": []map[string]any{
{"type": "move", "from": "body", "to": "body.original"},
{"type": "add", "field": "body.format", "value": "csv"},
{"type": "add", "field": "body.headers", "value": postgresCSVNames(inCluster.Spec.PostgresVersion)},
},
}

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

// Differentiate these from the CSV ones above.
// TODO(postgres-14): We can stop parsing CSV logs when 14 is EOL.
"operators": []map[string]any{
{"type": "move", "from": "body", "to": "body.original"},
{"type": "add", "field": "body.format", "value": "json"},
},
}

// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/processor/resourceprocessor#readme
outConfig.Processors["resource/postgres"] = 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.ContainerDatabase},
{"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/semantic-conventions/blob/v1.29.0/docs/database#readme
{"action": "insert", "key": "db.system", "value": "postgresql"},
{"action": "insert", "key": "db.version", "value": fmt.Sprint(inCluster.Spec.PostgresVersion)},
},
}

// https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/-/processor/transformprocessor#readme
outConfig.Processors["transform/postgres_logs"] = map[string]any{
"log_statements": slices.Clone(postgresLogsTransforms),
}

outConfig.Pipelines["logs/postgres"] = Pipeline{
Extensions: []ComponentID{"file_storage/postgres_logs"},
// TODO(logs): Choose only one receiver, maybe?
Receivers: []ComponentID{
"filelog/postgres_csvlog",
"filelog/postgres_jsonlog",
},
Processors: []ComponentID{
"resource/postgres",
"transform/postgres_logs",
SubSecondBatchProcessor,
CompactingProcessor,
},
Exporters: []ComponentID{DebugExporter},
}
}
}
Loading