Skip to content

Feat: Added Tracing - #265

Closed
ppmpreetham wants to merge 11 commits into
PostHog:mainfrom
ppmpreetham:feat/tracing
Closed

ppmpreetham wants to merge 11 commits into
PostHog:mainfrom
ppmpreetham:feat/tracing

Conversation

@ppmpreetham

Copy link
Copy Markdown

💡 Motivation and Context

Added tracing-subscriber (optional feature flag) integration that captures selected Rust tracing events as PostHog events.
This allows applications that already use tracing to send events to PostHog without implementing their own subscriber layer or field visitor.

This PR also Closes #205.

💚 How did you test it?

I've Added 4 integration tests covering:

  • Tracing field conversion and property preservation
  • Event filtering and event-name configuration
  • Distinct ID and shared property handling
  • SDK target exclusion and recursive capture prevention
  • E2E capture through the PostHog client transport

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

@ppmpreetham
ppmpreetham requested a review from a team as a code owner September 24, 2026 14:25

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Review findings from the validated snapshot. No approval submitted.

Comment thread src/tracing_layer.rs
}

// for OOMs
let _guard = match CapturingGuard::try_acquire() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

blocking: Prevent background-worker capture feedback — the thread-local guard only covers synchronous enqueue. With a global subscriber, a before_send hook that logs an application-target event runs on the transport worker after this guard expires, enqueues another event, and repeats on subsequent flushes. Application-target filtering does not prevent this loop. Reproduction: reproduced — cargo test --features tracing-subscriber --test review_feedback (focused review regression) observed four captures from one original event with the hook artificially capped at three log emissions; temporary worker suppression made the same test pass.

Comment thread src/tracing_layer.rs
/// use std::sync::Arc;
/// use tracing_subscriber::layer::SubscriberExt;
///
/// let layer = posthog_rs::PostHogLayer::new(Arc::new(client));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

blocking: Make the documentation examples compile — all seven new no_run snippets are compiled by rustdoc, but lack client setup, imports, or an async context. This main example also needs SubscriberInitExt for .init(). Reproduction: reproduced — cargo test --features tracing-subscriber --doc failed all seven new doctests.

Comment thread tests/test_tracing.rs

