|
| 1 | +# Instrumenting applications with EDOT SDKs on Kubernetes |
| 2 | + |
| 3 | +Elastic Distributions of OpenTelemetry (EDOT) SDKs cover multiple languages: |
| 4 | + |
| 5 | +* [EDOT Java](https://github.com/elastic/elastic-otel-java) |
| 6 | +* [EDOT .NET](https://github.com/elastic/elastic-otel-dotnet) |
| 7 | +* [EDOT Node.js](https://github.com/elastic/elastic-otel-node) |
| 8 | +* [EDOT Python](https://github.com/elastic/elastic-otel-python) |
| 9 | +* [EDOT PHP](https://github.com/elastic/elastic-otel-php/) |
| 10 | + |
| 11 | +This section provides guidance and examples for applications instrumentation in a Kubernetes environment for all supported languages. |
| 12 | + |
| 13 | +In Kubernetes environments with the OpenTelemetry Operator, [**automatic (or zero-code) instrumentation**](https://opentelemetry.io/docs/kubernetes/operator/automatic/) simplifies the process by injecting and configuring instrumentation libraries into the targeted Pods. |
| 14 | + |
| 15 | +On the other hand, **manual instrumentation** with OpenTelemetry involves adding specific OpenTelemetry SDKs and APIs directly into your application’s code. This approach provides more granular control over what and how data is captured, allowing you to customize trace spans, metrics, and logging based on your application’s logic. |
| 16 | + |
| 17 | +## Table of contents |
| 18 | + |
| 19 | +- [Supported languages](#supported-languages) |
| 20 | +- [Prerequisites](#prerequisites) |
| 21 | +- [Auto-instrumentation basics](#auto-instrumentation-basics) |
| 22 | +- [Configuring auto-instrumentation](#configuring-auto-instrumentation) |
| 23 | +- [Advanced configuration](#advanced-configuration) |
| 24 | +- [Manual instrumentation](#manual-instrumentation) |
| 25 | + |
| 26 | +## Supported languages |
| 27 | + |
| 28 | +The following table illustrates the different languages supported by OpenTelemetry (OTel) and the Elastic Stack, the type of SDK/API used for instrumentation (either zero-code or source code dependencies), and the corresponding deployment types (on-premises, ESS, or serverless) for each language. |
| 29 | + |
| 30 | +| Language | OTel SDK/API Type | Deployment Model Support | |
| 31 | +|------------|---------------------------------------------------------|-------------------------------------| |
| 32 | +| Java | EDOT Java - **zero-code instrumentation** | All deployment types | |
| 33 | +| Node.js | EDOT Node.js - **zero-code instrumentation** | All deployment types | |
| 34 | +| .NET | EDOT .NET - **zero-code instrumentation** | All deployment types | |
| 35 | +| PHP | EDOT PHP - source code dependencies | All deployment types | |
| 36 | +| Python | EDOT Python - **zero-code instrumentation** | All deployment types | |
| 37 | +| Swift | EDOT Swift - source code dependencies | ESS, on-premises | |
| 38 | +| Android | EDOT Android - source code dependencies | ESS, on-premises | |
| 39 | +| Javascript | Vanilla OTel RUM SDK/API - source code dependencies | ESS, on-premises | |
| 40 | +| Rust | Vanilla OTel Rust SDK/API - source code dependencies | All deployment types | |
| 41 | +| Ruby | Vanilla OTel Ruby SDK/API - source code dependencies | All deployment types | |
| 42 | +| Go | Vanilla OTel Go SDK/API - **zero-code instrumentation** | All deployment types | |
| 43 | +| C++ | Vanilla OTel C++ SDK/API - source code dependencies | All deployment types | |
| 44 | + |
| 45 | +## Prerequisites |
| 46 | + |
| 47 | +Before starting with application auto-instrumentation, ensure the following prerequisites are in place for proper setup: |
| 48 | + |
| 49 | +- Install the OpenTelemetry operator and EDOT collectors following the [getting started guide](./README.md). |
| 50 | +- Ensure a valid `kind: Instrumentation` object exists in the cluster. |
| 51 | + |
| 52 | +## Auto-instrumentation basics |
| 53 | + |
| 54 | +Zero-code instrumentation is handled by the operator through `Instrumentation` objects, used to automatically inject the necessary SDKs and configuration into application workloads. |
| 55 | + |
| 56 | +If you followed the [getting started guide](./README.md) to install the operator, there should be an `Instrumentation` object with name `elastic-instrumentation` in namespace `opentelemetry-operator-system`: |
| 57 | + |
| 58 | +```bash |
| 59 | +kubectl get instrumentation -A |
| 60 | +NAMESPACE NAME AGE ENDPOINT SAMPLER SAMPLER ARG |
| 61 | +opentelemetry-operator-system elastic-instrumentation 5d20h http://opentelemetry-kube-stack-daemon-collector.opentelemetry-operator-system.svc.cluster.local:4318 parentbased_traceidratio 1.0 |
| 62 | +``` |
| 63 | + |
| 64 | +The `Instrumentation` object stores important parameters: |
| 65 | + |
| 66 | +- The **exporter endpoint** represents the destination for the traces, in this case the HTTP receiver configured in the EDOT DaemonSet Collector. That endpoint has to be reachable by the Pods being instrumented. |
| 67 | + |
| 68 | +```yaml |
| 69 | + exporter: |
| 70 | + endpoint: http://opentelemetry-kube-stack-daemon-collector.opentelemetry-operator-system.svc.cluster.local:4318 |
| 71 | +``` |
| 72 | +
|
| 73 | +- Language-specific **images** used by the operator to inject the appropriate library into each Pod. |
| 74 | +
|
| 75 | +```yaml |
| 76 | + dotnet: |
| 77 | + image: docker.elastic.co/observability/elastic-otel-dotnet:edge |
| 78 | + java: |
| 79 | + image: docker.elastic.co/observability/elastic-otel-javaagent:1.0.0 |
| 80 | + nodejs: |
| 81 | + image: docker.elastic.co/observability/elastic-otel-node:0.4.1 |
| 82 | + python: |
| 83 | + image: docker.elastic.co/observability/elastic-otel-python:0.3.0 |
| 84 | +``` |
| 85 | +
|
| 86 | +## Configuring auto-instrumentation |
| 87 | +
|
| 88 | +To enable auto-instrumentation, add the corresponding language annotation to the **Pods** template (`spec.template.metadata.annotations`) in your Deployment or relevant workload object (StatefulSet, Job, CronJob, etc.). |
| 89 | + |
| 90 | +```yaml |
| 91 | +apiVersion: apps/v1 |
| 92 | +kind: Deployment |
| 93 | +metadata: |
| 94 | + name: myapp |
| 95 | +spec: |
| 96 | + ... |
| 97 | + template: |
| 98 | + metadata: |
| 99 | + annotations: |
| 100 | + instrumentation.opentelemetry.io/inject-<LANGUAGE>: "opentelemetry-operator-system/elastic-instrumentation" |
| 101 | + ... |
| 102 | + spec: |
| 103 | + containers: |
| 104 | + - image: myapplication-image |
| 105 | + name: app |
| 106 | + ... |
| 107 | +``` |
| 108 | + |
| 109 | +where ``<LANGUAGE>`` is one of: `go` , `java`, `nodejs`, `python`, `dotnet` |
| 110 | + |
| 111 | +> [!NOTE] |
| 112 | +> Ensure you add the annotations at Pod level and not directly at the workload `spec` level (Deployment, Job, etc.). |
| 113 | +> Ensure the annotation value must points to an existing `Instrumentation` object. |
| 114 | + |
| 115 | +Alternatively, you can enable auto-instrumentation by adding the annotation at the **namespace level**. This approach automatically applies instrumentation to all Pods within the specified namespace. |
| 116 | + |
| 117 | +```yaml |
| 118 | +apiVersion: v1 |
| 119 | +kind: Namespace |
| 120 | +metadata: |
| 121 | + name: mynamespace |
| 122 | + annotations: |
| 123 | + instrumentation.opentelemetry.io/inject-<LANGUAGE>: "opentelemetry-operator-system/elastic-instrumentation" |
| 124 | +``` |
| 125 | + |
| 126 | +After adding annotations to Pods or Namespaces, the applications must be restarted for the instrumentation injection to take effect: |
| 127 | + |
| 128 | +```bash |
| 129 | +kubectl rollout restart deployment/my-deployment |
| 130 | +``` |
| 131 | + |
| 132 | +In case you have multiple Instrumentation objects with different settings or images, ensure you point your Pods to the the desired `Instrumentation` objects in the annotations. |
| 133 | + |
| 134 | +The possible values for the annotation are detailed in the [Operator documentation](https://opentelemetry.io/docs/kubernetes/operator/automatic/#add-annotations-to-existing-deployments). For reference purposes, the values are: |
| 135 | + |
| 136 | +- `"true"`: to inject Instrumentation resource with default name from the current namespace. |
| 137 | +- `"my-instrumentation"`: to inject Instrumentation instance with name `"my-instrumentation"` in the current namespace. |
| 138 | +- `"my-other-namespace/my-instrumentation"`: to inject Instrumentation instance with name `"my-instrumentation"` from another namespace `"my-other-namespace"`. |
| 139 | +- `"false"`: do not inject. |
| 140 | + |
| 141 | +### Namespace based annotations example |
| 142 | + |
| 143 | +The following example creates a namespace with an annotation to instrument all Pods of the namespace with `java` libraries. |
| 144 | + |
| 145 | +``` |
| 146 | +kubectl create namespace java-apps |
| 147 | + |
| 148 | +#Annotate app namespace |
| 149 | +kubectl annotate namespace java-apps instrumentation.opentelemetry.io/inject-java="opentelemetry-operator-system/elastic-instrumentation" |
| 150 | + |
| 151 | +# Run a java example application in the namespace |
| 152 | +kubectl run otel-test -n java-apps --env OTEL_INSTRUMENTATION_METHODS_INCLUDE="test.Testing[methodB]" --image docker.elastic.co/demos/apm/k8s-webhook-test |
| 153 | +``` |
| 154 | +
|
| 155 | +## Advanced configuration |
| 156 | +
|
| 157 | +You can apply OTEL specific configuration to your applications at two different levels: |
| 158 | +- At Pod/container level, by using OTEL related environment variables. |
| 159 | +- At `Instrumentation` object level, for example configuring different settings per language. |
| 160 | +
|
| 161 | +Use cases: |
| 162 | +- Change the library to be injected. |
| 163 | +- Change the exporter endpoint. |
| 164 | +- Apply certain logging level settings (OTEL_LOG_LEVEL). |
| 165 | +
|
| 166 | +### Adding extra Instrumentation objects |
| 167 | +
|
| 168 | +Consider also the creation of different `Instrumentation` objects for different purposes, such as: |
| 169 | +
|
| 170 | +- Different configuration options for certain languages. |
| 171 | +- Trying out different versions of the SDKs. |
| 172 | +
|
| 173 | +(TBD: add instructions and references about Instrumentation objects) |
| 174 | +
|
| 175 | +
|
| 176 | +## Manual instrumentation |
| 177 | +(TBD, in-progress) |
| 178 | +
|
| 179 | +The manual instrumentation... |
| 180 | +Configuration requirements (does every language has its own requirements)? |
| 181 | +Exporter destination? HTTP vs OTLP? does each EDOT SDK support different protocols? |
| 182 | +
|
0 commit comments