forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add resource attributes to collector sidecar (open-telemetry#832)
* Add resource attributes to collector sidecar Signed-off-by: Ruben Vargas <[email protected]> * Update getAttributesEnv comments. Co-authored-by: Ben B. <[email protected]> * Use semconv package for attribute names Signed-off-by: Ruben Vargas <[email protected]> * Make env var names consistent Signed-off-by: Ruben Vargas <[email protected]> * Improve comments Signed-off-by: Ruben Vargas <[email protected]> * Rename attributes function Signed-off-by: Ruben Vargas <[email protected]> * fix import order Signed-off-by: Ruben Vargas <[email protected]> * Verify env vars on sidecar e2e test Signed-off-by: Ruben Vargas <[email protected]> * Update readme mentioning OTEL_RESOURCE_ATTRIBUTES env var on sidecar mode Signed-off-by: Ruben Vargas <[email protected]> * README Improvments Signed-off-by: Ruben Vargas <[email protected]> Co-authored-by: Ben B. <[email protected]>
- Loading branch information
1 parent
ef89e83
commit 5be4cdf
Showing
9 changed files
with
459 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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 constants | ||
|
||
const ( | ||
EnvOTELServiceName = "OTEL_SERVICE_NAME" | ||
EnvOTELExporterOTLPEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT" | ||
EnvOTELResourceAttrs = "OTEL_RESOURCE_ATTRIBUTES" | ||
EnvOTELPropagators = "OTEL_PROPAGATORS" | ||
EnvOTELTracesSampler = "OTEL_TRACES_SAMPLER" | ||
EnvOTELTracesSamplerArg = "OTEL_TRACES_SAMPLER_ARG" | ||
|
||
EnvPodName = "OTEL_RESOURCE_ATTRIBUTES_POD_NAME" | ||
EnvPodUID = "OTEL_RESOURCE_ATTRIBUTES_POD_UID" | ||
EnvNodeName = "OTEL_RESOURCE_ATTRIBUTES_NODE_NAME" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// 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 sidecar contains operations related to sidecar manipulation (Add, update, remove). | ||
package sidecar | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
semconv "go.opentelemetry.io/otel/semconv/v1.7.0" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/pkg/constants" | ||
) | ||
|
||
const resourceAttributesEnvName = "OTEL_RESOURCE_ATTRIBUTES" | ||
|
||
type podReferences struct { | ||
replicaset *appsv1.ReplicaSet | ||
deployment *appsv1.Deployment | ||
} | ||
|
||
// getResourceAttributesEnv returns a list of environment variables. The list contains OTEL_RESOURCE_ATTRIBUTES and additional environment variables that use Kubernetes downward API to read pod specification. | ||
// see: https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/ | ||
func getResourceAttributesEnv(ns corev1.Namespace, podReferences podReferences) []corev1.EnvVar { | ||
|
||
var envvars []corev1.EnvVar | ||
|
||
attributes := map[attribute.Key]string{ | ||
semconv.K8SPodNameKey: fmt.Sprintf("$(%s)", constants.EnvPodName), | ||
semconv.K8SPodUIDKey: fmt.Sprintf("$(%s)", constants.EnvPodUID), | ||
semconv.K8SNodeNameKey: fmt.Sprintf("$(%s)", constants.EnvNodeName), | ||
semconv.K8SNamespaceNameKey: ns.Name, | ||
} | ||
|
||
if podReferences.deployment != nil { | ||
attributes[semconv.K8SDeploymentUIDKey] = string(podReferences.deployment.UID) | ||
attributes[semconv.K8SDeploymentNameKey] = string(podReferences.deployment.Name) | ||
} | ||
|
||
if podReferences.replicaset != nil { | ||
attributes[semconv.K8SReplicaSetUIDKey] = string(podReferences.replicaset.UID) | ||
attributes[semconv.K8SReplicaSetNameKey] = string(podReferences.replicaset.Name) | ||
} | ||
|
||
envvars = append(envvars, corev1.EnvVar{ | ||
Name: constants.EnvPodName, | ||
ValueFrom: &corev1.EnvVarSource{ | ||
FieldRef: &corev1.ObjectFieldSelector{ | ||
FieldPath: "metadata.name", | ||
}, | ||
}, | ||
}) | ||
|
||
envvars = append(envvars, corev1.EnvVar{ | ||
Name: constants.EnvPodUID, | ||
ValueFrom: &corev1.EnvVarSource{ | ||
FieldRef: &corev1.ObjectFieldSelector{ | ||
FieldPath: "metadata.uid", | ||
}, | ||
}, | ||
}) | ||
|
||
envvars = append(envvars, corev1.EnvVar{ | ||
Name: constants.EnvNodeName, | ||
ValueFrom: &corev1.EnvVarSource{ | ||
FieldRef: &corev1.ObjectFieldSelector{ | ||
FieldPath: "spec.nodeName", | ||
}, | ||
}, | ||
}) | ||
|
||
envvars = append(envvars, corev1.EnvVar{ | ||
Name: resourceAttributesEnvName, | ||
Value: mapToValue(attributes), | ||
}) | ||
|
||
return envvars | ||
} | ||
|
||
func mapToValue(attributesMap map[attribute.Key]string) string { | ||
var parts []string | ||
|
||
// Sort it to make it predictable | ||
keys := make([]string, 0, len(attributesMap)) | ||
for k := range attributesMap { | ||
keys = append(keys, string(k)) | ||
} | ||
sort.Strings(keys) | ||
|
||
for _, key := range keys { | ||
parts = append(parts, fmt.Sprintf("%s=%s", key, attributesMap[attribute.Key(key)])) | ||
} | ||
return strings.Join(parts, ",") | ||
} | ||
|
||
// check if container doesn't have already the OTEL_RESOURCE_ATTRIBUTES, we don't want to override it if it's already specified. | ||
func hasResourceAttributeEnvVar(envvars []corev1.EnvVar) bool { | ||
for _, env := range envvars { | ||
if env.Name == resourceAttributesEnvName { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.