diff --git a/Directory.Packages.props b/Directory.Packages.props index 66c25c0ec..8ce6af119 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -63,6 +63,7 @@ + diff --git a/docs/development.md b/docs/development.md index c476e80ea..12ff18467 100644 --- a/docs/development.md +++ b/docs/development.md @@ -105,3 +105,25 @@ writes the connection string to `src/Web/Grand.Web/App_Data/Settings.cfg`, which is not tracked by git - delete that file to run the installer again against a fresh database. +## Telemetry in Grafana + +`src/Aspire/Aspire.AppHost` sends the traces, metrics and logs of the +applications it starts to the Aspire dashboard. To see them in Grafana as well, +set `Observability:Grafana` to `true` in its `appsettings.json`, or start it with +the environment variable set: + +```bash +Observability__Grafana=true dotnet run --project src/Aspire/Aspire.AppHost +``` + +The AppHost then starts two more containers: an OpenTelemetry Collector, which +receives what the applications export and sends it on to both the dashboard and +Grafana, and `grafana/otel-lgtm` - Grafana with Tempo (traces), Prometheus +(metrics) and Loki (logs). The `grafana` resource in the dashboard links to the +Grafana UI. Its data survives a restart in the `grandnode-grafana` volume. + +The collector's pipeline is `src/Aspire/Aspire.AppHost/otel-collector.yaml`. +Outside Aspire, point an installation at your own collector with the `Telemetry` +section of `App_Data/appsettings.json` or the standard `OTEL_EXPORTER_OTLP_*` +environment variables. + diff --git a/src/Aspire/Aspire.AppHost/Aspire.AppHost.csproj b/src/Aspire/Aspire.AppHost/Aspire.AppHost.csproj index 01e909130..55a36775e 100644 --- a/src/Aspire/Aspire.AppHost/Aspire.AppHost.csproj +++ b/src/Aspire/Aspire.AppHost/Aspire.AppHost.csproj @@ -20,6 +20,7 @@ + diff --git a/src/Aspire/Aspire.AppHost/ObservabilityConfiguration.cs b/src/Aspire/Aspire.AppHost/ObservabilityConfiguration.cs new file mode 100644 index 000000000..a1c6d63a0 --- /dev/null +++ b/src/Aspire/Aspire.AppHost/ObservabilityConfiguration.cs @@ -0,0 +1,28 @@ +using Microsoft.Extensions.Configuration; + +namespace Aspire.AppHost; + +public static class ObservabilityConfiguration +{ + public static void AddGrafanaObservability(this IDistributedApplicationBuilder builder) + { + //opt-in: two more containers to pull and run, which a plain run of the store does not need + if (!builder.Configuration.GetValue("Observability:Grafana")) + return; + + //Tempo, Loki, Prometheus and Grafana in one container, receiving OTLP itself + var grafana = builder.AddContainer("grafana", "grafana/otel-lgtm", "0.40.0") + .WithLifetime(ContainerLifetime.Persistent) + .WithVolume("grandnode-grafana", "/data") + .WithHttpEndpoint(targetPort: 3000) + .WithEndpoint(targetPort: 4317, name: "otlp", scheme: "http"); + + //an app exports to one OTLP endpoint only, which Aspire points at its dashboard - the collector takes + //that place and sends everything on to both the dashboard and Grafana + builder.AddOpenTelemetryCollector("otel-collector", settings => settings.CollectorTag = "0.165.0") + .WithConfig(Path.Combine(builder.AppHostDirectory, "otel-collector.yaml")) + .WithEnvironment("GRAFANA_OTLP_ENDPOINT", grafana.GetEndpoint("otlp")) + .WaitFor(grafana) + .WithAppForwarding(); + } +} diff --git a/src/Aspire/Aspire.AppHost/Program.cs b/src/Aspire/Aspire.AppHost/Program.cs index efb79fa91..6ef4b2abc 100644 --- a/src/Aspire/Aspire.AppHost/Program.cs +++ b/src/Aspire/Aspire.AppHost/Program.cs @@ -7,6 +7,7 @@ var redis = builder.AddRedis("redis").WithLifetime(ContainerLifetime.Persistent); +builder.AddGrafanaObservability(); builder.ConfigureGrandWebProject(mongodb, redis); await builder.Build().RunAsync(); diff --git a/src/Aspire/Aspire.AppHost/appsettings.json b/src/Aspire/Aspire.AppHost/appsettings.json index 31c092aa4..17e836b66 100644 --- a/src/Aspire/Aspire.AppHost/appsettings.json +++ b/src/Aspire/Aspire.AppHost/appsettings.json @@ -5,5 +5,10 @@ "Microsoft.AspNetCore": "Warning", "Aspire.Hosting.Dcp": "Warning" } + }, + "Observability": { + //true starts an OpenTelemetry Collector and Grafana (grafana/otel-lgtm) next to the apps; their traces, + //metrics and logs then reach Grafana as well as the Aspire dashboard + "Grafana": false } } diff --git a/src/Aspire/Aspire.AppHost/otel-collector.yaml b/src/Aspire/Aspire.AppHost/otel-collector.yaml new file mode 100644 index 000000000..5eeb2f723 --- /dev/null +++ b/src/Aspire/Aspire.AppHost/otel-collector.yaml @@ -0,0 +1,38 @@ +#OpenTelemetry Collector started by the AppHost when Observability:Grafana is true. It sends everything the apps +#export to both the Aspire dashboard and Grafana. ASPIRE_ENDPOINT, ASPIRE_API_KEY and the receivers' TLS come from +#the Aspire integration, GRAFANA_OTLP_ENDPOINT from ObservabilityConfiguration. +receivers: + otlp: + protocols: + grpc: + endpoint: 0.0.0.0:4317 + http: + endpoint: 0.0.0.0:4318 + +processors: + batch: + +exporters: + otlp_grpc/aspire: + endpoint: ${env:ASPIRE_ENDPOINT} + headers: + x-otlp-api-key: ${env:ASPIRE_API_KEY} + otlp_grpc/grafana: + endpoint: ${env:GRAFANA_OTLP_ENDPOINT} + tls: + insecure: true + +service: + pipelines: + traces: + receivers: [otlp] + processors: [batch] + exporters: [otlp_grpc/aspire, otlp_grpc/grafana] + metrics: + receivers: [otlp] + processors: [batch] + exporters: [otlp_grpc/aspire, otlp_grpc/grafana] + logs: + receivers: [otlp] + processors: [batch] + exporters: [otlp_grpc/aspire, otlp_grpc/grafana]