From f344425a3311fdcbf830b1aa09511624901196bc Mon Sep 17 00:00:00 2001 From: thorsten Date: Sat, 31 Oct 2026 19:49:48 +0100 Subject: [PATCH] docs: make README compact and ship a separate NuGet readme Collapse reference material (code config, databases, backend configs, FAQ, ...) into
blocks, drop the duplicated quick-start snippets and the manual table of contents, and slim the options table. nuget.org renders
as raw text, so the package now ships a short docs/README.nuget.md that links to the GitHub sections instead. --- AGENTS.md | 4 +- OpenTelemetryExtension.slnx | 4 + README.md | 620 +++++++----------- docs/README.nuget.md | 46 ++ ...penTelemetryExtension.Configuration.csproj | 2 +- 5 files changed, 299 insertions(+), 377 deletions(-) create mode 100644 docs/README.nuget.md diff --git a/AGENTS.md b/AGENTS.md index 59c43b2..0ca0570 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -22,6 +22,7 @@ infrastructure/helm/ # Helm charts + install scripts .github/workflows/ ci.yml # Build + unit tests (Category=Unit) + coverage → Coveralls deploy-nuget.yml # Manual NuGet publish + git tag + GitHub Release +docs/README.nuget.md # Short README packed into the NuGet package (nuget.org renders no
) release-notes/ # v{VERSION}.md per release OpenTelemetryExtension.slnx # Solution file (repo root) ``` @@ -240,6 +241,7 @@ API to confirm the data was ingested. 4. Add enabled/disabled unit tests in `TelemetryServiceCollectionExtensionsTests.cs` 5. Add the option to the `` block in the XML doc on `TelemetryOptions` -6. Update `README.md` configuration reference table +6. Update `README.md` configuration reference table (`docs/README.nuget.md` + only links to it) 7. Run the unit tests; when the telemetry stack is running, run the integration tests too (see [Integration tests](#integration-tests)) diff --git a/OpenTelemetryExtension.slnx b/OpenTelemetryExtension.slnx index 51109ed..45b492f 100644 --- a/OpenTelemetryExtension.slnx +++ b/OpenTelemetryExtension.slnx @@ -33,6 +33,10 @@ + + + + diff --git a/README.md b/README.md index 0dc26e7..40449db 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,12 @@ are exported over OTLP to Jaeger, Grafana, SigNoz, OpenObserve, the .NET Aspire Dashboard or any other OpenTelemetry backend — configured from `appsettings.json`, with sensible defaults and no boilerplate. +**[Quick Start](#-quick-start) · [Configuration](#-configuration) · [Backends](#-backends) · [Samples](#-samples) · [FAQ](#-faq)** + +--- + +## 🚀 Quick Start + ```bash dotnet add package OpenTelemetryExtension.Configuration ``` @@ -31,42 +37,61 @@ builder.Services.AddTelemetry(builder.Configuration); That's the whole setup. Incoming requests, outgoing `HttpClient` calls, .NET runtime metrics and every `ILogger` message now show up in your backend. +No backend yet? The .NET Aspire Dashboard is a single container — run it, start +your app and open : + +```bash +docker run --rm -it -p 18888:18888 -p 4317:18889 -p 4318:18890 -e DOTNET_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS=true mcr.microsoft.com/aspire/dashboard:latest +``` + ![Traces, metrics and logs from the sample app in the .NET Aspire Dashboard](https://raw.githubusercontent.com/thorstenalpers/OpenTelemetryExtension.Configuration/main/assets/Aspire-Dashboard.webp) ---- +
+Other ways to consume it — source copy, git submodule, fork + +
+ +- **Source copy or project reference** — the library is just two files: + [`TelemetryServiceCollectionExtensions.cs`](./src/OpenTelemetryExtension.Configuration/TelemetryServiceCollectionExtensions.cs) + and [`TelemetryOptions.cs`](./src/OpenTelemetryExtension.Configuration/TelemetryOptions.cs). + Copy them into your project, or reference + [the project](./src/OpenTelemetryExtension.Configuration) via ``. +- **Git submodule** — pin the source at a specific commit, then add a + `` to + `external/OpenTelemetryExtension.Configuration/src/OpenTelemetryExtension.Configuration/OpenTelemetryExtension.Configuration.csproj`: + ```bash + git submodule add https://github.com/thorstenalpers/OpenTelemetryExtension.Configuration.git external/OpenTelemetryExtension.Configuration + ``` +- **Fork** — adapt it, and open a pull request upstream if your change is generally useful. + +The API is identical in every case. -## Contents - -- [🤔 Why this package?](#-why-this-package) -- [✨ Features](#-features) -- [🔭 Works with your backend](#-works-with-your-backend) -- **Getting started** - - [✅ Requirements](#-requirements) - - [📦 Installation](#-installation) · [Other ways to consume it](#other-ways-to-consume-it) - - [🚀 Quick Start](#-quick-start) -- **Configuring telemetry** - - [⚙️ Configuration](#️-configuration) · [Options reference](#options-reference) · [Endpoint & protocol](#endpoint--protocol) · [Custom section name](#custom-section-name) - - [🧩 Code Configuration](#-code-configuration) · [Registering your own Sources & Meters](#registering-your-own-sources--meters) · [Databases](#databases) - - [🖥️ Using outside the Generic Host](#️-using-outside-the-generic-host) -- **Trying it out** - - [🧪 Samples](#-samples) - - [🔌 Running Locally with a Backend](#-running-locally-with-a-backend) · [Sample backend configurations](#sample-backend-configurations) -- **Project** - - [❓ FAQ](#-faq) - - [🤝 Contributing](#-contributing) - - [🐛 Report a Bug](#-report-a-bug) - - [📄 License](#-license) +
--- -## 🤔 Why this package? +## ✨ Features + +- 🚀 **One call, three signals** — tracing, metrics and logging via `AddTelemetry()`. +- ⚙️ **Configuration-driven** — every option bindable from `appsettings.json`, + environment variables or any `IConfiguration` source; override in code when needed. +- 🔋 **Batteries included** — ASP.NET Core, `HttpClient` and .NET runtime + instrumentation, each toggleable. Parent-based sampling, exceptions on spans + and `/health` excluded by default. +- 🌍 **Runs everywhere** — ASP.NET Core, worker, console, WPF, WinForms, MAUI and + .NET Framework via `netstandard2.0`. +- 🪶 **No lock-in** — plain OpenTelemetry underneath, two source files, any OTLP + backend over HTTP/protobuf or gRPC. + +
+What AddTelemetry() replaces -OpenTelemetry for .NET is powerful, but wiring it up by hand means five NuGet -packages to keep in sync and a block of setup code that gets copied from -service to service — and drifts. This is roughly what `AddTelemetry()` replaces: +
+ +Wiring OpenTelemetry up by hand means five NuGet packages to keep in sync and a +block of setup code that gets copied from service to service — and drifts: ```csharp -// Without this package var endpoint = new Uri(builder.Configuration["Otlp:Endpoint"]!); builder.Services.AddOpenTelemetry() @@ -94,213 +119,91 @@ builder.Logging.AddOpenTelemetry(l => }); ``` -```csharp -// With this package -builder.Services.AddTelemetry(builder.Configuration); -``` - -Everything in the first block is still there — it is just driven by -configuration. Switching from Jaeger to SigNoz, changing the sample rate, -adding an auth header or turning telemetry off in one environment is a config -change, not a code change. - ---- - -## ✨ Features +With this package all of it is driven by configuration: switching from Jaeger to +SigNoz, changing the sample rate, adding an auth header or turning telemetry off +in one environment is a config change, not a code change. -- 🚀 **One-call setup** — tracing, metrics and logging via a single `AddTelemetry()`. -- ⚙️ **Configuration-driven** — every option bindable from `appsettings.json`, - environment variables or any other `IConfiguration` source; override in code when you need to. -- 🔋 **Batteries included** — ASP.NET Core, `HttpClient` and .NET runtime - instrumentation out of the box, each individually toggleable. -- 🛡️ **Sensible defaults** — parent-based sampling, exceptions recorded on spans, - `/health` excluded from tracing, endpoint validated at startup. -- 🌍 **Runs everywhere** — ASP.NET Core, worker services, console, WPF, WinForms, - MAUI, and even .NET Framework via `netstandard2.0`. -- 🔌 **Any OTLP backend** — HTTP/protobuf or gRPC, with custom headers for auth. -- 🧩 **Extensible** — add your own `ActivitySource`s, `Meter`s and database - instrumentation from config or code. -- 🪶 **No lock-in** — it is plain OpenTelemetry underneath, the whole library is - two source files, and you can drop it at any time. +
--- -## 🔭 Works with your backend - -Anything that speaks [OTLP](https://opentelemetry.io/docs/specs/otlp/) works. -These backends ship with a ready-to-run start script and launch profile in this -repository — see [Running Locally with a Backend](#-running-locally-with-a-backend): +## ✅ Requirements -| Backend | Protocol used in the sample | -|---|---| -| [.NET Aspire Dashboard](https://aspire.dev/dashboard/standalone/) | gRPC + API key | -| [Jaeger](https://www.jaegertracing.io/) | gRPC | -| [OpenObserve](https://openobserve.ai/) | HTTP/protobuf or gRPC | -| [SigNoz](https://signoz.io/) | HTTP/protobuf | -| [Grafana](https://grafana.com/) / [Loki](https://grafana.com/oss/loki/) | HTTP/protobuf | -| [OpenSearch Dashboards](https://opensearch.org/) | HTTP/protobuf | +Any app type on **.NET 8+** gets the full build. **.NET Framework 4.6.1+** gets +the `netstandard2.0` build, which has everything except the ASP.NET Core +instrumentation. You also need an OTLP-compatible [backend](#-backends). -Using a hosted service (Grafana Cloud, Honeycomb, New Relic, Elastic, Datadog, …)? -Point `Endpoint` at its OTLP ingest URL and put the API key in `Headers`. +
+Which build does my app get? ---- - -## ✅ Requirements +
-**Every app type works** — ASP.NET Core, WPF, WinForms, console, worker services, -MAUI/WinUI, UWP. The package ships three targets and NuGet picks the best match -for your **target framework**; the app type plays no role: +NuGet picks the build from your **target framework**; the app type plays no role: -| Your target framework | Build you get | Bundles ASP.NET Core instrumentation | +| Your target framework | Build you get | ASP.NET Core instrumentation | |---|---|---| | `net10.0` — including `net10.0-windows` (WPF/WinForms) | `net10.0` | ✅ | | `net8.0`, `net9.0` — including their `-windows` variants | `net8.0` | ✅ | | .NET Framework 4.6.1+, .NET Standard 2.0 consumers | `netstandard2.0` | ❌ | -The last column is only about the optional -[ASP.NET Core instrumentation](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNetCore), -which traces *incoming HTTP requests* and is therefore meaningless outside a web -app. It is left out of the `netstandard2.0` build so .NET Framework clients don't -drag in an ASP.NET Core dependency they can't use. Everything else — OTLP export, -tracing, metrics, logging, `HttpClient` and runtime instrumentation — is -identical in all three builds. - -> A WPF app on `net10.0-windows` therefore gets the full `net10.0` build. Only -> WPF on **.NET Framework** falls back to `netstandard2.0`. - -You also need an **OTLP-compatible backend** to receive the telemetry — see -[Works with your backend](#-works-with-your-backend). - ---- - -## 📦 Installation - -```bash -dotnet add package OpenTelemetryExtension.Configuration -``` - -### Other ways to consume it - -The NuGet package is the recommended path, but it isn't the only one: - -- **As source / project reference or a plain copy** — the library is just two - files. Either clone - [`OpenTelemetryExtension.Configuration`](./src/OpenTelemetryExtension.Configuration) - into your repository and add a `` to it, or drop the two files - straight into your project and own them outright: - [`TelemetryServiceCollectionExtensions.cs`](./src/OpenTelemetryExtension.Configuration/TelemetryServiceCollectionExtensions.cs) - and - [`TelemetryOptions.cs`](./src/OpenTelemetryExtension.Configuration/TelemetryOptions.cs). -- **As a git submodule** — pin the source at a specific commit: - ```bash - git submodule add https://github.com/thorstenalpers/OpenTelemetryExtension.Configuration.git external/OpenTelemetryExtension.Configuration - ``` - then add a `` to - `external/OpenTelemetryExtension.Configuration/src/OpenTelemetryExtension.Configuration/OpenTelemetryExtension.Configuration.csproj`. -- **As a fork** — adapt it to your needs, and open a pull request back upstream - if your changes are generally useful. See [Contributing](#-contributing). - -In every case the API is identical — `AddTelemetry(...)` works the same whether -the type comes from a NuGet package or your own copy of the source. - ---- - -## 🚀 Quick Start - -### 1. Register - -```csharp -builder.Services.AddTelemetry(builder.Configuration); -``` - -### 2. Configure (`appsettings.json`) - -```json -{ - "Telemetry": { - "Endpoint": "http://localhost:4318", - "ServiceName": "my-api" - } -} -``` - -### 3. Start a backend - -No backend yet? The .NET Aspire Dashboard is a single container: - -```bash -docker run --rm -it -p 18888:18888 -p 4317:18889 -p 4318:18890 -e DOTNET_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS=true mcr.microsoft.com/aspire/dashboard:latest -``` +ASP.NET Core instrumentation traces *incoming HTTP requests*, so it is left out of +the `netstandard2.0` build to keep .NET Framework clients free of an ASP.NET Core +dependency. OTLP export, tracing, metrics, logging, `HttpClient` and runtime +instrumentation are identical in all three builds. -Run your app, then open — traces, metrics and logs -appear as soon as the app handles its first request. +
--- -## ⚙️ Configuration - -All keys live under the **`Telemetry`** section. Only `Endpoint` is required; everything -else has a default and telemetry is on out of the box. Bind from `appsettings.json` or set -the same properties in the `AddTelemetry(o => …)` callback. - -### Options reference - -| Key | Description | Type | Default | Example | -|---|---|---|---|---| -| `Endpoint` | [OTLP](https://opentelemetry.io/docs/languages/net/exporters/) endpoint for traces, metrics and logs. | `Uri` | — *(required)* | `https://otel.example.com:4317` | -| `Protocol` | [Transport protocol](https://opentelemetry.io/docs/languages/net/exporters/): `HttpProtobuf` (4318) or `Grpc` (4317). | `string` | `HttpProtobuf` | `Grpc` | -| `Headers` | [OTLP exporter headers](https://opentelemetry.io/docs/languages/net/exporters/), e.g. for auth. Format: `k1=v1,k2=v2`. | `string` | `""` | `x-otlp-api-key=secret` | -| `Enabled` | Master switch; `false` registers no OpenTelemetry at all. | `bool` | `true` | `false` | -| `ServiceName` | Logical [service name](https://opentelemetry.io/docs/specs/semconv/resource/) shown in the backend. | `string?` | `null` | `orders-api` | -| `ResourceAttributes` | Extra [resource attributes](https://opentelemetry.io/docs/specs/semconv/resource/). | `object` | `{}` | `{ "deployment.environment": "production" }` | -| `EnableTracing` | Distributed [tracing](https://opentelemetry.io/docs/concepts/signals/traces/). | `bool` | `true` | `false` | -| `EnableMetrics` | [Metrics](https://opentelemetry.io/docs/concepts/signals/metrics/) collection. | `bool` | `true` | `false` | -| `EnableLogging` | [Log](https://opentelemetry.io/docs/concepts/signals/logs/) export via OTLP. | `bool` | `true` | `false` | -| `SampleRatio` | Trace [sample](https://opentelemetry.io/docs/concepts/sampling/) fraction — `1.0` = all, `0.1` = 10% (`ParentBased(TraceIdRatioBased)`). | `double` | `1.0` | `0.1` | -| `EnableAspNetCoreInstrumentation` | Incoming [ASP.NET Core](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.AspNetCore) requests *(net8.0/net10.0 builds; no-op on netstandard2.0)*. | `bool` | `true` | `false` | -| `EnableHttpClientInstrumentation` | Outgoing [`HttpClient`](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Http) requests. | `bool` | `true` | `false` | -| `EnableRuntimeInstrumentation` | [.NET runtime metrics](https://www.nuget.org/packages/OpenTelemetry.Instrumentation.Runtime) (GC, memory, thread pool). | `bool` | `true` | `false` | -| `AdditionalTracingSources` | Extra [`ActivitySource`](https://learn.microsoft.com/dotnet/core/diagnostics/distributed-tracing-instrumentation-walkthroughs) names to collect. | `string[]` | `[]` | `[ "Npgsql", "MyApp" ]` | -| `AdditionalMeters` | Extra [`Meter`](https://learn.microsoft.com/dotnet/core/diagnostics/metrics-instrumentation) names to collect. | `string[]` | `[]` | `[ "MyApp.Orders" ]` | -| `RecordExceptions` | Record [exceptions](https://opentelemetry.io/docs/specs/semconv/exceptions/) with stack traces on spans. | `bool` | `true` | `false` | -| `ExcludedPaths` | Paths excluded from tracing (per-segment prefix match). | `string[]` | `[ "/health" ]` | `[ "/health", "/ready" ]` | -| `IncludeScopes` | Include [log scopes](https://learn.microsoft.com/dotnet/core/extensions/logging#log-scopes) in exported records. | `bool` | `true` | `false` | -| `IncludeFormattedMessage` | Include the formatted message in exported records. | `bool` | `true` | `false` | -| `ConfigureTracing` | **Code only.** Register extra [tracing](https://opentelemetry.io/docs/concepts/signals/traces/) — see [Code configuration](#-code-configuration). | `Action` | `null` | `t => t.AddSource("MyApp")` | -| `ConfigureMetrics` | **Code only.** Register extra [metrics](https://opentelemetry.io/docs/concepts/signals/metrics/). | `Action` | `null` | `m => m.AddMeter("MyApp")` | -| `ConfigureLogging` | **Code only.** Tweak the [logging](https://learn.microsoft.com/dotnet/core/extensions/logging) pipeline. | `Action` | `null` | `l => l.AddConsole()` | - -> 💡 See [`docs/appsettings.Example.json`](./docs/appsettings.Example.json) -> for a complete profile with every key set to a realistic, non-default value. - -### Endpoint & protocol - -Give `Endpoint` the *base* URL, not a signal path: - -- `HttpProtobuf` (default, port `4318`) — the per-signal path (`/v1/traces`, - `/v1/metrics`, `/v1/logs`) is appended for you. -- `Grpc` (port `4317`) — every signal goes to the base endpoint as-is. - -`Endpoint` is validated at registration and `AddTelemetry()` throws when it is -missing. The one exception is `Enabled: false`: validation is skipped, nothing is -registered, and the call becomes a no-op — so you can switch telemetry off in an -environment without also having to supply an endpoint. - -### Custom section name - -The section defaults to `Telemetry`, but you can bind any section by passing its name: - -```csharp -builder.Services.AddTelemetry(builder.Configuration, "MyTelemetry"); -// or together with a code callback: -builder.Services.AddTelemetry(builder.Configuration, o => { /* ... */ }, "MyTelemetry"); -``` - ---- +## 🔧 Configuration -## 🧩 Code Configuration +All keys live under the **`Telemetry`** section. Only `Endpoint` is required; +everything else has a default. The same properties can be set in the +`AddTelemetry(o => …)` callback. -Configure entirely in code instead of `appsettings.json`: +| Key | Type | Default | Description | +|---|---|---|---| +| **Connection** | | | | +| `Endpoint` | `Uri` | *required* | OTLP base URL for all signals. | +| `Protocol` | `string` | `HttpProtobuf` | `HttpProtobuf` (port 4318) or `Grpc` (port 4317). | +| `Headers` | `string` | `""` | Exporter headers, e.g. for auth: `k1=v1,k2=v2`. | +| **Identity** | | | | +| `ServiceName` | `string?` | `null` | Service name shown in the backend. | +| `ResourceAttributes` | `object` | `{}` | Extra resource attributes, e.g. `deployment.environment`. | +| **Signals** | | | | +| `Enabled` | `bool` | `true` | Master switch; `false` registers nothing and skips validation. | +| `EnableTracing` | `bool` | `true` | Distributed tracing. | +| `EnableMetrics` | `bool` | `true` | Metrics collection. | +| `EnableLogging` | `bool` | `true` | `ILogger` export via OTLP. | +| **Instrumentation** | | | | +| `EnableAspNetCoreInstrumentation` | `bool` | `true` | Incoming ASP.NET Core requests *(no-op on `netstandard2.0`)*. | +| `EnableHttpClientInstrumentation` | `bool` | `true` | Outgoing `HttpClient` requests. | +| `EnableRuntimeInstrumentation` | `bool` | `true` | GC, memory and thread-pool metrics. | +| `RecordExceptions` | `bool` | `true` | Record exceptions with stack traces on spans. | +| `ExcludedPaths` | `string[]` | `["/health"]` | Paths excluded from tracing (per-segment prefix match). | +| `AdditionalTracingSources` | `string[]` | `[]` | Extra `ActivitySource` names to collect. | +| `AdditionalMeters` | `string[]` | `[]` | Extra `Meter` names to collect. | +| `SampleRatio` | `double` | `1.0` | Fraction of traces sampled, parent-based. | +| **Logging** | | | | +| `IncludeScopes` | `bool` | `true` | Include log scopes in exported records. | +| `IncludeFormattedMessage` | `bool` | `true` | Include the formatted message in exported records. | +| **Code only** | | | | +| `ConfigureTracing` | `Action` | `null` | Register extra sources or instrumentation. | +| `ConfigureMetrics` | `Action` | `null` | Register extra meters. | +| `ConfigureLogging` | `Action` | `null` | Tweak the logging pipeline. | + +> 💡 [`docs/appsettings.Example.json`](./docs/appsettings.Example.json) sets every +> key to a realistic, non-default value. + +**Endpoint & protocol.** Give `Endpoint` the *base* URL. With `HttpProtobuf` the +per-signal path (`/v1/traces`, `/v1/metrics`, `/v1/logs`) is appended for you; +with `Grpc` every signal goes to the base URL as-is. `AddTelemetry()` throws at +registration when `Endpoint` is missing — unless `Enabled` is `false`. + +
+Configure in code — callback only, or on top of appsettings.json + +
```csharp builder.Services.AddTelemetry(o => @@ -309,111 +212,90 @@ builder.Services.AddTelemetry(o => o.ServiceName = "my-api"; o.ResourceAttributes = new() { ["deployment.environment"] = "production" }; o.SampleRatio = 0.1; - - // Code-only: register additional instrumentation - o.ConfigureTracing = tracing => tracing.AddSource("MyApp"); - o.ConfigureMetrics = metrics => metrics.AddMeter("MyApp"); - o.ConfigureLogging = logging => logging.AddConsole(); + o.ConfigureTracing = tracing => tracing.AddSource("MyApp"); }); ``` -Or bind `appsettings.json` first and layer code-only options on top — both -sources are combined, and bound values can still be overridden in the callback: +Bind `appsettings.json` first and layer code on top — bound values can still be +overridden in the callback: ```csharp builder.Services.AddTelemetry(builder.Configuration, o => -{ - // Everything from appsettings.json is already bound here. - o.ConfigureTracing = tracing => tracing.AddSource("MyApp"); -}); + o.ConfigureTracing = tracing => tracing.AddSource("MyApp")); ``` -### Registering your own Sources & Meters +
-The built-in instrumentation (ASP.NET Core, `HttpClient`, .NET runtime) is wired -up automatically. The three `Configure*` callbacks are the extension points for -the signals **your own** application emits, plus anything that needs an extra -NuGet package — such as [databases](#databases). - -| Hook | Builder | Used to register | -|---|---|---| -| `ConfigureTracing` | [`TracerProviderBuilder`](https://opentelemetry.io/docs/languages/net/instrumentation/#traces) | **Activity Sources** via `AddSource("Name")` | -| `ConfigureMetrics` | [`MeterProviderBuilder`](https://opentelemetry.io/docs/languages/net/instrumentation/#metrics) | **Meters** via `AddMeter("Name")` | -| `ConfigureLogging` | [`ILoggingBuilder`](https://learn.microsoft.com/dotnet/api/microsoft.extensions.logging.iloggingbuilder) | extra logging providers, filters, etc. | +
+Custom section name -**Custom metrics.** A [`Meter`](https://learn.microsoft.com/dotnet/api/system.diagnostics.metrics.meter) -creates instruments (counters, histograms, gauges). OpenTelemetry only collects -meters whose name you registered: +
```csharp -private static readonly Meter Meter = new("MyApp.Orders"); -private static readonly Counter OrdersPlaced = Meter.CreateCounter("orders.placed"); +builder.Services.AddTelemetry(builder.Configuration, "MyTelemetry"); +builder.Services.AddTelemetry(builder.Configuration, o => { /* ... */ }, "MyTelemetry"); +``` -OrdersPlaced.Add(1); +
-o.ConfigureMetrics = metrics => metrics.AddMeter("MyApp.Orders"); -``` +
+Your own spans and metrics — ActivitySource and Meter -**Custom spans.** An [`ActivitySource`](https://learn.microsoft.com/dotnet/api/system.diagnostics.activitysource) -is the tracing equivalent — it creates `Activity` objects (= spans): +
+ +OpenTelemetry only collects sources and meters whose name you registered — via +`AdditionalTracingSources` / `AdditionalMeters` in config, or in code: ```csharp private static readonly ActivitySource Activity = new("MyApp"); +private static readonly Meter Meter = new("MyApp.Orders"); +private static readonly Counter OrdersPlaced = Meter.CreateCounter("orders.placed"); using var span = Activity.StartActivity("ProcessOrder"); +OrdersPlaced.Add(1); o.ConfigureTracing = tracing => tracing.AddSource("MyApp"); +o.ConfigureMetrics = metrics => metrics.AddMeter("MyApp.Orders"); ``` -> The string passed to `AddMeter`/`AddSource` must **exactly match** the name you -> gave the `Meter`/`ActivitySource`. Prefer config? `AdditionalMeters` and -> `AdditionalTracingSources` do the same from `appsettings.json`. +The registered name must **exactly match** the name of the `ActivitySource` or `Meter`. + +
+ +
+Databases — SQL Server, EF Core, PostgreSQL, MySQL, Oracle -### Databases +
-Database instrumentation is **not** built in — it depends on your driver, so the -package stays free of database-specific dependencies. There are two kinds: +Database instrumentation depends on your driver, so it is not built in: | Kind | Examples | How to enable | |---|---|---| -| **Package-based** — ships an `Add…Instrumentation()` extension | SQL Server (`OpenTelemetry.Instrumentation.SqlClient`), EF Core (`OpenTelemetry.Instrumentation.EntityFrameworkCore`) | install the package + call it in `ConfigureTracing` | -| **Source-based** — the driver already emits an `ActivitySource` | Npgsql (`Npgsql`), MySqlConnector (`MySqlConnector`), Oracle (`Oracle.ManagedDataAccess.Core`) | register the source name — code *or* config, no extra package | +| **Package-based** | SQL Server (`OpenTelemetry.Instrumentation.SqlClient`), EF Core (`OpenTelemetry.Instrumentation.EntityFrameworkCore`) | install the package and call it in `ConfigureTracing` | +| **Source-based** | Npgsql (`Npgsql`), MySqlConnector (`MySqlConnector`), Oracle (`Oracle.ManagedDataAccess.Core`) | register the source name — config or code | `ConfigureTracing` is a single delegate, so chain everything in one assignment: ```csharp -// dotnet add package OpenTelemetry.Instrumentation.SqlClient -// dotnet add package OpenTelemetry.Instrumentation.EntityFrameworkCore o.ConfigureTracing = t => t .AddSqlClientInstrumentation() .AddEntityFrameworkCoreInstrumentation() - .AddSource("Npgsql") // PostgreSQL - .AddSource("MySqlConnector"); // MySQL + .AddSource("Npgsql") + .AddSource("MySqlConnector"); ``` -Source-based drivers need no C# at all: +Source-based drivers need no code at all: ```json { "Telemetry": { "Endpoint": "http://localhost:4318", - "AdditionalTracingSources": [ "Npgsql", "MyApp" ], - "AdditionalMeters": [ "MyApp.Orders" ] + "AdditionalTracingSources": [ "Npgsql" ] } } ``` -**Toggling SQL instrumentation from `appsettings.json`.** Because the SqlClient -package is optional, add a custom key and read it in the callback: - -```json -{ - "Telemetry": { - "Endpoint": "http://localhost:4318", - "EnableSqlClientInstrumentation": true - } -} -``` +To toggle an optional package from config, add your own key and read it in the callback: ```csharp builder.Services.AddTelemetry(builder.Configuration, opt => @@ -426,91 +308,75 @@ builder.Services.AddTelemetry(builder.Configuration, opt => }); ``` ---- +
-## 🖥️ Using outside the Generic Host +
+Console and desktop apps — with or without the Generic Host -`AddTelemetry()` works with **any** `IServiceCollection` — ASP.NET Core, WPF, -WinForms, console, MAUI/WinUI, UWP, worker services, etc. +
-**With the Generic Host** (recommended for desktop/console), the providers start -and flush automatically: +`AddTelemetry()` works with any `IServiceCollection`. With the Generic Host the +providers start and flush automatically: ```csharp var builder = Host.CreateApplicationBuilder(args); builder.Services.AddTelemetry(builder.Configuration); using var host = builder.Build(); -await host.RunAsync(); // telemetry starts here and flushes on shutdown +await host.RunAsync(); ``` -**Without a host** (e.g. a bare `ServiceCollection`), build the provider and -**dispose it on exit** so buffered telemetry is flushed: +Without a host, **dispose the provider on exit** so buffered telemetry is flushed: ```csharp var services = new ServiceCollection(); -services.AddTelemetry(o => -{ - o.Endpoint = new Uri("http://localhost:4318"); - o.ServiceName = "my-desktop-app"; -}); +services.AddTelemetry(o => o.Endpoint = new Uri("http://localhost:4318")); var provider = services.BuildServiceProvider(); // ... app runs ... -provider.Dispose(); // flushes traces, metrics and logs +provider.Dispose(); ``` ---- - -## 🧪 Samples - -Two runnable samples live under [`src/`](./src): - -| Sample | Project | Demonstrates | -|---|---|---| -| **Web API** | [`…Sample.WebApi`](./src/OpenTelemetryExtension.Configuration.Sample.WebApi) | ASP.NET Core minimal API configured from `appsettings.json`, ready-to-run backend profiles, EF Core and opt-in SQL instrumentation. | -| **WPF** | [`…Sample.Wpf`](./src/OpenTelemetryExtension.Configuration.Sample.Wpf) | Desktop app wiring `AddTelemetry()` through the **Generic Host**, emitting a custom `ActivitySource`/`Meter` and an `HttpClient` span on a button click. | - -The WPF sample exports to `http://localhost:4318` by default — point it at any -of the backends below. +
--- -## 🔌 Running Locally with a Backend +## 🔌 Backends -The [Web API sample](./src/OpenTelemetryExtension.Configuration.Sample.WebApi) -ships a start script (Docker Compose or Helm) in [`infrastructure/`](./infrastructure) -and a matching launch profile for each backend: +Anything that speaks [OTLP](https://opentelemetry.io/docs/specs/otlp/) works. For a +hosted service (Grafana Cloud, Honeycomb, New Relic, Elastic, Datadog, …) point +`Endpoint` at its OTLP ingest URL and put the API key in `Headers`. -1. **Start the backend** — run the script from the table. Docker scripts need - Docker; Helm scripts need a local Kubernetes cluster (e.g. k3s in WSL2). -2. **Run the sample** with the matching profile: - ```bash - cd src/OpenTelemetryExtension.Configuration.Sample.WebApi - dotnet run --launch-profile "Start Aspire" - ``` -3. **Generate traffic** — Swagger opens at `https://localhost:5073/swagger`; call an endpoint. -4. **Open the backend UI** and explore the telemetry. +These backends ship with a start script in [`infrastructure/`](./infrastructure) +and a matching launch profile in the [Web API sample](./src/OpenTelemetryExtension.Configuration.Sample.WebApi): -| Backend | Start infrastructure | Launch profile | Backend UI | +| Backend | Start script | Launch profile | UI | |---|---|---|---| -| .NET Aspire Dashboard | `docker/docker-install-aspire-dashboard.cmd` *(or `helm/helm-install-aspire-dashboard.cmd`)* | `Start Aspire` | | -| Jaeger | `docker/docker-install-jaeger.cmd` | `Start Jaeger` | | -| OpenObserve | `helm/helm-install-openobserve.cmd` *(or `docker/docker-install-openobserve.cmd`)* | `Start OpenObserve Http` / `Start OpenObserve Grpc` | (`admin@web.de`/`admin`) | -| SigNoz | `helm/helm-install-signoz.cmd` | `Start SigNoz` | (`admin@web.de`) | -| Grafana / Loki | `docker/docker-install-loki.cmd` | `Start Loki` | (`admin`/`admin`) | -| OpenSearch Dashboards | `docker/docker-install-opensearch.cmd` | `Start OpenSearch` | | +| [.NET Aspire Dashboard](https://aspire.dev/dashboard/standalone/) | `docker/docker-install-aspire-dashboard.cmd` | `Start Aspire` | | +| [Jaeger](https://www.jaegertracing.io/) | `docker/docker-install-jaeger.cmd` | `Start Jaeger` | | +| [OpenObserve](https://openobserve.ai/) | `helm/helm-install-openobserve.cmd` | `Start OpenObserve Http` / `Grpc` | | +| [SigNoz](https://signoz.io/) | `helm/helm-install-signoz.cmd` | `Start SigNoz` | | +| [Grafana](https://grafana.com/) / [Loki](https://grafana.com/oss/loki/) | `docker/docker-install-loki.cmd` | `Start Loki` | | +| [OpenSearch Dashboards](https://opensearch.org/) | `docker/docker-install-opensearch.cmd` | `Start OpenSearch` | | -*(Paths are relative to [`infrastructure/`](./infrastructure).)* +Start the backend, then run the sample with the matching profile and call an +endpoint from Swagger (`https://localhost:5073/swagger`): + +```bash +cd src/OpenTelemetryExtension.Configuration.Sample.WebApi +dotnet run --launch-profile "Start Aspire" +``` -### Sample backend configurations +Docker scripts need Docker; Helm scripts need a local Kubernetes cluster (e.g. k3s +in WSL2). Aspire also has a Helm script and OpenObserve a Docker one. Local logins: +OpenObserve `admin@web.de`/`admin`, SigNoz `admin@web.de`, Grafana `admin`/`admin`. -These are the exact `appsettings..json` files used by the sample's launch profiles. +
+.NET Aspire Dashboard — appsettings.aspire.json -#### .NET Aspire Dashboard — `appsettings.aspire.json` +
-The dashboard requires an API key on the OTLP endpoint (`x-otlp-api-key`). The -gRPC endpoint is exposed on NodePort `31889` (Helm) or host port `31889` (Docker). -Open and use the **Structured**, **Traces** or +The dashboard requires an API key on the OTLP endpoint. gRPC is exposed on port +`31889`; open and use the **Structured**, **Traces** or **Metrics** tab. ```json @@ -523,7 +389,12 @@ Open and use the **Structured**, **Traces** or } ``` -#### Jaeger — `appsettings.jaeger.json` +
+ +
+Jaeger — appsettings.jaeger.json + +
```json { @@ -536,7 +407,12 @@ Open and use the **Structured**, **Traces** or ![Traces from the sample app in the Jaeger UI](https://raw.githubusercontent.com/thorstenalpers/OpenTelemetryExtension.Configuration/main/assets/Jaeger.webp) -#### OpenObserve — HTTP/protobuf — `appsettings.openobserve-http.json` +
+ +
+OpenObserve — appsettings.openobserve-http.json + +
```json { @@ -550,72 +426,66 @@ Open and use the **Structured**, **Traces** or ![Telemetry from the sample app in the OpenObserve UI](https://raw.githubusercontent.com/thorstenalpers/OpenTelemetryExtension.Configuration/main/assets/OpenObserve.webp) +
+ +--- + +## 🧪 Samples + +| Sample | Demonstrates | +|---|---| +| [**Web API**](./src/OpenTelemetryExtension.Configuration.Sample.WebApi) | ASP.NET Core minimal API configured from `appsettings.json`, backend launch profiles, EF Core and opt-in SQL instrumentation. | +| [**WPF**](./src/OpenTelemetryExtension.Configuration.Sample.Wpf) | Desktop app on the Generic Host emitting a custom `ActivitySource`, `Meter` and `HttpClient` span; exports to `http://localhost:4318`. | + --- ## ❓ FAQ -**Does it hide OpenTelemetry from me?** +
+Does it hide OpenTelemetry from me? + No. It calls the official OpenTelemetry .NET SDK and exposes the underlying builders through `ConfigureTracing`, `ConfigureMetrics` and `ConfigureLogging`. Anything the SDK can do, you can still do. -**How do I turn telemetry off in tests or locally?** -Set `"Enabled": false` (or the environment variable `Telemetry__Enabled=false`). -Nothing is registered and no endpoint is required. +
-**Does it work with .NET Framework?** -Yes, through the `netstandard2.0` build (.NET Framework 4.6.1+). Only the ASP.NET -Core instrumentation is absent there. See [Requirements](#-requirements). +
+How do I turn telemetry off in tests or locally? -**Can I use it with .NET Aspire?** -Yes. Point `Endpoint` at the Aspire Dashboard's OTLP endpoint, or use it in -projects that are not part of an Aspire AppHost but should report to the same -dashboard. - -**Why is my custom metric or span missing?** -Its `Meter`/`ActivitySource` name is not registered. Add it to -`AdditionalMeters`/`AdditionalTracingSources` or call `AddMeter`/`AddSource` — -the names must match exactly. +Set `"Enabled": false` (or `Telemetry__Enabled=false`). Nothing is registered and +no endpoint is required. ---- +
-## 🤝 Contributing +
+Can I use it with .NET Aspire? -Contributions are welcome — bug fixes, new instrumentation options, documentation -improvements and ideas alike. For anything beyond a small fix, please open an -issue first so we can agree on the approach. +Yes. Point `Endpoint` at the Aspire Dashboard's OTLP endpoint — also from projects +that are not part of an Aspire AppHost but should report to the same dashboard. -Fork the repo, branch off `main` (`feature/` or `fix/`), add or update -tests for any public API change, and open a PR against `main`: +
-```bash -dotnet build OpenTelemetryExtension.slnx -c Release -dotnet test src/OpenTelemetryExtension.Configuration.Tests -c Release -``` +
+Why is my custom metric or span missing? -Full details — code style, the integration-test stack and the release process — -are in [CONTRIBUTING.md](./CONTRIBUTING.md). Versioning and release notes are -handled at release time, so you don't need to touch them in a feature PR. +Its `Meter`/`ActivitySource` name is not registered. Add it to +`AdditionalMeters`/`AdditionalTracingSources` or call `AddMeter`/`AddSource` — +the names must match exactly. -If this package saves you time, a ⭐ on -[GitHub](https://github.com/thorstenalpers/OpenTelemetryExtension.Configuration) -helps others find it. +
--- -## 🐛 Report a Bug - -Found something broken? Please -[open an issue](https://github.com/thorstenalpers/OpenTelemetryExtension.Configuration/issues/new/choose) -and include the package version, your target framework, the relevant `Telemetry` -configuration, the OTLP backend you export to, and what you expected versus what -happened. A minimal repro or stack trace speeds things up a lot. +## 🤝 Contributing -For security-sensitive reports, please follow [SECURITY.md](./SECURITY.md) -instead of opening a public issue. +Bug fixes, new options and documentation improvements are welcome — for anything +beyond a small fix, please open an issue first. Build, test and style details are +in [CONTRIBUTING.md](./CONTRIBUTING.md). ---- +Found a bug? [Open an issue](https://github.com/thorstenalpers/OpenTelemetryExtension.Configuration/issues/new/choose) +with the package version, target framework, your `Telemetry` configuration and +the backend. Security issues go through [SECURITY.md](./SECURITY.md). -## 📄 License - -Released under the [MIT License](./LICENSE). +Released under the [MIT License](./LICENSE). If this package saves you time, a ⭐ +helps others find it. diff --git a/docs/README.nuget.md b/docs/README.nuget.md new file mode 100644 index 0000000..922be5c --- /dev/null +++ b/docs/README.nuget.md @@ -0,0 +1,46 @@ +**Production-ready OpenTelemetry for .NET in one line.** Traces, metrics and logs +are exported over OTLP to Jaeger, Grafana, SigNoz, OpenObserve, the .NET Aspire +Dashboard or any other OpenTelemetry backend — configured from +`appsettings.json`, with sensible defaults and no boilerplate. + +## Quick Start + +```csharp +builder.Services.AddTelemetry(builder.Configuration); +``` + +```json +{ + "Telemetry": { + "Endpoint": "http://localhost:4318", + "ServiceName": "my-api" + } +} +``` + +That's the whole setup. Incoming requests, outgoing `HttpClient` calls, .NET +runtime metrics and every `ILogger` message now show up in your backend. + +No backend yet? The .NET Aspire Dashboard is a single container — run it, start +your app and open : + +```bash +docker run --rm -it -p 18888:18888 -p 4317:18889 -p 4318:18890 -e DOTNET_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS=true mcr.microsoft.com/aspire/dashboard:latest +``` + +![Traces, metrics and logs from the sample app in the .NET Aspire Dashboard](https://raw.githubusercontent.com/thorstenalpers/OpenTelemetryExtension.Configuration/main/assets/Aspire-Dashboard.webp) + +## Features + +- **One call, three signals** — tracing, metrics and logging via `AddTelemetry()`. +- **Configuration-driven** — every option bindable from `appsettings.json` or environment variables; override in code when needed. +- **Batteries included** — ASP.NET Core, `HttpClient` and .NET runtime instrumentation, each toggleable. +- **Runs everywhere** — ASP.NET Core, worker, console, WPF, WinForms, MAUI and .NET Framework via `netstandard2.0`. +- **No lock-in** — plain OpenTelemetry underneath; any OTLP backend over HTTP/protobuf or gRPC. + +## Documentation + +- [Configuration reference](https://github.com/thorstenalpers/OpenTelemetryExtension.Configuration#-configuration) — every option with its default +- [Backends](https://github.com/thorstenalpers/OpenTelemetryExtension.Configuration#-backends) — ready-to-run setups for Aspire, Jaeger, SigNoz, OpenObserve, Grafana/Loki and OpenSearch +- [Samples](https://github.com/thorstenalpers/OpenTelemetryExtension.Configuration#-samples) — ASP.NET Core Web API and WPF +- [FAQ](https://github.com/thorstenalpers/OpenTelemetryExtension.Configuration#-faq) · [Report a bug](https://github.com/thorstenalpers/OpenTelemetryExtension.Configuration/issues/new/choose) · [Release notes](https://github.com/thorstenalpers/OpenTelemetryExtension.Configuration/releases) diff --git a/src/OpenTelemetryExtension.Configuration/OpenTelemetryExtension.Configuration.csproj b/src/OpenTelemetryExtension.Configuration/OpenTelemetryExtension.Configuration.csproj index fc94ba1..7782bed 100644 --- a/src/OpenTelemetryExtension.Configuration/OpenTelemetryExtension.Configuration.csproj +++ b/src/OpenTelemetryExtension.Configuration/OpenTelemetryExtension.Configuration.csproj @@ -43,7 +43,7 @@ true /logo.png - + true /README.md