use opentelemetry::KeyValue;
use opentelemetry_otlp::{WithExportConfig, WithTonicConfig};
use opentelemetry_sdk::{Resource, runtime};
use tonic::transport::{Channel, ClientTlsConfig};

pub fn init_telemetry() -> opentelemetry_sdk::trace::TracerProvider {
    let endpoint = "https://signoz.racooncity.org".to_string();
    let channel = Channel::from_shared(endpoint.clone())
        .unwrap()
        .tls_config(ClientTlsConfig::new().with_native_roots())
        .unwrap()
        .connect_lazy();
    let exporter = opentelemetry_otlp::SpanExporter::builder()
        .with_tonic()
        .with_endpoint(endpoint.clone())
        .with_channel(channel)
        .build()
        .expect("Failed to Build Exporter");
    opentelemetry_sdk::trace::TracerProvider::builder()
        .with_batch_exporter(exporter, runtime::Tokio)
        .with_resource(Resource::new(vec![KeyValue::new(
            "service.name",
            "RhythmWorks",
        )]))
        .build()
}
