Skip to content

Commit 5200848

Browse files
committed
fix: remove dead code
1 parent 8a9b6e5 commit 5200848

File tree

4 files changed

+32
-79
lines changed

4 files changed

+32
-79
lines changed

Dockerfile.otlp-export

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
FROM rust:1.81
22

3-
COPY ./ ./
3+
COPY Cargo.toml Cargo.toml
4+
COPY Cargo.lock Cargo.lock
5+
COPY ./src/ ./src/
46

5-
RUN cargo run --example otlp-export --manifest-path ./Cargo.toml
7+
RUN cargo build --release --locked
8+
9+
COPY ./examples/build-helper.rs ./examples/build-helper.rs
10+
11+
RUN cargo build --example build-helper --release --locked
12+
13+
COPY ./examples/otlp-export.rs ./examples/otlp-export.rs
14+
15+
RUN cargo build --example otlp-export --release --locked
16+
17+
CMD ./target/release/examples/otlp-export

examples/build-helper.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use init4_bin_base::init4;
2+
use std::sync::{atomic::AtomicBool, Arc};
3+
4+
fn main() {
5+
let term: Arc<AtomicBool> = Default::default();
6+
7+
let _ = signal_hook::flag::register(signal_hook::consts::SIGINT, Arc::clone(&term));
8+
9+
init4();
10+
}

examples/otlp-export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! - 1 span every 5 seconds
66
//! - 1 event every 5 seconds
77
//!
8-
//! It can be killed via
8+
//! It can be killed via sigint or sigterm
99
1010
use init4_bin_base::{
1111
deps::tracing::{info, info_span},
@@ -23,7 +23,6 @@ async fn main() -> Result<(), std::io::Error> {
2323
signal_hook::flag::register(signal_hook::consts::SIGINT, Arc::clone(&term))?;
2424

2525
let _guard = init4();
26-
2726
let mut counter = 0;
2827
let _outer = info_span!("outer span").entered();
2928

@@ -37,5 +36,6 @@ async fn main() -> Result<(), std::io::Error> {
3736
}
3837

3938
info!("signal received, shutting down");
39+
4040
Ok(())
4141
}

src/utils/otlp.rs

Lines changed: 6 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::utils::from_env::{FromEnv, FromEnvErr, FromEnvVar};
12
use opentelemetry::{trace::TracerProvider, KeyValue};
23
use opentelemetry_sdk::trace::SdkTracerProvider;
34
use opentelemetry_sdk::Resource;
@@ -10,12 +11,7 @@ use tracing::level_filters::LevelFilter;
1011
use tracing_subscriber::Layer;
1112
use url::Url;
1213

13-
use crate::utils::from_env::{FromEnv, FromEnvErr, FromEnvVar};
14-
15-
use super::from_env::parse_env_if_present;
16-
17-
const OTEL_ENDPOINT: &str = "OTEL_ENDPOINT";
18-
const OTEL_PROTOCOL: &str = "OTEL_PROTOCOL";
14+
const OTEL_ENDPOINT: &str = "OTEL_EXPORTER_OTLP_ENDPOINT";
1915
const OTEL_LEVEL: &str = "OTEL_LEVEL";
2016
const OTEL_TIMEOUT: &str = "OTEL_TIMEOUT";
2117
const OTEL_ENVIRONMENT: &str = "OTEL_ENVIRONMENT_NAME";
@@ -65,18 +61,6 @@ impl Drop for OtelGuard {
6561
}
6662
}
6763

