1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use crate::Process;
use opentelemetry::trace::{TraceError, TracerProvider};
use opentelemetry::{global, sdk, KeyValue};
use opentelemetry_semantic_conventions as semcov;
pub mod agent;
#[cfg(any(feature = "collector_client", feature = "wasm_collector_client"))]
pub mod collector;
#[derive(Debug)]
struct TransformationConfig {
export_instrument_library: bool,
service_name: Option<String>,
}
impl Default for TransformationConfig {
fn default() -> Self {
TransformationConfig {
export_instrument_library: true,
service_name: None,
}
}
}
trait HasRequiredConfig {
fn set_transformation_config<T>(&mut self, f: T)
where
T: FnOnce(&mut TransformationConfig);
fn set_trace_config(&mut self, config: sdk::trace::Config);
fn set_batch_config(&mut self, config: sdk::trace::BatchConfig);
}
fn build_config_and_process(
config: Option<sdk::trace::Config>,
service_name_opt: Option<String>,
) -> (sdk::trace::Config, Process) {
let config = config.unwrap_or_default();
let service_name = service_name_opt.unwrap_or_else(|| {
config
.resource
.get(semcov::resource::SERVICE_NAME)
.map(|v| v.to_string())
.unwrap_or_else(|| "unknown_service".to_string())
});
let mut tags = config
.resource
.iter()
.filter(|(key, _)| **key != semcov::resource::SERVICE_NAME)
.map(|(key, value)| KeyValue::new(key.clone(), value.clone()))
.collect::<Vec<KeyValue>>();
tags.push(KeyValue::new(
semcov::resource::SERVICE_NAME,
service_name.clone(),
));
(config, Process { service_name, tags })
}
#[cfg(test)]
mod tests {
use crate::exporter::config::build_config_and_process;
use crate::new_agent_pipeline;
use opentelemetry::sdk::trace::Config;
use opentelemetry::sdk::Resource;
use opentelemetry::KeyValue;
use std::env;
#[test]
fn test_set_service_name() {
let service_name = "halloween_service".to_string();
let (_, process) = build_config_and_process(None, Some(service_name.clone()));
assert_eq!(process.service_name, service_name);
let trace_config = Config::default()
.with_resource(Resource::new(vec![KeyValue::new("test-key", "test-value")]));
let (_, process) = build_config_and_process(Some(trace_config), Some(service_name));
assert_eq!(process.tags.len(), 2);
}
#[tokio::test]
async fn test_read_from_env() {
env::set_var("OTEL_SERVICE_NAME", "test service");
let builder = new_agent_pipeline();
let exporter = builder.build_sync_agent_exporter().unwrap();
assert_eq!(exporter.process.service_name, "test service");
env::set_var("OTEL_SERVICE_NAME", "")
}
}
pub(crate) fn install_tracer_provider_and_get_tracer(
tracer_provider: sdk::trace::TracerProvider,
) -> Result<sdk::trace::Tracer, TraceError> {
let tracer = tracer_provider.versioned_tracer(
"opentelemetry-jaeger",
Some(env!("CARGO_PKG_VERSION")),
None,
);
let _ = global::set_tracer_provider(tracer_provider);
Ok(tracer)
}