> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Auto Instrumentation Using the OpenTelemetry Operator

> Explains using the OpenTelemetry Operator to auto-instrument Kubernetes workloads, centralizing instrumentation configuration and injecting language agents via Instrumentation custom resources and annotations.

This article explains how auto-instrumentation works with the OpenTelemetry Operator and how it simplifies collecting telemetry from applications running in Kubernetes. While manual instrumentation remains an option, operator-based auto-instrumentation lets you collect traces, metrics, and logs with minimal code changes.

<Callout icon="lightbulb" color="#1CB2FE">
  Auto-instrumentation is ideal when you need quick visibility into running workloads or when you want consistent observability settings across many deployments and namespaces.
</Callout>

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/d0VZi1GmqxTLJ3bx/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/manual-vs-auto-instrumentation-telemetry.jpg?fit=max&auto=format&n=d0VZi1GmqxTLJ3bx&q=85&s=393b5fe74670f6429a6408f8ea8594f7" alt="The image illustrates a comparison between manual instrumentation and auto-instrumentation for telemetry, highlighting that manual requires explicit coding, while auto collects data with minimal setup." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/manual-vs-auto-instrumentation-telemetry.jpg" />
</Frame>

Why use auto-instrumentation?

* Fast path to visibility when you lack instrumentation in running workloads.
* Centralized configuration reduces duplication and human error.
* Operator ensures consistent agent injection and environment configuration across pods.

What the Operator does

* Injects language-specific instrumentation libraries or agents into targeted pods.
* Sets environment variables and runtime flags required by those agents.
* Routes telemetry to a configured OpenTelemetry Collector or exporter endpoint.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/d0VZi1GmqxTLJ3bx/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/opentelemetry-operator-kubernetes-diagram.jpg?fit=max&auto=format&n=d0VZi1GmqxTLJ3bx&q=85&s=364c1994cb049d353ff201cfc5ad2630" alt="The image is a diagram illustrating the role of an OpenTelemetry Operator within a Kubernetes cluster, involving namespaces for auto-instrumentation and an OpenTelemetry Collector." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/opentelemetry-operator-kubernetes-diagram.jpg" />
</Frame>

Instrumentation custom resources (CRs)

A Kubernetes Custom Resource Definition (CRD) extends the API with new kinds. The OpenTelemetry Operator defines an Instrumentation custom resource that centralizes shared observability settings—exporter endpoints, propagators, samplers, and more—so you don't repeat them in every Deployment or Pod.

When an Instrumentation CR exists in a namespace, workloads can opt in via annotations. This model is particularly useful at scale (many namespaces or clusters) for enforcing consistent telemetry configuration.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/d0VZi1GmqxTLJ3bx/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/instrumentation-custom-resource-process-diagram.jpg?fit=max&auto=format&n=d0VZi1GmqxTLJ3bx&q=85&s=aadbe05dcc085450f4fc85f03fde5491" alt="The image illustrates an &#x22;Instrumentation Custom Resource&#x22; process that auto-instruments a pod, explaining its functions such as setting up auto-instrumentation, holding configuration details, and automatic application use without code changes." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/instrumentation-custom-resource-process-diagram.jpg" />
</Frame>

Example Instrumentation CR

This example creates an Instrumentation CR that directs the operator to export OTLP to a collector and configures propagators and sampling:

```yaml theme={null}
apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
  name: demo-instrumentation
spec:
  exporter:
    endpoint: http://demo-collector:4318
  propagators:
    - tracecontext
    - baggage
  sampler:
    type: parentbased_traceidratio
    argument: "1"
```

Exam-style recall question
What is the Kubernetes kind that automatically instruments applications in Kubernetes?
Answer: Instrumentation

How the CR maps to injected environment variables

The Operator translates the Instrumentation spec into environment variables and agent-specific settings inside the injected pod. Common mappings include:

* `spec.exporter.endpoint` → `OTEL_EXPORTER_OTLP_ENDPOINT` (or agent-specific equivalent).
* `spec.propagators` → `OTEL_PROPAGATORS` with comma-separated values.
* `spec.sampler` → `OTEL_TRACES_SAMPLER` and `OTEL_TRACES_SAMPLER_ARG` (or similar).

Illustrative example of injected env vars in a Pod:

```yaml theme={null}
# Example injected env vars in a Pod (illustrative)
env:
  - name: OTEL_EXPORTER_OTLP_ENDPOINT
    value: "http://demo-collector:4318"
  - name: OTEL_PROPAGATORS
    value: "tracecontext,baggage"
  - name: OTEL_TRACES_SAMPLER
    value: "parentbased_traceidratio"
  - name: OTEL_TRACES_SAMPLER_ARG
    value: "1"
```

Sampling options

You control sampling behavior from the Instrumentation CR. The snippet below shows sampler configuration and common choices:

```yaml theme={null}
# In the Instrumentation CR
spec:
  sampler:
    type: parentbased_traceidratio
    argument: "1"  # 1 = 100%; 0.25 = 25%; 0 = 0%
```

Common sampler types:

