#![allow(
clippy::expect_used,
clippy::unwrap_used,
reason = "integration test code — panics are acceptable failures"
)]
#![cfg(feature = "telemetry")]
use cognee_observability::{SettingsView, already_instrumented, init_telemetry};
use serial_test::serial;
use tracing_subscriber::Registry;
struct StaticSettings {
tracing_enabled: bool,
service_name: String,
otlp_endpoint: String,
otlp_headers: String,
otlp_protocol: String,
span_processor: String,
traces_sampler: String,
traces_sampler_arg: String,
}
impl StaticSettings {
fn new(tracing_enabled: bool, endpoint: &str) -> Self {
Self {
tracing_enabled,
service_name: "cognee-test".to_string(),
otlp_endpoint: endpoint.to_string(),
otlp_headers: String::new(),
otlp_protocol: "grpc".to_string(),
span_processor: "batch".to_string(),
traces_sampler: String::new(),
traces_sampler_arg: String::new(),
}
}
fn with_protocol(mut self, protocol: &str) -> Self {
self.otlp_protocol = protocol.to_string();
self
}
}
impl SettingsView for StaticSettings {
fn tracing_enabled(&self) -> bool {
self.tracing_enabled
}
fn service_name(&self) -> &str {
&self.service_name
}
fn otlp_endpoint(&self) -> &str {
&self.otlp_endpoint
}
fn otlp_headers(&self) -> &str {
&self.otlp_headers
}
fn otlp_protocol(&self) -> &str {
&self.otlp_protocol
}
fn span_processor(&self) -> &str {
&self.span_processor
}
fn traces_sampler(&self) -> &str {
&self.traces_sampler
}
fn traces_sampler_arg(&self) -> &str {
&self.traces_sampler_arg
}
}
#[tokio::test]
#[serial]
async fn init_telemetry_full_activation_lifecycle() {
assert!(
!already_instrumented(),
"OUR_PROVIDER_INSTALLED must default to unset on a fresh process"
);
let settings = StaticSettings::new(true, "http://127.0.0.1:1");
let (_layer, guard) = init_telemetry::<Registry>(&settings)
.expect("first init_telemetry call must succeed and install our SDK provider");
assert!(
guard.has_provider(),
"the first installation must return a guard that owns the SDK provider"
);
assert!(
already_instrumented(),
"OUR_PROVIDER_INSTALLED must be set after a successful init_telemetry call"
);
let (_layer2, guard2) = init_telemetry::<Registry>(&settings)
.expect("second init_telemetry call must succeed via the bridge branch");
assert!(
!guard2.has_provider(),
"second init_telemetry call must return a noop guard (bridge mode)"
);
let endpoint_only = StaticSettings::new(false, "http://127.0.0.1:1");
let (_layer3, guard3) = init_telemetry::<Registry>(&endpoint_only)
.expect("endpoint-only activation bridges to the installed provider");
assert!(
!guard3.has_provider(),
"bridge branch returns TelemetryGuard::noop"
);
let flag_only = StaticSettings::new(true, "");
let (_layer4, guard4) = init_telemetry::<Registry>(&flag_only)
.expect("flag-only activation bridges to the installed provider");
assert!(
!guard4.has_provider(),
"bridge branch returns TelemetryGuard::noop"
);
let bad_protocol = StaticSettings::new(true, "http://127.0.0.1:1").with_protocol("nonsense");
let (_layer5, guard5) = init_telemetry::<Registry>(&bad_protocol)
.expect("bridge branch ignores the configured protocol");
assert!(
!guard5.has_provider(),
"bridge branch returns TelemetryGuard::noop regardless of protocol"
);
}