|
| 1 | +//! # OpenTelemetry Datadog Exporter |
| 2 | +//! |
| 3 | +//! An OpenTelemetry exporter implementation |
| 4 | +//! |
| 5 | +//! See the [Datadog Docs](https://docs.datadoghq.com/agent/) for information on how to run the datadog-agent |
| 6 | +//! |
| 7 | +//! ## Quirks |
| 8 | +//! |
| 9 | +//! There are currently some incompatibilities between Datadog and OpenTelemetry, and this manifests |
| 10 | +//! as minor quirks to this exporter. |
| 11 | +//! |
| 12 | +//! Firstly Datadog uses operation_name to describe what OpenTracing would call a component. |
| 13 | +//! Or to put it another way, in OpenTracing the operation / span name's are relatively |
| 14 | +//! granular and might be used to identify a specific endpoint. In datadog, however, they |
| 15 | +//! are less granular - it is expected in Datadog that a service will have single |
| 16 | +//! primary span name that is the root of all traces within that service, with an additional piece of |
| 17 | +//! metadata called resource_name providing granularity - https://docs.datadoghq.com/tracing/guide/configuring-primary-operation/ |
| 18 | +//! |
| 19 | +//! The Datadog Golang API takes the approach of using a `resource.name` OpenTelemetry attribute to set the |
| 20 | +//! resource_name - https://github.com/DataDog/dd-trace-go/blob/ecb0b805ef25b00888a2fb62d465a5aa95e7301e/ddtrace/opentracer/tracer.go#L10 |
| 21 | +//! |
| 22 | +//! Unfortunately, this breaks compatibility with other OpenTelemetry exporters which expect |
| 23 | +//! a more granular operation name - as per the OpenTracing specification. |
| 24 | +//! |
| 25 | +//! This exporter therefore takes a different approach of naming the span with the name of the |
| 26 | +//! tracing provider, and using the span name to set the resource_name. This should in most cases |
| 27 | +//! lead to the behaviour that users expect. |
| 28 | +//! |
| 29 | +//! Datadog additionally has a span_type string that alters the rendering of the spans in the web UI. |
| 30 | +//! This can be set as the `span.type` OpenTelemetry span attribute. |
| 31 | +//! |
| 32 | +//! For standard values see here - https://github.com/DataDog/dd-trace-go/blob/ecb0b805ef25b00888a2fb62d465a5aa95e7301e/ddtrace/ext/app_types.go#L31 |
| 33 | +//! |
| 34 | +//! ## Performance |
| 35 | +//! |
| 36 | +//! For optimal performance, a batch exporter is recommended as the simple |
| 37 | +//! exporter will export each span synchronously on drop. You can enable the |
| 38 | +//! [`tokio`] or [`async-std`] features to have a batch exporter configured for |
| 39 | +//! you automatically for either executor when you install the pipeline. |
| 40 | +//! |
| 41 | +//! ```toml |
| 42 | +//! [dependencies] |
| 43 | +//! opentelemetry = { version = "*", features = ["tokio"] } |
| 44 | +//! opentelemetry-datadog = "*" |
| 45 | +//! ``` |
| 46 | +//! |
| 47 | +//! [`tokio`]: https://tokio.rs |
| 48 | +//! [`async-std`]: https://async.rs |
| 49 | +//! |
| 50 | +//! ## Kitchen Sink Full Configuration |
| 51 | +//! |
| 52 | +//! Example showing how to override all configuration options. See the |
| 53 | +//! [`DatadogPipelineBuilder`] docs for details of each option. |
| 54 | +//! |
| 55 | +//! [`DatadogPipelineBuilder`]: struct.DatadogPipelineBuilder.html |
| 56 | +//! |
| 57 | +//! ```no_run |
| 58 | +//! use opentelemetry::api::{KeyValue, Tracer}; |
| 59 | +//! use opentelemetry::sdk::{trace, IdGenerator, Resource, Sampler}; |
| 60 | +//! |
| 61 | +//! fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 62 | +//! let tracer = opentelemetry_contrib::datadog::new_pipeline() |
| 63 | +//! .with_service_name("my_app") |
| 64 | +//! .with_version(opentelemetry_contrib::datadog::ApiVersion::Version05) |
| 65 | +//! .with_agent_endpoint("http://localhost:8126") |
| 66 | +//! .with_trace_config( |
| 67 | +//! trace::config() |
| 68 | +//! .with_default_sampler(Sampler::AlwaysOn) |
| 69 | +//! .with_id_generator(IdGenerator::default()) |
| 70 | +//! ) |
| 71 | +//! .install()?; |
| 72 | +//! |
| 73 | +//! tracer.in_span("doing_work", |cx| { |
| 74 | +//! // Traced app logic here... |
| 75 | +//! }); |
| 76 | +//! |
| 77 | +//! Ok(()) |
| 78 | +//! } |
| 79 | +//! ``` |
| 80 | +#![deny(missing_docs, unreachable_pub, missing_debug_implementations)] |
| 81 | +#![cfg_attr(test, deny(warnings))] |
| 82 | + |
| 83 | +mod intern; |
| 84 | +mod model; |
| 85 | + |
| 86 | +pub use model::ApiVersion; |
| 87 | + |
| 88 | +use opentelemetry::{api::TracerProvider, exporter::trace, global, sdk}; |
| 89 | +use reqwest::header::CONTENT_TYPE; |
| 90 | +use reqwest::Url; |
| 91 | +use std::error::Error; |
| 92 | +use std::sync::Arc; |
| 93 | + |
| 94 | +/// Default Datadog collector endpoint |
| 95 | +const DEFAULT_AGENT_ENDPOINT: &str = "http://127.0.0.1:8126"; |
| 96 | + |
| 97 | +/// Default service name if no service is configured. |
| 98 | +const DEFAULT_SERVICE_NAME: &str = "OpenTelemetry"; |
| 99 | + |
| 100 | +/// Datadog span exporter |
| 101 | +#[derive(Debug)] |
| 102 | +pub struct DatadogExporter { |
| 103 | + client: reqwest::blocking::Client, |
| 104 | + request_url: Url, |
| 105 | + service_name: String, |
| 106 | + version: ApiVersion, |
| 107 | +} |
| 108 | + |
| 109 | +impl DatadogExporter { |
| 110 | + fn new(service_name: String, agent_endpoint: Url, version: ApiVersion) -> Self { |
| 111 | + let mut request_url = agent_endpoint; |
| 112 | + request_url.set_path(version.path()); |
| 113 | + |
| 114 | + DatadogExporter { |
| 115 | + client: reqwest::blocking::Client::new(), |
| 116 | + request_url, |
| 117 | + service_name, |
| 118 | + version, |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/// Create a new Datadog exporter pipeline builder. |
| 124 | +pub fn new_pipeline() -> DatadogPipelineBuilder { |
| 125 | + DatadogPipelineBuilder::default() |
| 126 | +} |
| 127 | + |
| 128 | +/// Builder for `ExporterConfig` struct. |
| 129 | +#[derive(Debug)] |
| 130 | +pub struct DatadogPipelineBuilder { |
| 131 | + service_name: String, |
| 132 | + agent_endpoint: String, |
| 133 | + trace_config: Option<sdk::Config>, |
| 134 | + version: ApiVersion, |
| 135 | +} |
| 136 | + |
| 137 | +impl Default for DatadogPipelineBuilder { |
| 138 | + fn default() -> Self { |
| 139 | + DatadogPipelineBuilder { |
| 140 | + service_name: DEFAULT_SERVICE_NAME.to_string(), |
| 141 | + agent_endpoint: DEFAULT_AGENT_ENDPOINT.to_string(), |
| 142 | + trace_config: None, |
| 143 | + version: ApiVersion::Version05, |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +impl DatadogPipelineBuilder { |
| 149 | + /// Create `ExporterConfig` struct from current `ExporterConfigBuilder` |
| 150 | + pub fn install(mut self) -> Result<sdk::Tracer, Box<dyn Error>> { |
| 151 | + let exporter = DatadogExporter::new( |
| 152 | + self.service_name.clone(), |
| 153 | + self.agent_endpoint.parse()?, |
| 154 | + self.version, |
| 155 | + ); |
| 156 | + |
| 157 | + let mut provider_builder = sdk::TracerProvider::builder().with_exporter(exporter); |
| 158 | + if let Some(config) = self.trace_config.take() { |
| 159 | + provider_builder = provider_builder.with_config(config); |
| 160 | + } |
| 161 | + let provider = provider_builder.build(); |
| 162 | + let tracer = provider.get_tracer("opentelemetry-datadog", Some(env!("CARGO_PKG_VERSION"))); |
| 163 | + global::set_provider(provider); |
| 164 | + |
| 165 | + Ok(tracer) |
| 166 | + } |
| 167 | + |
| 168 | + /// Assign the service name under which to group traces |
| 169 | + pub fn with_service_name<T: Into<String>>(mut self, name: T) -> Self { |
| 170 | + self.service_name = name.into(); |
| 171 | + self |
| 172 | + } |
| 173 | + |
| 174 | + /// Assign the Datadog collector endpoint |
| 175 | + pub fn with_agent_endpoint<T: Into<String>>(mut self, endpoint: T) -> Self { |
| 176 | + self.agent_endpoint = endpoint.into(); |
| 177 | + self |
| 178 | + } |
| 179 | + |
| 180 | + /// Assign the SDK trace configuration |
| 181 | + pub fn with_trace_config(mut self, config: sdk::Config) -> Self { |
| 182 | + self.trace_config = Some(config); |
| 183 | + self |
| 184 | + } |
| 185 | + |
| 186 | + /// Set version of Datadog trace ingestion API |
| 187 | + pub fn with_version(mut self, version: ApiVersion) -> Self { |
| 188 | + self.version = version; |
| 189 | + self |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +impl trace::SpanExporter for DatadogExporter { |
| 194 | + /// Export spans to datadog-agent |
| 195 | + fn export(&self, batch: Vec<Arc<trace::SpanData>>) -> trace::ExportResult { |
| 196 | + let data = match self.version.encode(&self.service_name, batch) { |
| 197 | + Ok(data) => data, |
| 198 | + Err(_) => return trace::ExportResult::FailedNotRetryable, |
| 199 | + }; |
| 200 | + |
| 201 | + let resp = self |
| 202 | + .client |
| 203 | + .post(self.request_url.clone()) |
| 204 | + .header(CONTENT_TYPE, self.version.content_type()) |
| 205 | + .body(data) |
| 206 | + .send(); |
| 207 | + |
| 208 | + match resp { |
| 209 | + Ok(response) if response.status().is_success() => trace::ExportResult::Success, |
| 210 | + _ => trace::ExportResult::FailedRetryable, |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments