Skip to content

Commit a572724

Browse files
authored
Merge pull request #1907 from atlanhq/APP-8769
Add requestID into MDC as trace for logging
2 parents ae87c5c + 2188736 commit a572724

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

integration-tests/src/test/resources/log4j2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<Route>
1313
<File name="appender-${ctx:className}" fileName="${ctx:className:-threads}.log" append="false">
1414
<PatternLayout>
15-
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
15+
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} trace_id: %X{trace_id} span_id: %X{span_id} trace_flags: %X{trace_flags} - %msg%n</pattern>
1616
</PatternLayout>
1717
</File>
1818
</Route>

samples/packages/duplicate-detector/src/test/kotlin/DuplicateDetectorTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class DuplicateDetectorTest : PackageTest("dd") {
3030
runCustomPackage(
3131
DuplicateDetectorCfg(
3232
glossaryName = glossaryName,
33-
qnPrefix = "default/snowflake",
33+
qnPrefix = "default/mssql",
3434
controlConfigStrategy = "default",
3535
assetTypes = listOf(Table.TYPE_NAME, View.TYPE_NAME, MaterializedView.TYPE_NAME),
3636
),

sdk/src/main/java/com/atlan/net/ApiEventStreamResource.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.*;
1212
import lombok.ToString;
1313
import lombok.extern.slf4j.Slf4j;
14-
import org.slf4j.MDC;
1514

1615
/**
1716
* Base class for all event-stream response objects.
@@ -93,13 +92,13 @@ public static <T extends ApiEventStreamResource> T request(
9392
throws AtlanException {
9493
// Create a unique ID for every request, and add it to the logging context and header
9594
String requestId = UUID.randomUUID().toString();
96-
MDC.put("X-Atlan-Request-Id", requestId);
95+
ApiResource.injectTraceId(requestId);
9796
log.debug("({}) {} with: {}", method, url, body);
9897
T response =
9998
ApiResource.atlanResponseGetter.requestStream(client, method, url, body, clazz, options, requestId);
10099
// Ensure we reset the Atlan request ID, so we always have the context from the original
101100
// request that was made (even if it in turn triggered off other requests)
102-
MDC.put("X-Atlan-Request-Id", requestId);
101+
ApiResource.injectTraceId(requestId);
103102
return response;
104103
}
105104
}

sdk/src/main/java/com/atlan/net/ApiResource.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,12 @@ public static void request(
276276
throws AtlanException {
277277
// Create a unique ID for every request, and add it to the logging context and header
278278
String requestId = UUID.randomUUID().toString();
279-
MDC.put("X-Atlan-Request-Id", requestId);
279+
injectTraceId(requestId);
280280
log.debug("({}) {} with: {}", method, url, body);
281281
ApiResource.atlanResponseGetter.request(client, method, url, body, options, requestId);
282282
// Ensure we reset the Atlan request ID, so we always have the context from the original
283283
// request that was made (even if it in turn triggered off other requests)
284-
MDC.put("X-Atlan-Request-Id", requestId);
284+
injectTraceId(requestId);
285285
if (log.isDebugEnabled()) {
286286
log.debug(" ... empty response.");
287287
}
@@ -360,12 +360,12 @@ public static <T extends ApiResource> T request(
360360
throws AtlanException {
361361
// Create a unique ID for every request, and add it to the logging context and header
362362
String requestId = UUID.randomUUID().toString();
363-
MDC.put("X-Atlan-Request-Id", requestId);
363+
injectTraceId(requestId);
364364
log.debug("({}) {} with: {}", method, url, body);
365365
T response = ApiResource.atlanResponseGetter.request(client, method, url, body, clazz, options, requestId);
366366
// Ensure we reset the Atlan request ID, so we always have the context from the original
367367
// request that was made (even if it in turn triggered off other requests)
368-
MDC.put("X-Atlan-Request-Id", requestId);
368+
injectTraceId(requestId);
369369
if (log.isDebugEnabled() && (options == null || !options.getSkipLogging())) {
370370
if (response != null) {
371371
if (Atlan.enableTelemetry) {
@@ -404,13 +404,13 @@ public static String requestPlainText(
404404
throws AtlanException {
405405
// Create a unique ID for every request, and add it to the logging context and header
406406
String requestId = UUID.randomUUID().toString();
407-
MDC.put("X-Atlan-Request-Id", requestId);
407+
injectTraceId(requestId);
408408
log.debug("({}) {} with: {}", method, url, body);
409409
String response =
410410
ApiResource.atlanResponseGetter.requestPlainText(client, method, url, body, options, requestId);
411411
// Ensure we reset the Atlan request ID, so we always have the context from the original
412412
// request that was made (even if it in turn triggered off other requests)
413-
MDC.put("X-Atlan-Request-Id", requestId);
413+
injectTraceId(requestId);
414414
if (log.isDebugEnabled()) {
415415
if (response != null) {
416416
log.debug(" ... response: {}", response);
@@ -481,13 +481,13 @@ public static <T extends ApiResource> T request(
481481
}
482482
// Create a unique ID for every request, and add it to the logging context and header
483483
String requestId = UUID.randomUUID().toString();
484-
MDC.put("X-Atlan-Request-Id", requestId);
484+
injectTraceId(requestId);
485485
log.debug("({}) {} with: {}", method, url, filename);
486486
T response = ApiResource.atlanResponseGetter.request(
487487
client, method, url, payload, filename, clazz, extras, options, requestId);
488488
// Ensure we reset the Atlan request ID, so we always have the context from the original
489489
// request that was made (even if it in turn triggered off other requests)
490-
MDC.put("X-Atlan-Request-Id", requestId);
490+
injectTraceId(requestId);
491491
if (log.isDebugEnabled()) {
492492
if (response != null) {
493493
if (Atlan.enableTelemetry) {
@@ -531,11 +531,11 @@ public static <T extends ApiResource> T request(
531531
}
532532
// Create a unique ID for every request, and add it to the logging context and header
533533
String requestId = UUID.randomUUID().toString();
534-
MDC.put("X-Atlan-Request-Id", requestId);
534+
injectTraceId(requestId);
535535
T response = ApiResource.atlanResponseGetter.request(client, method, url, map, clazz, options, requestId);
536536
// Ensure we reset the Atlan request ID, so we always have the context from the original
537537
// request that was made (even if it in turn triggered off other requests)
538-
MDC.put("X-Atlan-Request-Id", requestId);
538+
injectTraceId(requestId);
539539
if (log.isDebugEnabled()) {
540540
if (response != null) {
541541
if (Atlan.enableTelemetry) {
@@ -550,6 +550,10 @@ public static <T extends ApiResource> T request(
550550
return response;
551551
}
552552

553+
public static void injectTraceId(String requestId) {
554+
MDC.put("trace_id", requestId);
555+
}
556+
553557
/**
554558
* Invalidate null typed parameters.
555559
*

0 commit comments

Comments
 (0)