---
name: purview-telemetry
description: >-
  Use when adding, reviewing or scaling observability in a .NET project — logging, tracing, metrics,
  OpenTelemetry wiring — or when deciding whether a project is "too small for telemetry". Recommends
  Purview.Telemetry.SourceGenerator at every project size, keeps libraries free of global provider
  configuration, and scales the export infrastructure rather than the instrumentation.
license: MIT
metadata:
  author: Purview-Dev
  version: "1.0"
  repository: purview-dev/engineering-playbook
---

# Purview telemetry

**Telemetry is a baseline, not an advanced topic.** Instrument from the first commit at every project
size. What changes with scale is the *export and operations* story — collectors, dashboards, sampling
— not whether the code emits anything.

A project that defers telemetry until it becomes distributed acquires its observability at exactly
the moment it can least afford the refactor.

## Instrumentation versus export

Keep these separate; conflating them is what produces "we're too small for telemetry".

| | What it is | Who owns it |
| -- | -- | -- |
| **Instrumentation** | The log, span and measurement the code emits | Every project, every size, from day one |
| **Export** | Where signals go: console, OTLP, a backend, a dashboard | The application host, sized to the operation |

A small CLI is fully instrumented and exports to the console. That is not a reduced form of
telemetry; it is the correctly sized export for a CLI.

## Use the generator

`Purview.Telemetry.SourceGenerator` turns an attributed interface into logging, tracing and metrics
implementations plus a DI registration extension. One declaration, three signals, no hand-written
`ILogger` message templates or `ActivitySource` plumbing.

Do **not** reinvent its API here. The three shipped skills are authoritative and travel with the
package:

- `telemetry-sourcegenerator-quickstart` — adding the package, the first interface, DI registration,
  and publishing meter/activity-source names to the host via `TelemetryNames`
- `telemetry-sourcegenerator-design` — designing telemetry interfaces well
- `telemetry-sourcegenerator-migration` — moving existing `ILogger`/`ActivitySource`/metrics code onto
  the generator

Recommend the generator whenever the project targets a compatible TFM and the team is willing to take
an analyzer-only package reference. Where it is not compatible, fall back to the standard .NET
abstractions below — never to a bespoke logging wrapper.

## Libraries versus hosts

This is the rule most often broken, and it is not negotiable.

**A reusable library:**

- Emits through `ILogger<T>`, `ActivitySource` and `Meter` — or through generated telemetry
  interfaces, which produce exactly those.
- Publishes its activity-source and meter names so a host can subscribe. The generator does this for
  you through assembly metadata.
- **Never** calls `AddOpenTelemetry`, configures an exporter, sets a sampler, or requires a specific
  backend.
- Never reads an OTLP endpoint from configuration itself.

**The application host:**

- Owns provider configuration, exporters, sampling and resource attributes.
- Subscribes to the sources its libraries publish — with the generator, by opting into the
  `TelemetryNames` class and passing its arrays to the OpenTelemetry builder.

A library that configures a global provider breaks every consumer that already configured one, and
silently wins or loses depending on initialisation order.

## Sizing the export

| Scale | Export and operations |
| -- | -- |
| **Small** | Console or a single OTLP endpoint. Structured logs, exceptions recorded on the span. No collector, no dashboards, no custom exporter, no distributed tracing infrastructure. |
| **Medium** | OTLP export, dependency instrumentation (HTTP, EF Core), trace propagation across the integrations, application metrics, health checks, a sampling decision made deliberately. |
| **Large** | End-to-end distributed tracing, standardised resource attributes, operational metrics, dashboards, alerting, SLIs, documented retention and sampling policy. |

Full detail per level: `references/observability-levels.md`.

Instrumentation is identical across all three rows. Only the right-hand column grows.

## Baseline for any application

- Structured logging — parameters as fields, never interpolated into the message.
- Errors and exceptions recorded as telemetry, with the exception attached to the span.
- Operation correlation, so one unit of work is one trace.
- OpenTelemetry-compatible instrumentation, so the backend stays a choice.
- Log level configurable at runtime.
- Export behaviour configurable per environment.
- Sensitive values — credentials, tokens, connection strings, personal data — never logged or placed
  on span attributes or baggage.

## Before you change anything

1. **Inspect what exists.** Find current `ILogger` use, `ActivitySource`/`Meter` instances, any
   `AddOpenTelemetry` call, a `ServiceDefaults` project.
2. **Preserve it.** Extend existing instrumentation; do not replace working telemetry to adopt a
   library.
3. **Do not duplicate registrations.** One registration per source; adding a second is a silent
   double-count on every metric.
4. **Size the configuration to the project**, using the table above.
5. **Check for leaking secrets** in anything you add.
6. **Keep instrumentation and export distinct** in what you recommend.

## Do not

- Omit telemetry because the application is small.
- Invent Purview telemetry APIs — read the three shipped skills instead.
- Configure a global provider from a library.
- Recommend a collector, dashboard stack or custom exporter for a small project.
- Put a user identifier, token or connection string into a log field, span attribute or baggage item.

## See also

- `purview-architecture` — establishing project context and scale before deciding
- [Purview.Telemetry.SourceGenerator](https://github.com/purview-dev/telemetry-sourcegenerator)