68-
/// OTLP protocol choices
69-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
70-
pub enum OtlpProtocols {
71-
/// GRPC.
72-
Grpc,
73-
/// Binary.
74-
Binary,
75-
/// JSON.
76-
#[default]
77-
Json,
78-
}
79-
8064
/// Otlp parse error.
8165
#[derive(Debug, Clone, PartialEq, Eq)]
8266
pub struct OtlpParseError(String);
@@ -95,45 +79,12 @@ impl core::fmt::Display for OtlpParseError {
9579

9680
impl core::error::Error for OtlpParseError {}
9781

98-
impl FromEnvVar for OtlpProtocols {
99-
type Error = OtlpParseError;
100-
101-
fn from_env_var(env_var: &str) -> Result<Self, FromEnvErr<Self::Error>> {
102-
parse_env_if_present(env_var)
103-
}
104-
}
105-
106-
impl std::str::FromStr for OtlpProtocols {
107-
type Err = OtlpParseError;
108-
109-
fn from_str(s: &str) -> Result<Self, Self::Err> {
110-
match s {
111-
s if s.eq_ignore_ascii_case("grpc") => Ok(Self::Grpc),
112-
s if s.eq_ignore_ascii_case("binary") => Ok(Self::Binary),
113-
s if s.eq_ignore_ascii_case("json") => Ok(Self::Json),
114-
_ => Err(OtlpParseError(format!("Invalid protocol: {}", s))),
115-
}
116-
}
117-
}
118-
119-
impl From<OtlpProtocols> for opentelemetry_otlp::Protocol {
120-
fn from(protocol: OtlpProtocols) -> Self {
121-
match protocol {
122-
OtlpProtocols::Grpc => Self::Grpc,
123-
OtlpProtocols::Binary => Self::HttpBinary,
124-
OtlpProtocols::Json => Self::HttpJson,
125-
}
126-
}
127-
}
128-
12982
/// Otel configuration. This struct is intended to be loaded from the env vars
13083
///
13184
/// The env vars it checks are:
132-
/// - OTEL_ENDPOINT - optional. The endpoint to send traces to, should be some
133-
/// valid URL. If not specified, then [`OtelConfig::load`] will return
134-
/// [`None`].
135-
/// - OTEL_PROTOCOL - optional. Specifies the OTLP protocol to use, should be
136-
/// one of "grpc", "binary" or "json". Defaults to json.
85+
/// - `OTEL_EXPORTER_OTLP_ENDPOINT` - optional. The endpoint to send traces to,
86+
/// should be some valid URL. If not specified, then [`OtelConfig::load`]
87+
/// will return [`None`].
13788
/// - OTEL_LEVEL - optional. Specifies the minimum [`tracing::Level`] to
13889
/// export. Defaults to [`tracing::Level::DEBUG`].
13990
/// - OTEL_TIMEOUT - optional. Specifies the timeout for the exporter in
@@ -146,8 +97,7 @@ pub struct OtelConfig {
14697
/// The endpoint to send traces to, should be some valid HTTP endpoint for
14798
/// OTLP.
14899
pub endpoint: Url,
149-
/// Defaults to JSON.
150-
pub protocol: OtlpProtocols,
100+
151101
/// Defaults to DEBUG.
152102
pub level: tracing::Level,
153103
/// Defaults to 1 second. Specified in Milliseconds.
@@ -164,8 +114,6 @@ impl FromEnv for OtelConfig {
164114
// load endpoint from env. ignore empty values (shortcut return None), parse, and print the error if any using inspect_err
165115
let endpoint = Url::from_env_var(OTEL_ENDPOINT).inspect_err(|e| eprintln!("{e}"))?;
166116

167-
let protocol = OtlpProtocols::from_env_var(OTEL_PROTOCOL).unwrap_or_default();
168-
169117
let level = tracing::Level::from_env_var(OTEL_LEVEL).unwrap_or(tracing::Level::DEBUG);
170118

171119
let timeout = Duration::from_env_var(OTEL_TIMEOUT).unwrap_or(Duration::from_millis(1000));
@@ -174,7 +122,6 @@ impl FromEnv for OtelConfig {
174122

175123
Ok(Self {
176124
endpoint,
177-
protocol,
178125
level,
179126
timeout,
180127
environment,
@@ -186,8 +133,6 @@ impl OtelConfig {
186133
/// Load from env vars.
187134
///
188135
/// The env vars it checks are:
189-
/// - `OTEL_ENDPOINT` - optional. The endpoint to send traces to, should be
190-
/// some valid URL. If not specified, then [`OtelConfig::load`] will
191136
/// return [`None`].
192137
/// - `OTEL_PROTOCOL` - optional. Specifies the OTLP protocol to use, should
193138
/// be one of "grpc", "binary" or "json". Defaults to json.
@@ -242,7 +187,6 @@ mod test {
242187

243188
fn clear_env() {
244189
std::env::remove_var(OTEL_ENDPOINT);
245-
std::env::remove_var(OTEL_PROTOCOL);
246190
std::env::remove_var(OTEL_LEVEL);
247191
std::env::remove_var(OTEL_TIMEOUT);
248192
std::env::remove_var(OTEL_ENVIRONMENT);
@@ -265,25 +209,12 @@ mod test {
265209

266210
let cfg = OtelConfig::load().unwrap();
267211
assert_eq!(cfg.endpoint, URL.parse().unwrap());
268-
assert_eq!(cfg.protocol, OtlpProtocols::Json);
269212
assert_eq!(cfg.level, tracing::Level::DEBUG);
270213
assert_eq!(cfg.timeout, std::time::Duration::from_millis(1000));
271214
assert_eq!(cfg.environment, "unknown");
272215
})
273216
}
274217

275-
#[test]
276-
#[serial_test::serial]
277-
fn test_env_read_protocol() {
278-
run_clear_env(|| {
279-
std::env::set_var(OTEL_ENDPOINT, URL);
280-
std::env::set_var(OTEL_PROTOCOL, "grpc");
281-
282-
let cfg = OtelConfig::load().unwrap();
283-
assert_eq!(cfg.protocol, OtlpProtocols::Grpc);
284-
})
285-
}
286-
287218
#[test]
288219
#[serial_test::serial]
289220
fn test_env_read_level() {

0 commit comments

Comments
 (0)