Skip to content

perf(telemetry): mirror the Recall telemetry cleanup in the Server, the BFF, and the telemetry skills - #553

Merged
pdevito3 merged 17 commits into
mainfrom
perf/telemetry-cleanup
Dec 4, 2026
Merged

pdevito3 merged 17 commits into
mainfrom
perf/telemetry-cleanup

Conversation

@pdevito3

@pdevito3 pdevito3 commented Dec 4, 2026

Copy link
Copy Markdown
Owner

Why

Recall cleaned up its telemetry in #224, #225, and #227. The template had the same faults: an exception for each MediatR request, a caller that left counted as a server error, repeated log properties, a span and an Information log for each script of a page load, and a trace for each idle cleanup run.

What changed

  • RaceRetryBehavior has no generic constraint, so the DI container throws no first-chance exception for each request that is not marked.
  • RequestTracingBehavior: a caller that left keeps error.type, but gets no Error status and no app.errors count.
  • The Server, the BFF, and the AppHost log with no MachineName, ThreadId, Application, or Environment. The two enricher packages are gone.
  • app.db.call_site is gone. It repeated the code.* tags.
  • service.version is the informational version (Telemetry.ServiceVersion), so -p:Version and the commit show in each signal.
  • BFF: a served file of the SPA build has no span and logs at Verbose. SpaFile.IsRequested asks the web root file provider, and the instrumentation Filter drops the span at start. A page load keeps its log and span.
  • BffSessionCleanupService and DPoPReplayCacheCleanupService check for work in a SuppressInstrumentationScope, and trace only a run that deletes rows or fails.
  • The MCP SDK logs from Warning.
  • docs/telemetry.md, rule 9 of docs/backend/feature-telemetry.md, and the feature-telemetry and aspire-telemetry skills describe each change.

Scope

