@@ -43,11 +43,12 @@ func AddToPod(
43
43
spec * v1beta1.InstrumentationSpec ,
44
44
pullPolicy corev1.PullPolicy ,
45
45
inInstanceConfigMap * corev1.ConfigMap ,
46
- outPod * corev1.PodSpec ,
46
+ template * corev1.PodTemplateSpec ,
47
47
volumeMounts []corev1.VolumeMount ,
48
48
sqlQueryPassword string ,
49
49
logDirectories []string ,
50
50
includeLogrotate bool ,
51
+ thisPodServesMetrics bool ,
51
52
) {
52
53
if spec == nil ||
53
54
! (feature .Enabled (ctx , feature .OpenTelemetryLogs ) ||
@@ -76,14 +77,13 @@ func AddToPod(
76
77
}},
77
78
}
78
79
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 ... )
80
+ // If the user has specified files to be mounted in the spec, add them to
81
+ // the projected config volume
82
+ if spec .Config != nil && spec .Config .Files != nil {
83
+ configVolume .Projected .Sources = append (configVolume .Projected .Sources ,
84
+ spec .Config .Files ... )
82
85
}
83
86
84
- // Add configVolume to the pod's volumes
85
- outPod .Volumes = append (outPod .Volumes , configVolume )
86
-
87
87
// Create collector container
88
88
container := corev1.Container {
89
89
Name : naming .ContainerCollector ,
@@ -113,6 +113,28 @@ func AddToPod(
113
113
VolumeMounts : append (volumeMounts , configVolumeMount ),
114
114
}
115
115
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
+
116
138
// If this is a pod that uses logrotate for log rotation, add config volume
117
139
// and mount for logrotate config
118
140
if includeLogrotate {
@@ -136,18 +158,17 @@ func AddToPod(
136
158
}},
137
159
}
138
160
container .VolumeMounts = append (container .VolumeMounts , logrotateConfigVolumeMount )
139
- outPod . Volumes = append (outPod .Volumes , logrotateConfigVolume )
161
+ template . Spec . Volumes = append (template . Spec .Volumes , logrotateConfigVolume )
140
162
}
141
163
142
- if feature .Enabled (ctx , feature .OpenTelemetryMetrics ) {
143
- container .Ports = []corev1.ContainerPort {{
144
- ContainerPort : int32 (8889 ),
145
- Name : "otel-metrics" ,
146
- Protocol : corev1 .ProtocolTCP ,
147
- }}
148
- }
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 )
167
+ template .Spec .Containers = append (template .Spec .Containers , container )
149
168
150
- outPod .Containers = append (outPod .Containers , container )
169
+ // add the OTel collector label to the Pod
170
+ initialize .Labels (template )
171
+ template .Labels [naming .LabelCollectorDiscovery ] = "true"
151
172
}
152
173
153
174
// startCommand generates the command script used by the collector container
@@ -192,7 +213,8 @@ while read -r -t 5 -u "${fd}" ||:; do
192
213
done
193
214
` , mkdirScript , configDirectory , logrotateCommand )
194
215
195
- wrapper := `monitor() {` + startScript + `}; export directory="$1"; export -f monitor; exec -a "$0" bash -ceu monitor`
216
+ wrapper := `monitor() {` + startScript +
217
+ `}; export directory="$1"; export -f monitor; exec -a "$0" bash -ceu monitor`
196
218
197
219
return []string {"bash" , "-ceu" , "--" , wrapper , "collector" , configDirectory }
198
220
}
0 commit comments