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
Create initial API for OTel instrumentation. Allow users to configure…
… exporters via API and add them to logs pipelines.
  • Loading branch information
dsessler7 committed Feb 11, 2025
commit 8e37a1fa17da5396cac48c10245b0df0782de8a9
421 changes: 421 additions & 0 deletions config/crd/bases/postgres-operator.crunchydata.com_pgadmins.yaml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

27 changes: 19 additions & 8 deletions internal/collector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ package collector
import (
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/yaml"

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

// ComponentID represents a component identifier within an OpenTelemetry
// Collector YAML configuration. Each value is a "type" followed by an optional
// slash-then-name: `type[/name]`
type ComponentID string
type ComponentID = string

// PipelineID represents a pipeline identifier within an OpenTelemetry Collector
// YAML configuration. Each value is a signal followed by an optional
// slash-then-name: `signal[/name]`
type PipelineID = string

// Config represents an OpenTelemetry Collector YAML configuration.
// See: https://opentelemetry.io/docs/collector/configuration
Expand All @@ -35,11 +42,6 @@ type Pipeline struct {
Receivers []ComponentID
}

// PipelineID represents a pipeline identifier within an OpenTelemetry Collector
// YAML configuration. Each value is a signal followed by an optional
// slash-then-name: `signal[/name]`
type PipelineID string

func (c *Config) ToYAML() (string, error) {
const yamlGeneratedWarning = "" +
"# Generated by postgres-operator. DO NOT EDIT.\n" +
Expand Down Expand Up @@ -71,8 +73,8 @@ func (c *Config) ToYAML() (string, error) {
}

// NewConfig creates a base config for an OTel collector container
func NewConfig() *Config {
return &Config{
func NewConfig(spec *v1beta1.InstrumentationSpec) *Config {
config := &Config{
Exporters: map[ComponentID]any{
// TODO: Do we want a DebugExporter outside of development?
// https://pkg.go.dev/go.opentelemetry.io/collector/exporter/debugexporter#section-readme
Expand All @@ -90,4 +92,13 @@ func NewConfig() *Config {
Receivers: map[ComponentID]any{},
Pipelines: map[PipelineID]Pipeline{},
}

// If there are exporters defined in the spec, add them to the config.
if spec != nil && spec.Config != nil && spec.Config.Exporters != nil {
for k, v := range spec.Config.Exporters {
config.Exporters[k] = v
}
}

return config
}
36 changes: 33 additions & 3 deletions internal/collector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
)

func TestConfigToYAML(t *testing.T) {
result, err := NewConfig().ToYAML()
assert.NilError(t, err)
assert.DeepEqual(t, result, `# Generated by postgres-operator. DO NOT EDIT.
t.Run("NilInstrumentationSpec", func(t *testing.T) {
result, err := NewConfig(nil).ToYAML()
assert.NilError(t, err)
assert.DeepEqual(t, result, `# Generated by postgres-operator. DO NOT EDIT.
# Your changes will not be saved.
exporters:
debug:
Expand All @@ -30,4 +31,33 @@ service:
extensions: []
pipelines: {}
`)
})

t.Run("InstrumentationSpecDefined", func(t *testing.T) {
spec := testInstrumentationSpec()

result, err := NewConfig(spec).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
googlecloud:
log:
default_log_name: opentelemetry.io/collector-exported-log
project: google-project-name
extensions: {}
processors:
batch/1s:
timeout: 1s
batch/200ms:
timeout: 200ms
groupbyattrs/compact: {}
receivers: {}
service:
extensions: []
pipelines: {}
`)
})
}
29 changes: 29 additions & 0 deletions internal/collector/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2024 - 2025 Crunchy Data Solutions, Inc.
//
// SPDX-License-Identifier: Apache-2.0

package collector

import (
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)

func testInstrumentationSpec() *v1beta1.InstrumentationSpec {
spec := v1beta1.InstrumentationSpec{
Config: &v1beta1.InstrumentationConfigSpec{
Exporters: map[string]any{
"googlecloud": map[string]any{
"log": map[string]any{
"default_log_name": "opentelemetry.io/collector-exported-log",
},
"project": "google-project-name",
},
},
},
Logs: &v1beta1.InstrumentationLogsSpec{
Exporters: []string{"googlecloud"},
},
}

return spec.DeepCopy()
}
7 changes: 7 additions & 0 deletions internal/collector/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)

// AddToConfigMap populates the shared ConfigMap with fields needed to run the Collector.
Expand All @@ -33,6 +34,7 @@ func AddToConfigMap(
// AddToPod adds the OpenTelemetry collector container to a given Pod
func AddToPod(
ctx context.Context,
spec *v1beta1.InstrumentationSpec,
pullPolicy corev1.PullPolicy,
inInstanceConfigMap *corev1.ConfigMap,
outPod *corev1.PodSpec,
Expand Down Expand Up @@ -63,6 +65,11 @@ func AddToPod(
}},
}

// If the user has specified files to be mounted in the spec, add them to the projected config volume
if spec != nil && spec.Config != nil && spec.Config.Files != nil {
configVolume.Projected.Sources = append(configVolume.Projected.Sources, spec.Config.Files...)
}

container := corev1.Container{
Name: naming.ContainerCollector,
Image: "ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector-contrib:0.117.0",
Expand Down
13 changes: 12 additions & 1 deletion internal/collector/patroni.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ func EnablePatroniLogging(ctx context.Context,
}},
}

// If there are exporters to be added to the logs pipelines defined in
// the spec, add them to the pipeline. Otherwise, add the DebugExporter.
var exporters []ComponentID
if inCluster.Spec.Instrumentation != nil &&
inCluster.Spec.Instrumentation.Logs != nil &&
inCluster.Spec.Instrumentation.Logs.Exporters != nil {
exporters = inCluster.Spec.Instrumentation.Logs.Exporters
} else {
exporters = []ComponentID{DebugExporter}
}

outConfig.Pipelines["logs/patroni"] = Pipeline{
Extensions: []ComponentID{"file_storage/patroni_logs"},
Receivers: []ComponentID{"filelog/patroni_jsonlog"},
Expand All @@ -109,7 +120,7 @@ func EnablePatroniLogging(ctx context.Context,
SubSecondBatchProcessor,
CompactingProcessor,
},
Exporters: []ComponentID{DebugExporter},
Exporters: exporters,
}
}
}
Expand Down
91 changes: 89 additions & 2 deletions internal/collector/patroni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
)

