生 rust 开口遥测OTLP,带Hive

frebpwbc  于 2022-11-12  发布在  Hive
关注(0)|答案(2)|浏览(134)

我尝试使用opentelemetryopen telemetry-otlp通过OTLP向Honeycomb提供可观测性数据。
我使用类似的东西作为概念证明(如果你想运行它,请提取到这个repo中:(第10页)

fn init_tracer(metadata: &MetadataMap) -> Result<sdktrace::Tracer, TraceError> {
    let opentelemetry_endpoint =
        env::var("OTEL_ENDPOINT").unwrap_or_else(|_| "https://api.honeycomb.io".to_owned());
    let opentelemetry_endpoint =
        Url::parse(&opentelemetry_endpoint).expect("OTEL_ENDPOINT is not a valid url");

    opentelemetry_otlp::new_pipeline()
        .tracing()
        .with_exporter(
            opentelemetry_otlp::new_exporter()
                .tonic()
                .with_endpoint(opentelemetry_endpoint.as_str())
                .with_metadata(metadata.clone())
                .with_tls_config(
                    ClientTlsConfig::new().domain_name(
                        opentelemetry_endpoint
                            .host_str()
                            .expect("OTEL_ENDPOINTshould have a valid host"),
                    ),
                ),
        )
        .install_batch(opentelemetry::runtime::Tokio)
}

# [tokio::main]

async fn main() -> anyhow::Result<()> {

    let mut metadata = MetadataMap::with_capacity(2);

    metadata.insert(
        "x-honeycomb-team",
        "...honeycomb api key...".parse().unwrap(),
    );

    metadata.insert("x-honeycomb-dataset", "my-api".parse().unwrap());

    let tracer = init_tracer(&metadata).expect("failed to instantiate opentelemetry tracing");

    tracing_subscriber::registry()
        .with(tracing_subscriber::EnvFilter::from_default_env())
        .with(tracing_opentelemetry::layer().with_tracer(tracer))
        .with(tracing_subscriber::fmt::layer())
        .try_init()
        .expect("failed to register tracer with registry");

    let tracer = global::tracer("ex.com/basic");

但我得到的是:

2022-11-02T17:01:01.088429Z DEBUG hyper::client::connect::http: connecting to 52.5.162.226:443
2022-11-02T17:01:01.170767Z DEBUG hyper::client::connect::http: connected to 52.5.162.226:443
2022-11-02T17:01:01.171870Z DEBUG rustls::client::hs: No cached session for DnsName(DnsName(DnsName("api.honeycomb.io")))
2022-11-02T17:01:01.172555Z DEBUG rustls::client::hs: Not resuming any session
2022-11-02T17:01:01.269218Z DEBUG rustls::client::hs: ALPN protocol is Some(b"h2")
2022-11-02T17:01:01.269398Z DEBUG rustls::client::hs: Using ciphersuite TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
2022-11-02T17:01:01.269504Z DEBUG rustls::client::tls12::server_hello: Server supports tickets
2022-11-02T17:01:01.269766Z DEBUG rustls::client::tls12: ECDHE curve is ECParameters { curve_type: NamedCurve, named_group: secp256r1 }
2022-11-02T17:01:01.269843Z DEBUG rustls::client::tls12: Server DNS name is DnsName(DnsName(DnsName("api.honeycomb.io")))
2022-11-02T17:01:01.271123Z  WARN rustls::conn: Sending fatal alert BadCertificate
2022-11-02T17:01:01.271861Z DEBUG tonic::transport::service::reconnect: reconnect::poll_ready: hyper::Error(Connect, Custom { kind: InvalidData, error: InvalidCertificateData("invalid peer certificate: UnknownIssuer") })
2022-11-02T17:01:01.271967Z DEBUG tower::buffer::worker: service.ready=true processing request
2022-11-02T17:01:01.272169Z DEBUG tonic::transport::service::reconnect: error: error trying to connect: invalid peer certificate contents: invalid peer certificate: UnknownIssuer
OpenTelemetry trace error occurred. Exporter otlp encountered the following error(s): the grpc server returns error (The service is currently unavailable): , detailed error message: error trying to connect: invalid peer certificate contents: invalid peer certificate: UnknownIssuer

这似乎表明我的TLS设置有些地方不正确...有没有人有Rust中的opentelemetry代码片段可以与Honeycomb一起工作?

ddrv8njm

ddrv8njm1#

问题是:您需要为ClientTlsConfig提供一个根证书,目标站点(api.honeycomb.io)将链接回该证书。
我发现一个合适的根证书安装在我的容器中,然后让程序加载它。
下面是代码:

let pem = tokio::fs::read("/etc/ssl/certs/Starfield_Services_Root_Certificate_Authority_-_G2.pem").await.expect("read the cert file");
let cert = Certificate::from_pem(pem);

let mut metadata = MetadataMap::with_capacity(1);
metadata.insert("x-honeycomb-team", honeycomb_api_key.parse().unwrap());

let opentelemetry_endpoint =
        env::var("OTEL_ENDPOINT").unwrap_or_else(|_| "https://api.honeycomb.io".to_owned());

let opentelemetry_endpoint =
        Url::parse(&opentelemetry_endpoint).expect("OTEL_ENDPOINT is not a valid url");

opentelemetry_otlp::new_pipeline()
    .tracing()
    .with_exporter(
        opentelemetry_otlp::new_exporter()
            .tonic()
            .with_endpoint(opentelemetry_endpoint.as_str())
            .with_metadata(metadata.clone())
            .with_tls_config(
                ClientTlsConfig::new().ca_certificate(cert)
            ),
    )
    .install_batch(opentelemetry::runtime::Tokio)
}

前两行是新的;它们从文件系统加载一个根证书,然后用它来配置ClientTlsConfig。
我根据证书详细信息输出选择了根证书文件:openssl s_client -connect api.honeycomb.io:443 -servername localhost
证书链中的最后一个条目类似于文件名。它包括:/CN=Starfield Services Root Certificate Authority - G2

332nm8kg

332nm8kg2#

除了@jessitron的答案,我还可以使用一个单独的OpenTelemetry收集器服务让Honeycomb与Rust服务一起工作。
这样,您就可以使用更简单的配置向收集器Ala报告跟踪:

use opentelemetry::sdk::trace as sdktrace;
use opentelemetry::trace::{TraceContextExt, TraceError, Tracer};
use opentelemetry::{global, Key};
use opentelemetry_otlp::WithExportConfig;
use std::env;
use tonic::metadata::MetadataMap;
use tonic::transport::ClientTlsConfig;
use tracing_subscriber::prelude::*;
use url::Url;

fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
    // Configure OTEL_ENDPOINT to point at your collector
    let opentelemetry_endpoint =
        env::var("OTEL_ENDPOINT").unwrap_or_else(|_| "http://localhost:4317".to_owned());
    let opentelemetry_endpoint =
        Url::parse(&opentelemetry_endpoint).expect("OTEL_ENDPOINT is not a valid url");

    opentelemetry_otlp::new_pipeline()
        .tracing()
        .with_exporter(
            opentelemetry_otlp::new_exporter()
                .tonic()
                .with_endpoint(opentelemetry_endpoint.as_str()),
        )
        .install_batch(opentelemetry::runtime::Tokio)
}

