Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Ability to Override Service Names #939

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,15 @@ spec:

In the above case, `myapp` and `myapp2` containers will be instrumented, `myapp3` will not.

#### Override Service Name

The operator automatically determines the most suitable service name from the Kubernetes object names. However the service name can be overridden by the `instrumentation.opentelemetry.io/service-name` annotation in the pod specification. Follows the precedence order for determining the service name:

1. The `instrumentation.opentelemetry.io/service-name` pod annotation (can be `,` seperated list for multiple container services)
2. The Deployment/Statefulset/Cronjob/Job Name
3. The Pod Name
4. The Container Name

#### Use customized or vendor instrumentation

By default, the operator uses upstream auto-instrumentation libraries. Custom auto-instrumentation can be configured by
Expand Down
5 changes: 5 additions & 0 deletions pkg/instrumentation/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
const (
volumeName = "opentelemetry-auto-instrumentation"
initContainerName = "opentelemetry-auto-instrumentation"
svcNameAnnotation = "instrumentation.opentelemetry.io/service-name"
)

// inject a new sidecar container to the given pod, based on the given OpenTelemetryCollector.
Expand Down Expand Up @@ -208,6 +209,10 @@ func (i *sdkInjector) injectCommonSDKConfig(ctx context.Context, otelinst v1alph
}

func chooseServiceName(pod corev1.Pod, resources map[string]string, index int) string {
serviceNames := strings.Split(pod.Annotations[svcNameAnnotation], ",")
if name := serviceNames[index]; name != "" {
return name
}
if name := resources[string(semconv.K8SDeploymentNameKey)]; name != "" {
return name
}
Expand Down
98 changes: 98 additions & 0 deletions pkg/instrumentation/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,104 @@ func TestSDKInjection(t *testing.T) {
},
},
},
{
name: "Service Name Annotation Added",
inst: v1alpha1.Instrumentation{
Spec: v1alpha1.InstrumentationSpec{
Exporter: v1alpha1.Exporter{
Endpoint: "https://collector:4317",
},
Resource: v1alpha1.Resource{
Attributes: map[string]string{
"fromcr": "val",
},
},
Propagators: []v1alpha1.Propagator{"jaeger"},
Sampler: v1alpha1.Sampler{
Type: "parentbased_traceidratio",
Argument: "0.25",
},
},
},
pod: corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "project1",
Name: "app",
Annotations: map[string]string{
"instrumentation.opentelemetry.io/service-name": "my-app",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Env: []corev1.EnvVar{
{
Name: "OTEL_EXPORTER_OTLP_ENDPOINT",
Value: "explicitly_set",
},
{
Name: "OTEL_PROPAGATORS",
Value: "b3",
},
{
Name: "OTEL_TRACES_SAMPLER",
Value: "always_on",
},
{
Name: "OTEL_RESOURCE_ATTRIBUTES",
Value: "foo=bar,k8s.container.name=other,",
},
},
},
},
},
},
expected: corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: "project1",
Name: "app",
Annotations: map[string]string{
"instrumentation.opentelemetry.io/service-name": "my-app",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Env: []corev1.EnvVar{
{
Name: "OTEL_EXPORTER_OTLP_ENDPOINT",
Value: "explicitly_set",
},
{
Name: "OTEL_PROPAGATORS",
Value: "b3",
},
{
Name: "OTEL_TRACES_SAMPLER",
Value: "always_on",
},
{
Name: "OTEL_SERVICE_NAME",
Value: "my-app",
},
{
Name: "OTEL_RESOURCE_ATTRIBUTES_NODE_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "spec.nodeName",
},
},
},
{
Name: "OTEL_RESOURCE_ATTRIBUTES",
Value: "foo=bar,k8s.container.name=other,fromcr=val,k8s.namespace.name=project1,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=app",
},
},
},
},
},
},
},
}

for _, test := range tests {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: opentelemetry.io/v1alpha1
kind: OpenTelemetryCollector
metadata:
name: sidecar
spec:
mode: sidecar
config: |
receivers:
otlp:
protocols:
grpc:
http:
processors:

exporters:
logging:

service:
pipelines:
traces:
receivers: [otlp]
processors: []
exporters: [logging]
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
name: java
spec:
env:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

somebody can define OTEL_SERVICE_NAME here. What is the precedence order of setting the service name? We should document this in the readme and have a test.

Copy link
Author

@blakeromano blakeromano Jun 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly am not crazy sure on these answers. If you don't mind helping me fill the gaps @pavolloffay

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blakeromano, I'm going to check what is missing to help you out.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yuriolisa I apologize been busy I was wondering if you had time to help me out 😸

- name: OTEL_TRACES_EXPORTER
value: otlp
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://localhost:4317
- name: OTEL_EXPORTER_OTLP_TIMEOUT
value: "20"
- name: OTEL_TRACES_SAMPLER
value: parentbased_traceidratio
- name: OTEL_TRACES_SAMPLER_ARG
value: "0.85"
- name: SPLUNK_TRACE_RESPONSE_HEADER_ENABLED
value: "true"
exporter:
endpoint: http://localhost:4317
propagators:
- jaeger
- b3
sampler:
type: parentbased_traceidratio
argument: "0.25"
java:
env:
- name: OTEL_JAVAAGENT_DEBUG
value: "true"
- name: OTEL_INSTRUMENTATION_JDBC_ENABLED
value: "false"
- name: SPLUNK_PROFILER_ENABLED
value: "false"
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
apiVersion: v1
kind: Pod
metadata:
annotations:
sidecar.opentelemetry.io/inject: "true"
instrumentation.opentelemetry.io/inject-python: "true"
instrumentation.opentelemetry.io/service-name: "ServiceName"
labels:
app: my-pod
spec:
containers:
- name: myapp
env:
- name: PYTHONPATH
value: /otel-auto-instrumentation/opentelemetry/instrumentation/auto_instrumentation:/otel-auto-instrumentation
- name: OTEL_TRACES_EXPORTER
value: otlp_proto_http
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://localhost:4317
- name: OTEL_EXPORTER_OTLP_TIMEOUT
value: "20"
- name: OTEL_TRACES_SAMPLER
value: parentbased_traceidratio
- name: OTEL_TRACES_SAMPLER_ARG
value: "0.85"
- name: SPLUNK_TRACE_RESPONSE_HEADER_ENABLED
value: "true"
- name: OTEL_SERVICE_NAME
value: ServiceName
- name: OTEL_RESOURCE_ATTRIBUTES_POD_NAME
- name: OTEL_RESOURCE_ATTRIBUTES_NODE_NAME
- name: OTEL_PROPAGATORS
value: jaeger,b3
- name: OTEL_RESOURCE_ATTRIBUTES
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
- mountPath: /otel-auto-instrumentation
name: opentelemetry-auto-instrumentation
- name: otc-container
status:
phase: Running
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
selector:
matchLabels:
app: my-pod
replicas: 1
template:
metadata:
labels:
app: my-pod
annotations:
sidecar.opentelemetry.io/inject: "true"
instrumentation.opentelemetry.io/inject-python: "true"
instrumentation.opentelemetry.io/service-name: "ServiceName"
spec:
containers:
- name: myapp
image: ghcr.io/anuraaga/flask-hello-world:latest