Skip to content

Commit b4430fc

Browse files
committed
Only add custom queries to projected config volume if metrics feature is enabled and the pod serves metrics. Reorganize some code.
1 parent e92fbea commit b4430fc

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

internal/collector/instance.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func AddToPod(
4848
sqlQueryPassword string,
4949
logDirectories []string,
5050
includeLogrotate bool,
51-
includeMetricsPort bool,
51+
thisPodServesMetrics bool,
5252
) {
5353
if spec == nil ||
5454
!(feature.Enabled(ctx, feature.OpenTelemetryLogs) ||
@@ -84,21 +84,6 @@ func AddToPod(
8484
spec.Config.Files...)
8585
}
8686

87-
// If the user has specified custom queries to add, put the queries file(s)
88-
// in the projected config volume
89-
if spec.Metrics != nil && spec.Metrics.CustomQueries != nil &&
90-
spec.Metrics.CustomQueries.Add != nil {
91-
for _, querySet := range spec.Metrics.CustomQueries.Add {
92-
projection := querySet.Queries.AsProjection(querySet.Name +
93-
"/" + querySet.Queries.Key)
94-
configVolume.Projected.Sources = append(configVolume.Projected.Sources,
95-
corev1.VolumeProjection{ConfigMap: &projection})
96-
}
97-
}
98-
99-
// Add configVolume to the pod's volumes
100-
template.Spec.Volumes = append(template.Spec.Volumes, configVolume)
101-
10287
// Create collector container
10388
container := corev1.Container{
10489
Name: naming.ContainerCollector,
@@ -128,6 +113,28 @@ func AddToPod(
128113
VolumeMounts: append(volumeMounts, configVolumeMount),
129114
}
130115

116+
// If metrics feature is enabled and this Pod serves metrics, add the
117+
// Prometheus port to this container
118+
if feature.Enabled(ctx, feature.OpenTelemetryMetrics) && thisPodServesMetrics {
119+
container.Ports = []corev1.ContainerPort{{
120+
ContainerPort: int32(PrometheusPort),
121+
Name: "otel-metrics",
122+
Protocol: corev1.ProtocolTCP,
123+
}}
124+
125+
// If the user has specified custom queries to add, put the queries
126+
// file(s) in the projected config volume
127+
if spec.Metrics != nil && spec.Metrics.CustomQueries != nil &&
128+
spec.Metrics.CustomQueries.Add != nil {
129+
for _, querySet := range spec.Metrics.CustomQueries.Add {
130+
projection := querySet.Queries.AsProjection(querySet.Name +
131+
"/" + querySet.Queries.Key)
132+
configVolume.Projected.Sources = append(configVolume.Projected.Sources,
133+
corev1.VolumeProjection{ConfigMap: &projection})
134+
}
135+
}
136+
}
137+
131138
// If this is a pod that uses logrotate for log rotation, add config volume
132139
// and mount for logrotate config
133140
if includeLogrotate {
@@ -154,17 +161,12 @@ func AddToPod(
154161
template.Spec.Volumes = append(template.Spec.Volumes, logrotateConfigVolume)
155162
}
156163

157-
if feature.Enabled(ctx, feature.OpenTelemetryMetrics) && includeMetricsPort {
158-
container.Ports = []corev1.ContainerPort{{
159-
ContainerPort: int32(PrometheusPort),
160-
Name: "otel-metrics",
161-
Protocol: corev1.ProtocolTCP,
162-
}}
163-
}
164-
164+
// Add configVolume to the Pod's volumes and add the collector container to
165+
// the Pod's containers
166+
template.Spec.Volumes = append(template.Spec.Volumes, configVolume)
165167
template.Spec.Containers = append(template.Spec.Containers, container)
166168

167-
// add the proper label to support Pod discovery by Prometheus
169+
// add the OTel collector label to the Pod
168170
initialize.Labels(template)
169171
template.Labels[naming.LabelCollectorDiscovery] = "true"
170172
}

0 commit comments

Comments
 (0)