Skip to content

Commit b50f6b3

Browse files
Add support for W3C traceparent HTTP header
The contents of the HTTP header "traceparent" is extracted and logged with every log event in field "w3c_traceparent". This is done for regular application log messages and request logs. The feature allows correlation of log messages with requests and possible traces. Signed-off-by: Karsten Schnitter <[email protected]>
1 parent 6e40037 commit b50f6b3

File tree

11 files changed

+84
-2
lines changed

11 files changed

+84
-2
lines changed

cf-java-logging-support-core/beats/app-logs/docs/fields.asciidoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ required: False
9191
A unique identifier that can be used to correlate multiple messages to a request.
9292

9393

94+
==== w3c_traceparent
95+
96+
type: string
97+
98+
example: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
99+
100+
required: False
101+
102+
The content of the W3C traceparent header as defined in
103+
https://www.w3.org/TR/trace-context/#traceparent-header.
104+
The traceparent allows correlation of logs to the request.
105+
106+
94107
==== sap_passport
95108

96109
type: string

cf-java-logging-support-core/beats/app-logs/etc/app-logs.template.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@
198198
"index": "not_analyzed",
199199
"type": "string"
200200
},
201+
"w3c_traceparent": {
202+
"doc_values": true,
203+
"index": "not_analyzed",
204+
"type": "string"
205+
},
201206
"written_at": {
202207
"doc_values": true,
203208
"ignore_malformed": true,

cf-java-logging-support-core/beats/app-logs/etc/fields.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ ctx:
7676
description: |
7777
A unique identifier that can be used to correlate multiple messages to a request.
7878
79+
- name: "w3c_traceparent"
80+
type: string
81+
required: false
82+
example: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
83+
description: |
84+
The content of the W3C traceparent header as defined in
85+
https://www.w3.org/TR/trace-context/#traceparent-header.
86+
The traceparent allows correlation of logs to the request.
87+
7988
- name: "sap_passport"
8089
type: string
8190
required: false

cf-java-logging-support-core/beats/request-metrics/docs/fields.asciidoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ required: False
100100
A unique identifier that can be used to correlate multiple messages to a request.
101101

102102

103+
==== w3c_traceparent
104+
105+
type: string
106+
107+
example: 00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
108+
109+
required: False
110+
111+
The content of the W3C traceparent header as defined in
112+
https://www.w3.org/TR/trace-context/#traceparent-header.
113+
The traceparent allows correlation of logs to the request.
114+
115+
103116
==== sap_passport
104117

105118
type: string

cf-java-logging-support-core/beats/request-metrics/etc/fields.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ ctx:
8585
description: |
8686
A unique identifier that can be used to correlate multiple messages to a request.
8787
88+
- name: "w3c_traceparent"
89+
type: string
90+
required: false
91+
example: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01"
92+
description: |
93+
The content of the W3C traceparent header as defined in
94+
https://www.w3.org/TR/trace-context/#traceparent-header.
95+
The traceparent allows correlation of logs to the request.
96+
8897
- name: "sap_passport"
8998
type: string
9099
required: false

cf-java-logging-support-core/beats/request-metrics/etc/request-metrics.template.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@
263263
"index": "not_analyzed",
264264
"type": "string"
265265
},
266+
"w3c_traceparent": {
267+
"doc_values": true,
268+
"index": "not_analyzed",
269+
"type": "string"
270+
},
266271
"written_at": {
267272
"doc_values": true,
268273
"ignore_malformed": true,

cf-java-logging-support-core/src/main/java/com/sap/hcp/cf/logging/common/Fields.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public interface Fields {
1313
public String WRITTEN_TS = "written_ts";
1414
public String CORRELATION_ID = "correlation_id";
1515
public String REQUEST_ID = "request_id";
16+
public String W3C_TRACEPARENT = "w3c_traceparent";
1617
public String SAP_PASSPORT = "sap_passport";
1718
public String SAP_PASSPORT_ACTION = "sap_passport_Action";
1819
public String SAP_PASSPORT_ACTIONTYPE = "sap_passport_ActionType";

cf-java-logging-support-core/src/main/java/com/sap/hcp/cf/logging/common/request/HttpHeaders.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public enum HttpHeaders implements HttpHeader {
3636
X_VCAP_REQUEST_ID("x-vcap-request-id", Fields.REQUEST_ID, true), //
3737
CORRELATION_ID("X-CorrelationID", Fields.CORRELATION_ID, true,
3838
X_VCAP_REQUEST_ID), //
39+
W3C_TRACEPARENT("traceparent", Fields.W3C_TRACEPARENT, true),
3940
SAP_PASSPORT("sap-passport", Fields.SAP_PASSPORT, true), //
4041
TENANT_ID("tenantid", Fields.TENANT_ID, true); //
4142

cf-java-logging-support-core/src/test/java/com/sap/hcp/cf/logging/common/request/HttpHeadersTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void resetLogContext() {
2424

2525
@Test
2626
public void hasCorrectNumberOfTypes() throws Exception {
27-
assertThat(HttpHeaders.values().length, is(equalTo(19)));
27+
assertThat(HttpHeaders.values().length, is(equalTo(20)));
2828
}
2929

3030
@Test
@@ -34,6 +34,7 @@ public void hasCorrectNames() throws Exception {
3434
assertThat(HttpHeaders.CORRELATION_ID.getName(), is("X-CorrelationID"));
3535
assertThat(HttpHeaders.REFERER.getName(), is("referer"));
3636
assertThat(HttpHeaders.TENANT_ID.getName(), is("tenantid"));
37+
assertThat(HttpHeaders.W3C_TRACEPARENT.getName(), is("traceparent"));
3738
assertThat(HttpHeaders.X_CUSTOM_HOST.getName(), is("x-custom-host"));
3839
assertThat(HttpHeaders.X_FORWARDED_FOR.getName(), is("x-forwarded-for"));
3940
assertThat(HttpHeaders.X_FORWARDED_HOST.getName(), is("x-forwarded-host"));
@@ -56,6 +57,7 @@ public void hasCorrectFields() throws Exception {
5657
assertThat(HttpHeaders.CORRELATION_ID.getField(), is(Fields.CORRELATION_ID));
5758
assertThat(HttpHeaders.REFERER.getField(), is(nullValue()));
5859
assertThat(HttpHeaders.TENANT_ID.getField(), is(Fields.TENANT_ID));
60+
assertThat(HttpHeaders.W3C_TRACEPARENT.getField(), is(Fields.W3C_TRACEPARENT));
5961
assertThat(HttpHeaders.X_CUSTOM_HOST.getField(), is(Fields.X_CUSTOM_HOST));
6062
assertThat(HttpHeaders.X_FORWARDED_FOR.getField(), is(Fields.X_FORWARDED_FOR));
6163
assertThat(HttpHeaders.X_FORWARDED_HOST.getField(), is(Fields.X_FORWARDED_HOST));
@@ -93,6 +95,7 @@ public void hasCorrectAliases() throws Exception {
9395
assertThat(HttpHeaders.CORRELATION_ID.getAliases(), containsInAnyOrder(HttpHeaders.X_VCAP_REQUEST_ID));
9496
assertThat(HttpHeaders.REFERER.getAliases(), is(empty()));
9597
assertThat(HttpHeaders.TENANT_ID.getAliases(), is(empty()));
98+
assertThat(HttpHeaders.W3C_TRACEPARENT.getAliases(), is(empty()));
9699
assertThat(HttpHeaders.X_CUSTOM_HOST.getAliases(), is(empty()));
97100
assertThat(HttpHeaders.X_FORWARDED_FOR.getAliases(), is(empty()));
98101
assertThat(HttpHeaders.X_FORWARDED_HOST.getAliases(), is(empty()));
@@ -111,7 +114,8 @@ public void hasCorrectAliases() throws Exception {
111114
@Test
112115
public void propagatesCorrectHeaders() throws Exception {
113116
assertThat(HttpHeaders.propagated(), containsInAnyOrder(HttpHeaders.CORRELATION_ID, HttpHeaders.SAP_PASSPORT,
114-
HttpHeaders.TENANT_ID, HttpHeaders.X_VCAP_REQUEST_ID));
117+
HttpHeaders.TENANT_ID, HttpHeaders.W3C_TRACEPARENT,
118+
HttpHeaders.X_VCAP_REQUEST_ID));
115119
}
116120

117121
}

cf-java-logging-support-servlet/src/test/java/com/sap/hcp/cf/logging/servlet/filter/RequestLogTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,19 @@ public void logsSapPassportFromRequestHeader() throws Exception {
139139
}
140140
}
141141

142+
@Test
143+
public void logsW3cTraceparentFromRequestHeader() throws Exception {
144+
String traceparent = "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01";
145+
HttpGet get = createRequestWithHeader(HttpHeaders.W3C_TRACEPARENT.getName(), traceparent);
146+
try (CloseableHttpResponse response = client.execute(get)) {
147+
assertThat("Application log without traceparent.", getRequestMessage(), hasEntry(Fields.W3C_TRACEPARENT,
148+
traceparent));
149+
assertThat("Request log without traceparent.", getRequestLog(), hasEntry(Fields.W3C_TRACEPARENT,
150+
traceparent));
151+
}
152+
153+
}
154+
142155
@Test
143156
public void writesCorrelationIdFromHeadersAsResponseHeader() throws Exception {
144157
String correlationId = UUID.randomUUID().toString();

0 commit comments

Comments
 (0)