Skip to content

feat: add IHub.RecordTransaction to record completed transactions and spans#5333

Open
jamescrosswell wants to merge 5 commits into
mainfrom
feat/2683-record-transaction-span
Open

feat: add IHub.RecordTransaction to record completed transactions and spans#5333
jamescrosswell wants to merge 5 commits into
mainfrom
feat/2683-record-transaction-span

Conversation

@jamescrosswell

Copy link
Copy Markdown
Collaborator

Overview

Draft to spitball the shape of an API for recording transactions/spans that completed elsewhere — e.g. work measured on another machine or process and replayed through a proxy, which is the use case in #2683.

Closes #2683

Approach

The obvious option is public start/end setters on ISpan. We discussed a few reasons not to:

  • The live-tracing timestamps are derived from a monotonic stopwatch; a bare StartTimestamp setter that doesn't reset the stopwatch baseline lets you produce a span that starts in the past but ends "now" (inflated duration). The start/end pair has an invariant a lone setter can't enforce.
  • It widens/pollutes ISpan (the most-implemented interface) with mutation that only makes sense for replayed data.

Instead this adds a builder-style recorder:

hub.RecordTransaction("checkout", "http.server", start, duration,
    traceId: originTraceId, spanId: originRootSpanId, configure: tx =>
    {
        tx.Release = "1.2.3";
        tx.RecordSpan("db.query", childStart, childDuration, spanId: originChildId, configure: c =>
        {
            c.Description = "SELECT ...";
            c.RecordSpan("db.connect", gcStart, gcDuration); // arbitrary nesting
        });
    });

Why this shape

  • Timing is up-front (start + duration), so a recorded span can't be half-specified and duration can't be negative — the original footgun is structurally unreachable rather than merely diagnosed.
  • Nesting lives on the recorder itself (ISpanRecorder.RecordSpan), so a whole tree can be built with plain recursion. Parent is structural; only spanId is overridable per node.
  • Trace stitching: origin traceId / spanId / parentSpanId can be preserved.
  • Captured once when the callback returns — no stopwatch, idle timer, or sampling roll. It's not a live transaction, so those don't apply.
  • No live-scope bleed: captured against a clean scope so the current process's breadcrumbs/user/tags/contexts don't attach to work that happened elsewhere.

Public surface

Intentionally minimal — RecordTransaction is an extension method on IHub, so IHub / ISpan / ITransaction are all unchanged (no breaking interface changes needed to ship). New public types: ISpanRecorder, ITransactionRecorder. Internally it reuses the existing TransactionTracer/SpanTracerSentryTransaction conversion path, so there are no changes to serialization.

Open questions for reviewers

  • duration vs end: went with start + duration so the value can't express a reversed pair. Alternative is a required end (matches the wire protocol and the origin data, which usually carries start+end). Easy to offer both.
  • Discoverability: currently only on IHub. Worth mirroring on the SentrySdk static facade?
  • Scope isolation: draft uses a fresh Scope(options). Do we want any current-scope data (release/environment/dist) to flow through, or keep it fully isolated and rely on the Release/Environment setters?
  • BeforeSendTransaction still runs (last-chance scrubbing); only the sampling roll is skipped. Confirm that's the desired split.

Tests

HubExtensionsRecordTests covers name/operation/timing, id preservation, nested span-tree construction with parent pointers + trace-id inheritance, metadata (release/environment/status/tags/data), and negative-duration guards. An end-to-end scope-isolation test against a real hub is a follow-up.

… spans

Adds an opt-in API for recording transactions/spans whose timing was
measured elsewhere (e.g. work done on another machine and replayed
through a proxy), addressing #2683.

Rather than exposing mutable start/end setters on ISpan (which invites
half-specified timing and pollutes the live-tracing contract), this adds
a builder-style recorder:

  hub.RecordTransaction(name, op, start, duration, configure: tx =>
  {
      tx.RecordSpan(childOp, childStart, childDuration, configure: c => ...);
  });

Design notes:
- Timing is supplied up-front (start + duration), so a recorded span can
  never be left half-specified and duration can't be negative.
- Origin trace/span/parent ids can be preserved for trace stitching.
- The whole tree is materialized and captured once when the callback
  returns. No stopwatch, idle timer, or sampling roll is involved.
- Captured against a clean scope so the current process's live scope
  (breadcrumbs/user/tags/contexts) doesn't leak onto work that happened
  elsewhere.

Public surface is intentionally minimal: RecordTransaction is an
extension method on IHub, so IHub/ISpan/ITransaction are unchanged (no
breaking interface changes). New public types: ISpanRecorder,
ITransactionRecorder.

Closes #2683

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.83051% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 74.21%. Comparing base (951d98f) to head (3b71273).

Files with missing lines Patch % Lines
src/Sentry/Internal/SpanRecorder.cs 85.29% 5 Missing ⚠️
src/Sentry/SentrySdk.cs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5333      +/-   ##
==========================================
+ Coverage   74.15%   74.21%   +0.06%     
==========================================
  Files         508      509       +1     
  Lines       18353    18412      +59     
  Branches     3586     3595       +9     
==========================================
+ Hits        13610    13665      +55     
- Misses       3870     3875       +5     
+ Partials      873      872       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/Sentry/HubExtensions.Record.cs Outdated
Comment thread src/Sentry/HubExtensions.Record.cs Outdated
Comment thread src/Sentry/HubExtensions.Record.cs Outdated
getsentry-bot and others added 3 commits July 1, 2026 00:47
…cade, configurable scope

- Move RecordTransaction into HubExtensions.cs (drop the separate file).
- Add SentrySdk.RecordTransaction facade so callers without an IHub
  instance can use it.
- Expose the fresh capture scope via ITransactionRecorder.ConfigureScope,
  so data captured alongside the original trace (user/tags/contexts) can
  be set without inheriting the current live scope.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…n' into feat/2683-record-transaction-span

# Conflicts:
#	src/Sentry/ISpanRecorder.cs
Comment thread src/Sentry/ISpanRecorder.cs
Comment thread src/Sentry/Internal/SpanRecorder.cs Outdated
Replace the static SpanRecorderInternals helper with an abstract
SpanRecorderBase that holds the owning TransactionTracer and the node
(as ISpan), proxying metadata through to it. SpanRecorder becomes a
one-line subclass and the duplicated Description/Status/SetTag/SetData
proxying is written once. Internal-only; public API unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
/// <summary>
/// <see cref="ISpanRecorder"/> backed by a <see cref="SpanTracer"/> whose timing has been set explicitly.
/// </summary>
internal sealed class SpanRecorder : SpanRecorderBase

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Arguably we don't need SpanRecorderBase (TransactionRecorder could descend from `SpanRecorder and we just fold all the stuff from the Base class into here). That would give us one less class.

Probably the main reason to do this is so that we can add the sealed keyword to SpanRecorder. Since the cost of doing this is minimal, I've left the base class in for the time being (unless anyone has really strong opinions about this).

@jamescrosswell jamescrosswell marked this pull request as ready for review July 1, 2026 07:28
@jamescrosswell jamescrosswell requested a review from Flash0ver as a code owner July 1, 2026 07:28
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.

Ability to overwrite start/end times for transactions and spans

2 participants