Skip to content

Commit 532333f

Browse files
GaryPWhiteTommyCpp
andauthored
automatically add traces / metrics paths (#806)
* automatically add traces / metrics paths * Update opentelemetry-otlp/src/span.rs Co-authored-by: Zhongyang Wu <zhongyang.wu@outlook.com> * fmt * merge from upstream * more formatting -- oops * sometimes compilation doesn't use timeout * remove fromstr * ah, import only when using 'grpc-tonic' * formatting for rustdoc links * fmt * _timeout instead of timeout Co-authored-by: Zhongyang Wu <zhongyang.wu@outlook.com>
1 parent 2585d10 commit 532333f

File tree

4 files changed

+77
-57
lines changed

4 files changed

+77
-57
lines changed

opentelemetry-otlp/src/exporter/mod.rs

+18-47
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,17 @@ use crate::Protocol;
1212
use std::str::FromStr;
1313
use std::time::Duration;
1414

15-
/// Target to which the exporter is going to send spans or metrics, defaults to https://localhost:4317.
15+
/// Target to which the exporter is going to send signals, defaults to https://localhost:4317.
16+
/// Learn about the relationship between this constant and metrics/spans/logs at
17+
/// <https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp>
1618
pub const OTEL_EXPORTER_OTLP_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_ENDPOINT";
17-
/// Default target to which the exporter is going to send spans or metrics.
19+
/// Default target to which the exporter is going to send signals.
1820
pub const OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT: &str = "https://localhost:4317";
19-
/// Max waiting time for the backend to process each spans or metrics batch, defaults to 10 seconds.
21+
/// Max waiting time for the backend to process each signal batch, defaults to 10 seconds.
2022
pub const OTEL_EXPORTER_OTLP_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TIMEOUT";
21-
/// Default max waiting time for the backend to process each spans or metrics batch.
23+
/// Default max waiting time for the backend to process each signal batch.
2224
pub const OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT: u64 = 10;
2325

24-
/// Target to which the exporter is going to send spans, defaults to https://localhost:4317.
25-
pub const OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT";
26-
/// Max waiting time for the backend to process each spans batch, defaults to 10s.
27-
pub const OTEL_EXPORTER_OTLP_TRACES_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT";
28-
2926
#[cfg(feature = "grpc-sys")]
3027
pub(crate) mod grpcio;
3128
#[cfg(feature = "http-proto")]
@@ -36,7 +33,7 @@ pub(crate) mod tonic;
3633
/// Configuration for the OTLP exporter.
3734
#[derive(Debug)]
3835
pub struct ExportConfig {
39-
/// The address of the OTLP collector. If not set, the default address is used.
36+
/// The base address of the OTLP collector. If not set, the default address is used.
4037
pub endpoint: String,
4138

4239
/// The protocol to use when communicating with the collector.
@@ -129,18 +126,15 @@ impl<B: HasExportConfig> WithExportConfig for B {
129126
}
130127

131128
fn with_env(mut self) -> Self {
132-
let endpoint = match std::env::var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) {
129+
let endpoint = match std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT) {
133130
Ok(val) => val,
134-
Err(_) => std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT)
135-
.unwrap_or_else(|_| OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT.to_string()),
131+
Err(_) => OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT.to_string(),
136132
};
137133
self.export_config().endpoint = endpoint;
138134

139-
let timeout = match std::env::var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT) {
135+
let timeout = match std::env::var(OTEL_EXPORTER_OTLP_TIMEOUT) {
140136
Ok(val) => u64::from_str(&val).unwrap_or(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
141-
Err(_) => std::env::var(OTEL_EXPORTER_OTLP_TIMEOUT)
142-
.map(|val| u64::from_str(&val).unwrap_or(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT))
143-
.unwrap_or(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT),
137+
Err(_) => OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT,
144138
};
145139
self.export_config().timeout = Duration::from_secs(timeout);
146140
self
@@ -159,17 +153,20 @@ impl<B: HasExportConfig> WithExportConfig for B {
159153
mod tests {
160154
use crate::exporter::{
161155
WithExportConfig, OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_TIMEOUT,
162-
OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
163-
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
156+
OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT,
164157
};
165158
use crate::new_exporter;
166159

167160
#[test]
168-
fn test_pipeline_builder_from_env() {
169-
std::env::set_var(OTEL_EXPORTER_OTLP_ENDPOINT, "https://otlp_endpoint:4317");
161+
fn test_pipeline_builder_from_env_default_vars() {
162+
let expected_endpoint = "https://otlp_endpoint:4317";
163+
std::env::set_var(OTEL_EXPORTER_OTLP_ENDPOINT, expected_endpoint);
170164
std::env::set_var(OTEL_EXPORTER_OTLP_TIMEOUT, "bad_timeout");
171165

172166
let mut exporter_builder = new_exporter().tonic().with_env();
167+
assert_eq!(exporter_builder.exporter_config.endpoint, expected_endpoint);
168+
169+
exporter_builder = new_exporter().tonic().with_env();
173170
assert_eq!(
174171
exporter_builder.exporter_config.timeout,
175172
std::time::Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT)
@@ -187,31 +184,5 @@ mod tests {
187184
std::env::remove_var(OTEL_EXPORTER_OTLP_TIMEOUT);
188185
assert!(std::env::var(OTEL_EXPORTER_OTLP_ENDPOINT).is_err());
189186
assert!(std::env::var(OTEL_EXPORTER_OTLP_TIMEOUT).is_err());
190-
191-
// test from traces env var
192-
std::env::set_var(
193-
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
194-
"https://otlp_traces_endpoint:4317",
195-
);
196-
std::env::set_var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT, "bad_timeout");
197-
198-
let mut exporter_builder = new_exporter().tonic().with_env();
199-
assert_eq!(
200-
exporter_builder.exporter_config.timeout,
201-
std::time::Duration::from_secs(OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT)
202-
);
203-
204-
std::env::set_var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT, "60");
205-
206-
exporter_builder = new_exporter().tonic().with_env();
207-
assert_eq!(
208-
exporter_builder.exporter_config.timeout,
209-
std::time::Duration::from_secs(60)
210-
);
211-
212-
std::env::remove_var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT);
213-
std::env::remove_var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT);
214-
assert!(std::env::var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT).is_err());
215-
assert!(std::env::var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT).is_err());
216187
}
217188
}