func TestEnablePatroniLogging(t *testing.T) {
t.Run("Enabled", func(t *testing.T) {
t.Run("NilInstrumentationSpec", 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()
config := NewConfig(nil)

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

Expand Down Expand Up @@ -93,6 +93,93 @@ service:
- groupbyattrs/compact
receivers:
- filelog/patroni_jsonlog
`)
})

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

cluster := new(v1beta1.PostgresCluster)
cluster.Spec.Instrumentation = testInstrumentationSpec()
config := NewConfig(cluster.Spec.Instrumentation)

EnablePatroniLogging(ctx, cluster, 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
googlecloud:
log:
default_log_name: opentelemetry.io/collector-exported-log
project: google-project-name
extensions:
file_storage/patroni_logs:
create_directory: true
directory: /pgdata/patroni/log/receiver
fsync: true
processors:
batch/1s:
timeout: 1s
batch/200ms:
timeout: 200ms
groupbyattrs/compact: {}
resource/patroni:
attributes:
- action: insert
key: k8s.container.name
value: database
- action: insert
key: k8s.namespace.name
value: ${env:K8S_POD_NAMESPACE}
- action: insert
key: k8s.pod.name
value: ${env:K8S_POD_NAME}
transform/patroni_logs:
log_statements:
- context: log
statements:
- set(instrumentation_scope.name, "patroni")
- set(cache, ParseJSON(body["original"]))
- set(severity_text, cache["levelname"])
- set(severity_number, SEVERITY_NUMBER_DEBUG) where severity_text == "DEBUG"
- set(severity_number, SEVERITY_NUMBER_INFO) where severity_text == "INFO"
- 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 == "CRITICAL"
- set(time, Time(cache["asctime"], "%F %T,%L"))
- set(attributes["log.record.original"], body["original"])
- set(body, cache["message"])
receivers:
filelog/patroni_jsonlog:
include:
- /pgdata/patroni/log/*.log
operators:
- from: body
to: body.original
type: move
storage: file_storage/patroni_logs
service:
extensions:
- file_storage/patroni_logs
pipelines:
logs/patroni:
exporters:
- googlecloud
processors:
- resource/patroni
- transform/patroni_logs
- batch/200ms
- groupbyattrs/compact
receivers:
- filelog/patroni_jsonlog
`)
})
}
21 changes: 17 additions & 4 deletions internal/collector/pgadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import (

"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"
)

func EnablePgAdminLogging(ctx context.Context, configmap *corev1.ConfigMap) error {
func EnablePgAdminLogging(ctx context.Context, spec *v1beta1.InstrumentationSpec,
configmap *corev1.ConfigMap,
) error {
if !feature.Enabled(ctx, feature.OpenTelemetryLogs) {
return nil
}
otelConfig := NewConfig()
otelConfig := NewConfig(spec)
otelConfig.Extensions["file_storage/pgadmin"] = map[string]any{
"directory": "/var/log/pgadmin/receiver",
"create_directory": true,
Expand All @@ -28,6 +31,7 @@ func EnablePgAdminLogging(ctx context.Context, configmap *corev1.ConfigMap) erro
"create_directory": true,
"fsync": true,
}

otelConfig.Receivers["filelog/pgadmin"] = map[string]any{
"include": []string{"/var/lib/pgadmin/logs/pgadmin.log"},
"storage": "file_storage/pgadmin",
Expand Down Expand Up @@ -70,6 +74,15 @@ func EnablePgAdminLogging(ctx context.Context, configmap *corev1.ConfigMap) erro
},
}

// If there are exporters to be added to the logs pipelines defined in
// the spec, add them to the pipeline. Otherwise, add the DebugExporter.
var exporters []ComponentID
if spec != nil && spec.Logs != nil && spec.Logs.Exporters != nil {
exporters = spec.Logs.Exporters
} else {
exporters = []ComponentID{DebugExporter}
}

otelConfig.Pipelines["logs/pgadmin"] = Pipeline{
Extensions: []ComponentID{"file_storage/pgadmin"},
Receivers: []ComponentID{"filelog/pgadmin"},
Expand All @@ -79,7 +92,7 @@ func EnablePgAdminLogging(ctx context.Context, configmap *corev1.ConfigMap) erro
SubSecondBatchProcessor,
CompactingProcessor,
},
Exporters: []ComponentID{DebugExporter},
Exporters: exporters,
}

otelConfig.Pipelines["logs/gunicorn"] = Pipeline{
Expand All @@ -91,7 +104,7 @@ func EnablePgAdminLogging(ctx context.Context, configmap *corev1.ConfigMap) erro
SubSecondBatchProcessor,
CompactingProcessor,
},
Exporters: []ComponentID{DebugExporter},
Exporters: exporters,
}

otelYAML, err := otelConfig.ToYAML()
Expand Down
Loading