Skip to content

Commit 476378a

Browse files
authored
fix: Accept inbound tracing headers in Kafka consume method instrumentation (#2488)
* Process inbound tracing headers on Kafka consumer side * Assert on tracing supportability metrics in Kafka integration test * Remove stray comment
1 parent d626535 commit 476378a

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/Kafka/KafkaConsumerWrapper.cs

+16-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Concurrent;
6+
using System.Collections.Generic;
67
using System.Text;
78
using Confluent.Kafka;
89
using NewRelic.Agent.Api;
@@ -65,15 +66,15 @@ public AfterWrappedMethodDelegate BeforeWrappedMethod(InstrumentedMethodCall ins
6566
segment.SetMessageBrokerDestination(topic);
6667
transaction.SetKafkaMessageBrokerTransactionName(MessageBrokerDestinationType.Topic, BrokerVendorName, topic);
6768

68-
// get the Message.Headers property and add distributed trace headers
69+
// get the Message.Headers property and process distributed trace headers
6970
var messageAccessor = MessageAccessorDictionary.GetOrAdd(type, GetMessageAccessorFunc);
7071
var messageAsObject = messageAccessor(resultAsObject);
7172

7273
var headersSize = 0L;
7374
if (messageAsObject is MessageMetadata messageMetaData)
7475
{
7576
headersSize = GetHeadersSize(messageMetaData.Headers);
76-
transaction.InsertDistributedTraceHeaders(messageMetaData, DistributedTraceHeadersSetter);
77+
transaction.AcceptDistributedTraceHeaders(messageMetaData, DistributedTraceHeadersGetter, TransportType.Kafka);
7778
}
7879

7980
ReportSizeMetrics(agent, transaction, topic, headersSize, messageAsObject);
@@ -133,14 +134,22 @@ private static Func<object, object> GetKeyAccessorFunc(Type t) =>
133134
private static Func<object, object> GetValueAccessorFunc(Type t) =>
134135
VisibilityBypasser.Instance.GeneratePropertyAccessor<object>(t, "Value");
135136

136-
private static void DistributedTraceHeadersSetter(MessageMetadata carrier, string key, string value)
137+
private static IEnumerable<string> DistributedTraceHeadersGetter(MessageMetadata carrier, string key)
137138
{
138-
carrier.Headers ??= new Headers();
139-
if (!string.IsNullOrEmpty(key))
139+
if (carrier.Headers != null)
140140
{
141-
carrier.Headers.Remove(key);
142-
carrier.Headers.Add(key, Encoding.ASCII.GetBytes(value));
141+
var headerValues = new List<string>();
142+
foreach (var item in carrier.Headers)
143+
{
144+
if (item.Key.Equals(key, StringComparison.OrdinalIgnoreCase))
145+
{
146+
var decodedHeaderValue = Encoding.UTF8.GetString(item.GetValueBytes());
147+
headerValues.Add(decodedHeaderValue);
148+
}
149+
}
150+
return headerValues;
143151
}
152+
return null;
144153
}
145154

146155
private static long TryGetSize(object obj)

tests/Agent/IntegrationTests/ContainerIntegrationTests/Tests/KafkaTests.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public void Test()
8383
new Assertions.ExpectedMetric { metricName = consumeTransactionName, callCount = 2 },
8484
new Assertions.ExpectedMetric { metricName = messageBrokerConsume, callCount = 2 },
8585
new Assertions.ExpectedMetric { metricName = messageBrokerConsume, metricScope = consumeTransactionName, callCount = 2 },
86+
new Assertions.ExpectedMetric { metricName = "Supportability/TraceContext/Create/Success", callCount = 2 },
87+
new Assertions.ExpectedMetric { metricName = "Supportability/TraceContext/Accept/Success", callCount = 2 },
8688
};
8789

8890
NrAssert.Multiple(
@@ -93,7 +95,7 @@ public void Test()
9395
() => Assert.True(consumeTxnSpan.UserAttributes.ContainsKey("kafka.consume.byteCount")),
9496
() => Assert.InRange((long)consumeTxnSpan.UserAttributes["kafka.consume.byteCount"], 450, 470), // includes headers
9597
() => Assert.True(consumeTxnSpan.IntrinsicAttributes.ContainsKey("traceId")),
96-
() => Assert.False(consumeTxnSpan.IntrinsicAttributes.ContainsKey("parentId"))
98+
() => Assert.True(consumeTxnSpan.IntrinsicAttributes.ContainsKey("parentId"))
9799
);
98100
}
99101

0 commit comments

Comments
 (0)