8000 refactor tracing context extraction by alexgallotta · Pull Request #615 · DataDog/datadog-lambda-extension · GitHub
[go: up one dir, main page]

Skip to content

refactor tracing context extraction #615

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 11 additions & 19 deletions bottlecap/src/lifecycle/invocation/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ use serde_json::{json, Value};
use tokio::sync::{mpsc::Sender, watch};
use tracing::debug;

use crate::traces::propagation::datadog_extraction::{
extract_tags_datadog_context, DATADOG_PARENT_ID_KEY, DATADOG_SAMPLING_PRIORITY_KEY,
DATADOG_SPAN_ID_KEY, DATADOG_TRACE_ID_KEY,
};
use crate::traces::propagation::extract_composite;
use crate::{
config::{self, AwsConfig},
lifecycle::invocation::{
Expand All @@ -26,17 +31,7 @@ use crate::{
},
tags::{lambda::tags::resolve_runtime_from_proc, provider},
telemetry::events::{ReportMetrics, RuntimeDoneMetrics, Status},
traces::{
context::SpanContext,
propagation::{
text_map_propagator::{
DatadogHeaderPropagator, DATADOG_PARENT_ID_KEY, DATADOG_SAMPLING_PRIORITY_KEY,
DATADOG_SPAN_ID_KEY, DATADOG_TRACE_ID_KEY,
},
DatadogCompositePropagator, Propagator,
},
trace_processor,
},
traces::{context::SpanContext, trace_processor},
};

pub const MS_TO_NS: f64 = 1_000_000.0;
Expand All @@ -61,7 +56,6 @@ pub struct Processor {
// Extracted span context from inferred span, headers, or payload
pub extracted_span_context: Option<SpanContext>,
// Used to extract the trace context from inferred span, headers, or payload
propagator: DatadogCompositePropagator,
// Helper to send enhanced metrics
enhanced_metrics: EnhancedMetrics,
// AWS configuration from the Lambda environment
Expand All @@ -85,15 +79,12 @@ impl Processor {
.get_canonical_resource_name()
.unwrap_or(String::from("aws.lambda"));

let propagator = DatadogCompositePropagator::new(Arc::clone(&config));

Processor {
context_buffer: ContextBuffer::default(),
inferrer: SpanInferrer::new(config.service_mapping.clone()),
span: create_empty_span(String::from("aws.lambda"), resource, service),
cold_start_span: None,
extracted_span_context: None,
propagator,
enhanced_metrics: EnhancedMetrics::new(metrics_aggregator, Arc::clone(&config)),
aws_config: aws_config.clone(),
tracer_detected: false,
Expand Down Expand Up @@ -463,18 +454,18 @@ impl Processor {
headers: &HashMap<String, String>,
payload_value: &Value,
) -> Option<SpanContext> {
if let Some(sc) = self.inferrer.get_span_context(&self.propagator) {
if let Some(sc) = self.inferrer.get_span_context(&self.config) {
return Some(sc);
}

if let Some(payload_headers) = payload_value.get("headers") {
if let Some(sc) = self.propagator.extract(payload_headers) {
if let Some(sc) = extract_composite(&self.config, payload_headers) {
debug!("Extracted trace context from event headers");
return Some(sc);
}
}

if let Some(sc) = self.propagator.extract(headers) {
if let Some(sc) = extract_composite(&self.config, headers) {
debug!("Extracted trace context from headers");
return Some(sc);
}
Expand Down Expand Up @@ -564,7 +555,7 @@ impl Processor {

// Extract tags from headers
// Used for 128 bit trace ids
tags = DatadogHeaderPropagator::extract_tags(headers);
tags = extract_tags_datadog_context(headers);
}

// We should always use the generated trace id from the tracer
Expand Down Expand Up @@ -644,6 +635,7 @@ impl Processor {
#[cfg(test)]
mod tests {
use super::*;
use crate::traces::propagation::datadog_extraction::DATADOG_TRACE_ID_KEY;
use crate::LAMBDA_RUNTIME_SLUG;
use base64::{engine::general_purpose::STANDARD, Engine};
use dogstatsd::aggregator::Aggregator;
Expand Down
17 changes: 11 additions & 6 deletions bottlecap/src/lifecycle/invocation/span_inferrer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::collections::HashMap;

use datadog_trace_protobuf::pb::Span;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use tracing::debug;

use crate::config::AwsConfig;
use crate::config::{AwsConfig, Config};

use crate::lifecycle::invocation::{
generate_span_id,
Expand All @@ -22,8 +22,9 @@ use crate::lifecycle::invocation::{
Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_ARN_TAG,
},
};
use crate::traces::context::SpanContext;
use crate::traces::propagation::extract_composite;
use crate::traces::span_pointers::SpanPointer;
use crate::traces::{context::SpanContext, propagation::Propagator};

#[derive(Default)]
pub struct SpanInferrer {
Expand Down Expand Up @@ -305,13 +306,17 @@ impl SpanInferrer {
/// If the carrier is set, it will try to extract the span context,
/// otherwise it will
///
pub fn get_span_context(&self, propagator: &impl Propagator) -> Option<SpanContext> {
pub fn get_span_context(&self, config: &Arc<Config>) -> Option<SpanContext> {
// Step Functions `SpanContext` is deterministically generated
if self.generated_span_context.is_some() {
return self.generated_span_context.clone();
}

if let Some(sc) = self.carrier.as_ref().and_then(|c| propagator.extract(c)) {
if let Some(sc) = self
.carrier
.as_ref()
.and_then(|c| extract_composite(config, c))
{
debug!("Extracted trace context from inferred span");
return Some(sc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};

use super::DATADOG_CARRIER_KEY;
use crate::traces::propagation::datadog_extraction::{
extract_tags_datadog_context, DATADOG_HIGHER_ORDER_TRACE_ID_BITS_KEY, DATADOG_TAGS_KEY,
};
use crate::{
lifecycle::invocation::triggers::{
ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG,
},
traces::{
context::{Sampling, SpanContext},
propagation::text_map_propagator::{
DatadogHeaderPropagator, DATADOG_HIGHER_ORDER_TRACE_ID_BITS_KEY, DATADOG_TAGS_KEY,
},
},
traces::context::{Sampling, SpanContext},
};

use super::DATADOG_CARRIER_KEY;

pub const DATADOG_LEGACY_LAMBDA_PAYLOAD: &str = "Payload";

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
Expand Down Expand Up @@ -153,7 +150,7 @@ impl StepFunctionEvent {
.parse()
.unwrap_or(Self::generate_trace_id(self.execution.id.clone()).0);

let tags = DatadogHeaderPropagator::extract_tags(&HashMap::from([(
let tags = extract_tags_datadog_context(&HashMap::from([(
DATADOG_TAGS_KEY.to_string(),
trace_tags.to_string(),
)]));
Expand Down Expand Up @@ -261,7 +258,7 @@ impl ServiceNameResolver for StepFunctionEvent {
mod tests {
use super::*;
use crate::lifecycle::invocation::triggers::test_utils::read_json_file;
use crate::traces::propagation::text_map_propagator::DATADOG_SAMPLING_DECISION_KEY;
use crate::traces::propagation::datadog_extraction::DATADOG_SAMPLING_DECISION_KEY;

#[test]
fn test_new_event() {
Expand Down
2 changes: 1 addition & 1 deletion bottlecap/src/lifecycle/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tokio::sync::Mutex;
use tracing::{debug, error, warn};

use crate::l 60CB ifecycle::invocation::processor::Processor as InvocationProcessor;
use crate::traces::propagation::text_map_propagator::{
use crate::traces::propagation::datadog_extraction::{
DATADOG_HIGHER_ORDER_TRACE_ID_BITS_KEY, DATADOG_SAMPLING_PRIORITY_KEY, DATADOG_TAGS_KEY,
DATADOG_TRACE_ID_KEY,
};
Expand Down
Loading
Loading
0