| Sampler type               | Behavior                                                                       |
| -------------------------- | ------------------------------------------------------------------------------ |
| `always_on`                | Sample all traces                                                              |
| `always_off`               | Sample no traces                                                               |
| `traceidratio`             | Sample approximately the fraction set by `argument` (e.g., `"0.2"` = 20%)      |
| `parentbased_traceidratio` | Parent-based sampling; when no parent exists, `argument` controls the fraction |

Enabling auto-instrumentation for workloads

Workloads opt in by adding annotations on the Pod template. Each runtime has a specific annotation key.

Pod template annotations example:

```yaml theme={null}
spec:
  template:
    metadata:
      annotations:
        instrumentation.opentelemetry.io/inject-java: "true"
        instrumentation.opentelemetry.io/inject-python: "true"
        instrumentation.opentelemetry.io/inject-nodejs: "true"
        instrumentation.opentelemetry.io/inject-dotnet: "true"
```

Patch an existing Deployment to enable Python auto-instrumentation:

```bash theme={null}
kubectl -n appns patch deploy web --type merge -p '
{
  "spec": {"template": {"metadata": {"annotations": {
      "instrumentation.opentelemetry.io/inject-python": "true"
  }}}}
}'
```

Annotation values and semantics

| Annotation value | Meaning                                                                     |
| ---------------- | --------------------------------------------------------------------------- |
| `"true"`         | Use the default Instrumentation CR in the same namespace                    |
| `"name"`         | Use an Instrumentation CR in the same namespace by name                     |
| `"ns/name"`      | Use an Instrumentation CR from another namespace (useful for shared config) |
| `"false"`        | Explicitly opt out of injection for this workload                           |

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/d0VZi1GmqxTLJ3bx/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/annotating-workloads-annotations-guidelines.jpg?fit=max&auto=format&n=d0VZi1GmqxTLJ3bx&q=85&s=07b5016e3d7405e6b65ba4157d055689" alt="The image titled &#x22;Annotating Workloads&#x22; provides guidelines on using annotations for instrumentation in different namespaces, with a color-coded legend for pods representing true, name, ns/name, and false values." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/annotating-workloads-annotations-guidelines.jpg" />
</Frame>

Namespace-level opt-in

Instead of annotating individual pod templates, you can annotate a Namespace to enable injection for all pods in that namespace:

```yaml theme={null}
# Opt-in at Namespace level (affects all Pods in the namespace)
apiVersion: v1
kind: Namespace
metadata:
  name: appns
  annotations:
    instrumentation.opentelemetry.io/inject-python: "true"
```

<Callout icon="warning" color="#FF6B6B">
  Use namespace-level injection with care. It will affect all pods in the namespace, including system, test, or utility pods that may not need instrumentation.
</Callout>

Supported languages

The Operator supports injecting language-specific agents and setting the appropriate environment variables. Commonly supported runtimes include Java, .NET, Node.js, Python, and Go.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/d0VZi1GmqxTLJ3bx/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/opentelemetry-supported-languages-list.jpg?fit=max&auto=format&n=d0VZi1GmqxTLJ3bx&q=85&s=46988884ea139deab8b382d6400d6f7f" alt="The image lists supported programming languages for OpenTelemetry instrumentation, including Java, .NET, Node.js, Python, and Go, with corresponding URLs." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/opentelemetry-supported-languages-list.jpg" />
</Frame>

Key takeaways

* The OpenTelemetry Operator centralizes observability settings through the Instrumentation custom resource.
* Instrumentation CRs define exporters, propagators, samplers, and other shared configuration to avoid per-deployment duplication.
* Workloads opt in to auto-instrumentation via pod annotations or namespace annotations; annotations can reference Instrumentation CRs across namespaces.
* Apply namespace-level injection carefully to prevent unintentional agent injection.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/d0VZi1GmqxTLJ3bx/images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/kubernetes-observability-operators-instrumentation.jpg?fit=max&auto=format&n=d0VZi1GmqxTLJ3bx&q=85&s=ea066513d4299450c03a394c70626a3b" alt="The image presents key takeaways about Kubernetes observability, highlighting the role of operators and instrumentation in centralizing various components. The design features a gradient background with numbered points." width="1920" height="1080" data-path="images/Prep-Course-OpenTelemetry-Certified-Associate-OTCA-Certification/OpenTelemetry-in-Kubernetes/Auto-Instrumentation-Using-the-OpenTelemetry-Operator/kubernetes-observability-operators-instrumentation.jpg" />
</Frame>

Links and references

* OpenTelemetry: [https://opentelemetry.io/](https://opentelemetry.io/)
* OpenTelemetry Operator (GitHub): [https://github.com/open-telemetry/opentelemetry-operator](https://github.com/open-telemetry/opentelemetry-operator)
* OpenTelemetry Collector: [https://opentelemetry.io/docs/collector/](https://opentelemetry.io/docs/collector/)

That covers the essentials of OpenTelemetry auto-instrumentation using the Operator.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/prep-course-opentelemetry-certified-associate-certification-otca/module/09e46ae9-895f-4e3f-98dd-c7be94303a09/lesson/4ac6b8ec-ac71-475f-bb59-b5823a7b89db" />
</CardGroup>
