Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
feat: with opentelemetry working
Browse files Browse the repository at this point in the history
  • Loading branch information
kjuulh committed May 1, 2023
1 parent 07d1445 commit 9109786
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions crates/dagger-sdk/examples/caching/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rand::Rng;

#[tracing::instrument]
#[tokio::main]
async fn main() -> eyre::Result<()> {
let client = dagger_sdk::connect().await?;
Expand Down
10 changes: 10 additions & 0 deletions crates/dagger-sdk/examples/tracing/main.rs
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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(())
}
10 changes: 2 additions & 8 deletions crates/dagger-sdk/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Query>;

pub async fn connect() -> Result<DaggerConn, ConnectError> {
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)?;
Expand Down
6 changes: 5 additions & 1 deletion crates/dagger-sdk/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 10 additions & 2 deletions crates/dagger-sdk/src/querybuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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()),
Expand All @@ -39,6 +41,7 @@ impl Selection {
}
}

#[instrument(skip(self))]
pub fn select(&self, name: &str) -> Selection {
Self {
name: Some(name.to_string()),
Expand All @@ -48,6 +51,7 @@ impl Selection {
}
}

#[instrument(skip(self, value))]
pub fn arg<S>(&self, name: &str, value: S) -> Selection
where
S: Serialize,
Expand All @@ -70,6 +74,7 @@ impl Selection {
s
}

#[instrument(skip(self, value))]
pub fn arg_enum<S>(&self, name: &str, value: S) -> Selection
where
S: Serialize,
Expand All @@ -93,6 +98,7 @@ impl Selection {
s
}

#[instrument(skip(self))]
pub fn build(&self) -> Result<String, DaggerError> {
let mut fields = vec!["query".to_string()];

Expand All @@ -118,14 +124,13 @@ impl Selection {
Ok(fields.join("{") + &"}".repeat(fields.len() - 1))
}

#[instrument(skip(self, gql_client))]
pub async fn execute<D>(&self, gql_client: DynGraphQLClient) -> Result<D, DaggerError>
where
D: for<'de> Deserialize<'de>,
{
let query = self.build()?;

tracing::trace!(query = query.as_str(), "dagger-query");

let resp: Option<serde_json::Value> = match gql_client.query(&query).await {
Ok(r) => r,
Err(e) => return Err(DaggerError::Query(e)),
Expand All @@ -136,6 +141,7 @@ impl Selection {
Ok(resp.unwrap())
}

#[instrument(skip(self))]
fn path(&self) -> Vec<Selection> {
let mut selections: Vec<Selection> = vec![];
let mut cur = self;
Expand All @@ -152,6 +158,7 @@ impl Selection {
selections
}

#[instrument(skip(self, resp))]
pub(crate) fn unpack_resp<D>(
&self,
resp: Option<serde_json::Value>,
Expand All @@ -165,6 +172,7 @@ impl Selection {
}
}

#[instrument(skip(self, r))]
fn unpack_resp_value<D>(&self, r: serde_json::Value) -> Result<D, DaggerError>
where
D: for<'de> Deserialize<'de>,
Expand Down

0 comments on commit 9109786

Please sign in to comment.