opentelemetry-otlp/src/lib.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,21 @@ mod transform;
189189

190190
pub use crate::exporter::ExportConfig;
191191
#[cfg(feature = "trace")]
192-
pub use crate::span::{OtlpTracePipeline, SpanExporter, SpanExporterBuilder};
192+
pub use crate::span::{
193+
OtlpTracePipeline, SpanExporter, SpanExporterBuilder, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
194+
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
195+
};
193196

194197
#[cfg(feature = "metrics")]
195-
pub use crate::metric::{MetricsExporter, OtlpMetricPipeline};
198+
pub use crate::metric::{
199+
MetricsExporter, OtlpMetricPipeline, OTEL_EXPORTER_OTLP_METRICS_ENDPOINT,
200+
OTEL_EXPORTER_OTLP_METRICS_TIMEOUT,
201+
};
196202

197203
pub use crate::exporter::{
198204
HasExportConfig, WithExportConfig, OTEL_EXPORTER_OTLP_ENDPOINT,
199205
OTEL_EXPORTER_OTLP_ENDPOINT_DEFAULT, OTEL_EXPORTER_OTLP_TIMEOUT,
200-
OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT, OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
201-
OTEL_EXPORTER_OTLP_TRACES_TIMEOUT,
206+
OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT,
202207
};
203208

204209
use opentelemetry::sdk::export::ExportError;

opentelemetry-otlp/src/metric.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,25 @@ use opentelemetry_proto::tonic::collector::metrics::v1::{
2525
metrics_service_client::MetricsServiceClient, ExportMetricsServiceRequest,
2626
};
2727
use std::fmt::{Debug, Formatter};
28+
#[cfg(feature = "grpc-tonic")]
29+
use std::str::FromStr;
2830
use std::sync::Arc;
2931
use std::sync::Mutex;
3032
use std::time;
33+
use std::time::Duration;
3134
use tonic::metadata::KeyAndValueRef;
3235
#[cfg(feature = "grpc-tonic")]
3336
use tonic::transport::Channel;
3437
#[cfg(feature = "grpc-tonic")]
3538
use tonic::Request;
3639

