|
| 1 | +--- |
| 2 | +title: "☁️ 🤔 Intermezzo – Insightful spans with OpenTelemetry" |
| 3 | +slug: cloud-agnostic-7 |
| 4 | +tags: [software-development, dotnet, web, azure] |
| 5 | +date: 2025-05-02 19:00:00 |
| 6 | +topic: "azure-ahead" |
| 7 | +--- |
| 8 | + |
| 9 | +<TopicToc |
| 10 | + title="Cloud agnostic series" |
| 11 | + topicId="azure-ahead" |
| 12 | + active={frontmatter.title} |
| 13 | + closed |
| 14 | + /> |
| 15 | + |
| 16 | +Let's take a short break from looking at service replacements and marvel at the fun that is having good [OpenTelemetry][otel] |
| 17 | +data. |
| 18 | + |
| 19 | +When we started ahead many years, OpenTelemetry didn't exist yet. Aspire leans into it quite nicely, |
| 20 | +providing a dashboard in which captured open telemetry data is seen. |
| 21 | + |
| 22 | +When you start a project from scratch, you may miss exposing open telemetry from your project. This is done through |
| 23 | +some adequate service registrations, which in the solution I have galvanized into |
| 24 | +a `IHostApplicationBuilder` extension method to [be found here][1]. |
| 25 | + |
| 26 | +Much of what is encoded there comes directly from the [official documentation][2]. |
| 27 | + |
| 28 | +The next challenge was seeing traces that span Frontend and Backend activities |
| 29 | +when it is correct to do so. |
| 30 | + |
| 31 | +I wasn't quite sure [what is going on][3] with OTel support around RabbitMQ, so I decided to do the |
| 32 | +necessary boilerplate myself. |
| 33 | + |
| 34 | +What I do on the sending side e.g. when enqueueing a message is to create a new `Activity`: |
| 35 | + |
| 36 | +<GHEmbed showHint repo="ahead-dockerized" branch="snapshot_1" file="Ahead.Web/Infrastructure/QueueSender.cs" start={12} end={13} /> |
| 37 | + |
| 38 | +Let us revisit the sending of the message to RabbitMQ. We will send telemetry information alongside the message in the **headers** of the message: |
| 39 | + |
| 40 | +<GHEmbed repo="ahead-dockerized" branch="snapshot_1" file="Ahead.Web/Infrastructure/QueueSender.cs" start={25} end={35} /> |
| 41 | + |
| 42 | +What happens in `TryInjectTraceContextIntoDictionary`? Let's take a look: |
| 43 | + |
| 44 | +<GHEmbed repo="ahead-dockerized" branch="snapshot_1" file="Ahead.Common/OTelUtilities.cs" start={13} end={20} /> |
| 45 | + |
| 46 | +The `TraceContextPropagator` is a helper class from the **OpenTelemetry** package(s). It helps to inject the proper |
| 47 | +information into the headers of the message which we will use on the _"other side"_ to attach ourselves to the correct trace context: |
| 48 | + |
| 49 | +<GHEmbed repo="ahead-dockerized" branch="snapshot_1" file="Ahead.Backend/Infrastructure/QueueListener.cs" start={29} end={36} /> |
| 50 | + |
| 51 | +This allows us to get traces that span our projects and provide us additional insights into what is going on: |
| 52 | + |
| 53 | + |
| 54 | +<figcaption>Aspire OpenTelemetry trace of a queueing operation</figcaption> |
| 55 | + |
| 56 | +Essentially the same was done for the Broadcasting part, which allows us to see who consumed a particular broadcast message: |
| 57 | + |
| 58 | + |
| 59 | +<figcaption>Aspire OpenTelemetry trace of a publish operation</figcaption> |
| 60 | + |
| 61 | +When having a solution spanning multiple processes these insights are extremely valuable in understanding the internal workings of |
| 62 | +the machine you're creating – a machine that will inevitably increase in complexity as your product grows to be successful with your customers. |
| 63 | + |
| 64 | +[1]: https://github.com/flq/ahead-dockerized/blob/main/Ahead.Common/OTelConfiguration.cs |
| 65 | +[2]: https://learn.microsoft.com/en-us/dotnet/core/diagnostics/observability-otlp-example |
| 66 | +[3]: https://github.com/dotnet/aspire/issues/583 |
| 67 | +[otel]: https://opentelemetry.io |
0 commit comments