microsoft / microsoft/testfx

Roslyn analyzer: warn when Microsoft.Testing.Extensions.OpenTelemetry is referenced but AddOpenTelemetryProvider is never called

Open
#8,931 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area/mtp-observability
Dominant language
C#
Stars
1k
Forks
312
Avg merge
7h 46m
Merged PRs (30d)
465

Description

Background

#8409 / #8930 fixed the misleading XML doc and README for AddOpenTelemetryProvider — calling it without configuring instrumentation and exporters produces no telemetry, and the docs now say so explicitly. That closes the documentation bug but doesn't catch users who reference the package and forget to wire it in their Main. Both the auto-generated entry point and explicit Main users (with or without AddSelfRegisteredExtensions) can fall into this trap.

The OTel extension cannot piggy-back on the standard TestingPlatformBuilderHook self-registration mechanism (the way AppInsights, TrxReport, CrashDump, Retry, … do) because AddOpenTelemetryProvider() with no delegates still produces no MTP telemetry and no exporter, so a no-arg hook would be useless. The wiring is intrinsically caller-supplied.

Proposal

Ship a Roslyn analyzer in Microsoft.Testing.Extensions.OpenTelemetry (analyzers/dotnet/cs/) that emits a warning when the package is referenced by an executable project (test host) but Microsoft.Testing.Extensions.OpenTelemetryProviderExtensions.AddOpenTelemetryProvider is never invoked in the compilation.

  • Diagnostic id: MTPOTEL001 (TBD).
  • Default severity: Warning.
  • Title: OpenTelemetry extension is referenced but never configured.
  • Message: The 'Microsoft.Testing.Extensions.OpenTelemetry' package is referenced but 'AddOpenTelemetryProvider' is never called. Wire it from your test host's Main (or AddSelfRegisteredExtensions equivalent) to enable OpenTelemetry, or remove the package reference.
  • Suppressible per project via <NoWarn>MTPOTEL001</NoWarn> for the rare case where the package is referenced solely for AddTestingPlatformInstrumentation(TracerProviderBuilder) / AddTestingPlatformInstrumentation(MeterProviderBuilder) on a caller-owned OTel pipeline.
  • Scope: only run on output-type Exe projects (or, more precisely, projects that reference Microsoft.Testing.Platform) to avoid noise in libraries that transit the dependency.

Why an analyzer rather than a runtime warning

A runtime warning (e.g. emitted from a startup hook) would also trip explicit-Main users who deliberately reference the package without enabling OTel — the same false-positive concern raised in the #8409 discussion. With an analyzer, that scenario has a clean, idiomatic .NET escape hatch (<NoWarn> / .editorconfig severity / #pragma warning disable) instead of forcing us to invent a bespoke opt-out property or attribute.

Out of scope

  • Detecting whether an exporter was actually registered inside the withTracing / withMetrics delegate. That's a syntactic dead-end (the delegate body is arbitrary user code that can defer to runtime), and the docs/README change in #8930 already covers that part of the contract.
  • Generalising the analyzer to other extensions: most MTP extensions self-register cleanly, so this is OTel-specific for now. If a future extension hits the same pattern we can lift the analyzer into a shared infrastructure project.

Related

  • #8409 — original bug report (docs/contract).
  • #8930 — docs-only fix that ships first.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in the analyzers/dotnet/cs/ directory and inspect the existing analyzer structure and tests, if present. Define the diagnostic for executable projects that reference Microsoft.Testing.Platform, then verify it distinguishes calls to AddOpenTelemetryProvider from projects that only reference the package. Done means the warning, suppression behavior, and stated scope are covered by analyzer tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
testing-qa, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.