8000 Update opentelemetry.md (#28984) · DataDog/documentation@9b3771c · GitHub
[go: up one dir, main page]

Skip to content

Commit 9b3771c

Browse files
mtoffl01mabdinur
andauthored
Update opentelemetry.md (#28984)
Co-authored-by: mabdinur <munir.abdinur@datadoghq.com>
1 parent 523f5cf commit 9b3771c

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

content/en/tracing/other_telemetry/connect_logs_and_traces/opentelemetry.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ further_reading:
2121
text: 'Ease troubleshooting with cross product correlation.'
2222
---
2323

24-
<div class="alert alert-info">If you are using the latest versions of Datadog tracing libraries, Datadog automatically links OpenTelemetry traces and logs using <code>TraceId</code>. If you're using older versions, follow the steps on this page to manually correlate traces and logs.</div>
25-
2624
Connecting OpenTelemetry language SDK logs and traces within Datadog is similar to connecting [Datadog SDK logs and traces][1], with a few additional steps:
2725

28-
1. OpenTelemetry `TraceId` and `SpanId` properties differ from Datadog conventions. Therefore it's necessary to translate `TraceId` and `SpanId` from their OpenTelemetry formats ([a 128bit unsigned int and 64bit unsigned int represented as a 32-hex-character and 16-hex-character lowercase string, respectively][2]) into their Datadog Formats([a 64bit unsigned int][3]).
26+
1. Datadog supports the OpenTelemetry standard of 128-bit Trace IDs by default, but the `SpanId` formats differ between vendors. Therefore, it's necessary to translate the `SpanId` from its OpenTelemetry format ([a 16-hex-character lowercase string][2]) into its Datadog format ([a 64bit unsigned int][3]).
2927

3028
2. Ensure your logs are sent as JSON, because your language level logs must be turned into Datadog attributes for trace-log correlation to work.
3129

@@ -34,7 +32,7 @@ See the following examples for language-specific information about how to correl
3432
{{< tabs >}}
3533
{{% tab "Python" %}}
3634

37-
To manually correlate your traces with your logs, patch the logging module you are using with a processor that translates OpenTelemetry formatted `trace_id` and `span_id` into the Datadog format. The following example uses the [structlog logging library][1]. For other logging libraries, it may be more appropriate to [modify the Datadog SDK examples][2]. You can also find [an example OpenTelemetry instrumented Python application with trace and log correlation][3] in the `trace-examples` GitHub repository.
35+
To manually correlate your traces with your logs, patch the logging module you are using with a processor that translates the OpenTelemetry formatted `span_id` into Datadog format. The following example uses the [structlog logging library][1]. For other logging libraries, it may be more appropriate to [modify the Datadog SDK examples][2]. You can also find [an example OpenTelemetry instrumented Python application with trace and log correlation][3] in the `trace-examples` GitHub repository.
3836

3937
```python
4038
# ########## injection.py
@@ -43,15 +41,19 @@ from opentelemetry import trace
4341
class CustomDatadogLogProcessor(object):
4442
def __call__(self, logger, method_name, event_dict):
4543
# An example of adding datadog formatted trace context to logs
46-
# from: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/b53b9a012f76c4fc883c3c245fddc29142706d0d/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/propagator.py#L122-L129
44+
# from: https://github.com/open-telemetry/opentelemetry-python-contrib/blob/b53b9a012f76c4fc883c3c245fddc29142706d0d/exporter/opentelemetry-exporter-datadog/src/opentelemetry/exporter/datadog/propagator.py#L127-L129
4745
current_span = trace.get_current_span()
4846
if not current_span.is_recording():
4947
return event_dict
5048

5149
context = current_span.get_span_context() if current_span is not None else None
5250
if context is not None:
53-
event_dict["dd.trace_id"] = str(context.trace_id & 0xFFFFFFFFFFFFFFFF)
51+
# if trace_id > 2**64 store as hex otherwise store the id as an integer
52+
event_dict["dd.trace_id"] = str(context.trace_id) if context.trace_id < 2**64 else f"{context.trace_id:032x}"
5453
event_dict["dd.span_id"] = str(context.span_id)
54+
event_dict["dd.service"] = span.attributes.get("service.name")
55+
event_dict["dd.env"] = span.attributes.get("deployment.environment")
56+
event_dict["dd.version"] = span.attributes.get("service.version")
5557

5658
return event_dict
5759
# ##########
@@ -130,7 +132,7 @@ After setting up the Grok Parser Rule, add the `Trace Id Remapper` and `Span Id
130132

131133
{{% tab "Node.js" %}}
132134

133-
To manually correlate your traces with your logs, patch the logging module you are using with a processor that translates OpenTelemetry formatted `trace_id` and `span_id` into the Datadog format. The following example uses the [winston logging library][1]. For other logging libraries, it may be more appropriate to [modify the Datadog SDK examples][2]. You can also find [an example OpenTelemetry instrumented Node.js application with trace and log correlation][3] in the `trace-examples` GitHub repository.
135+
To manually correlate your traces with your logs, patch the logging module you are using with a processor that translates the OpenTelemetry formatted `span_id` into Datadog format. The following example uses the [winston logging library][1]. For other logging libraries, it may be more appropriate to [modify the Datadog SDK examples][2]. You can also find [an example OpenTelemetry instrumented Node.js application with trace and log correlation][3] in the `trace-examples` GitHub repository.
134136

135137
```js
136138
// ########## logger.js
@@ -144,9 +146,7 @@ const tracingFormat = function () {
144146
return winston.format(info => {
145147
const span = opentelemetry.trace.getSpan(opentelemetry.context.active());
146148
if (span) {
147-
const { spanId, traceId } = span.spanContext();
148-
const traceIdEnd = traceId.slice(traceId.length / 2);
149-
info['dd.trace_id'] = BigInt(`0x${traceIdEnd}`).toString();
149+
const { spanId } = span.spanContext();
150150
info['dd.span_id'] = BigInt(`0x${spanId}`).toString();
151151
}
152152
return info;
@@ -181,7 +181,7 @@ logger.info("Example log line with trace correlation info")
181181

182182
{{% tab "Ruby" %}}
183183

184-
To manually correlate your traces with your logs, patch the logging module you are using with a processor that translates OpenTelemetry formatted `trace_id` and `span_id` into the Datadog format. The following example uses the [Ruby Standard Logging Library][1]. For Rails applications or other logging libraries, it may be more appropriate to [modify the Datadog SDK examples][2]. You can also find [an example OpenTelemetry instrumented Ruby application with trace and log correlation][3] in the `trace-examples` GitHub repository.
184+
To manually correlate your traces with your logs, patch the logging module you are using with a processor that translates the OpenTelemetry formatted `span_id` into Datadog format. The following example uses the [Ruby Standard Logging Library][1]. For Rails applications or other logging libraries, it may be more appropriate to [modify the Datadog SDK examples][2]. You can also find [an example OpenTelemetry instrumented Ruby application with trace and log correlation][3] in the `trace-examples` GitHub repository.
185185

186186
```ruby
187187
logger = Logger.new(STDOUT)
@@ -190,7 +190,8 @@ original_formatter = Logger::Formatter.new
190190
logger.formatter = proc do |severity, datetime, progname, msg|
191191
current_span = OpenTelemetry::Trace.current_span(OpenTelemetry::Context.current).context
192192

193-
dd_trace_id = current_span.trace_id.unpack1('H*')[16, 16].to_i(16).to_s
193+
trace_id_int = current_span.trace_id.unpack1('H*').to_i(16)
194+
dd_trace_id = trace_id_int < 2**64 ? trace_id_int.to_s : format("%032x", trace_id_int)
194195
dd_span_id = current_span.span_id.unpack1('H*').to_i(16).to_s
195196

196197
if current_span
@@ -213,19 +214,20 @@ logger.info("Example log line with trace correlation info")
213214

214215
{{% tab "Java" %}}
215216

216-
To manually correlate your traces with your logs, first enable the [openTelemetry-java-instrumentation Logger MDC Instrumentation][1]. Then, patch the logging module you are using with a processor that translates OpenTelemetry formatted `trace_id` and `span_id` into the Datadog format. The following example uses [Spring Boot and Logback][2]. For other logging libraries, it may be more appropriate to [modify the Datadog SDK examples][3].
217+
To manually correlate your traces with your logs, first enable the [openTelemetry-java-instrumentation Logger MDC Instrumentation][1]. Then, patch the logging module you are using with a processor that translates the OpenTelemetry formatted `span_id` into Datadog format. The following example uses [Spring Boot and Logback][2]. For other logging libraries, it may be more appropriate to [modify the Datadog SDK examples][3].
217218

218219
```java
219220
String traceIdValue = Span.current().getSpanContext().getTraceId();
220221
String traceIdHexString = traceIdValue.substring(traceIdValue.length() - 16 );
221-
long datadogTraceId = Long.parseUnsignedLong(traceIdHexString, 16);
222-
String datadogTraceIdString = Long.toUnsignedString(datadogTraceId);
223222

224223
String spanIdHexString = Span.current().getSpanContext().getSpanId();
225224
long datadogSpanId = Long.parseUnsignedLong(spanIdHexString, 16);
226225
String datadogSpanIdString = Long.toUnsignedString(datadogSpanId);
227226

228-
logging.pattern.console = %d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg dd.trace_id=%X{datadogTraceIdString} dd.span_id=%X{datadogSpanIdString} %n
227+
MDC.put("dd.trace_id", traceIdHexString);
228+
MDC.put("dd.span_id", datadogSpanIdString);
229+
230+
logger.info("Log Message");
229231
```
230232

231233
See [Java Log Collection][4] on how to send your Java logs to Datadog.
@@ -251,7 +253,7 @@ For trace and log correlation in PHP, modify the [Datadog SDK PHP examples][1] t
251253

252254
{{% tab "Go" %}}
253255

254-
To manually correlate your traces with your logs, patch the logging module you are using with a function that translates OpenTelemetry formatted `trace_id` and `span_id` into the Datadog format. The following example uses the [logrus Library][1].
256+
To manually correlate your traces with your logs, patch the logging module you are using with a function that translates the OpenTelemetry formatted `span_id` into Datadog format. The following example uses the [logrus Library][1].
255257

256258
```go
257259
package main
@@ -272,8 +274,8 @@ func main() {
272274
log.SetFormatter(&log.JSONFormatter{})
273275

274276
standardFields := log.Fields{
275-
"dd.trace_id": convertTraceID(span.SpanContext().TraceID().String()),
276-
"dd.span_id": convertTraceID(span.SpanContext().SpanID().String()),
277+
"dd.trace_id": span.SpanContext().TraceID().String(),
278+
"dd.span_id": convertSpanID(span.SpanContext().SpanID().String()),
277279
"dd.service": "serviceName",
278280
"dd.env": "serviceEnv",
279281
"dd.version": "serviceVersion",
@@ -282,7 +284,7 @@ func main() {
282284
log.WithFields(standardFields).WithContext(ctx).Info("hello world")
283285
}
284286

285-
func convertTraceID(id string) string {
287+
func convertSpanID(id string) string {
286288
if len(id) < 16 {
287289
return ""
288290
}
@@ -309,13 +311,12 @@ func convertTraceID(id string) string {
309311

310312
{{% tab ".NET" %}}
311313

312-
To manually correlate traces with logs, convert the OpenTelemetry `TraceId` and `SpanId` into the format used by Datadog. Add those IDs to your logs under the `dd.trace_id` and `dd.span_id` attributes. The following example uses the [Serilog library][1], and shows how to convert the OpenTelemetry (`System.DiagnosticSource.Activity`) trace and span IDs into Datadog's required format:
314+
To manually correlate traces with logs, convert the OpenTelemetry `SpanId` into the format used by Datadog. Add the trace ID to your logs along with the converted span ID, under `dd.trace_id` and `dd.span_id` attributes, respectively. The following example uses the [Serilog library][1], and shows how to convert the OpenTelemetry (`System.DiagnosticSource.Activity`) span ID into Datadog's required format:
313315

314316
```csharp
315-
var stringTraceId = Activity.Current.TraceId.ToString();
316-
var stringSpanId = Activity.Current.SpanId.ToString();
317+
var ddTraceId = Activity.Current.TraceId.ToString();
317318

318-
var ddTraceId = Convert.ToUInt64(stringTraceId.Substring(16), 16).ToString();
319+
var stringSpanId = Activity.Current.SpanId.ToString();
319320
var ddSpanId = Convert.ToUInt64(stringSpanId, 16).ToString();
320321

321322
using (LogContext.PushProperty("dd.trace_id", ddTraceId))

0 commit comments

Comments
 (0)
0