'Failed to send tracing data to OpenTelemetry-Collector and then export to Jaeger

I'm testing sending trace data to OpenTelemetry-Colletor and then export to Jaeger, but the program reports the error: 【cannot send span to the batch span processor because the channel is closed】. Is there something wrong with my code?

code

 #[tokio::test]
    async fn open_telemetry_collector() {
        fn init_otel_collector_subscriber() {
            let tracer = opentelemetry_otlp::new_pipeline()
                .tracing()
                .with_exporter(opentelemetry_otlp::new_exporter().tonic())
                .with_trace_config(
                    opentelemetry::sdk::trace::config()
                        .with_sampler(opentelemetry::sdk::trace::Sampler::AlwaysOn)
                        .with_resource(opentelemetry::sdk::Resource::new(vec![
                            opentelemetry::KeyValue::new(
                                "service.name",
                                "open_telemetry_collector",
                            ),
                        ])),
                )
                .install_batch(opentelemetry::runtime::Tokio)
                .unwrap();
            let otel_layer = tracing_opentelemetry::layer().with_tracer(tracer);
            tracing_subscriber::registry().with(otel_layer).init();
        }

        init_otel_collector_subscriber();

        t().await;

        // wait 5s to ensure to send trace data to otelcol
        tokio::time::sleep(std::time::Duration::from_millis(5000)).await;
    }

    #[tracing::instrument]
    async fn t() {
        let work_result = expensive_work().await;

        tokio::time::sleep(std::time::Duration::from_millis(10))
            .instrument(tracing::info_span!("faster_work"))
            .await;

        tracing::warn!("About to exit!");
        tracing::trace!("status: {}", work_result);
    }

    #[tracing::instrument]
    #[inline]
    async fn expensive_work() -> &'static str {
        tokio::time::sleep(std::time::Duration::from_millis(250))
            .instrument(tracing::info_span!("expensive_step_1"))
            .await;
        tokio::time::sleep(std::time::Duration::from_millis(250))
            .instrument(tracing::info_span!("expensive_step_2"))
            .await;

        "success"
    }

run result

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source