40+
/// Target to which the exporter is going to send metrics, defaults to https://localhost:4317/v1/metrics.
41+
/// Learn about the relationship between this constant and default/spans/logs at
42+
/// <https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp>
43+
pub const OTEL_EXPORTER_OTLP_METRICS_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_METRICS_ENDPOINT";
44+
/// Max waiting time for the backend to process each metrics batch, defaults to 10s.
45+
pub const OTEL_EXPORTER_OTLP_METRICS_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_METRICS_TIMEOUT";
46+
3747
impl OtlpPipeline {
3848
/// Create a OTLP metrics pipeline.
3949
pub fn metrics<SP, SO, I, IO>(
@@ -263,8 +273,20 @@ impl MetricsExporter {
263273
mut tonic_config: TonicConfig,
264274
export_selector: T,
265275
) -> Result<MetricsExporter> {
266-
let endpoint =
267-
Channel::from_shared(config.endpoint).map_err::<crate::Error, _>(Into::into)?;
276+
let endpoint = match std::env::var(OTEL_EXPORTER_OTLP_METRICS_ENDPOINT) {
277+
Ok(val) => val,
278+
Err(_) => format!("{}{}", config.endpoint, "/v1/metrics"),
279+
};
280+
281+
let _timeout = match std::env::var(OTEL_EXPORTER_OTLP_METRICS_TIMEOUT) {
282+
Ok(val) => match u64::from_str(&val) {
283+
Ok(seconds) => Duration::from_secs(seconds),
284+
Err(_) => config.timeout,
285+
},
286+
Err(_) => config.timeout,
287+
};
288+
289+
let endpoint = Channel::from_shared(endpoint).map_err::<crate::Error, _>(Into::into)?;
268290

269291
#[cfg(all(feature = "tls"))]
270292
let channel = match tonic_config.tls_config {
@@ -273,7 +295,7 @@ impl MetricsExporter {
273295
.map_err::<crate::Error, _>(Into::into)?,
274296
None => endpoint,
275297
}
276-
.timeout(config.timeout)
298+
.timeout(_timeout)
277299
.connect_lazy();
278300

279301
#[cfg(not(feature = "tls"))]

opentelemetry-otlp/src/span.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use std::fmt::{self, Debug};
66
use std::time::Duration;
77

8+
#[cfg(feature = "grpc-tonic")]
9+
use std::str::FromStr;
810
#[cfg(feature = "grpc-tonic")]
911
use {
1012
crate::exporter::tonic::{TonicConfig, TonicExporterBuilder},
@@ -63,6 +65,13 @@ use opentelemetry::{
6365

6466
use async_trait::async_trait;
6567

68+
/// Target to which the exporter is going to send spans, defaults to https://localhost:4317/v1/traces.
69+
/// Learn about the relationship between this constant and default/metrics/logs at
70+
/// <https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp>
71+
pub const OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT";
72+
/// Max waiting time for the backend to process each spans batch, defaults to 10s.
73+
pub const OTEL_EXPORTER_OTLP_TRACES_TIMEOUT: &str = "OTEL_EXPORTER_OTLP_TRACES_TIMEOUT";
74+
6675
impl OtlpPipeline {
6776
/// Create a OTLP tracing pipeline.
6877
pub fn tracing(self) -> OtlpTracePipeline {
@@ -313,18 +322,31 @@ impl SpanExporter {
313322
config: ExportConfig,
314323
tonic_config: TonicConfig,
315324
) -> Result<Self, crate::Error> {
316-
let endpoint = TonicChannel::from_shared(config.endpoint.clone())?;
325+
let endpoint_str = match std::env::var(OTEL_EXPORTER_OTLP_TRACES_ENDPOINT) {
326+
Ok(val) => val,
327+
Err(_) => format!("{}{}", config.endpoint, "/v1/traces"),
328+
};
329+
330+
let endpoint = TonicChannel::from_shared(endpoint_str)?;
331+
332+
let _timeout = match std::env::var(OTEL_EXPORTER_OTLP_TRACES_TIMEOUT) {
333+
Ok(val) => match u64::from_str(&val) {
334+
Ok(seconds) => Duration::from_secs(seconds),
335+
Err(_) => config.timeout,
336+
},
337+
Err(_) => config.timeout,
338+
};
317339

318340
#[cfg(feature = "tls")]
319341
let channel = match tonic_config.tls_config.as_ref() {
320342
Some(tls_config) => endpoint.tls_config(tls_config.clone())?,
321343
None => endpoint,
322344
}
323-
.timeout(config.timeout)
345+
.timeout(_timeout)
324346
.connect_lazy();
325347

326348
#[cfg(not(feature = "tls"))]
327-
let channel = endpoint.timeout(config.timeout).connect_lazy();
349+
let channel = endpoint.timeout(_timeout).connect_lazy();
328350

329351
SpanExporter::from_tonic_channel(config, tonic_config, channel)
330352
}

0 commit comments

Comments
 (0)