Skip to content

Commit 9b42ce5

Browse files
committed
rebase and backport context loop fix
1 parent d365eba commit 9b42ce5

8 files changed

Lines changed: 681 additions & 762 deletions

File tree

bottlecap/src/lifecycle/invocation/processor.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use serde_json::{json, Value};
1212
use tokio::sync::{mpsc::Sender, watch};
1313
use tracing::debug;
1414

15+
use crate::traces::propagation::extract_composite;
1516
use crate::{
1617
config::{self, AwsConfig},
1718
lifecycle::invocation::{
@@ -28,16 +29,10 @@ use crate::{
2829
telemetry::events::{ReportMetrics, RuntimeDoneMetrics, Status},
2930
traces::{
3031
context::SpanContext,
31-
propagation::{
32-
text_map_propagator::{
33-
DatadogHeaderPropagator, DATADOG_PARENT_ID_KEY, DATADOG_SAMPLING_PRIORITY_KEY,
34-
DATADOG_SPAN_ID_KEY, DATADOG_TRACE_ID_KEY,
35-
},
36-
DatadogCompositePropagator, Propagator,
37-
},
3832
trace_processor,
3933
},
4034
};
35+
use crate::traces::propagation::datadog_propagation::{extract_tags_datadog_context, DATADOG_PARENT_ID_KEY, DATADOG_SAMPLING_PRIORITY_KEY, DATADOG_SPAN_ID_KEY, DATADOG_TRACE_ID_KEY};
4136

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

88-
let propagator = DatadogCompositePropagator::new(Arc::clone(&config));
89-
9082
Processor {
9183
context_buffer: ContextBuffer::default(),
9284
inferrer: SpanInferrer::new(config.service_mapping.clone()),
9385
span: create_empty_span(String::from("aws.lambda"), resource, service),
9486
cold_start_span: None,
9587
extracted_span_context: None,
96-
propagator,
9788
enhanced_metrics: EnhancedMetrics::new(metrics_aggregator, Arc::clone(&config)),
9889
aws_config: aws_config.clone(),
9990
tracer_detected: false,
@@ -463,18 +454,18 @@ impl Processor {
463454
headers: &HashMap<String, String>,
464455
payload_value: &Value,
465456
) -> Option<SpanContext> {
466-
if let Some(sc) = self.inferrer.get_span_context(&self.propagator) {
457+
if let Some(sc) = self.inferrer.get_span_context(&self.config) {
467458
return Some(sc);
468459
}
469460

470461
if let Some(payload_headers) = payload_value.get("headers") {
471-
if let Some(sc) = self.propagator.extract(payload_headers) {
462+
if let Some(sc) = extract_composite(&self.config, payload_headers) {
472463
debug!("Extracted trace context from event headers");
473464
return Some(sc);
474465
}
475466
}
476467

477-
if let Some(sc) = self.propagator.extract(headers) {
468+
if let Some(sc) = extract_composite(&self.config, headers) {
478469
debug!("Extracted trace context from headers");
479470
return Some(sc);
480471
}
@@ -564,7 +555,7 @@ impl Processor {
564555

565556
// Extract tags from headers
566557
// Used for 128 bit trace ids
567-
tags = DatadogHeaderPropagator::extract_tags(headers);
558+
tags = extract_tags_datadog_context(headers);
568559
}
569560

570561
// We should always use the generated trace id from the tracer
@@ -648,6 +639,7 @@ mod tests {
648639
use base64::{engine::general_purpose::STANDARD, Engine};
649640
use dogstatsd::aggregator::Aggregator;
650641
use dogstatsd::metric::EMPTY_TAGS;
642+
use crate::traces::propagation::datadog_propagation::DATADOG_TRACE_ID_KEY;
651643

652644
fn setup() -> Processor {
653645
let aws_config = AwsConfig {

bottlecap/src/lifecycle/invocation/span_inferrer.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use std::collections::HashMap;
2-
31
use datadog_trace_protobuf::pb::Span;
42
use serde_json::Value;
3+
use std::collections::HashMap;
4+
use std::sync::Arc;
55
use tracing::debug;
66

7-
use crate::config::AwsConfig;
7+
use crate::config::{AwsConfig, Config};
88

99
use crate::lifecycle::invocation::{
1010
generate_span_id,
@@ -22,8 +22,9 @@ use crate::lifecycle::invocation::{
2222
Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_ARN_TAG,
2323
},
2424
};
25+
use crate::traces::context::SpanContext;
26+
use crate::traces::propagation::extract_composite;
2527
use crate::traces::span_pointers::SpanPointer;
26-
use crate::traces::{context::SpanContext, propagation::Propagator};
2728

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

314-
if let Some(sc) = self.carrier.as_ref().and_then(|c| propagator.extract(c)) {
315+
if let Some(sc) = self
316+
.carrier
317+
.as_ref()
318+
.and_then(|c| extract_composite(config, c))
319+
{
315320
debug!("Extracted trace context from inferred span");
316321
return Some(sc);
317322
}

bottlecap/src/lifecycle/invocation/triggers/step_function_event.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ use serde::{Deserialize, Serialize};
44
use serde_json::Value;
55
use sha2::{Digest, Sha256};
66

7+
use super::DATADOG_CARRIER_KEY;
78
use crate::{
89
lifecycle::invocation::triggers::{
910
ServiceNameResolver, Trigger, FUNCTION_TRIGGER_EVENT_SOURCE_TAG,
1011
},
1112
traces::{
1213
context::{Sampling, SpanContext},
13-
propagation::text_map_propagator::{
14-
DatadogHeaderPropagator, DATADOG_HIGHER_ORDER_TRACE_ID_BITS_KEY, DATADOG_TAGS_KEY,
15-
},
1614
},
1715
};
18-
19-
use super::DATADOG_CARRIER_KEY;
16+
use crate::traces::propagation::datadog_propagation::{extract_tags_datadog_context, DATADOG_HIGHER_ORDER_TRACE_ID_BITS_KEY, DATADOG_TAGS_KEY};
2017

2118
pub const DATADOG_LEGACY_LAMBDA_PAYLOAD: &str = "Payload";
2219

@@ -153,7 +150,7 @@ impl StepFunctionEvent {
153150
.parse()
154151
.unwrap_or(Self::generate_trace_id(self.execution.id.clone()).0);
155152

156-
let tags = DatadogHeaderPropagator::extract_tags(&HashMap::from([(
153+
let tags = extract_tags_datadog_context(&HashMap::from([(
157154
DATADOG_TAGS_KEY.to_string(),
158155
trace_tags.to_string(),
159156
)]));
@@ -261,7 +258,7 @@ impl ServiceNameResolver for StepFunctionEvent {
261258
mod tests {
262259
use super::*;
263260
use crate::lifecycle::invocation::triggers::test_utils::read_json_file;
264-
use crate::traces::propagation::text_map_propagator::DATADOG_SAMPLING_DECISION_KEY;
261+
use crate::traces::propagation::datadog_propagation::DATADOG_SAMPLING_DECISION_KEY;
265262

266263
#[test]
267264
fn test_new_event() {

bottlecap/src/lifecycle/listener.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ use tokio::sync::Mutex;
1414
use tracing::{debug, error, warn};
1515

1616
use crate::lifecycle::invocation::processor::Processor as InvocationProcessor;
17-
use crate::traces::propagation::text_map_propagator::{
18-
DATADOG_HIGHER_ORDER_TRACE_ID_BITS_KEY, DATADOG_SAMPLING_PRIORITY_KEY, DATADOG_TAGS_KEY,
19-
DATADOG_TRACE_ID_KEY,
20-
};
17+
use crate::traces::propagation::datadog_propagation::{DATADOG_HIGHER_ORDER_TRACE_ID_BITS_KEY, DATADOG_SAMPLING_PRIORITY_KEY, DATADOG_TAGS_KEY, DATADOG_TRACE_ID_KEY};
2118

2219
const HELLO_PATH: &str = "/lambda/hello";
2320
const START_INVOCATION_PATH: &str = "/lambda/start-invocation";

0 commit comments

Comments
 (0)