Skip to content

Commit 4ebde14

Browse files
committed
If user has provided custom queries, add the queries file to the volume and create a new sqlquery receiver.
Pass PodTemplateSpec to collector.AddToPod and add a collector label for discovery purposes.
1 parent ea3e1c9 commit 4ebde14

File tree

12 files changed

+84
-33
lines changed

12 files changed

+84
-33
lines changed

internal/collector/instance.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func AddToPod(
4343
spec *v1beta1.InstrumentationSpec,
4444
pullPolicy corev1.PullPolicy,
4545
inInstanceConfigMap *corev1.ConfigMap,
46-
outPod *corev1.PodSpec,
46+
template *corev1.PodTemplateSpec,
4747
volumeMounts []corev1.VolumeMount,
4848
sqlQueryPassword string,
4949
logDirectories []string,
@@ -76,13 +76,27 @@ func AddToPod(
7676
}},
7777
}
7878

79-
// If the user has specified files to be mounted in the spec, add them to the projected config volume
80-
if spec != nil && spec.Config != nil && spec.Config.Files != nil {
81-
configVolume.Projected.Sources = append(configVolume.Projected.Sources, spec.Config.Files...)
79+
// If the user has specified files to be mounted in the spec, add them to
80+
// the projected config volume
81+
if spec.Config != nil && spec.Config.Files != nil {
82+
configVolume.Projected.Sources = append(configVolume.Projected.Sources,
83+
spec.Config.Files...)
84+
}
85+
86+
// If the user has specified custom queries to add, put the queries file(s)
87+
// in the projected config volume
88+
if spec.Metrics != nil && spec.Metrics.CustomQueries != nil &&
89+
spec.Metrics.CustomQueries.Add != nil {
90+
for _, querySet := range spec.Metrics.CustomQueries.Add {
91+
projection := querySet.Queries.AsProjection(querySet.Name +
92+
"/" + querySet.Queries.Key)
93+
configVolume.Projected.Sources = append(configVolume.Projected.Sources,
94+
corev1.VolumeProjection{ConfigMap: &projection})
95+
}
8296
}
8397

8498
// Add configVolume to the pod's volumes
85-
outPod.Volumes = append(outPod.Volumes, configVolume)
99+
template.Spec.Volumes = append(template.Spec.Volumes, configVolume)
86100

87101
// Create collector container
88102
container := corev1.Container{
@@ -136,18 +150,22 @@ func AddToPod(
136150
}},
137151
}
138152
container.VolumeMounts = append(container.VolumeMounts, logrotateConfigVolumeMount)
139-
outPod.Volumes = append(outPod.Volumes, logrotateConfigVolume)
153+
template.Spec.Volumes = append(template.Spec.Volumes, logrotateConfigVolume)
140154
}
141155

142156
if feature.Enabled(ctx, feature.OpenTelemetryMetrics) {
143157
container.Ports = []corev1.ContainerPort{{
144-
ContainerPort: int32(8889),
158+
ContainerPort: int32(PrometheusPort),
145159
Name: "otel-metrics",
146160
Protocol: corev1.ProtocolTCP,
147161
}}
148162
}
149163

150-
outPod.Containers = append(outPod.Containers, container)
164+
template.Spec.Containers = append(template.Spec.Containers, container)
165+
166+
// add the proper label to support Pod discovery by Prometheus
167+
initialize.Labels(template)
168+
template.Labels[naming.LabelCollectorDiscovery] = "true"
151169
}
152170

153171
// startCommand generates the command script used by the collector container

internal/collector/naming.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const LogsBatchProcessor = "batch/logs"
1010
const OneSecondBatchProcessor = "batch/1s"
1111
const SubSecondBatchProcessor = "batch/200ms"
1212
const Prometheus = "prometheus"
13+
const PrometheusPort = 9187
1314
const PGBouncerMetrics = "metrics/pgbouncer"
1415
const PostgresMetrics = "metrics/postgres"
1516
const PatroniMetrics = "metrics/patroni"

