From ff1ff4360b104c291c907c62e114513d41f775e0 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Mon, 4 Mar 2024 07:31:54 +0800 Subject: [PATCH] Add configuration of OTLP export endpoint to standalone dashboard sample and improve readme (#136) --- .../StandaloneDashboard/ConsoleApp/Program.cs | 11 ++++++++--- .../ConsoleApp/Properties/launchSettings.json | 10 ++++++++++ .../ConsoleApp/appsettings.Development.json | 7 +++++++ .../ConsoleApp/appsettings.json | 8 ++++++++ samples/StandaloneDashboard/README.md | 17 ++++++++++++----- 5 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 samples/StandaloneDashboard/ConsoleApp/Properties/launchSettings.json create mode 100644 samples/StandaloneDashboard/ConsoleApp/appsettings.Development.json create mode 100644 samples/StandaloneDashboard/ConsoleApp/appsettings.json diff --git a/samples/StandaloneDashboard/ConsoleApp/Program.cs b/samples/StandaloneDashboard/ConsoleApp/Program.cs index 794f2df2..67a5bf88 100644 --- a/samples/StandaloneDashboard/ConsoleApp/Program.cs +++ b/samples/StandaloneDashboard/ConsoleApp/Program.cs @@ -40,9 +40,14 @@ static IHostApplicationBuilder ConfigureOpenTelemetry(IHostApplicationBuilder bu tracing.AddSource(PokemonDownloader.ActivitySourceName); }); - builder.Services.Configure(logging => logging.AddOtlpExporter()); - builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter()); - builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter()); + // Use the OTLP exporter if the endpoint is configured. + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + if (useOtlpExporter) + { + builder.Services.Configure(logging => logging.AddOtlpExporter()); + builder.Services.ConfigureOpenTelemetryMeterProvider(metrics => metrics.AddOtlpExporter()); + builder.Services.ConfigureOpenTelemetryTracerProvider(tracing => tracing.AddOtlpExporter()); + } return builder; } diff --git a/samples/StandaloneDashboard/ConsoleApp/Properties/launchSettings.json b/samples/StandaloneDashboard/ConsoleApp/Properties/launchSettings.json new file mode 100644 index 00000000..af72d728 --- /dev/null +++ b/samples/StandaloneDashboard/ConsoleApp/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "ConsoleApp": { + "commandName": "Project", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/samples/StandaloneDashboard/ConsoleApp/appsettings.Development.json b/samples/StandaloneDashboard/ConsoleApp/appsettings.Development.json new file mode 100644 index 00000000..dc23838b --- /dev/null +++ b/samples/StandaloneDashboard/ConsoleApp/appsettings.Development.json @@ -0,0 +1,7 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug" + } + } +} diff --git a/samples/StandaloneDashboard/ConsoleApp/appsettings.json b/samples/StandaloneDashboard/ConsoleApp/appsettings.json new file mode 100644 index 00000000..4909257a --- /dev/null +++ b/samples/StandaloneDashboard/ConsoleApp/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information" + } + }, + "OTEL_EXPORTER_OTLP_ENDPOINT": "http://localhost:4317" +} diff --git a/samples/StandaloneDashboard/README.md b/samples/StandaloneDashboard/README.md index 254483d7..88eb9335 100644 --- a/samples/StandaloneDashboard/README.md +++ b/samples/StandaloneDashboard/README.md @@ -12,12 +12,15 @@ description: "A sample of using the Aspire dashboard to view telemetry from non- # Standalone Aspire dashboard sample app -The Aspire dashboard can be used to view OpenTelemetry data from any app. The dashboard supports running standalone, and any app can send it open telemetry. +View telemetry from any app in the Aspire dashboard. The dashboard supports running standalone, and apps configured with an [OpenTelemetry SDK](https://opentelemetry.io/docs/getting-started/dev/) can send it data. -This is a simple .NET app that downloads data from [PokeAPI](https://pokeapi.co/). The app sends telemetry to the Aspire dashboard which can be viewed in the dashboard telemetry UI. +This sample is a .NET console app that downloads data from [PokeAPI](https://pokeapi.co/). The app sends telemetry to the Aspire dashboard which is viewed in the dashboard telemetry UI. ![Screenshot of the standalone .NET Aspire dashboard](./images/aspire-dashboard-screenshot.png) +> [!NOTE] +> [PokeAPI](https://pokeapi.co/) is a free, open-source RESTful API that is not owned by Microsoft. + ## Demonstrates - How to run the Aspire dashboard from a Docker container @@ -66,6 +69,10 @@ dotnet run --project ConsoleApp 1. The console app launches, downloads information about all Pokemon and then exits. 2. View the Aspire dashboard at http://localhost:18888 to see app telemetry. - 1. View structured logs to see the list of downloaded Pokemon. - 2. View traces to see HTTP requests made. - 3. View metrics to see numeric data about the app such as average HTTP request duration. + 1. View structured logs to see the list of downloaded Pokemon. + 2. View traces to see HTTP requests made. + 3. View metrics to see numeric data about the app such as average HTTP request duration. + +## Configure OpenTelemetry + +The telemetry export endpoint is configured with the `OTEL_EXPORTER_OTLP_ENDPOINT` setting. This value is set to `http://localhost:4317` in the sample's appsettings.json file. Removing the `OTEL_EXPORTER_OTLP_ENDPOINT` value disables exporting telemetry.