# [tokio::main]

async fn main() {
    let tracer = init_tracer().expect("failed to instantiate opentelemetry tracing");

    tracing_subscriber::registry()
        .with(tracing_subscriber::EnvFilter::from_default_env())
        .with(tracing_opentelemetry::layer().with_tracer(tracer))
        .with(tracing_subscriber::fmt::layer())
        .try_init()
        .expect("failed to register tracer with registry");

    const LEMONS_KEY: Key = Key::from_static_str("lemons");
    const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another");

    let tracer = global::tracer("ex.com/basic");

    tracer.in_span("operation", |cx| {
        let span = cx.span();
        span.add_event(
            "Nice operation!".to_string(),
            vec![Key::new("bogons").i64(100)],
        );
        span.set_attribute(ANOTHER_KEY.string("yes"));

        tracer.in_span("Sub operation...", |cx| {
            let span = cx.span();
            span.set_attribute(LEMONS_KEY.string("five"));

            span.add_event("Sub span event", vec![]);
        });
    });

    loop {
        tracing::info!("just sleeping, press ctrl-c to exit");
        tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
    }
}

作为最后一个帮助,如果您使用Helm图表部署收集器,您将需要使用类似于以下内容的配置:

values:
    mode: deployment
    config: 
      exporters:
        otlp/honeycomb:
          endpoint: api.honeycomb.io:443
          headers:
            "x-honeycomb-team": "{{{honeycomb-api-key}}}"
      service:
        pipelines:
          traces:
            receivers: 
              - otlp
            exporters: 
              - otlp/honeycomb

再次感谢@jessitron的离合器帮助。:)

相关问题