forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapachehttpd.go
287 lines (257 loc) · 11.2 KB
/
apachehttpd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package instrumentation
import (
"fmt"
"sort"
"strings"
"github.com/go-logr/logr"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
corev1 "k8s.io/api/core/v1"
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
)
const (
apacheDefaultConfigDirectory = "/usr/local/apache2/conf"
apacheConfigFile = "httpd.conf"
apacheAgentConfigFile = "opentemetry_agent.conf"
apacheAgentDirectory = "/opt/opentelemetry-webserver"
apacheAgentSubDirectory = "/agent"
apacheAgentDirFull = apacheAgentDirectory + apacheAgentSubDirectory
apacheAgentConfigDirectory = "/source-conf"
apacheAgentConfDirFull = apacheAgentDirectory + apacheAgentConfigDirectory
apacheAgentInitContainerName = "otel-agent-attach-apache"
apacheAgentCloneContainerName = "otel-agent-source-container-clone"
apacheAgentConfigVolume = "otel-apache-conf-dir"
apacheAgentVolume = "otel-apache-agent"
apacheAttributesEnvVar = "OTEL_APACHE_AGENT_CONF"
apacheServiceInstanceId = "<<SID-PLACEHOLDER>>"
apacheServiceInstanceIdEnvVar = "APACHE_SERVICE_INSTANCE_ID"
)
/*
Apache injection is different from other languages in:
- OpenTelemetry parameters are not passed as environmental variables, but via a configuration file
- OpenTelemetry module needs to be specified in the Apache HTTPD config file, but that is already specified by
an author of the application image and the configuration must be preserved
Therefore, following approach is taken:
1) Inject an init container created as a *clone* of the application container and copy config file to an empty shared volume
2) Inject a second init container with the OpenTelemetry module itself - i.e. instrumentation image
3) Take the Apache HTTPD configuration file saved on volume and inject reference to OpenTelemetry module into config
4) Create on the same volume a configuration file for OpenTelemetry module
5) Copy OpenTelemetry module from second init container (instrumentation image) to another shared volume
6) Inject mounting of volumes / files into appropriate directories in application container
*/
func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod corev1.Pod, useLabelsForResourceAttributes bool, index int, otlpEndpoint string, resourceMap map[string]string) corev1.Pod {
// caller checks if there is at least one container
container := &pod.Spec.Containers[index]
// inject env vars
for _, env := range apacheSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
if idx == -1 {
container.Env = append(container.Env, env)
}
}
// First make a clone of the instrumented container to take the existing Apache configuration from
// and create init container from it
if isApacheInitContainerMissing(pod, apacheAgentCloneContainerName) {
// Inject volume for original Apache configuration
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Name: apacheAgentConfigVolume,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: volumeSize(apacheSpec.VolumeSizeLimit),
},
}})
apacheConfDir := getApacheConfDir(apacheSpec.ConfigPath)
cloneContainer := container.DeepCopy()
cloneContainer.Name = apacheAgentCloneContainerName
cloneContainer.Command = []string{"/bin/sh", "-c"}
cloneContainer.Args = []string{"cp -r " + apacheConfDir + "/* " + apacheAgentConfDirFull}
cloneContainer.VolumeMounts = append(cloneContainer.VolumeMounts, corev1.VolumeMount{
Name: apacheAgentConfigVolume,
MountPath: apacheAgentConfDirFull,
})
// remove resource requirements since those are then reserved for the lifetime of a pod
// and we definitely do not need them for the init container for cp command
cloneContainer.Resources = apacheSpec.Resources
// remove livenessProbe, readinessProbe, and startupProbe, since not supported on init containers
cloneContainer.LivenessProbe = nil
cloneContainer.ReadinessProbe = nil
cloneContainer.StartupProbe = nil
pod.Spec.InitContainers = append(pod.Spec.InitContainers, *cloneContainer)
// drop volume mount with volume-provided Apache config from original container
// since it could over-write configuration provided by the injection
idxFound := -1
for idx, volume := range container.VolumeMounts {
if strings.Contains(volume.MountPath, apacheConfDir) { // potentially passes config, which we want to pass to init copy only
idxFound = idx
break
}
}
if idxFound >= 0 {
volumeMounts := container.VolumeMounts
volumeMounts = append(volumeMounts[:idxFound], volumeMounts[idxFound+1:]...)
container.VolumeMounts = volumeMounts
}
// Inject volumes info instrumented container - Apache config dir + Apache agent
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: apacheAgentVolume,
MountPath: apacheAgentDirFull,
})
container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: apacheAgentConfigVolume,
MountPath: apacheConfDir,
})
}
// Inject second init container with instrumentation image
// Create / update config files
// Copy OTEL module to a shared volume
if isApacheInitContainerMissing(pod, apacheAgentInitContainerName) {
// Inject volume for agent
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Name: apacheAgentVolume,
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: volumeSize(apacheSpec.VolumeSizeLimit),
},
}})
pod.Spec.InitContainers = append(pod.Spec.InitContainers, corev1.Container{
Name: apacheAgentInitContainerName,
Image: apacheSpec.Image,
Command: []string{"/bin/sh", "-c"},
Args: []string{
// Copy agent binaries to shared volume
"cp -r /opt/opentelemetry/* " + apacheAgentDirFull + " && " +
// setup logging configuration from template
"export agentLogDir=$(echo \"" + apacheAgentDirFull + "/logs\" | sed 's,/,\\\\/,g') && " +
"cat " + apacheAgentDirFull + "/conf/opentelemetry_sdk_log4cxx.xml.template | sed 's/__agent_log_dir__/'${agentLogDir}'/g' > " + apacheAgentDirFull + "/conf/opentelemetry_sdk_log4cxx.xml &&" +
// Create agent configuration file by pasting content of env var to a file
"echo \"$" + apacheAttributesEnvVar + "\" > " + apacheAgentConfDirFull + "/" + apacheAgentConfigFile + " && " +
"sed -i 's/" + apacheServiceInstanceId + "/'${" + apacheServiceInstanceIdEnvVar + "}'/g' " + apacheAgentConfDirFull + "/" + apacheAgentConfigFile + " && " +
// Include a link to include Apache agent configuration file into httpd.conf
"echo 'Include " + getApacheConfDir(apacheSpec.ConfigPath) + "/" + apacheAgentConfigFile + "' >> " + apacheAgentConfDirFull + "/" + apacheConfigFile,
},
Env: []corev1.EnvVar{
{
Name: apacheAttributesEnvVar,
Value: getApacheOtelConfig(pod, useLabelsForResourceAttributes, apacheSpec, index, otlpEndpoint, resourceMap),
},
{Name: apacheServiceInstanceIdEnvVar,
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
},
Resources: apacheSpec.Resources,
VolumeMounts: []corev1.VolumeMount{
{
Name: apacheAgentVolume,
MountPath: apacheAgentDirFull,
},
{
Name: apacheAgentConfigVolume,
MountPath: apacheAgentConfDirFull,
},
},
})
}
return pod
}
// Calculate if we already inject InitContainers.
func isApacheInitContainerMissing(pod corev1.Pod, containerName string) bool {
for _, initContainer := range pod.Spec.InitContainers {
if initContainer.Name == containerName {
return false
}
}
return true
}
// Calculate Apache HTTPD agent configuration file based on attributes provided by the injection rules
// and by the pod values.
func getApacheOtelConfig(pod corev1.Pod, useLabelsForResourceAttributes bool, apacheSpec v1alpha1.ApacheHttpd, index int, otelEndpoint string, resourceMap map[string]string) string {
template := `
#Load the Otel Webserver SDK
LoadFile %[1]s/sdk_lib/lib/libopentelemetry_common.so
LoadFile %[1]s/sdk_lib/lib/libopentelemetry_resources.so
LoadFile %[1]s/sdk_lib/lib/libopentelemetry_trace.so
LoadFile %[1]s/sdk_lib/lib/libopentelemetry_otlp_recordable.so
LoadFile %[1]s/sdk_lib/lib/libopentelemetry_exporter_ostream_span.so
LoadFile %[1]s/sdk_lib/lib/libopentelemetry_exporter_otlp_grpc.so
#Load the Otel ApacheModule SDK
LoadFile %[1]s/sdk_lib/lib/libopentelemetry_webserver_sdk.so
#Load the Apache Module. In this example for Apache 2.4
#LoadModule otel_apache_module %[1]s/WebServerModule/Apache/libmod_apache_otel.so
#Load the Apache Module. In this example for Apache 2.2
#LoadModule otel_apache_module %[1]s/WebServerModule/Apache/libmod_apache_otel22.so
LoadModule otel_apache_module %[1]s/WebServerModule/Apache/libmod_apache_otel%[2]s.so
#Attributes
`
if otelEndpoint == "" {
otelEndpoint = "http://localhost:4317/"
}
serviceName := chooseServiceName(pod, useLabelsForResourceAttributes, resourceMap, index)
serviceNamespace := pod.GetNamespace()
if len(serviceNamespace) == 0 {
serviceNamespace = resourceMap[string(semconv.K8SNamespaceNameKey)]
if len(serviceNamespace) == 0 {
serviceNamespace = "apache-httpd"
}
}
// Namespace name override TBD
// There are two versions of the OTEL modules - for Apache HTTPD 2.4 and 2.2.
// 2.4 is default and the module does not have any version suffix
// 2.2 has version suffix "22"
versionSuffix := ""
if apacheSpec.Version == "2.2" {
versionSuffix = "22"
}
attrMap := map[string]string{
"ApacheModuleEnabled": "ON",
// ApacheModule Otel Exporter details
"ApacheModuleOtelSpanExporter": "otlp",
"ApacheModuleOtelExporterEndpoint": otelEndpoint,
// Service name and other IDs
"ApacheModuleServiceName": serviceName,
"ApacheModuleServiceNamespace": serviceNamespace,
"ApacheModuleServiceInstanceId": apacheServiceInstanceId,
"ApacheModuleResolveBackends": " ON",
"ApacheModuleTraceAsError": " ON",
}
for _, attr := range apacheSpec.Attrs {
attrMap[attr.Name] = attr.Value
}
configFileContent := fmt.Sprintf(template,
apacheAgentDirectory+apacheAgentSubDirectory,
versionSuffix)
keys := make([]string, 0, len(attrMap))
for key := range attrMap {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
configFileContent += fmt.Sprintf("%s %s\n", key, attrMap[key])
}
return configFileContent
}
func getApacheConfDir(configuredDir string) string {
apacheConfDir := apacheDefaultConfigDirectory
if configuredDir != "" {
apacheConfDir = configuredDir
if apacheConfDir[len(apacheConfDir)-1] == '/' {
apacheConfDir = apacheConfDir[:len(apacheConfDir)-1]
}
}
return apacheConfDir
}