#[cfg(not(feature = "async-client"))]
{
posthog_rs::client(options)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

blocking: Run blocking-client tests outside the async runtime — this branch constructs the blocking client directly inside #[tokio::test], causing reqwest runtime handling to panic before the tracing behavior is exercised. Use a runtime-free test context or a blocking thread for the blocking-client variant. Reproduction: reproduced — cargo test --no-default-features --features tracing-subscriber --test test_tracing failed all four tests with “Cannot drop a runtime in a context where blocking is not allowed.”

Comment thread examples/README.md
Captures selected `tracing` events as PostHog events:

```bash
cargo run --example tracing --features tracing-subscriber

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

suggestion: Include the documented tracing example — the README points to an example target absent from this PR; add it or replace this command with an available quickstart. Reproduction: reproduced — cargo run --example tracing --features tracing-subscriber failed because no example target named tracing exists.

@marandaneto
marandaneto requested a review from a team September 24, 2026 14:44

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Additional non-blocking integration design and documentation suggestions.

Comment thread src/tracing_layer.rs
S: Subscriber,
{
fn on_event(&self, event: &TracingEvent<'_>, _ctx: Context<'_, S>) {
let metadata = event.metadata();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

suggestion: Preserve tracing severity and source metadata — please consider recording the level, target, module, file, and line under agreed property names. Currently the target is used for naming, but severity and source location are omitted, so otherwise identical INFO and ERROR events cannot be distinguished after capture. Reproduction: reproduced — a focused captures_event_with_field_and_context_fidelity assertion for severity failed, and review_metadata_defaults_and_span_context confirmed the captured properties omit severity and source metadata.

Comment thread src/tracing_layer.rs
/// Tracing fields are stored as PostHog properties, while the existing
/// PostHog client handles batching and delivery.
///
/// Use `tracing_subscriber` filters to control which events are captured.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

suggestion: Document safe capture defaults with an allowlist — without a subscriber filter, this layer captures DEBUG/TRACE events and third-party targets as analytics events. Please show an application-target allowlist in the working quickstart and explain the volume/privacy implications of forwarding arbitrary tracing fields. Reproduction: reproduced — cargo test --features tracing-subscriber --test test_tracing review_metadata_defaults_and_span_context confirmed that an unfiltered layer captures both TRACE and DEBUG events from a third-party target.

Comment thread src/tracing_layer.rs
/// `distinct_id` is read from the tracing fields.
pub fn new(client: Arc<Client>) -> Self {
Self {
client,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

suggestion: Document that parent-span context is not inherited — fields on an enclosing info_span!("request", distinct_id = "user-123", request_id = "...") are not copied into its events; without another identity source, those events become anonymous. Please make this explicit and show the supported identity/context setup. Optional span-field enrichment can be a follow-up without introducing trace export. Reproduction: reproduced — review_metadata_defaults_and_span_context captured events inside such a span and confirmed a generated anonymous identity and no inherited request ID.

Comment thread src/tracing_layer.rs
TargetAndName,
/// Derive the event name from the tracing metadata.
Custom(Arc<dyn Fn(&Metadata<'_>) -> String + Send + Sync>),
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

suggestion: Clarify the boundary of custom event naming — EventNamer::Custom receives only metadata, so it cannot derive a name from an event field or inspect parent-span context. Please document that limitation and consider field-aware mapping only if a concrete use case requires it; prefer the existing before_send hook for transformations it already supports rather than adding an overlapping public callback. Reproduction: not reproduced — this is an API-design suggestion based on the callback signature, not a runtime defect.

@dustinbyrne

Copy link
Copy Markdown
Contributor

hey @ppmpreetham, this is super cool! out of curiousity, have you seen our distributed tracing? some additional information here: https://posthog.com/docs/distributed-tracing/start-here

i wonder if this is a more natural direction to integrate with rust traces? cc @PostHog/team-apm

@marandaneto

Copy link
Copy Markdown
Member

was your intention to add support for https://posthog.com/docs/ai-observability/traces or just capturing events with the tracing_layer subscriber? since we dont capture any traces right now in this PR

@marandaneto
marandaneto requested a review from a team September 24, 2026 14:48
@marandaneto
marandaneto marked this pull request as draft September 24, 2026 14:48
@marandaneto

Copy link
Copy Markdown
Member

moving to draft until this and this is cleared up

@ppmpreetham

Copy link
Copy Markdown
Author

hey @ppmpreetham, this is super cool! out of curiousity, have you seen our distributed tracing? some additional information here: https://posthog.com/docs/distributed-tracing/start-here

i wonder if this is a more natural direction to integrate with rust traces? cc @PostHog/team-apm

hey @dustinbyrne, thank you! I wasn't aware of the distributed tracing.
The intent of this PR is specifically the tracing_subscriber::Layer integration from #205 which is capturing tracing events/logs as PostHog events

I agree distributed tracing could be a more natural direction for Rust traces, but I think that would be a separate, broader piece of work

@ppmpreetham

Copy link
Copy Markdown
Author

hey @marandaneto, This PR is just for capturing events proposed in #205. Trace/AI observability support isn't part of this PR, but could be added later if there's interest.

@marandaneto

Copy link
Copy Markdown
Member

hey @marandaneto, This PR is just for capturing events proposed in #205. Trace/AI observability support isn't part of this PR, but could be added later if there's interest.

i wonder if this should be log events then? https://posthog.com/docs/logs
or do you really need product analytic events? https://posthog.com/docs/product-analytics

whats the problem you are trying to solve in your app? whats the visibility you need?

@ppmpreetham

Copy link
Copy Markdown
Author

The main problem I'm trying to solve is avoiding a separate instrumentation path for applications that already use tracing. It was to let an app select specific tracing events and send them to PostHog with the existing client, rather than having to write its own Visit/Layer integration.

I wasn't solving a specific problem in my own app. I came across #205 and wanted to contribute. I don't have a strong preference between Logs and Product Analytics here, so I'm happy to align the implementation with whichever use case makes more sense for PostHog.

@marandaneto

Copy link
Copy Markdown
Member

The main problem I'm trying to solve is avoiding a separate instrumentation path for applications that already use tracing. It was to let an app select specific tracing events and send them to PostHog with the existing client, rather than having to write its own Visit/Layer integration.

I wasn't solving a specific problem in my own app. I came across #205 and wanted to contribute. I don't have a strong preference between Logs and Product Analytics here, so I'm happy to align the implementation with whichever use case makes more sense for PostHog.

thx will check with APM team and come back to you

@marandaneto
marandaneto requested a review from a team September 25, 2026 06:54
@frankh

frankh commented Sep 25, 2026

Copy link
Copy Markdown

closing this as posthog now supports tracing, we don't want to be sending traces as posthog events

clients should use their existing tracing library to ship otel events, see https://posthog.com/docs/distributed-tracing

@frankh frankh closed this Sep 25, 2026
@ppmpreetham

Copy link
Copy Markdown
Author

thanks!
i'll look into implementing other features at #29

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.

Add a tracing-subscriber Layer integration

4 participants