Skip to content

Commit 45edb11

Browse files
committed
chore(tracer): clean up span builder api
1 parent c4f59da commit 45edb11

File tree

4 files changed

+26
-100
lines changed

4 files changed

+26
-100
lines changed

opentelemetry-sdk/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
## vNext
44

5-
### Added
65
- Added `Resource::get_ref(&self, key: &Key) -> Option<&Value>` to allow retrieving a reference to a resource value without cloning.
6+
- **Breaking** Removed the following public hidden methods from the `SdkTracer` [#3227][3227]:
7+
- `id_generator`, `should_sample`
8+
9+
[3227]: https://github.com/open-telemetry/opentelemetry-rust/pull/3227
710

811
## 0.31.0
912

opentelemetry-sdk/src/trace/tracer.rs

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
use crate::trace::{
1111
provider::SdkTracerProvider,
1212
span::{Span, SpanData},
13-
IdGenerator, ShouldSample, SpanEvents, SpanLimits, SpanLinks,
13+
SpanEvents, SpanLimits, SpanLinks,
1414
};
1515
use opentelemetry::{
1616
trace::{
17-
SamplingDecision, Span as _, SpanBuilder, SpanContext, SpanKind, TraceContextExt,
17+
SamplingDecision, Span as _, SpanBuilder, SpanContext, SpanKind, Status, TraceContextExt,
1818
TraceFlags,
1919
},
2020
Context, InstrumentationScope, KeyValue,
@@ -105,14 +105,11 @@ impl SdkTracer {
105105
let SpanBuilder {
106106
name,
107107
start_time,
108-
end_time,
109108
events,
110-
status,
111109
..
112110
} = builder;
113111

114112
let start_time = start_time.unwrap_or_else(opentelemetry::time::now);
115-
let end_time = end_time.unwrap_or(start_time);
116113
let spans_events_limit = span_limits.max_events_per_span as usize;
117114
let span_events: SpanEvents = if let Some(mut events) = events {
118115
let dropped_count = events.len().saturating_sub(spans_events_limit);
@@ -141,33 +138,17 @@ impl SdkTracer {
141138
span_kind: builder.span_kind.take().unwrap_or(SpanKind::Internal),
142139
name,
143140
start_time,
144-
end_time,
141+
end_time: start_time,
145142
attributes: attribute_options,
146143
dropped_attributes_count,
147144
events: span_events,
148145
links: span_links,
149-
status,
146+
status: Status::default(),
150147
}),
151148
self.clone(),
152149
span_limits,
153150
)
154151
}
155-
156-
/// The [`IdGenerator`] associated with this tracer.
157-
///
158-
// Note: this is necessary for tracing-opentelemetry's `PreSampledTracer`.
159-
#[doc(hidden)]
160-
pub fn id_generator(&self) -> &dyn IdGenerator {
161-
&*self.provider.config().id_generator
162-
}
163-
164-
/// The [`ShouldSample`] associated with this tracer.
165-
///
166-
// Note: this is necessary for tracing-opentelemetry's `PreSampledTracer`.
167-
#[doc(hidden)]
168-
pub fn should_sample(&self) -> &dyn ShouldSample {
169-
&*self.provider.config().sampler
170-
}
171152
}
172153

