Skip to content

Commit

Permalink
Add service.version to resource span
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwlewis committed Nov 8, 2023
1 parent 4f197ef commit 95c44b2
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions libcnb/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use opentelemetry::{
};
use std::path::Path;

const TELEMETRY_EXPORT_ROOT: &str = "/tmp/cnb-telemetry";

pub(crate) struct BuildpackTrace {
provider: TracerProvider,
span: Span,
}

pub(crate) fn start_trace(buildpack: &Buildpack, phase_name: &'static str) -> BuildpackTrace {
let trace_name = format!("{}-{phase_name}", buildpack.id.replace(['/', '.'], "_"));
let tracing_file_path = Path::new("/tmp")
.join("cnb-telemetry")
.join(format!("{trace_name}.jsonl"));
let tracing_file_path = Path::new(TELEMETRY_EXPORT_ROOT).join(format!("{trace_name}.jsonl"));

// Ensure tracing file path parent exists by creating it.
if let Some(parent_dir) = tracing_file_path.parent() {
Expand All @@ -39,23 +39,19 @@ pub(crate) fn start_trace(buildpack: &Buildpack, phase_name: &'static str) -> Bu
};

let lib_name = option_env!("CARGO_PKG_NAME").unwrap_or("libcnb");
let lib_version = option_env!("CARGO_PKG_VERSION");

let provider = opentelemetry::sdk::trace::TracerProvider::builder()
.with_simple_exporter(exporter)
.with_config(
Config::default()
.with_resource(Resource::new(vec![KeyValue::new("service.name", lib_name)])),
)
.with_config(Config::default().with_resource(Resource::new(vec![
KeyValue::new("service.name", lib_name),
KeyValue::new("service.version", lib_version.unwrap_or_default()),
])))
.build();

global::set_tracer_provider(provider.clone());

let tracer = provider.versioned_tracer(
lib_name,
option_env!("CARGO_PKG_VERSION"),
None as Option<&str>,
None,
);
let tracer = provider.versioned_tracer(lib_name, lib_version, None as Option<&str>, None);

let mut span = tracer.start(trace_name);
span.set_attributes(vec![
Expand Down

0 comments on commit 95c44b2

Please sign in to comment.