|
| 1 | +--- |
| 2 | +title: Enable browser telemetry |
| 3 | +description: Learn how to enable browser telemetry in the .NET Aspire dashboard. |
| 4 | +ms.date: 10/11/2024 |
| 5 | +--- |
| 6 | + |
| 7 | +# Enable browser telemetry |
| 8 | + |
| 9 | +The .NET Aspire dashboard can be configure to receive telemetry sent from browser apps. This feature is useful for monitoring client-side performance and user interactions. Browser telemetry requires additional configuration to setup. This article discusses how to enable browser telemetry in the .NET Aspire dashboard. |
| 10 | + |
| 11 | +In addition to [enabling CORS](configuration.md#otlp-cors), you also need the [JavaScript OTEL SDK](https://opentelemetry.io/docs/languages/js/getting-started/browser/) in your browser app. |
| 12 | + |
| 13 | +## OTLP configuration |
| 14 | + |
| 15 | +The .NET Aspire dashboard receives telemetry through OTLP endpoints. [HTTP OTLP endpoints](https://opentelemetry.io/docs/specs/otlp/#otlphttp) and gRPC OTLP endpoints are supported by the dashboard. Browser apps must use HTTP OLTP to send telemetry to the dashboard because browser apps don't support gRPC. |
| 16 | + |
| 17 | +To configure the gPRC or HTTP endpoints, specify the following environment variables: |
| 18 | + |
| 19 | +- `DOTNET_DASHBOARD_OTLP_ENDPOINT_URL`: The gRPC endpoint to which the dashboard connects for its data. |
| 20 | +- `DOTNET_DASHBOARD_OTLP_HTTP_ENDPOINT_URL`: The HTTP endpoint to which the dashboard connects for its data. |
| 21 | + |
| 22 | +Configuration of the HTTP OTLP endpoint depends on whether the dashboard is started by the app host or is run standalone. |
| 23 | + |
| 24 | +### Configure OTLP HTTP with app host |
| 25 | + |
| 26 | +If the dashboard and your app are started by the app host, the dashboard OTLP endpoints are configured in the app host's _launchSettings.json_ file. |
| 27 | + |
| 28 | +Consider the following example JSON file: |
| 29 | + |
| 30 | +:::code language="json" source="snippets/BrowserTelemetry/BrowserTelemetry.AppHost/Properties/launchSettings.json" highlight="12,25,40"::: |
| 31 | + |
| 32 | +The preceding launch settings JSON file configures all profiles to include the `DOTNET_DASHBOARD_OTLP_HTTP_ENDPOINT_URL` environment variable. |
| 33 | + |
| 34 | +### Configure OTLP HTTP with standalone dashboard |
| 35 | + |
| 36 | +If the dashboard is used standalone, without the rest of .NET Aspire, the OTLP HTTP endpoint is enabled by default on port `18890`. However, the port must be mapped when the dashboard container is started: |
| 37 | + |
| 38 | +### [Bash](#tab/bash) |
| 39 | + |
| 40 | +```bash |
| 41 | +docker run --rm -it \ |
| 42 | + -p 18888:18888 \ |
| 43 | + -p 4317:18889 \ |
| 44 | + -p 4318:18890 -d \ |
| 45 | + --name aspire-dashboard \ |
| 46 | + mcr.microsoft.com/dotnet/aspire-dashboard:8.1.0 |
| 47 | +``` |
| 48 | + |
| 49 | +### [PowerShell](#tab/powershell) |
| 50 | + |
| 51 | +```powershell |
| 52 | +docker run --rm -it ` |
| 53 | + -p 18888:18888 ` |
| 54 | + -p 4317:18889 ` |
| 55 | + -p 4318:18890 -d ` |
| 56 | + --name aspire-dashboard ` |
| 57 | + mcr.microsoft.com/dotnet/aspire-dashboard:8.1.0 |
| 58 | +``` |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +The preceding command runs the dashboard container and maps gRPC OTLP to port `4317` and HTTP OTLP to port `4318`. |
| 63 | + |
| 64 | +## CORS configuration |
| 65 | + |
| 66 | +By default, browser apps are restricted from making cross domain API calls. This impacts sending telemetry to the dashboard because the dashboard and the browser app are always on different domains. Configuring CORS in the .NET Aspire dashboard removes the restriction. |
| 67 | + |
| 68 | +If the dashboard and your app are started by the app host, no CORS configuration is required. .NET Aspire automatically configures the dashboard to allow all resource origins. |
| 69 | + |
| 70 | +If the dashboard is used standlone then CORS must be configured manually. The domain used to view the browser app must be configured as an allowed origin by specifing the `DASHBOARD__OTLP__CORS__ALLOWEDORIGINS` environment variable when the dashboard container is started: |
| 71 | + |
| 72 | +### [Bash](#tab/bash) |
| 73 | + |
| 74 | +```bash |
| 75 | +docker run --rm -it \ |
| 76 | + -p 18888:18888 \ |
| 77 | + -p 4317:18889 \ |
| 78 | + -p 4318:18890 -d \ |
| 79 | + -e DASHBOARD__OTLP__CORS__ALLOWEDORIGINS=https://localhost:8080 \ |
| 80 | + --name aspire-dashboard \ |
| 81 | + mcr.microsoft.com/dotnet/aspire-dashboard:8.1.0 |
| 82 | +``` |
| 83 | + |
| 84 | +### [PowerShell](#tab/powershell) |
| 85 | + |
| 86 | +```powershell |
| 87 | +docker run --rm -it ` |
| 88 | + -p 18888:18888 ` |
| 89 | + -p 4317:18889 ` |
| 90 | + -p 4318:18890 -d ` |
| 91 | + -e DASHBOARD__OTLP__CORS__ALLOWEDORIGINS=https://localhost:8080 ` |
| 92 | + --name aspire-dashboard ` |
| 93 | + mcr.microsoft.com/dotnet/aspire-dashboard:8.1.0 |
| 94 | +``` |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +The preceding command runs the dashboard container and configures `https://localhost:8080` as an allowed origin. That means a browser app that is accessed using `https://localhost:8080` has permission to send the dashboard telemetry. |
| 99 | + |
| 100 | +Multiple origins can be allowed with a comma separated value. Or all origins can be allowed with the `*` wildcard. For example, `DASHBOARD__OTLP__CORS__ALLOWEDORIGINS=*`. |
| 101 | + |
| 102 | +For more information, see [.NET Aspire dashboard configuration: OTLP CORS](configuration.md#otlp-cors). |
| 103 | + |
| 104 | +## OTLP endpoint security |
| 105 | + |
| 106 | +Dashboard OTLP endpoints can be secured with API key authentication. When enabled, HTTP OTLP requests to the dashboard must include the API key as the `x-otlp-api-key` header. By default a new API key is generated each time the dashboard is run. |
| 107 | + |
| 108 | +API key authentication is automatically enabled when the dashboard is run from the app host. Dashboard authentication can be disabled by setting `DOTNET_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS` to `true` in the app host's _launchSettings.json_ file. |
| 109 | + |
| 110 | +OTLP endpoints are unsecured by default in the standalone dashboard. |
| 111 | + |
| 112 | +## Browser app configuration |
| 113 | + |
| 114 | +A browser app uses the [JavaScript OTEL SDK](https://opentelemetry.io/docs/languages/js/getting-started/browser/) to send telemetry to the dashboard. Successfully sending telemetry to the dashboard requires the SDK to be correctly configured. |
| 115 | + |
| 116 | +### OTLP exporter |
| 117 | + |
| 118 | +OTLP exporters must be included in the browser app and configured with the SDK. For example, exporting distributed tracing with OTLP uses the [@opentelemetry/exporter-trace-otlp-proto](https://www.npmjs.com/package/@opentelemetry/exporter-trace-otlp-proto) package. |
| 119 | + |
| 120 | +When OTLP is added to the SDK, OTLP options must be specified. OTLP options includes: |
| 121 | + |
| 122 | +- `url`: The address that HTTP OTLP requests are made to. The address should be the dashboard HTTP OTLP endpoint and the path to the OTLP HTTP API. For example, `https://localhost:4318/v1/traces` for the trace OTLP exporter. If the browser app is launched by the app host then the HTTP OTLP endpoint is available from the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable. |
| 123 | + |
| 124 | +- `headers`: The headers sent with requests. If OTLP endpoint API key authentication is enabled the `x-otlp-api-key` header must be sent with OTLP requests. If the browser app is launched by the app host then the API key is available from the `OTEL_EXPORTER_OTLP_HEADERS` environment variable. |
| 125 | + |
| 126 | +### Browser metadata |
| 127 | + |
| 128 | +When a browser app is configured to collect distributed traces, the browser app can set the trace parent a browser's spans using the `meta` element in the HTML. The value of the `name="traceparent"` meta element should correspond to the current trace. |
| 129 | + |
| 130 | +In a .NET app, for example, the trace parent value would likely be assigned from the <xref:System.Diagnostics.Activity.Current?displayProperty=nameWithType> and passing its <xref:System.Diagnostics.Activity.Id?displayProperty=nameWithType> value as the `content`. For example, consider the following Razor code: |
| 131 | + |
| 132 | +```razor |
| 133 | +<head> |
| 134 | + @if (Activity.Current is { } currentActivity) |
| 135 | + { |
| 136 | + <meta name="traceparent" content="@currentActivity.Id" /> |
| 137 | + } |
| 138 | + <!-- Other elements omitted for brevity... --> |
| 139 | +</head> |
| 140 | +``` |
| 141 | + |
| 142 | +The preceding code sets the `traceparent` meta element to the current activity ID. |
| 143 | + |
| 144 | +## Example browser telemetry code |
| 145 | + |
| 146 | +The following JavaScript code demonstrates the initialization of the OpenTelemetry JavaScript SDK and the sending of telemetry data to the dashboard: |
| 147 | + |
| 148 | +:::code language="javascript" source="snippets/BrowserTelemetry/BrowserTelemetry.Web/Scripts/index.js"::: |
| 149 | + |
| 150 | +The preceding JavaScript code defines an `initializeTelemetry` function that expects the OTLP endpoint URL, the headers, and the resource attributes. These parameters are provided by the consuming browser app that pulls them from the environment variables set by the app host. Consider the following Razor code: |
| 151 | + |
| 152 | +:::code language="razor" source="snippets/BrowserTelemetry/BrowserTelemetry.Web/Pages/Shared/_Layout.cshtml" highlight="31-38"::: |
| 153 | + |
| 154 | +> [!TIP] |
| 155 | +> The bundling and minification of the JavaScript code is beyond the scope of this article. |
| 156 | +
|
| 157 | +For the complete working example of how to configure the JavaScript OTEL SDK to send telemetry to the dashboard, see the [browser telemetry sample](https://github.com/dotnet/aspire/tree/main/playground/BrowserTelemetry). |
| 158 | + |
| 159 | +## See also |
| 160 | + |
| 161 | +- [.NET Aspire dashboard configuration](configuration.md) |
| 162 | +- [Standalone .NET Aspire dashboard](standalone.md) |
| 163 | +- [Browser telemetry sample](https://github.com/dotnet/aspire/tree/main/playground/BrowserTelemetry) |
0 commit comments