From 91097868dc72c15d42a6501b95520ce31501105b Mon Sep 17 00:00:00 2001 From: kjuulh Date: Mon, 1 May 2023 10:19:56 +0200 Subject: [PATCH] feat: with opentelemetry working --- crates/dagger-sdk/examples/caching/main.rs | 1 + crates/dagger-sdk/examples/tracing/main.rs | 10 ++++++++++ crates/dagger-sdk/src/client.rs | 10 ++-------- crates/dagger-sdk/src/logging.rs | 6 +++++- crates/dagger-sdk/src/querybuilder.rs | 12 ++++++++++-- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/crates/dagger-sdk/examples/caching/main.rs b/crates/dagger-sdk/examples/caching/main.rs index 1209121..97513a5 100644 --- a/crates/dagger-sdk/examples/caching/main.rs +++ b/crates/dagger-sdk/examples/caching/main.rs @@ -1,5 +1,6 @@ use rand::Rng; +#[tracing::instrument] #[tokio::main] async fn main() -> eyre::Result<()> { let client = dagger_sdk::connect().await?; diff --git a/crates/dagger-sdk/examples/tracing/main.rs b/crates/dagger-sdk/examples/tracing/main.rs index e387c05..c79c1ab 100644 --- a/crates/dagger-sdk/examples/tracing/main.rs +++ b/crates/dagger-sdk/examples/tracing/main.rs @@ -1,8 +1,14 @@ use dagger_sdk::HostDirectoryOpts; +use opentelemetry::global; +use tracing::Level; +#[tracing::instrument] #[tokio::main] async fn main() -> eyre::Result<()> { let client = dagger_sdk::connect().await?; + global::set_text_map_propagator(opentelemetry_jaeger::Propagator::new()); + let span = tracing::span!(Level::INFO, "start main"); + let _enter = span.enter(); let host_source_dir = client.host().directory_opts( "examples/build-the-application/app", @@ -33,5 +39,9 @@ async fn main() -> eyre::Result<()> { println!("build dir contents: \n {:?}", entries); + drop(_enter); + + global::shutdown_tracer_provider(); // sending remaining spans + Ok(()) } diff --git a/crates/dagger-sdk/src/client.rs b/crates/dagger-sdk/src/client.rs index 75657d4..27a272a 100644 --- a/crates/dagger-sdk/src/client.rs +++ b/crates/dagger-sdk/src/client.rs @@ -7,20 +7,14 @@ use dagger_core::engine::Engine as DaggerEngine; use crate::errors::ConnectError; use crate::gen::Query; -use crate::logging::{StdLogger, TracingLogger}; +use crate::logging::StdLogger; use crate::querybuilder::query; pub type DaggerConn = Arc; pub async fn connect() -> Result { let cfg = if cfg!(feature = "otel") { - let cfg = Config::new( - None, - None, - None, - None, - Some(Arc::new(TracingLogger::default())), - ); + let cfg = Config::new(None, None, None, None, Some(Arc::new(StdLogger::default()))); #[cfg(feature = "otel")] crate::logging::otel_logging().map_err(ConnectError::FailedToInstallOtelTracer)?; diff --git a/crates/dagger-sdk/src/logging.rs b/crates/dagger-sdk/src/logging.rs index f6005fd..3ad94e5 100644 --- a/crates/dagger-sdk/src/logging.rs +++ b/crates/dagger-sdk/src/logging.rs @@ -11,9 +11,13 @@ pub fn otel_logging() -> eyre::Result<()> { use tracing_subscriber::layer::SubscriberExt; use tracing_subscriber::Registry; + std::env::set_var("OTEL_BSP_MAX_EXPORT_BATCH_SIZE", "25"); + let tracer = opentelemetry_jaeger::new_agent_pipeline() .with_service_name("dagger_sdk") - .install_simple(); + .with_max_packet_size(9216) + .with_auto_split_batch(true) + .install_batch(opentelemetry::runtime::Tokio)?; let telemetry = tracing_opentelemetry::layer().with_tracer(tracer); diff --git a/crates/dagger-sdk/src/querybuilder.rs b/crates/dagger-sdk/src/querybuilder.rs index 183e268..656254d 100644 --- a/crates/dagger-sdk/src/querybuilder.rs +++ b/crates/dagger-sdk/src/querybuilder.rs @@ -2,6 +2,7 @@ use std::{collections::HashMap, ops::Add, sync::Arc}; use dagger_core::graphql_client::DynGraphQLClient; use serde::{Deserialize, Serialize}; +use tracing::instrument; use crate::errors::{DaggerError, DaggerUnpackError}; @@ -30,6 +31,7 @@ pub struct Selection { } impl Selection { + #[instrument(skip(self))] pub fn select_with_alias(&self, alias: &str, name: &str) -> Selection { Self { name: Some(name.to_string()), @@ -39,6 +41,7 @@ impl Selection { } } + #[instrument(skip(self))] pub fn select(&self, name: &str) -> Selection { Self { name: Some(name.to_string()), @@ -48,6 +51,7 @@ impl Selection { } } + #[instrument(skip(self, value))] pub fn arg(&self, name: &str, value: S) -> Selection where S: Serialize, @@ -70,6 +74,7 @@ impl Selection { s } + #[instrument(skip(self, value))] pub fn arg_enum(&self, name: &str, value: S) -> Selection where S: Serialize, @@ -93,6 +98,7 @@ impl Selection { s } + #[instrument(skip(self))] pub fn build(&self) -> Result { let mut fields = vec!["query".to_string()]; @@ -118,14 +124,13 @@ impl Selection { Ok(fields.join("{") + &"}".repeat(fields.len() - 1)) } + #[instrument(skip(self, gql_client))] pub async fn execute(&self, gql_client: DynGraphQLClient) -> Result where D: for<'de> Deserialize<'de>, { let query = self.build()?; - tracing::trace!(query = query.as_str(), "dagger-query"); - let resp: Option = match gql_client.query(&query).await { Ok(r) => r, Err(e) => return Err(DaggerError::Query(e)), @@ -136,6 +141,7 @@ impl Selection { Ok(resp.unwrap()) } + #[instrument(skip(self))] fn path(&self) -> Vec { let mut selections: Vec = vec![]; let mut cur = self; @@ -152,6 +158,7 @@ impl Selection { selections } + #[instrument(skip(self, resp))] pub(crate) fn unpack_resp( &self, resp: Option, @@ -165,6 +172,7 @@ impl Selection { } } + #[instrument(skip(self, r))] fn unpack_resp_value(&self, r: serde_json::Value) -> Result where D: for<'de> Deserialize<'de>,