Skip to content

Commit 5c2d05a

Browse files
Update manual_instrumentation.md adding .Net entry (#27424)
Co-authored-by: buraizu <bryce.eadie@datadoghq.com>
1 parent a4ed773 commit 5c2d05a

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

content/en/data_streams/manual_instrumentation.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,75 @@ set_consume_checkpoint(
9696

9797
{{< /code-block >}}
9898
{{% /tab %}}
99+
{{% tab ".Net" %}}
100+
The following example propagates the trace context. See [Trace Context Propagation][1] for more information.
101+
102+
<div class="alert alert-warning">
103+
<strong>Note:</strong> In async operations, this may not work as expected because the context derived from the incoming message can be lost when producing a new message in different threads.
104+
</div>
105+
106+
#### Producer configuration
107+
108+
{{< code-block lang="csharp" >}}
109+
using Datadog.Trace;
110+
111+
using (var scope = Tracer.Instance.StartActive("produce"))
112+
{
113+
var headers = new Headers();
114+
var msg = new Message { Value = "<ANY-VALUE>", Headers = headers};
115+
116+
new SpanContextInjector().InjectIncludingDsm(
117+
msg.Headers,
118+
SetHeader,
119+
scope.Span.Context,
120+
messageType: "<DATASTREAM-TYPE>",
121+
target: "<QUEUE-OR-TOPIC-NAME>"
122+
);
123+
124+
// Produce the message
125+
}
126+
127+
// Specific to how the header is modeled
128+
static void SetHeader(Headers headers, string key, string value)
129+
{
130+
headers.Add(new Header(key, value));
131+
}
132+
{{< /code-block >}}
133+
134+
#### Consumer configuration
135+
136+
{{< code-block lang="csharp" >}}
137+
using Datadog.Trace;
138+
139+
var startTime = DateTimeOffset.UtcNow;
140+
var msg = consumer.Consume();
141+
var parentContext = new SpanContextExtractor().ExtractIncludingDsm(
142+
msg.Headers,
143+
GetHeader,
144+
messageType: "<DATASTREAM-TYPE>",
145+
source: "<QUEUE-OR-TOPIC-NAME>"
146+
);
147+
148+
using (var scope = Tracer.Instance.StartActive("consume",
149+
new SpanCreationSettings,
150+
{
151+
Parent = parentContext,
152+
StartTime = startTime
153+
})
154+
)
155+
{
156+
// Do something with the message
157+
}
158+
159+
// Specific to how the header is modeled
160+
static IEnumerable<string?> GetHeader(Headers headers, string key)
161+
{
162+
yield return header.GetByKey(key);
163+
}
164+
{{< /code-block >}}
165+
166+
[1]: /tracing/trace_collection/trace_context_propagation/?tab=net#additional-use-cases
167+
{{% /tab %}}
99168
{{< /tabs >}}
100169
## Further Reading
101170

0 commit comments

Comments
 (0)