internal/collector/patroni.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package collector
77
import (
88
"context"
99
"slices"
10+
"strconv"
1011

1112
"github.com/crunchydata/postgres-operator/internal/feature"
1213
"github.com/crunchydata/postgres-operator/internal/naming"
@@ -136,7 +137,7 @@ func EnablePatroniMetrics(ctx context.Context,
136137
if feature.Enabled(ctx, feature.OpenTelemetryMetrics) {
137138
// Add Prometheus exporter
138139
outConfig.Exporters[Prometheus] = map[string]any{
139-
"endpoint": "0.0.0.0:9187",
140+
"endpoint": "0.0.0.0:" + strconv.Itoa(PrometheusPort),
140141
}
141142

142143
// Add Prometheus Receiver

internal/collector/pgbouncer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"slices"
13+
"strconv"
1314

1415
"github.com/crunchydata/postgres-operator/internal/feature"
1516
"github.com/crunchydata/postgres-operator/internal/naming"
@@ -174,7 +175,7 @@ func EnablePgBouncerMetrics(ctx context.Context, config *Config, sqlQueryUsernam
174175
if feature.Enabled(ctx, feature.OpenTelemetryMetrics) {
175176
// Add Prometheus exporter
176177
config.Exporters[Prometheus] = map[string]any{
177-
"endpoint": "0.0.0.0:9187",
178+
"endpoint": "0.0.0.0:" + strconv.Itoa(PrometheusPort),
178179
}
179180

180181
// Add SqlQuery Receiver

internal/collector/postgres_metrics.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"slices"
13+
"strconv"
1314

1415
"github.com/crunchydata/postgres-operator/internal/feature"
1516
"github.com/crunchydata/postgres-operator/internal/pgmonitor"
@@ -56,7 +57,7 @@ func EnablePostgresMetrics(ctx context.Context, inCluster *v1beta1.PostgresClust
5657

5758
// Add Prometheus exporter
5859
config.Exporters[Prometheus] = map[string]any{
59-
"endpoint": "0.0.0.0:9187",
60+
"endpoint": "0.0.0.0:" + strconv.Itoa(PrometheusPort),
6061
}
6162

6263
config.Receivers[FiveSecondSqlQuery] = map[string]any{
@@ -76,6 +77,7 @@ func EnablePostgresMetrics(ctx context.Context, inCluster *v1beta1.PostgresClust
7677
"initial_delay": "10s",
7778
"queries": slices.Clone(fiveMinuteMetrics),
7879
}
80+
7981
// Add Metrics Pipeline
8082
config.Pipelines[PostgresMetrics] = Pipeline{
8183
Receivers: []ComponentID{FiveSecondSqlQuery, FiveMinuteSqlQuery},
@@ -85,6 +87,30 @@ func EnablePostgresMetrics(ctx context.Context, inCluster *v1beta1.PostgresClust
8587
},
8688
Exporters: []ComponentID{Prometheus},
8789
}
90+
91+
// Add custom queries if they are defined in the spec
92+
if inCluster.Spec.Instrumentation != nil &&
93+
inCluster.Spec.Instrumentation.Metrics != nil &&
94+
inCluster.Spec.Instrumentation.Metrics.CustomQueries != nil &&
95+
inCluster.Spec.Instrumentation.Metrics.CustomQueries.Add != nil {
96+
for _, querySet := range inCluster.Spec.Instrumentation.Metrics.CustomQueries.Add {
97+
// Create a receiver for the query set
98+
receiverName := "sqlquery/" + querySet.Name
99+
config.Receivers[receiverName] = map[string]any{
100+
"driver": "postgres",
101+
"datasource": fmt.Sprintf(`host=localhost dbname=postgres port=5432 user=%s password=${env:PGPASSWORD}`, pgmonitor.MonitoringUser),
102+
"collection_interval": querySet.CollectionInterval,
103+
// Give Postgres time to finish setup.
104+
"initial_delay": "10s",
105+
"queries": "${file:/etc/otel-collector/" + querySet.Name + "/" + querySet.Queries.Key + "}",
106+
}
107+
108+
// Add the receiver to the pipeline
109+
pipeline := config.Pipelines[PostgresMetrics]
110+
pipeline.Receivers = append(pipeline.Receivers, receiverName)
111+
config.Pipelines[PostgresMetrics] = pipeline
112+
}
113+
}
88114
}
89115
}
90116

internal/controller/postgrescluster/instance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,7 @@ func (r *Reconciler) reconcileInstance(
12201220

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

internal/controller/postgrescluster/pgbackrest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ func (r *Reconciler) generateRepoHostIntent(ctx context.Context, postgresCluster
697697
if postgresCluster.Spec.Instrumentation != nil && feature.Enabled(ctx, feature.OpenTelemetryLogs) {
698698
collector.AddToPod(ctx, postgresCluster.Spec.Instrumentation, postgresCluster.Spec.ImagePullPolicy,
699699
&corev1.ConfigMap{ObjectMeta: naming.PGBackRestConfig(postgresCluster)},
700-
&repo.Spec.Template.Spec, []corev1.VolumeMount{}, "",
700+
&repo.Spec.Template, []corev1.VolumeMount{}, "",
701701
[]string{pgBackRestLogPath}, true)
702702

703703
containersToAdd = append(containersToAdd, naming.ContainerCollector)

internal/controller/postgrescluster/pgbouncer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ func (r *Reconciler) generatePGBouncerDeployment(
472472
err := errors.WithStack(r.setControllerReference(cluster, deploy))
473473

474474
if err == nil {
475-
pgbouncer.Pod(ctx, cluster, configmap, primaryCertificate, secret, &deploy.Spec.Template.Spec)
475+
pgbouncer.Pod(ctx, cluster, configmap, primaryCertificate, secret, &deploy.Spec.Template)
476476
}
477477

478478
// Add tmp directory and volume for log files

internal/controller/standalone_pgadmin/statefulset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func statefulset(
132132
}
133133

134134
collector.AddToPod(ctx, pgadmin.Spec.Instrumentation, pgadmin.Spec.ImagePullPolicy,
135-
configmap, &sts.Spec.Template.Spec, volumeMounts, "", []string{}, false)
135+
configmap, &sts.Spec.Template, volumeMounts, "", []string{}, false)
136136
}
137137

138138
return sts

internal/naming/labels.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ const (
4040
// LabelMovePGWalDir is used to identify the Job that moves an existing pg_wal directory.
4141
LabelMovePGWalDir = labelPrefix + "move-pgwal-dir"
4242

43+
// LabelCollectorDiscovery is added to Pods running the OpenTelemetry "collector"
44+
// container to support discovery by Prometheus
45+
LabelCollectorDiscovery = labelPrefix + "crunchy-otel-collector"
46+
4347
// LabelPGBackRest is used to indicate that a resource is for pgBackRest
4448
LabelPGBackRest = labelPrefix + "pgbackrest"
4549

0 commit comments

Comments
 (0)