Not ported from Recall: AlwaysOnSampler (the template's Server spans have the BFF as parent), the job ID tags and job failure logs, the model provider spans, and the SQL text rule. No release workflow exists, so no build passes -p:Version yet, and an image reports 1.0.0.

Tradeoffs

  • The BFF decides "a file of the SPA" from the file provider, not from a URL prefix as Recall does. A prefix rule also quiets /local-api and /signout-oidc.
  • The span of a SPA file is dropped at start, not at end. Dropping it at end left orphan session query spans for a signed-in page load and exemplars that point to no trace.

Verification

  • dotnet build FullstackTemplate.slnx: 0 warnings. Full suite at 4aa79bf..e97dfea: 4218 of 4218. After the review fixes: BFF 575 of 575, Functional 519 of 519, Unit 2529 of 2529, Integration 599 of 599.
  • Each behavior change has a test that fails on the old code, including a signed-in page load where each exported span has an exported parent, and an idle cleanup with no pooling that exported two parentless CONNECT spans before.
  • Live isolated stack (dev path): no span or log for page redirects, no removed log properties, no app.db.call_site on query spans. The published path is proven by tests only.

… exception

The open generic constraint on IRunsAgainWhenRaced made the DI container
throw and catch a TypeLoadException and an ArgumentException for each
request that did not match, which dotnet.exceptions counted. The behavior
now takes each request and runs again only a marked command.
A request that the caller cancelled threw OperationCanceledException,
which set the Error status and counted app.errors. The span keeps its
error.type tag, but the cancel by the caller is a client outcome.
Each log line had ThreadId, MachineName, Application, and Environment,
and Environment was Production also in Development. The resource
attributes give the host, the service, and the environment once. The
Server, the BFF, and the AppHost no longer add them, so the
Serilog.Enrichers.Environment and Serilog.Enrichers.Thread packages are
gone.
A tagged query span has code.file.path and code.line.number, so the tag
repeated them in each query span.
service.version was the assembly version, 1.0.0 in each build, so an
operator could not tell which build wrote a span. The Server and the BFF
now read the informational version, which -p:Version sets and which
names the commit in a build from a git checkout.
A published BFF gave a request log and a span for each script and style
of a page load, and a dev BFF for each redirect of a page to the Vite dev
server. SpaResponse marks the response where the BFF serves the SPA: the
static files and the SPA fallback. A marked response with a status under
400 logs at Verbose, and its span loses the Recorded flag, so no exporter
sends it. A proxied /api call, a BFF endpoint, an OIDC callback, a
missing file, and a failure keep their log and span.
…ails

Each run of BffSessionCleanupService started a bff.sessions.cleanup trace
with its queries, and most runs delete nothing. The run now checks for an
ended session or an expired logout token first, with no instrumentation.
It starts the trace only when the check finds rows. A run that fails
before the trace starts gets the trace in the catch, with the error.
The MCP SDK wrote three or four Information lines for each tool call, the
same facts as the request log and the span of the call. They were in the
trace that the client sent in the _meta of the call, so they did not even
join the request trace. The MCP log tests now wait for the request log
and read the parent trace and the request trace. The Server has no auth
handler of its own that logs each challenge, and the JwtBearer challenge
already logs from Warning.
…skills

The telemetry doc gets a section on the log properties and the levels
of the categories, and splits the resource attributes from the version.
The aspire-telemetry skill names each signal that is absent on purpose,
so that an agent does not look for it. The feature-telemetry skill tells
a hosted service on a timer to trace only a run that works or fails.
The tracer of each BFF in a test listens to the sources of the whole
process, so a test that ran at the same time put its query spans in the
exporter of the cleanup tests. The tests now read the cleanup span, its
children, and the tagged check queries. The check needs no suppression
scope: no activity is current, so the Npgsql command filter sends no
span for it.
…s or fails

Each run of DPoPReplayCacheCleanupService started a dpop.replay_cache.cleanup
trace, and most runs delete nothing. The run now asks HasExpiredDPoPProofs
first and starts the trace only when the check finds an expired proof. The
check goes through MediatR, whose RequestTracingBehavior starts a span, so
it runs in a SuppressInstrumentationScope. A run that fails before the trace
starts gets the trace in the catch, with the error. A cancel at shutdown is
not an error.

Rule 9 of feature-telemetry.md and the telemetry skills now name the
suppression scope for a check through MediatR.
Cut the comments that repeat the code or claim facts about our own code, and correct the claim that the assembly version is 1.0.0.0 in each build.
…he page load

The old SpaResponse mark cleared the Recorded flag at the end of the request.
By then a signed-in page load had already exported the spans of its session
read, and the request duration metric had taken the span as its exemplar. Both
then pointed at a span that no exporter got.

Now SpaFile decides from the web root file provider whether the static file
middleware serves the request. The AspNetCore instrumentation Filter drops
such a span at its start, as it drops a health probe, and the request log of
such a file is Verbose when its status is under 400. The index.html of a page
and the dev redirect keep their span and their log.
…cope

With no current activity, the Npgsql command filter drops the span of each
check query. But Npgsql 10 also starts a span with no parent when it opens a
physical connection, and the BFF has no sampler for a parentless client span.
So a cleanup run that found no work could still export a CONNECT trace.

The check now runs in a SuppressInstrumentationScope, as the DPoP replay cache
cleanup does. Rule 9 of the feature telemetry doc and the two telemetry skills
now say that the check always runs in the scope.
BeforeAnyRow is the time at which a cleanup finds no row. The DPoP test names
the expiry of its proof and the cleanup time after it. The proof expires after
real time, so the hosted cleanup of the test server keeps it until the run.
…et SPA files

The server also enriches a log with GrpcStatusDetailRedaction. ThreadId is
gone because no resource attribute replaces it and it is noise in async code,
not because it is the same on each line. The SDK derives the assembly version
from -p:Version too, and the informational version also keeps the pre-release
label. The request log, the file table, and the aspire-telemetry skill now
describe SpaFile: only a file of the built SPA is quiet, and a page load keeps
its log and its span.
@pdevito3

pdevito3 commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

review-gate: passed f4d00b0

Two independent thermo-nuclear reviews ran. The first, at e81168a, found one medium and three low findings (orphan session spans under a dropped page-load span, exemplars that point to dropped spans, a parentless CONNECT span on an idle BFF cleanup, two doc errors). Commits eb6fad9 to f4d00b0 fix all four, and the second review confirmed each fix and found no high or medium finding.

Open findings:

  • L-A (low): docs/telemetry.md said a failed SPA file keeps its span. Fixed after the gate in b5a4bd2 (docs only).
  • L-B (low, stays): SpaFile.IsRequested also asks the web root in a dev BFF. A dev BFF has no wwwroot today, so nothing is quieted. A check on SpaOrigin.DevServer would make this explicit, but no failure exists now.

pdevito3 added a commit that referenced this pull request Dec 4, 2026
…ok its port (#555)

## Why

CI on #553 failed
`a_worker_whose_worker_group_stops_on_a_fault_of_the_job_store_is_unhealthy`
with `Failed to bind to address http://127.0.0.1:32785: address already
in use`. `ServerProcess.FreePort` closes its listener before the Server
child binds the port, so another process can take it first. Port 32785
is at the bottom of the range that Docker gives to the host ports of
test containers.

## What changed

- `ServerProcess.StartAsync` starts the Server again on a new port when
the child stops with `AddressInUseException`, at most three times. A
failed start is disposed before the next one.
- After the child exits, the helper waits for the exit, so the output
holds every line before it looks for `AddressInUseException`.

## Tradeoffs

A child that binds port 0 and reports its address removes the race. It
needs a log override for `Microsoft.Hosting.Lifetime` and a parse of two
unnamed `http://127.0.0.1:N` lines (the `http` and the `grpc`
endpoints), so the retry is the smaller fix.

## Verification

- A test hack bound (without listen) the first six ports that `FreePort`
gave. On the old helper, 4 of 6 `ServerRoleTests` failed with `The
Server stopped with exit code 1`. With the retry, 6 of 6 passed. Without
the hack, 6 of 6 passed.
- The last commit (wait for the exit) is not run locally, because the
local Docker daemon stopped answering. CI runs the role tests.
@pdevito3

pdevito3 commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

verifier: PASS+NOTES b5a4bd2

Base: c43bb23 (main). Head: b5a4bd2.

What I ran

  • Two detached worktrees, head and base. Each ran launch.sh --preview (isolated stack, ZeroFactor, the BFF serves the production SPA build, so the static file path is the real published path).
  • In both worktrees only, not committed: Bff:Session:CleanupInterval and Auth:DPoP:CleanupInterval set to 15 s in appsettings.Development.json, so that the idle cleanups ran about 15 times in each stack.
  • In Chromium with the verify skill: login-logout, navigation-and-theme, and a scratch recipe that signs in, loads /, and reloads /about. Then dotnet aspire otel spans/logs --format Json and the dashboard span details.
  • At head: dotnet test of FullstackTemplate.Bff.Tests (575 of 575 pass) and FullstackTemplate.FunctionalTests (519 of 519 pass).

What I saw (base -> head)

Check Base Head
Root spans GET /assets/* (served js/css, 200) 71 0 (a direct curl of /assets/about-leHzb3Ce.js gave 200 and no span)
BFF HTTP GET /assets/... Information logs 71 0
Page loads /, /about keep a span and a request log yes yes. Trace 236516df...: GET /about (200) with child bff_sessions.get parented to it
A path that is not a file (/assets/missing-zzz.js 404, /favicon.ico) span span, as designed
Spans whose parent is not in the export (all resources) 0 0 (469 spans)
Idle cleanup traces (bff.sessions.cleanup, dpop.replay_cache.cleanup) 5 + 5 in about 75 s 0 in about 4 min
Root postgresql/CONNECT spans 0 0
Cleanup with work (I inserted one ended session and one expired DPoP proof) n/a one root bff.sessions.cleanup (app.deleted_count=1) and one dpop.replay_cache.cleanup, each with its delete spans parented
Logs with MachineName, ThreadId, Application, Environment 171 logs carry them none in 115 logs
app.db.call_site on Server spans n/a 0 of 254
service.version (bff and server) 1.0.0.0 1.0.0+b5a4bd26d0c458b4da88cf1a12fddad7f2fb2865

All three recipes passed at base and at head, with no page errors.

Notes

  • The parentless CONNECT span did not show in the live stack at base or at head, because Npgsql pools connections there. Only the PR's no-pooling test proves that part. It passed in the Functional and BFF runs above.
  • The first login-logout run at head failed one step with a 5 s timeout on "Signed in as Admin User". The first GET /api/v1/users/me on the new database took 4574 ms at head, and 260 ms at base. The rerun passed, and so did all later recipes. Nothing in this diff explains it, so I count it as cold-start noise. I did not reproduce it.

@pdevito3
pdevito3 merged commit 50cf23b into main Dec 4, 2026
42 of 43 checks passed
@pdevito3
pdevito3 deleted the perf/telemetry-cleanup branch December 4, 2026 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant