Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 56ba6a3

Browse files
authoredNov 3, 2024··
Merge branch 'main' into move-errors-to-sdk
2 parents a047fad + fcd7cae commit 56ba6a3

File tree

2 files changed

+58
-31
lines changed

2 files changed

+58
-31
lines changed
 

‎opentelemetry-sdk/src/trace/config.rs

+31-22
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! can be set for the default OpenTelemetry limits and Sampler.
55
use crate::trace::{span_limit::SpanLimits, IdGenerator, RandomIdGenerator, Sampler, ShouldSample};
66
use crate::Resource;
7-
use opentelemetry::global::{handle_error, Error};
7+
use opentelemetry::otel_warn;
88
use std::borrow::Cow;
99
use std::env;
1010
use std::str::FromStr;
@@ -125,13 +125,14 @@ impl Default for Config {
125125
"always_on" => Box::new(Sampler::AlwaysOn),
126126
"always_off" => Box::new(Sampler::AlwaysOff),
127127
"traceidratio" => {
128-
let ratio = sampler_arg.and_then(|r| r.parse::<f64>().ok());
128+
let ratio = sampler_arg.as_ref().and_then(|r| r.parse::<f64>().ok());
129129
if let Some(r) = ratio {
130130
Box::new(Sampler::TraceIdRatioBased(r))
131131
} else {
132-
handle_error(
133-
Error::Other(String::from(
134-
"Missing or invalid OTEL_TRACES_SAMPLER_ARG value. Falling back to default: 1.0"))
132+
otel_warn!(
133+
name: "TracerProvider.Config.InvalidSamplerArgument",
134+
message = "OTEL_TRACES_SAMPLER is set to 'traceidratio' but OTEL_TRACES_SAMPLER_ARG environment variable is missing or invalid. OTEL_TRACES_SAMPLER_ARG must be a valid float between 0.0 and 1.0 representing the desired sampling probability (0.0 = no traces sampled, 1.0 = all traces sampled, 0.5 = 50% of traces sampled). Falling back to default ratio: 1.0 (100% sampling)",
135+
otel_traces_sampler_arg = format!("{:?}", sampler_arg)
135136
);
136137
Box::new(Sampler::TraceIdRatioBased(1.0))
137138
}
@@ -143,43 +144,51 @@ impl Default for Config {
143144
Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOff)))
144145
}
145146
"parentbased_traceidratio" => {
146-
let ratio = sampler_arg.and_then(|r| r.parse::<f64>().ok());
147+
let ratio = sampler_arg.as_ref().and_then(|r| r.parse::<f64>().ok());
147148
if let Some(r) = ratio {
148149
Box::new(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(
149150
r,
150151
))))
151152
} else {
152-
handle_error(
153-
Error::Other(String::from(
154-
"Missing or invalid OTEL_TRACES_SAMPLER_ARG value. Falling back to default: 1.0"
155-
)));
153+
otel_warn!(
154+
name: "TracerProvider.Config.InvalidSamplerArgument",
155+
message = "OTEL_TRACES_SAMPLER is set to 'parentbased_traceidratio' but OTEL_TRACES_SAMPLER_ARG environment variable is missing or invalid. OTEL_TRACES_SAMPLER_ARG must be a valid float between 0.0 and 1.0 representing the desired sampling probability (0.0 = no traces sampled, 1.0 = all traces sampled, 0.5 = 50% of traces sampled). Falling back to default ratio: 1.0 (100% sampling)",
156+
otel_traces_sampler_arg = format!("{:?}", sampler_arg)
157+
);
156158
Box::new(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(
157159
1.0,
158160
))))
159161
}
160162
}
161163
"parentbased_jaeger_remote" => {
162-
handle_error(
163-
Error::Other(String::from(
164-
"Unimplemented parentbased_jaeger_remote sampler. Falling back to default: parentbased_always_on"
165-
)));
164+
otel_warn!(
165+
name: "TracerProvider.Config.UnsupportedSampler",
166+
message = "OTEL_TRACES_SAMPLER is set to 'parentbased_jaeger_remote' which is not implemented in this SDK version. Using fallback sampler: ParentBased(AlwaysOn). Configure an alternative sampler using OTEL_TRACES_SAMPLER"
167+
);
166168
Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
167169
}
168170
"jaeger_remote" => {
169-
handle_error(
170-
Error::Other(String::from("Unimplemented jaeger_remote sampler. Falling back to default: parentbased_always_on")));
171+
otel_warn!(
172+
name: "TracerProvider.Config.UnsupportedSampler",
173+
message = "OTEL_TRACES_SAMPLER is set to 'jaeger_remote' which is implemented in this SDK version. Using fallback sampler: ParentBased(AlwaysOn). Configure an alternative sampler using OTEL_TRACES_SAMPLER"
174+
);
171175
Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
172176
}
173177
"xray" => {
174-
handle_error(
175-
Error::Other(String::from("Unimplemented xray sampler. Falling back to default: parentbased_always_on")));
178+
otel_warn!(
179+
name: "TracerProvider.Config.UnsupportedSampler",
180+
message = "OTEL_TRACES_SAMPLER is set to 'xray'. AWS X-Ray sampler is not implemented in this SDK version. Using fallback sampler: ParentBased(AlwaysOn). Configure an alternative sampler using OTEL_TRACES_SAMPLER"
181+
);
176182
Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
177183
}
178184
s => {
179-
handle_error(
180-
Error::Other(format!("Unrecognised OTEL_TRACES_SAMPLER value: {}. Falling back to default: parentbased_always_on",
181-
s
182-
)));
185+
otel_warn!(
186+
name: "TracerProvider.Config.InvalidSamplerType",
187+
message = format!(
188+
"Unrecognized sampler type '{}' in OTEL_TRACES_SAMPLER environment variable. Valid values are: always_on, always_off, traceidratio, parentbased_always_on, parentbased_always_off, parentbased_traceidratio. Using fallback sampler: ParentBased(AlwaysOn)",
189+
s
190+
),
191+
);
183192
Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
184193
}
185194
}

‎opentelemetry-sdk/src/trace/span_processor.rs

+27-9
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use futures_util::{
4545
stream::{self, FusedStream, FuturesUnordered},
4646
StreamExt as _,
4747
};
48-
use opentelemetry::global;
48+
use opentelemetry::{otel_debug, otel_error};
4949
use opentelemetry::{
5050
trace::{TraceError, TraceResult},
5151
Context,
@@ -134,7 +134,11 @@ impl SpanProcessor for SimpleSpanProcessor {
134134
.and_then(|mut exporter| futures_executor::block_on(exporter.export(vec![span])));
135135

136136
if let Err(err) = result {
137-
global::handle_error(err);
137+
// TODO: check error type, and log `error` only if the error is user-actiobable, else log `debug`
138+
otel_debug!(
139+
name: "SimpleProcessor.OnEnd.Error",
140+
reason = format!("{:?}", err)
141+
);
138142
}
139143
}
140144

@@ -246,7 +250,10 @@ impl<R: RuntimeChannel> SpanProcessor for BatchSpanProcessor<R> {
246250
let result = self.message_sender.try_send(BatchMessage::ExportSpan(span));
247251

248252
if let Err(err) = result {
249-
global::handle_error(TraceError::Other(err.into()));
253+
otel_debug!(
254+
name: "BatchSpanProcessor.OnEnd.ExportQueueingFailed",
255+
reason = format!("{:?}", TraceError::Other(err.into()))
256+
);
250257
}
251258
}
252259

@@ -313,14 +320,22 @@ impl<R: RuntimeChannel> BatchSpanProcessorInternal<R> {
313320
let result = export_task.await;
314321

315322
if let Some(channel) = res_channel {
323+
// If a response channel is provided, attempt to send the export result through it.
316324
if let Err(result) = channel.send(result) {
317-
global::handle_error(TraceError::from(format!(
318-
"failed to send flush result: {:?}",
319-
result
320-
)));
325+
otel_debug!(
326+
name: "BatchSpanProcessor.Flush.SendResultError",
327+
reason = format!("{:?}", result)
328+
);
321329
}
322330
} else if let Err(err) = result {
323-
global::handle_error(err);
331+
// If no channel is provided and the export operation encountered an error,
332+
// log the error directly here.
333+
// TODO: Consider returning the status instead of logging it.
334+
otel_error!(
335+
name: "BatchSpanProcessor.Flush.ExportError",
336+
reason = format!("{:?}", err),
337+
message = "Failed during the export process"
338+
);
324339
}
325340

326341
Ok(())
@@ -354,7 +369,10 @@ impl<R: RuntimeChannel> BatchSpanProcessorInternal<R> {
354369
let export_task = self.export();
355370
let task = async move {
356371
if let Err(err) = export_task.await {
357-
global::handle_error(err);
372+
otel_error!(
373+
name: "BatchSpanProcessor.Export.Error",
374+
reason = format!("{}", err)
375+
);
358376
}
359377

360378
Ok(())

0 commit comments

Comments
 (0)
Please sign in to comment.