|
| 1 | +# Using Exemplars in OpenTelemetry .NET |
| 2 | + |
| 3 | +Exemplars are example data points for aggregated data. They provide specific |
| 4 | +context to otherwise general aggregations. One common use case is to gain |
| 5 | +ability to correlate metrics to traces (and logs). While OpenTelemetry .NET |
| 6 | +supports Exemplars, it is only useful if the telemetry backend also supports the |
| 7 | +capabilities. This tutorial uses well known open source backends to demonstrate |
| 8 | +the concept. The following are the components involved: |
| 9 | + |
| 10 | +* Test App - We use existing example app from the repo. This app is already |
| 11 | +instrumented with OpenTelemetry for logs, metrics and traces, and is configured |
| 12 | +to export them to the configured OTLP end point. |
| 13 | +* OpenTelemetry Collector - An instance of collector is run, which receives |
| 14 | +telemetry from the above app using OTLP. The collector then exports metrics to |
| 15 | +Prometheus, traces to Tempo. |
| 16 | +* Prometheus - Prometheus is used as the Metric backend. |
| 17 | +* Tempo - Tempo is used as the Tracing backend. |
| 18 | +* Grafana - UI to query metrics from Prometheus, traces from Tempo, and to |
| 19 | + navigate between metrics and traces using Exemplar. |
| 20 | + |
| 21 | +All these components except the test app require additional configuration to |
| 22 | +enable Exemplar feature. To make it easy for users, these components are |
| 23 | +pre-configured to enable Exemplars, and a docker-compose is provided to spun |
| 24 | + them all up, in the required configurations. |
| 25 | + |
| 26 | +## Pre-requisite |
| 27 | + |
| 28 | +Install docker: <https://docs.docker.com/get-docker/> |
| 29 | + |
| 30 | +## Setup |
| 31 | + |
| 32 | +As mentioned in the intro, this tutorial uses OTel Collector, Prometheus, Tempo, |
| 33 | +and Grafana, and they must be up and running before proceeding. The following |
| 34 | +spins all of them with the correct configurations to support Exemplars. |
| 35 | + |
| 36 | +Navigate to current directory and run the following: |
| 37 | + |
| 38 | +```sh |
| 39 | +docker-compose up -d |
| 40 | +``` |
| 41 | + |
| 42 | +If the above step succeeds, all dependencies would be spun up and ready now. To |
| 43 | +test, navigate to Grafana running at: "http://localhost:3000/". |
| 44 | + |
| 45 | +## Run test app |
| 46 | + |
| 47 | +Now that the required dependencies are ready, lets run the demo app. |
| 48 | +This tutorial is using the existing ASP.NET Core app from the repo. |
| 49 | + |
| 50 | +Navigate to [Example Asp.Net Core App](../../../examples/AspNetCore/Program.cs) |
| 51 | +directory and run the following command: |
| 52 | + |
| 53 | +```sh |
| 54 | +dotnet run |
| 55 | +``` |
| 56 | + |
| 57 | +Once the application is running, navigate to |
| 58 | +[http://localhost:5000/weatherforecast]("http://localhost:5000/weatherforecast") |
| 59 | +from a web browser. You may use the following Powershell script to generate load |
| 60 | +to the application. |
| 61 | + |
| 62 | +```powershell |
| 63 | +while($true) |
| 64 | +{ |
| 65 | + Invoke-WebRequest http://localhost:5000/weatherforecast |
| 66 | + Start-Sleep -Milliseconds 500 |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## Use Exemplars to navigate from Metrics to Traces |
| 71 | + |
| 72 | +The application sends metrics (with exemplars), and traces to the OTel |
| 73 | +Collector, which export metrics and traces to Prometheus and Tempo |
| 74 | +respectively. |
| 75 | + |
| 76 | +Please wait for 2 minutes before continuing so that enough data is generated |
| 77 | +and exported. |
| 78 | + |
| 79 | +Open Grafana, select Explore, and select Prometheus as the source. Select the |
| 80 | +metric named "http_server_duration_bucket", and plot the chart. Toggle on the |
| 81 | +"Exemplar" option from the UI and hit refresh. |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +The Exemplars appear as special "diamond shaped dots" along with the metric |
| 86 | +charts in the UI. Select any Exemplar to see the exemplar data, which includes |
| 87 | +the timestamp when the measurement was recorded, the raw value, and trace |
| 88 | +context when the recording was done. The "trace_id" enables jumping to the |
| 89 | +tracing backed (tempo). Click on the "Query with Tempo" button next to the |
| 90 | +"trace_id" field to open the corresponding `Trace` in Tempo. |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +## References |
| 95 | + |
| 96 | +* [Exemplar specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#exemplar) |
| 97 | +* [Exemplars in Prometheus](https://prometheus.io/docs/prometheus/latest/feature_flags/#exemplars-storage) |
| 98 | +* [Exemplars in Grafana](https://grafana.com/docs/grafana/latest/fundamentals/exemplars/) |
| 99 | +* [Tempo](https://github.com/grafana/tempo) |
0 commit comments