Sending (browser) OpenTelemetry traces from an Angular Application to .NET Aspire

profile
Tim Deschryver
timdeschryver.dev

This post is the glue between the previous post on OpenTelemetry in Angular and How to include an Angular project within .NET Aspire.

In the latter, I mentioned that this wasn't supported yet, but as of .NET 9 the .NET Aspire Dashboard has been enhanced to support OpenTelemetry data from the browser.

Note

Read more about What's new in .NET Aspire 9.0.

The OpenTelemetry JavaScript SDK has also gone through some changes - keep in mind that this feature is still experimental -, so an update is long overdue.

OpenTelemetry link

OpenTelemetry is an open-source observability framework for cloud-native software, designed to capture and export telemetry data such as traces, metrics, and logs. It provides a set of APIs, libraries, agents, and instrumentation to help developers monitor and troubleshoot their applications. By using OpenTelemetry, you can gain insights into the performance and behavior of your application, identify bottlenecks, and improve overall reliability. This is particularly useful in production environments, but it can also be helpful during development phase to better understand and debug your code.

For more information see What is OpenTelemetry?.

After this blog post, you will be able to follow the traces within the .NET Aspire Dashboard, starting from the Angular application to the .NET backend API (and external services if you have them, such as SQL server or other services).

An overview from all traces send to the dashboard.
The detail page of one trace, here you are able to you follow the whole trace.

Configuring Aspire link

Add an Angular Application to the AppHost link

Nothing much has changed to include a frontend project in Aspire compared to my previous post, How to include an Angular project within .NET Aspire To add an Angular application to the Aspire setup, you can use the AddNpmApp method. This method accepts the project name and the path to the project folder.

Because the application uses a backend API, we add a reference to the API project using the WithReference method. The WaitFor method ensures that the API project is started before the Angular application.

Enable Browser OpenTelemetry link

To send data from an Angular application to the Aspire Dashboard, the data needs to be send via the browser over HTTP requests. Compared to backend services, this is less secure. In previous versions of Aspire, this resulted in CORS issues, which meant that the Aspire Dashboard didn't support traces from the browser.

This problem has been solved in .NET 9.0.

To enable the Aspire Dashboard to receive traces from the browser, set the DOTNET_DASHBOARD_OTLP_HTTP_ENDPOINT_URL environment variable. This changes how the telemetry data is received by the Aspire Dashboard, instead of using gRPC it now uses Protobuf over HTTP.

This action also configures the CORS settings for the Aspire Dashboard. By default, it adds all registered origins to the CORS settings. In this case, the Angular Application and the .NET WeatherForecast API are registered as origins, https://localhost:7234,http://localhost:5187,http://localhost:4200

Add OpenTelemetry instrumentation to Angular link

Install the required packages link

To record and send telemetry data from the Angular application to the Aspire Dashboard, you need to install the following packages.

Automatically register and and traces link

Below is a revised version of the Angular instrumentation code from the previous post, OpenTelemetry in Angular. I encourage you to read that post for more details on what each part does. The main change is that this uses the trace exporter from @opentelemetry/exporter-trace-otlp-proto instead of @opentelemetry/exporter-trace-otlp-http. This change is required to send the traces in the Protobuf format.

To register the instrumentation, include it within the application configuration.

Update the Angular proxy server link

Lastly, update the Angular proxy server. In the proxy.conf.js file, add a new entry for the /v1/traces endpoint. This entry should point to the configured URL in the Aspire Dashboard, which is the previously set DOTNET_DASHBOARD_OTLP_HTTP_ENDPOINT_URL environment variable. You can hardcode the URL, but it's better to use an environment variable, this makes it less brittle and easier to change.

You can also see that headers are added to the request. This is required to authenticate the request to the Aspire Dashboard. The headers are set by the dashboard in the OTEL_EXPORTER_OTLP_HEADERS environment variable, which is a comma-separated list of key-value pairs. To view the settings, open the Aspire Dashboard and open in the Details slide-in, which gives the following value for the environment variable x-otlp-api-key=6b8a4ea8c5480f4959846230024508b8. The key is randomly generated each time the Aspire Dashboard is started.

This key protects the OLTP HTTP endpoint from unauthorized access. It's also possible to disable this security layer (or make the key a static value), but this is less secure and therefore not recommended.

Conclusion link

The .NET 9 release has brought a lot of new features and improvements to .NET Aspire. One of the features I was waiting for, was the support for OpenTelemetry for frontend (Angular) applications, and .NET 9 delivered.

In this post, I've shown you that it's built-in to send browser tracer data to the Aspire Dashboard. By changing the OpenTelemetry service to use the HTTP endpoint, you can now view traces from the browser in the Aspire Dashboard. This makes the overall develoepr experience better, as you can now see the full picture of your application, from the frontend to the backend.

Demo application link

You can find the example application on my GitHub.

Outgoing links

Feel free to update this blog post on GitHub, thanks in advance!

Join My Newsletter (WIP)

Join my weekly newsletter to receive my latest blog posts and bits, directly in your inbox.

Support me

I appreciate it if you would support me if have you enjoyed this post and found it useful, thank you in advance.

Buy Me a Coffee at ko-fi.com PayPal logo

Share this post