diff --git a/examples/instrumentation-quickstart/build.gradle b/examples/instrumentation-quickstart/build.gradle index ca4abab6..f2d74b74 100644 --- a/examples/instrumentation-quickstart/build.gradle +++ b/examples/instrumentation-quickstart/build.gradle @@ -42,9 +42,15 @@ spotless { dependencies { implementation 'org.springframework.boot:spring-boot-starter-webflux' - // Cannot be updated until logback is updated to 1.3+, probably in the next Spring Boot - // major version - implementation 'net.logstash.logback:logstash-logback-encoder:7.3' + // Use log4j2 for logging + // https://docs.spring.io/spring-boot/docs/2.7.18/reference/html/howto.html#howto.logging.log4j + implementation "org.springframework.boot:spring-boot-starter-log4j2" + modules { + module("org.springframework.boot:spring-boot-starter-logging") { + replacedBy("org.springframework.boot:spring-boot-starter-log4j2", "Use Log4j2 instead of Logback") + } + } + implementation "org.apache.logging.log4j:log4j-layout-template-json" testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.testcontainers:testcontainers:1.19.4' diff --git a/examples/instrumentation-quickstart/otel-collector-config.yaml b/examples/instrumentation-quickstart/otel-collector-config.yaml index c195d404..513c8a0b 100644 --- a/examples/instrumentation-quickstart/otel-collector-config.yaml +++ b/examples/instrumentation-quickstart/otel-collector-config.yaml @@ -35,6 +35,18 @@ receivers: layout: '%Y-%m-%dT%H:%M:%S.%fZ' severity: parse_from: body.severity + preset: none + # parse minimal set of severity strings that Cloud Logging explicitly supports + # https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry#LogSeverity + mapping: + debug: debug + info: info + info3: notice + warn: warning + error: error + fatal: critical + fatal3: alert + fatal4: emergency # set trace_flags to SAMPLED if GCP attribute is set to true - type: add diff --git a/examples/instrumentation-quickstart/src/main/resources/log4j2.xml b/examples/instrumentation-quickstart/src/main/resources/log4j2.xml new file mode 100644 index 00000000..5a8d68ae --- /dev/null +++ b/examples/instrumentation-quickstart/src/main/resources/log4j2.xml @@ -0,0 +1,49 @@ +<!-- + Copyright 2024 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --> + +<Configuration status="WARN"> + <Appenders> + <Console name="Console" target="SYSTEM_OUT"> + <!-- TODO(#331): output logging.googleapis.com/trace_sampled as a boolean --> + <!-- [START opentelemetry_instrumentation_setup_logging] --> + <!-- Format JSON logs for the Cloud Logging agent + https://cloud.google.com/logging/docs/structured-logging#special-payload-fields --> + + <!-- Log4j2's JsonTemplateLayout includes a template for Cloud Logging's special JSON fields + https://logging.apache.org/log4j/2.x/manual/json-template-layout.html#event-templates --> + <JsonTemplateLayout eventTemplateUri="classpath:GcpLayout.json"> + <!-- Extend the included GcpLayout to include the trace and span IDs from Mapped + Diagnostic Context (MDC) so that Cloud Logging can correlate Logs and Spans --> + <EventTemplateAdditionalField + key="logging.googleapis.com/trace" + format="JSON" + value='{"$resolver": "mdc", "key": "trace_id"}' + /> + <EventTemplateAdditionalField + key="logging.googleapis.com/spanId" + format="JSON" + value='{"$resolver": "mdc", "key": "span_id"}' + /> + </JsonTemplateLayout> + <!-- [END opentelemetry_instrumentation_setup_logging] --> + </Console> + </Appenders> + <Loggers> + <Root level="info"> + <AppenderRef ref="Console" /> + </Root> + </Loggers> +</Configuration>