|
| 1 | +use async_trait::async_trait; |
| 2 | +use bytes::Bytes; |
| 3 | +use http::{Request, Response}; |
| 4 | +use hyper::client::{connect::Connect, HttpConnector}; |
| 5 | +use hyper::{Body, Client}; |
| 6 | +use once_cell::sync::Lazy; |
| 7 | +use opentelemetry::{ |
| 8 | + global, |
| 9 | + trace::{TraceContextExt, TraceError, Tracer, TracerProvider as _}, |
| 10 | + Key, KeyValue, |
| 11 | +}; |
| 12 | +use opentelemetry_http::{HttpClient, HttpError, ResponseExt}; |
| 13 | +use opentelemetry_otlp::WithExportConfig; |
| 14 | +use opentelemetry_sdk::trace::{self as sdktrace, Config}; |
| 15 | +use opentelemetry_sdk::Resource; |
| 16 | + |
| 17 | +use std::error::Error; |
| 18 | + |
| 19 | +static RESOURCE: Lazy<Resource> = Lazy::new(|| { |
| 20 | + Resource::new(vec![KeyValue::new( |
| 21 | + opentelemetry_semantic_conventions::resource::SERVICE_NAME, |
| 22 | + "basic-otlp-http-hyper", |
| 23 | + )]) |
| 24 | +}); |
| 25 | + |
| 26 | +struct HyperClient<C> { |
| 27 | + inner: hyper::Client<C>, |
| 28 | +} |
| 29 | + |
| 30 | +impl Default for HyperClient<HttpConnector> { |
| 31 | + fn default() -> Self { |
| 32 | + Self { |
| 33 | + inner: Client::new(), |
| 34 | + } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<C> std::fmt::Debug for HyperClient<C> { |
| 39 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 40 | + f.debug_struct("HyperClient") |
| 41 | + .field("inner", &self.inner) |
| 42 | + .finish() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[async_trait] |
| 47 | +impl<C: Connect + Clone + Send + Sync + 'static> HttpClient for HyperClient<C> { |
| 48 | + async fn send(&self, request: Request<Vec<u8>>) -> Result<Response<Bytes>, HttpError> { |
| 49 | + let request = request.map(Body::from); |
| 50 | + |
| 51 | + let (parts, body) = self |
| 52 | + .inner |
| 53 | + .request(request) |
| 54 | + .await? |
| 55 | + .error_for_status()? |
| 56 | + .into_parts(); |
| 57 | + let body = hyper::body::to_bytes(body).await?; |
| 58 | + |
| 59 | + Ok(Response::from_parts(parts, body)) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> { |
| 64 | + opentelemetry_otlp::new_pipeline() |
| 65 | + .tracing() |
| 66 | + .with_exporter( |
| 67 | + opentelemetry_otlp::new_exporter() |
| 68 | + .http() |
| 69 | + .with_http_client(HyperClient::default()) |
| 70 | + .with_endpoint("http://localhost:4318/v1/traces"), |
| 71 | + ) |
| 72 | + .with_trace_config(Config::default().with_resource(RESOURCE.clone())) |
| 73 | + .install_batch(opentelemetry_sdk::runtime::Tokio) |
| 74 | +} |
| 75 | + |
| 76 | +#[tokio::main] |
| 77 | +async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> { |
| 78 | + let result = init_tracer_provider(); |
| 79 | + assert!( |
| 80 | + result.is_ok(), |
| 81 | + "Init tracer failed with error: {:?}", |
| 82 | + result.err() |
| 83 | + ); |
| 84 | + |
| 85 | + let tracer_provider = result.unwrap(); |
| 86 | + global::set_tracer_provider(tracer_provider.clone()); |
| 87 | + |
| 88 | + let tracer = global::tracer_provider().tracer_builder("basic").build(); |
| 89 | + |
| 90 | + tracer.in_span("Main operation", |cx| { |
| 91 | + let span = cx.span(); |
| 92 | + span.add_event( |
| 93 | + "Nice operation!".to_string(), |
| 94 | + vec![Key::new("bogons").i64(100)], |
| 95 | + ); |
| 96 | + span.set_attribute(KeyValue::new("another.key", "yes")); |
| 97 | + |
| 98 | + tracer.in_span("Sub operation...", |cx| { |
| 99 | + let span = cx.span(); |
| 100 | + span.set_attribute(KeyValue::new("another.key", "yes")); |
| 101 | + span.add_event("Sub span event", vec![]); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + global::shutdown_tracer_provider(); |
| 106 | + Ok(()) |
| 107 | +} |
0 commit comments