Repository navigation
Feat: Added Tracing - #265
ppmpreetham wants to merge 11 commits into
Conversation
marandaneto
left a comment
There was a problem hiding this comment.
Review findings from the validated snapshot. No approval submitted.
| } | ||
|
|
||
| // for OOMs | ||
| let _guard = match CapturingGuard::try_acquire() { |
There was a problem hiding this comment.
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.
| /// use std::sync::Arc; | ||
| /// use tracing_subscriber::layer::SubscriberExt; | ||
| /// | ||
| /// let layer = posthog_rs::PostHogLayer::new(Arc::new(client)); |
There was a problem hiding this comment.
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.
|
|
||
| #[cfg(not(feature = "async-client"))] | ||
| { | ||
| posthog_rs::client(options) |
There was a problem hiding this comment.
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.”
| Captures selected `tracing` events as PostHog events: | ||
|
|
||
| ```bash | ||
| cargo run --example tracing --features tracing-subscriber |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Additional non-blocking integration design and documentation suggestions.
| S: Subscriber, | ||
| { | ||
| fn on_event(&self, event: &TracingEvent<'_>, _ctx: Context<'_, S>) { | ||
| let metadata = event.metadata(); |
There was a problem hiding this comment.
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.
| /// 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. |
There was a problem hiding this comment.
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.
| /// `distinct_id` is read from the tracing fields. | ||
| pub fn new(client: Arc<Client>) -> Self { | ||
| Self { | ||
| client, |
There was a problem hiding this comment.
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.
| TargetAndName, | ||
| /// Derive the event name from the tracing metadata. | ||
| Custom(Arc<dyn Fn(&Metadata<'_>) -> String + Send + Sync>), | ||
| } |
There was a problem hiding this comment.
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.
|
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 |
|
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 |
hey @dustinbyrne, thank you! I wasn't aware of the distributed tracing. 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 |
|
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 whats the problem you are trying to solve in your app? whats the visibility you need? |
|
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 |
|
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 |
|
thanks! |
💡 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:
📝 Checklist
If releasing new changes
sampo addto generate a changeset file