无法使用 Opentelemetry 连接到 newrelic

问题描述 投票:0回答:1

我正在尝试通过 http 使用 opentelemetry-otlp 将跟踪数据发送到 newrelic

下面是我正在使用的代码片段

fn main() -> {
   let result = init_tracer_provider();
   
   globa::set_tracer_provider(result.unwrap().clone());

   let tracer = tracer_provider.tracer("my-service-tracer");
   let telemetry_layer = tracing_opentelemetry::layer().with_tracer(tracer);

   let subscriber = Registry:default()
                      .with(telemetry_layer);
   subscriber::set_global_default(subscriber).expect("Some message");
}


fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
    let http_client = hyper_client_config::HyperClient::default();
    opentelemetry_otlp::new_pipeline()
        .tracing()
        .with_exporter(
            http_exporter()
                .with_protocol(Protocol::HttpJson)
                .with_endpoint("https://trace-api.newrelic.com/v1/traces")
                .with_headers(get_http_headers(api_key)
                .with_http_client(http_client),
        )
        .with_trace_config(Config::default().with_resource(RESOURCE.clone()))
        .install_batch(opentelemetry_sdk::runtime::Tokio)
}

fn get_http_headers(api_key : &String) -> HashMap<String,String> {
    let mut headers = HashMap::new();
    headers.insert(String::from_str("Api-Key"), String::from_str(api_key); 
    headers.insert(String::from_str("Content-Type"), String::from_str("application/json");
    headers.insert(String::from_str("Data-Format"), String::from_str("newrelic);  
    headers
}

另一个维护

hyper_client_config.rs
的文件。我指的是实现
HttpClient
 所需的特质 
with_http_client

use async_trait::async_trait;
use bytes::Bytes;
use http::{Request, Response};
use http_body_util::{BodyExt, Full};
use hyper_util::{
    client::legacy::{
        connect::{Connect, HttpConnector},
        Client,
    },
    rt::TokioExecutor,
};
use opentelemetry_http::{HttpClient, HttpError, ResponseExt};

pub struct HyperClient<C> {
    inner: hyper_util::client::legacy::Client<C, Full<Bytes>>,
}

impl Default for HyperClient<HttpConnector> {
    fn default() -> Self {
        Self {
            inner: Client::builder(TokioExecutor::new()).build_http(),
        }
    }
}

impl<C> std::fmt::Debug for HyperClient<C> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HyperClient")
            .field("inner", &self.inner)
            .finish()
    }
}

#[async_trait]
impl<C: Connect + Clone + Send + Sync + 'static> HttpClient for HyperClient<C> {
    async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> {
        let request = request.map(|body| Full::new(Bytes::from(body)));

        let (parts, body) = self
            .inner
            .request(request)
            .await?
            .error_for_status()?
            .into_parts();
        let body = body.collect().await?.to_bytes();

        Ok(Response::from_parts(parts, body))
    }
}

但是我在日志中收到此错误

Opentelemetry trace error occurred. client error (Connect)

https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs

我指的是这个github代码。有人可以帮忙吗。我有什么遗漏的吗

rust rust-cargo newrelic open-telemetry
1个回答
0
投票

.with_endpoint("https://trace-api.newrelic.com/v1/traces")
看起来错误,对于 otlp newrelic 端点

环境 gRPC HTTP 端点 支持的端口
美国OTLP https://otlp.nr-data.net 443、4317、4318
欧盟OTLP https://otlp.eu01.nr-data.net 443、4317、4318
美国 FedRAMP OTLP https://gov-otlp.nr-data.net 443、4317、4318
(有关更多信息,请参阅 FedRAMP 合规性)
无限追踪 https://{trace-observer} 443

Rust 代码示例

fn init_tracer() -> Result<trace::Tracer, TraceError> {
    opentelemetry_otlp::new_pipeline()
        .tracing()
        .with_exporter(
            opentelemetry_otlp::new_exporter()
                .http()
                .with_endpoint("https://otlp.nr-data.net/v1/traces")
                .with_headers(HashMap::from([(
                    "api-key".to_string(),
                    "LICENSEKEY".to_string(),
                )]))
        )
        .with_trace_config(
            trace::config().with_resource(Resource::new(vec![KeyValue::new(
                opentelemetry_semantic_conventions::resource::SERVICE_NAME,
                "rust-otlp-newrelic-tracing",
            )])),
        )
        .install_batch(runtime::Tokio)
}

检查参考中的 git repo

参考:

© www.soinside.com 2019 - 2024. All rights reserved.