Skip to content

Commit aa1d23c

Browse files
authored
Change OTel quickstart to use log4j2 instead of logback (#330)
* Change OTel quickstart to use log4j2 instead of logback Log4j2's `JsonTemplateLayout` supports most of the Cloud Logging special JSON keys directly through the [`GcpLayout` event template](https://logging.apache.org/log4j/2.x/manual/json-template-layout.html#event-templates). I still needed to map the OTel MDC keys for trace correlation, but am planning to contribute [this upstream](https://github.com/apache/logging-log4j2/blob/rel/2.17.2/log4j-layout-template-json/src/main/resources/GcpLayout.json). One outstanding issue is converting the `trace_flags` hex value to a boolean, but it doesn't break Cloud Console if `logging.googleapis.com/trace_sampled` key is unset/false. See apache/logging-log4j2#2482. * undo one collector config change, add issue ID to TODO
1 parent 97a8f20 commit aa1d23c

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

examples/instrumentation-quickstart/build.gradle

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,15 @@ spotless {
4242
dependencies {
4343
implementation 'org.springframework.boot:spring-boot-starter-webflux'
4444

45-
// Cannot be updated until logback is updated to 1.3+, probably in the next Spring Boot
46-
// major version
47-
implementation 'net.logstash.logback:logstash-logback-encoder:7.3'
45+
// Use log4j2 for logging
46+
// https://docs.spring.io/spring-boot/docs/2.7.18/reference/html/howto.html#howto.logging.log4j
47+
implementation "org.springframework.boot:spring-boot-starter-log4j2"
48+
modules {
49+
module("org.springframework.boot:spring-boot-starter-logging") {
50+
replacedBy("org.springframework.boot:spring-boot-starter-log4j2", "Use Log4j2 instead of Logback")
51+
}
52+
}
53+
implementation "org.apache.logging.log4j:log4j-layout-template-json"
4854

4955
testImplementation 'org.springframework.boot:spring-boot-starter-test'
5056
testImplementation 'org.testcontainers:testcontainers:1.19.4'

examples/instrumentation-quickstart/otel-collector-config.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ receivers:
3535
layout: '%Y-%m-%dT%H:%M:%S.%fZ'
3636
severity:
3737
parse_from: body.severity
38+
preset: none
39+
# parse minimal set of severity strings that Cloud Logging explicitly supports
40+
# https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity
41+
mapping:
42+
debug: debug
43+
info: info
44+
info3: notice
45+
warn: warning
46+
error: error
47+
fatal: critical
48+
fatal3: alert
49+
fatal4: emergency
3850

3951
# set trace_flags to SAMPLED if GCP attribute is set to true
4052
- type: add
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!--
2+
Copyright 2024 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<Configuration status="WARN">
18+
<Appenders>
19+
<Console name="Console" target="SYSTEM_OUT">
20+
<!-- TODO(#331): output logging.googleapis.com/trace_sampled as a boolean -->
21+
<!-- [START opentelemetry_instrumentation_setup_logging] -->
22+
<!-- Format JSON logs for the Cloud Logging agent
23+
https://cloud.google.com/logging/docs/structured-logging#special-payload-fields -->
24+
25+
<!-- Log4j2's JsonTemplateLayout includes a template for Cloud Logging's special JSON fields
26+
https://logging.apache.org/log4j/2.x/manual/json-template-layout.html#event-templates -->
27+
<JsonTemplateLayout eventTemplateUri="classpath:GcpLayout.json">
28+
<!-- Extend the included GcpLayout to include the trace and span IDs from Mapped
29+
Diagnostic Context (MDC) so that Cloud Logging can correlate Logs and Spans -->
30+
<EventTemplateAdditionalField
31+
key="logging.googleapis.com/trace"
32+
format="JSON"
33+
value='{"$resolver": "mdc", "key": "trace_id"}'
34+
/>
35+
<EventTemplateAdditionalField
36+
key="logging.googleapis.com/spanId"
37+
format="JSON"
38+
value='{"$resolver": "mdc", "key": "span_id"}'
39+
/>
40+
</JsonTemplateLayout>
41+
<!-- [END opentelemetry_instrumentation_setup_logging] -->
42+
</Console>
43+
</Appenders>
44+
<Loggers>
45+
<Root level="info">
46+
<AppenderRef ref="Console" />
47+
</Root>
48+
</Loggers>
49+
</Configuration>

0 commit comments

Comments
 (0)