Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opentelemetry: Add PreSampledTracer interface #962

Merged
merged 4 commits into from
Sep 1, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add trait docs
  • Loading branch information
jtescher committed Sep 1, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit ab381907f7762904ee71006855c2ef9560d379d0
2 changes: 1 addition & 1 deletion tracing-opentelemetry/src/layer.rs
Original file line number Diff line number Diff line change
@@ -511,7 +511,7 @@ mod tests {
self.invalid()
}
}

impl PreSampledTracer for TestTracer {
fn sampled_span_context(&self, _builder: &mut api::SpanBuilder) -> api::SpanContext {
api::SpanContext::empty_context()
29 changes: 23 additions & 6 deletions tracing-opentelemetry/src/tracer.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
use opentelemetry::{api, sdk};

/// A protocol for OpenTelemetry [`Tracer`]s that are capable of producing
/// sampled span contexts _before_ starting their associated spans.
/// An interface for authors of OpenTelemetry SDKs to build pre-sampled tracers.
///
/// This enables interoperability between `tracing` and `opentelemetry` by
/// allowing otel trace ids to be associated _after_ a `tracing` span has been
/// created. See the [`OpenTelemetrySpanExt`] in this crate for usage examples.
/// The OpenTelemetry spec does not allow trace ids to be updated after a span
/// has been created. In order to associate extracted parent trace ids with
/// existing `tracing` spans, `tracing-opentelemetry` builds up otel span data
/// using a [`SpanBuilder`] instead, and creates / exports full otel spans only
/// when the associated `tracing` span is closed. However, in order to properly
/// inject otel [`SpanContext`] information to downstream requests, the sampling
/// state must now be known _before_ the otel span has been created.
///
/// The logic for coming to a sampling decision and creating an injectable span
/// context from a [`SpanBuilder`] is encapsulated in the
/// [`PreSampledTracer::sampled_span_context`] method and has been implemented
/// for the standard OpenTelemetry SDK, but this trait may be implemented by
/// authors of alternate OpenTelemetry SDK implementations if they wish to have
/// `tracing` compatibility.
///
/// See the [`OpenTelemetrySpanExt::set_parent`] and
/// [`OpenTelemetrySpanExt::context`] methods for example usage.
///
/// [`Tracer`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/tracer/trait.Tracer.html
/// [`OpenTelemetrySpanExt`]: trait.OpenTelemetrySpanExt.html
/// [`SpanBuilder`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/tracer/struct.SpanBuilder.html
/// [`SpanContext`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span_context/struct.SpanContext.html
/// [`PreSampledTracer::sampled_span_context`]: trait.PreSampledTracer.html#tymethod.sampled_span_context
/// [`OpenTelemetrySpanExt::set_parent`]: trait.OpenTelemetrySpanExt.html#tymethod.set_parent
/// [`OpenTelemetrySpanExt::context`]: trait.OpenTelemetrySpanExt.html#tymethod.context
pub trait PreSampledTracer {
/// Produce a pre-sampled span context for the given span builder.
fn sampled_span_context(&self, builder: &mut api::SpanBuilder) -> api::SpanContext;