Skip to content

Commit

Permalink
Method to add service_name to all spans
Browse files Browse the repository at this point in the history
It's useful to have a way to set the service name for all spans at once,
rather than having to set it every time.

This commit adds a with_service_name method to PipelineBuilder
and Exporter to allow users to set the service name in one place,
following the behaviour of the official DataDog and Zipkin exporters.

It also sets a default service_name if the user does not configure one.
  • Loading branch information
isobelhooper committed Apr 6, 2021
1 parent f219b77 commit dc21605
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ use std::convert::TryInto;
use std::error::Error as StdError;
use tags::{get_tags_for_event, get_tags_for_span};

/// Default service name if no service is configured.
const DEFAULT_SERVICE_NAME: &str = "unknown_service";

/// Create a new Application Insights exporter pipeline builder
pub fn new_pipeline(instrumentation_key: String) -> PipelineBuilder<()> {
PipelineBuilder {
Expand All @@ -191,6 +194,7 @@ pub fn new_pipeline(instrumentation_key: String) -> PipelineBuilder<()> {
endpoint: None,
instrumentation_key,
sample_rate: None,
service_name: DEFAULT_SERVICE_NAME.to_string(),
}
}

Expand All @@ -202,6 +206,7 @@ pub struct PipelineBuilder<C> {
endpoint: Option<http::Uri>,
instrumentation_key: String,
sample_rate: Option<f64>,
service_name: String,
}

impl<C> PipelineBuilder<C> {
Expand All @@ -215,6 +220,7 @@ impl<C> PipelineBuilder<C> {
endpoint: self.endpoint,
instrumentation_key: self.instrumentation_key,
sample_rate: self.sample_rate,
service_name: self.service_name,
}
}

Expand Down Expand Up @@ -288,6 +294,22 @@ impl<C> PipelineBuilder<C> {
..self
}
}

/// Assign the service name under which to group traces.
///
/// This will be translated, along with the service namespace, to the Cloud Role Name.
///
/// ```
/// # use opentelemetry::{KeyValue, sdk};
/// let tracer = opentelemetry_application_insights::new_pipeline("...".into())
/// .with_client(reqwest::blocking::Client::new())
/// .with_service_name("my-application")
/// .install_simple();
/// ```
pub fn with_service_name<T: Into<String>>(mut self, name: T) -> Self {
self.service_name = name.into();
self
}
}

impl<C> PipelineBuilder<C>
Expand Down Expand Up @@ -368,6 +390,7 @@ pub struct Exporter<C> {
endpoint: http::Uri,
instrumentation_key: String,
sample_rate: f64,
service_name: String,
}

impl<C> Exporter<C> {
Expand All @@ -380,6 +403,7 @@ impl<C> Exporter<C> {
.expect("hardcoded endpoint is valid uri"),
instrumentation_key,
sample_rate: 100.0,
service_name: DEFAULT_SERVICE_NAME.to_string(),
}
}

Expand All @@ -405,9 +429,28 @@ impl<C> Exporter<C> {
self
}

fn create_envelopes(&self, span: SpanData) -> Vec<Envelope> {
/// Assign the service name under which to group traces.
///
/// This will be translated, along with the service namespace, to the Cloud Role Name.
pub fn with_service_name<T: Into<String>>(mut self, name: T) -> Self {
self.service_name = name.into();
self
}

fn create_envelopes(&self, mut span: SpanData) -> Vec<Envelope> {
let mut result = Vec::with_capacity(1 + span.message_events.len());

if span
.attributes
.get(&semcov::resource::SERVICE_NAME)
.is_none()
{
span.attributes.insert(opentelemetry::KeyValue::new(
semcov::resource::SERVICE_NAME,
self.service_name.clone(),
))
}

let (data, tags, name) = match span.span_kind {
SpanKind::Server | SpanKind::Consumer => {
let data: RequestData = (&span).into();
Expand Down

0 comments on commit dc21605

Please sign in to comment.