173154
impl opentelemetry::trace::Tracer for SdkTracer {
@@ -181,7 +162,7 @@ impl opentelemetry::trace::Tracer for SdkTracer {
181162
/// trace. A span is said to be a _root span_ if it does not have a parent. Each
182163
/// trace includes a single root span, which is the shared ancestor of all other
183164
/// spans in the trace.
184-
fn build_with_context(&self, mut builder: SpanBuilder, parent_cx: &Context) -> Self::Span {
165+
fn build_with_context(&self, builder: SpanBuilder, parent_cx: &Context) -> Self::Span {
185166
if parent_cx.is_telemetry_suppressed() {
186167
return Span::new(
187168
SpanContext::empty_context(),
@@ -203,10 +184,7 @@ impl opentelemetry::trace::Tracer for SdkTracer {
203184
}
204185

205186
let config = provider.config();
206-
let span_id = builder
207-
.span_id
208-
.take()
209-
.unwrap_or_else(|| config.id_generator.new_span_id());
187+
let span_id = config.id_generator.new_span_id();
210188
let trace_id;
211189
let mut psc = &SpanContext::empty_context();
212190

@@ -221,25 +199,17 @@ impl opentelemetry::trace::Tracer for SdkTracer {
221199
trace_id = sc.trace_id();
222200
psc = sc;
223201
} else {
224-
trace_id = builder
225-
.trace_id
226-
.unwrap_or_else(|| config.id_generator.new_trace_id());
202+
trace_id = config.id_generator.new_trace_id();
227203
};
228204

229-
// In order to accommodate use cases like `tracing-opentelemetry` we there is the ability
230-
// to use pre-sampling. Otherwise, the standard method of sampling is followed.
231-
let samplings_result = if let Some(sr) = builder.sampling_result.take() {
232-
sr
233-
} else {
234-
config.sampler.should_sample(
235-
Some(parent_cx),
236-
trace_id,
237-
&builder.name,
238-
builder.span_kind.as_ref().unwrap_or(&SpanKind::Internal),
239-
builder.attributes.as_ref().unwrap_or(&Vec::new()),
240-
builder.links.as_deref().unwrap_or(&[]),
241-
)
242-
};
205+
let samplings_result = config.sampler.should_sample(
206+
Some(parent_cx),
207+
trace_id,
208+
&builder.name,
209+
builder.span_kind.as_ref().unwrap_or(&SpanKind::Internal),
210+
builder.attributes.as_ref().unwrap_or(&Vec::new()),
211+
builder.links.as_deref().unwrap_or(&[]),
212+
);
243213

244214
let trace_flags = parent_cx.span().span_context().trace_flags();
245215
let trace_state = samplings_result.trace_state;

opentelemetry/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
## vNext
44

55
- Add `reserve` method to `opentelemetry::propagation::Injector` to hint at the number of elements that will be added to avoid multiple resize operations of the underlying data structure. Has an empty default implementation.
6+
- **Breaking** Removed the following public fields and methods from the `SpanBuilder` [#3227][3227]:
7+
- `trace_id`, `span_id`, `end_time`, `status`, `sampling_result`
8+
- `with_trace_id`, `with_span_id`, `with_end_time`, `with_status`, `with_sampling_result`
9+
10+
[3227]: https://github.com/open-telemetry/opentelemetry-rust/pull/3227
611

712
## v0.31.0
813

opentelemetry/src/trace/tracer.rs

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
trace::{Event, Link, Span, SpanKind, Status, TraceContextExt, TraceState},
3-
Context, KeyValue, SpanId, TraceId,
2+
trace::{Event, Link, Span, SpanKind, TraceContextExt, TraceState},
3+
Context, KeyValue,
44
};
55
use std::borrow::Cow;
66
use std::time::SystemTime;
@@ -240,12 +240,6 @@ pub trait Tracer {
240240
/// ```
241241
#[derive(Clone, Debug, Default)]
242242
pub struct SpanBuilder {
243-
/// Trace id, useful for integrations with external tracing systems.
244-
pub trace_id: Option<TraceId>,
245-
246-
/// Span id, useful for integrations with external tracing systems.
247-
pub span_id: Option<SpanId>,
248-
249243
/// Span kind
250244
pub span_kind: Option<SpanKind>,
251245

@@ -255,9 +249,6 @@ pub struct SpanBuilder {
255249
/// Span start time
256250
pub start_time: Option<SystemTime>,
257251

258-
/// Span end time
259-
pub end_time: Option<SystemTime>,
260-
261252
/// Span attributes that are provided at the span creation time.
262253
/// More attributes can be added afterwards.
263254
/// Providing duplicate keys will result in multiple attributes
@@ -269,12 +260,6 @@ pub struct SpanBuilder {
269260

270261
/// Span Links
271262
pub links: Option<Vec<Link>>,
272-
273-
/// Span status
274-
pub status: Status,
275-
276-
/// Sampling result
277-
pub sampling_result: Option<SamplingResult>,
278263
}
279264

280265
/// SpanBuilder methods
@@ -287,22 +272,6 @@ impl SpanBuilder {
287272
}
288273
}
289274

290-
/// Specify trace id to use if no parent context exists
291-
pub fn with_trace_id(self, trace_id: TraceId) -> Self {
292-
SpanBuilder {
293-
trace_id: Some(trace_id),
294-
..self
295-
}
296-
}
297-
298-
/// Assign span id
299-
pub fn with_span_id(self, span_id: SpanId) -> Self {
300-
SpanBuilder {
301-
span_id: Some(span_id),
302-
..self
303-
}
304-
}
305-
306275
/// Assign span kind
307276
pub fn with_kind(self, span_kind: SpanKind) -> Self {
308277
SpanBuilder {
@@ -319,14 +288,6 @@ impl SpanBuilder {
319288
}
320289
}
321290

322-
/// Assign span end time
323-
pub fn with_end_time<T: Into<SystemTime>>(self, end_time: T) -> Self {
324-
SpanBuilder {
325-
end_time: Some(end_time.into()),
326-
..self
327-
}
328-
}
329-
330291
/// Assign span attributes from an iterable.
331292
/// Providing duplicate keys will result in multiple attributes
332293
/// with the same key, as there is no de-duplication performed.
@@ -357,19 +318,6 @@ impl SpanBuilder {
357318
}
358319
}
359320

360-
/// Assign status code
361-
pub fn with_status(self, status: Status) -> Self {
362-
SpanBuilder { status, ..self }
363-
}
364-
365-
/// Assign sampling result
366-
pub fn with_sampling_result(self, sampling_result: SamplingResult) -> Self {
367-
SpanBuilder {
368-
sampling_result: Some(sampling_result),
369-
..self
370-
}
371-
}
372-
373321
/// Builds a span with the given tracer from this configuration.
374322
pub fn start<T: Tracer>(self, tracer: &T) -> T::Span {
375323
Context::map_current(|cx| tracer.build_with_context(self, cx))

0 commit comments

Comments
 (0)