Skip to content

Commit

Permalink
feat: add InstrumentationLibrary to should_sample parameters. (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyCpp authored Jan 10, 2022
1 parent a914409 commit 9f3abed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
16 changes: 15 additions & 1 deletion opentelemetry/src/sdk/trace/sampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
//! MUST NOT allow this combination.

use crate::{
sdk::InstrumentationLibrary,
trace::{Link, SpanKind, TraceContextExt, TraceId, TraceState},
Context, KeyValue,
};
Expand All @@ -58,6 +59,7 @@ pub trait ShouldSample: Send + Sync + std::fmt::Debug {
span_kind: &SpanKind,
attributes: &[KeyValue],
links: &[Link],
instrumentation_library: &InstrumentationLibrary,
) -> SamplingResult;
}

Expand Down Expand Up @@ -109,6 +111,7 @@ impl ShouldSample for Sampler {
span_kind: &SpanKind,
attributes: &[KeyValue],
links: &[Link],
instrumentation_library: &InstrumentationLibrary,
) -> SamplingResult {
let decision = match self {
// Always sample the trace
Expand All @@ -119,7 +122,15 @@ impl ShouldSample for Sampler {
Sampler::ParentBased(delegate_sampler) => {
parent_context.filter(|cx| cx.has_active_span()).map_or(
delegate_sampler
.should_sample(parent_context, trace_id, name, span_kind, attributes, links)
.should_sample(
parent_context,
trace_id,
name,
span_kind,
attributes,
links,
instrumentation_library,
)
.decision,
|ctx| {
let span = ctx.span();
Expand Down Expand Up @@ -253,6 +264,7 @@ mod tests {
&SpanKind::Internal,
&[],
&[],
&InstrumentationLibrary::default(),
)
.decision
== SamplingDecision::RecordAndSample
Expand Down Expand Up @@ -286,13 +298,15 @@ mod tests {
fn filter_parent_sampler_for_active_spans() {
let sampler = Sampler::ParentBased(Box::new(Sampler::AlwaysOn));
let cx = Context::current_with_value("some_value");
let instrumentation_library = InstrumentationLibrary::default();
let result = sampler.should_sample(
Some(&cx),
TraceId::from_u128(1),
"should sample",
&SpanKind::Internal,
&[],
&[],
&instrumentation_library,
);

assert_eq!(result.decision, SamplingDecision::RecordAndSample);
Expand Down
5 changes: 5 additions & 0 deletions opentelemetry/src/sdk/trace/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl Tracer {
attributes: &[KeyValue],
links: &[Link],
config: &Config,
instrumentation_library: &InstrumentationLibrary,
) -> Option<(TraceFlags, Vec<KeyValue>, TraceState)> {
let sampling_result = config.sampler.should_sample(
Some(parent_cx),
Expand All @@ -84,6 +85,7 @@ impl Tracer {
span_kind,
attributes,
links,
instrumentation_library,
);

self.process_sampling_result(sampling_result, parent_cx)
Expand Down Expand Up @@ -216,6 +218,7 @@ impl crate::trace::Tracer for Tracer {
&attribute_options,
link_options.as_deref().unwrap_or(&[]),
provider.config(),
&self.instrumentation_lib,
)
} else {
// has parent that is local: use parent if sampled, or don't record.
Expand Down Expand Up @@ -323,6 +326,7 @@ mod tests {
sdk::{
self,
trace::{Config, Sampler, SamplingDecision, SamplingResult, ShouldSample},
InstrumentationLibrary,
},
testing::trace::TestSpan,
trace::{
Expand All @@ -344,6 +348,7 @@ mod tests {
_span_kind: &SpanKind,
_attributes: &[KeyValue],
_links: &[Link],
_instrumentation_library: &InstrumentationLibrary,
) -> SamplingResult {
let trace_state = parent_context
.unwrap()
Expand Down

0 comments on commit 9f3abed

Please sign in to comment.