Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method to add service_name to all spans #34

Merged
merged 7 commits into from
May 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ use opentelemetry::{
},
},
trace::{Event, SpanKind, StatusCode, TracerProvider},
Key, Value,
Key, KeyValue, Value,
};
use opentelemetry_semantic_conventions as semcov;
use std::collections::HashMap;
Expand Down Expand Up @@ -267,6 +267,9 @@ impl<C> PipelineBuilder<C> {

/// Assign the SDK config for the exporter pipeline.
///
/// If there is an existing `sdk::Config` in the `PipelineBuilder` the `sdk::Resource`s
/// are merged and any other parameters are overwritten.
///
/// Note: This example requires [`reqwest`] and the **reqwest-client-blocking** feature.
///
/// [`reqwest`]: https://crates.io/crates/reqwest
Expand All @@ -283,6 +286,47 @@ impl<C> PipelineBuilder<C> {
/// .install_simple();
/// ```
pub fn with_trace_config(self, config: sdk::trace::Config) -> Self {
let merged_resource = match self.config {
Some(base_config) => base_config.resource.merge(&config.resource),
None => (*config.resource).clone(),
};

let config = config.with_resource(merged_resource);

PipelineBuilder {
config: Some(config),
..self
}
}

/// Assign the service name under which to group traces by adding a service.name
/// `sdk::Resource` or overriding a previous setting of it.
Comment on lines +302 to +303
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that "overriding a previous setting" is not true with the current version 0.13.0 of opentelemetry. We just updated the sdk::Resource::merge logic to reflect the latest spec in open-telemetry/opentelemetry-rust#537. This will be included in the next release, though.

///
/// If a `sdk::Config` does not exist on the `PipelineBuilder` one will be created.
///
/// 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>>(self, name: T) -> Self {
let config = match self.config {
Some(config) => config,
None => sdk::trace::Config::default(),
};
frigus02 marked this conversation as resolved.
Show resolved Hide resolved

let merged_resource = config
.resource
.merge(&sdk::Resource::new(vec![KeyValue::new(
"service.name",
frigus02 marked this conversation as resolved.
Show resolved Hide resolved
name.into(),
)]));
let config = config.with_resource(merged_resource);

PipelineBuilder {
config: Some(config),
..self
Expand Down