|
| 1 | +# OpenTelemetry |
| 2 | + |
| 3 | +Enables requests served by NGINX for distributed telemetry via The OpenTelemetry Project. |
| 4 | + |
| 5 | +Using the third party module [opentelemetry-cpp-contrib/nginx](https://github.com/open-telemetry/opentelemetry-cpp-contrib/tree/main/instrumentation/nginx) the NGINX ingress controller can configure NGINX to enable [OpenTelemetry](http://opentelemetry.io) instrumentation. |
| 6 | +By default this feature is disabled. |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +To enable the instrumentation we must enable OpenTelemetry in the configuration ConfigMap: |
| 11 | +```yaml |
| 12 | +data: |
| 13 | + enable-opentelemetry: "true" |
| 14 | +``` |
| 15 | +
|
| 16 | +To enable or disable instrumentation for a single Ingress, use |
| 17 | +the `enable-opentelemetry` annotation: |
| 18 | +```yaml |
| 19 | +kind: Ingress |
| 20 | +metadata: |
| 21 | + annotations: |
| 22 | + nginx.ingress.kubernetes.io/enable-opentelemetry: "true" |
| 23 | +``` |
| 24 | + |
| 25 | +We must also set the host to use when uploading traces: |
| 26 | + |
| 27 | +```yaml |
| 28 | +otlp-collector-host: "otel-coll-collector.otel.svc" |
| 29 | +``` |
| 30 | +NOTE: While the option is called `otlp-collector-host`, you will need to point this to any backend that recieves otlp-grpc. |
| 31 | + |
| 32 | +Next you will need to deploy a distributed telemetry system which uses OpenTelemetry. |
| 33 | +[opentelemetry-collector](https://github.com/open-telemetry/opentelemetry-collector), [Jaeger](https://www.jaegertracing.io/) |
| 34 | +[Tempo](https://github.com/grafana/tempo), and [zipkin](https://zipkin.io/) |
| 35 | +have been tested. |
| 36 | + |
| 37 | +Other optional configuration options: |
| 38 | +```yaml |
| 39 | +# specifies the name to use for the server span |
| 40 | +opentelemetry-operation-name |
| 41 | +
|
| 42 | +# sets whether or not to trust incoming telemetry spans |
| 43 | +opentelemetry-trust-incoming-span |
| 44 | +
|
| 45 | +# specifies the port to use when uploading traces, Default: 4317 |
| 46 | +otlp-collector-port |
| 47 | +
|
| 48 | +# specifies the service name to use for any traces created, Default: nginx |
| 49 | +otel-service-name |
| 50 | +
|
| 51 | +# The maximum queue size. After the size is reached data are dropped. |
| 52 | +otel-max-queuesize |
| 53 | +
|
| 54 | +# The delay interval in milliseconds between two consecutive exports. |
| 55 | +otel-schedule-delay-millis |
| 56 | + |
| 57 | +# How long the export can run before it is cancelled. |
| 58 | +otel-schedule-delay-millis |
| 59 | +
|
| 60 | +# The maximum batch size of every export. It must be smaller or equal to maxQueueSize. |
| 61 | +otel-max-export-batch-size |
| 62 | +
|
| 63 | +# specifies sample rate for any traces created, Default: 0.01 |
| 64 | +otel-sampler-ratio |
| 65 | +
|
| 66 | +# specifies the sampler to be used when sampling traces. |
| 67 | +# The available samplers are: AlwaysOn, AlwaysOff, TraceIdRatioBased, Default: AlwaysOff |
| 68 | +otel-sampler |
| 69 | +
|
| 70 | +# Uses sampler implementation which by default will take a sample if parent Activity is sampled, Default: false |
| 71 | +otel-sampler-parent-based |
| 72 | +``` |
| 73 | + |
| 74 | +Note that you can also set whether to trust incoming spans (global default is true) per-location using annotations like the following: |
| 75 | +```yaml |
| 76 | +kind: Ingress |
| 77 | +metadata: |
| 78 | + annotations: |
| 79 | + nginx.ingress.kubernetes.io/opentelemetry-trust-incoming-span: "true" |
| 80 | +``` |
| 81 | + |
| 82 | +## Examples |
| 83 | + |
| 84 | +The following examples show how to deploy and test different distributed telemetry systems. These example can be performed using Docker Desktop. |
| 85 | + |
| 86 | +In the [esigo/nginx-example](https://github.com/esigo/nginx-example) |
| 87 | +GitHub repository is an example of a simple hello service: |
| 88 | + |
| 89 | +```mermaid |
| 90 | +graph TB |
| 91 | + subgraph Browser |
| 92 | + start["http://esigo.dev/hello/nginx"] |
| 93 | + end |
| 94 | +
|
| 95 | + subgraph app |
| 96 | + sa[service-a] |
| 97 | + sb[service-b] |
| 98 | + sa --> |name: nginx| sb |
| 99 | + sb --> |hello nginx!| sa |
| 100 | + end |
| 101 | +
|
| 102 | + subgraph otel |
| 103 | + otc["Otel Collector"] |
| 104 | + end |
| 105 | +
|
| 106 | + subgraph observability |
| 107 | + tempo["Tempo"] |
| 108 | + grafana["Grafana"] |
| 109 | + backend["Jaeger"] |
| 110 | + zipkin["Zipkin"] |
| 111 | + end |
| 112 | +
|
| 113 | + subgraph ingress-nginx |
| 114 | + ngx[nginx] |
| 115 | + end |
| 116 | +
|
| 117 | + subgraph ngx[nginx] |
| 118 | + ng[nginx] |
| 119 | + om[OpenTelemetry module] |
| 120 | + end |
| 121 | +
|
| 122 | + subgraph Node |
| 123 | + app |
| 124 | + otel |
| 125 | + observability |
| 126 | + ingress-nginx |
| 127 | + om --> |otlp-gRPC| otc --> |jaeger| backend |
| 128 | + otc --> |zipkin| zipkin |
| 129 | + otc --> |otlp-gRPC| tempo --> grafana |
| 130 | + sa --> |otlp-gRPC| otc |
| 131 | + sb --> |otlp-gRPC| otc |
| 132 | + start --> ng --> sa |
| 133 | + end |
| 134 | +``` |
| 135 | + |
| 136 | +To install the example and collectors run: |
| 137 | + |
| 138 | +1. Enable Ingress addon with: |
| 139 | + |
| 140 | + ```yaml |
| 141 | + opentelemetry: |
| 142 | + enabled: true |
| 143 | + image: registry.k8s.io/ingress-nginx/opentelemetry:v20230107-helm-chart-4.4.2-2-g96b3d2165@sha256:331b9bebd6acfcd2d3048abbdd86555f5be76b7e3d0b5af4300b04235c6056c9 |
| 144 | + containerSecurityContext: |
| 145 | + allowPrivilegeEscalation: false |
| 146 | + ``` |
| 147 | + |
| 148 | +2. Enable OpenTelemetry and set the otlp-collector-host: |
| 149 | + |
| 150 | + ```yaml |
| 151 | + $ echo ' |
| 152 | + apiVersion: v1 |
| 153 | + kind: ConfigMap |
| 154 | + data: |
| 155 | + enable-opentelemetry: "true" |
| 156 | + opentelemetry-config: "/etc/nginx/opentelemetry.toml" |
| 157 | + opentelemetry-operation-name: "HTTP $request_method $service_name $uri" |
| 158 | + opentelemetry-trust-incoming-span: "true" |
| 159 | + otlp-collector-host: "otel-coll-collector.otel.svc" |
| 160 | + otlp-collector-port: "4317" |
| 161 | + otel-max-queuesize: "2048" |
| 162 | + otel-schedule-delay-millis: "5000" |
| 163 | + otel-max-export-batch-size: "512" |
| 164 | + otel-service-name: "nginx-proxy" # Opentelemetry resource name |
| 165 | + otel-sampler: "AlwaysOn" # Also: AlwaysOff, TraceIdRatioBased |
| 166 | + otel-sampler-ratio: "1.0" |
| 167 | + otel-sampler-parent-based: "false" |
| 168 | + metadata: |
| 169 | + name: ingress-nginx-controller |
| 170 | + namespace: ingress-nginx |
| 171 | + ' | kubectl replace -f - |
| 172 | + ``` |
| 173 | + |
| 174 | +4. Deploy otel-collector, grafana and Jaeger backend: |
| 175 | + |
| 176 | + ```bash |
| 177 | + # add helm charts needed for grafana and OpenTelemetry collector |
| 178 | + helm repo add open-telemetry https://open-telemetry.github.io/opentelemetry-helm-charts |
| 179 | + helm repo add grafana https://grafana.github.io/helm-charts |
| 180 | + helm repo update |
| 181 | + # deply cert-manager needed for OpenTelemetry collector operator |
| 182 | + kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.9.1/cert-manager.yaml |
| 183 | + # create observability namespace |
| 184 | + kubectl apply -f https://raw.githubusercontent.com/esigo/nginx-example/main/observability/namespace.yaml |
| 185 | + # install OpenTelemetry collector operator |
| 186 | + helm upgrade --install otel-collector-operator -n otel --create-namespace open-telemetry/opentelemetry-operator |
| 187 | + # deploy OpenTelemetry collector |
| 188 | + kubectl apply -f https://raw.githubusercontent.com/esigo/nginx-example/main/observability/collector.yaml |
| 189 | + # deploy Jaeger all-in-one |
| 190 | + kubectl apply -f https://github.com/jaegertracing/jaeger-operator/releases/download/v1.37.0/jaeger-operator.yaml -n observability |
| 191 | + kubectl apply -f https://raw.githubusercontent.com/esigo/nginx-example/main/observability/jaeger.yaml -n observability |
| 192 | + # deploy zipkin |
| 193 | + kubectl apply -f https://raw.githubusercontent.com/esigo/nginx-example/main/observability/zipkin.yaml -n observability |
| 194 | + # deploy tempo and grafana |
| 195 | + helm upgrade --install tempo grafana/tempo --create-namespace -n observability |
| 196 | + helm upgrade -f https://raw.githubusercontent.com/esigo/nginx-example/main/observability/grafana/grafana-values.yaml --install grafana grafana/grafana --create-namespace -n observability |
| 197 | + ``` |
| 198 | + |
| 199 | +3. Build and deploy demo app: |
| 200 | + |
| 201 | + ```bash |
| 202 | + # build images |
| 203 | + make images |
| 204 | +
|
| 205 | + # deploy demo app: |
| 206 | + make deploy-app |
| 207 | + ``` |
| 208 | + |
| 209 | +5. Make a few requests to the Service: |
| 210 | + |
| 211 | + ```bash |
| 212 | + kubectl port-forward --namespace=ingress-nginx service/ingress-nginx-controller 8090:80 |
| 213 | + curl http://esigo.dev:8090/hello/nginx |
| 214 | +
|
| 215 | +
|
| 216 | + StatusCode : 200 |
| 217 | + StatusDescription : OK |
| 218 | + Content : {"v":"hello nginx!"} |
| 219 | +
|
| 220 | + RawContent : HTTP/1.1 200 OK |
| 221 | + Connection: keep-alive |
| 222 | + Content-Length: 21 |
| 223 | + Content-Type: text/plain; charset=utf-8 |
| 224 | + Date: Mon, 10 Oct 2022 17:43:33 GMT |
| 225 | +
|
| 226 | + {"v":"hello nginx!"} |
| 227 | +
|
| 228 | + Forms : {} |
| 229 | + Headers : {[Connection, keep-alive], [Content-Length, 21], [Content-Type, text/plain; charset=utf-8], [Date, |
| 230 | + Mon, 10 Oct 2022 17:43:33 GMT]} |
| 231 | + Images : {} |
| 232 | + InputFields : {} |
| 233 | + Links : {} |
| 234 | + ParsedHtml : System.__ComObject |
| 235 | + RawContentLength : 21 |
| 236 | + ``` |
| 237 | + |
| 238 | +6. View the Grafana UI: |
| 239 | + |
| 240 | + ```bash |
| 241 | + kubectl port-forward --namespace=observability service/grafana 3000:80 |
| 242 | + ``` |
| 243 | + In the Grafana interface we can see the details: |
| 244 | +  |
| 245 | + |
| 246 | +7. View the Jaeger UI: |
| 247 | + |
| 248 | + ```bash |
| 249 | + kubectl port-forward --namespace=observability service/jaeger-all-in-one-query 16686:16686 |
| 250 | + ``` |
| 251 | + In the Jaeger interface we can see the details: |
| 252 | +  |
| 253 | + |
| 254 | +8. View the Zipkin UI: |
| 255 | + |
| 256 | + ```bash |
| 257 | + kubectl port-forward --namespace=observability service/zipkin 9411:9411 |
| 258 | + ``` |
| 259 | + In the Zipkin interface we can see the details: |
| 260 | +  |
0 commit comments