Auto-instrumentation with OpenTelemetry Operator in Kubernetes
This guide walks you through setting up cert-manager, opentelemetry-operator, and observe-agent using Helm and configuring an Instrumentation custom resource to enable auto-instrumentation in your Kubernetes cluster. Note that this guide only applies to Kubernetes clusters.
Prerequisites
- A Kubernetes cluster (v1.20+ recommended)
kubectlinstalled and configuredhelminstalled (v3.0+)
Set up auto-instrumentation
Perform the following tasks to set up auto-instrumentation
- Install cert-manager.
- Install otel-operator.
- Configure the OpenTelemetry Instrumentation custom resource
- Install observe-agent
- Deploy your instrumented application
Install cert-manager
cert-managercert-manager is required to handle TLS certificates in your cluster and for the Operator to be able to communicate with the Kubernetes cluster control plane and modify resources as required. Install it using Helm:
# Add the Jetstack Helm repository
helm repo add jetstack https://charts.jetstack.io --force-update
# Install cert-manager with CRDs
helm install \
cert-manager jetstack/cert-manager \
--namespace cert-manager \
--create-namespace \
--version v1.17.0 \
--set crds.enabled=trueInstall otel-operator
otel-operatorThe OpenTelemetry Operator manages telemetry collection and auto-instrumentation. Install it using the official Helm chart:
# Add the OpenTelemetry Helm repository
helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts
helm repo update
# Install otel-operator
helm install opentelemetry-operator open-telemetry/opentelemetry-operator \
--namespace observe \
--set "manager.collectorImage.repository=observeinc/observe-agent" \
--set "manager.extraArgs={--enable-go-instrumentation}"Check that the operator is running:
kubectl get pods -n observeConfigure the OpenTelemetry Instrumentation custom resource
Instrumentation custom resourceCreate an Instrumentation resource to enable automatic telemetry for supported workloads. To work with the observe-agent helm chart, the instrumentation will use the forwarder daemonset cluster endpoint as the target for the exporter. Below is an example YAML that configures auto-instrumentation for multiple languages:
apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
name: observe-auto-instrumentation
namespace: observe
spec:
exporter:
endpoint: http://observe-agent-forwarder.observe.svc.cluster.local:4318
propagators:
- tracecontext
- baggage
- b3
sampler:
type: parentbased_always_on
java:
env:
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://observe-agent-forwarder.observe.svc.cluster.local:4318
nodejs:
env:
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://observe-agent-forwarder.observe.svc.cluster.local:4318
python:
env:
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://observe-agent-forwarder.observe.svc.cluster.local:4318
dotnet:
env:
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://observe-agent-forwarder.observe.svc.cluster.local:4318
go:
env:
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: http://observe-agent-forwarder.observe.svc.cluster.local:4318Apply the configuration:
kubectl apply -f instrumentation.yamlInstall observe-agent
observe-agentFinally, install the observe-agent Helm chart to collect and forward telemetry data to your Observe backend. See Install on Kubernetes for detailed instructions.
Deploy your instrumented application
The operator will look for specific annotations on your workloads to enable auto-instrumentation and will automatically inject the sidecar or instrumentation code for any service with matching annotations. Note that the instrumentation annotation includes the observe namespace before the name of the instrumentation. Also note that in the case of golang since the autoinstrumentation is eBPF based, you'll need to add an additional annotation that indicates the path of the main executable within the container.
Partial example golang deployment with instrumentation enabled:
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-app
spec:
replicas: 1
selector:
matchLabels:
app: sample-app
template:
metadata:
labels:
app: sample-app
annotations:
instrumentation.opentelemetry.io/inject-go: "observe/observe-auto-instrumentation"
instrumentation.opentelemetry.io/otel-go-auto-target-exe: "/usr/local/bin/sample-app"For more detail on the available annotations, see the OpenTelemetry auto-instrumentation injection in the OpenTelemetry Operator for Kubernetes documentation. Once your workloads are annotated, restart them in order for the operator to inject the necessary sidecar or instrumentation code.
References
Updated about 2 months ago