Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change OTel quickstart to use log4j2 instead of logback #330

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions examples/instrumentation-quickstart/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
24 changes: 19 additions & 5 deletions examples/instrumentation-quickstart/otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,26 @@ 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
field: body.trace_flags
value: "01"
if: body["logging.googleapis.com/trace_sampled"] == true
# TODO(aaronabbott): uncomment once the log4j2 config is correctly outputing the
# logging.googleapis.com/trace_sampled value
## set trace_flags to SAMPLED if GCP attribute is set to true
# - type: add
# field: body.trace_flags
# value: "01"
# if: body["logging.googleapis.com/trace_sampled"] == true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this returns false, will that just be a no-op? (do we actually need to comment out this config?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good point. I was trying to prevent collector log spam, but it looks like the log spam is actually coming from the remove operator:

2024-04-18T14:52:50.317Z error helper/transformer.go:98 Failed to process entry {"kind": "receiver", "name": "filelog", "data_type": "logs", "operator_id": "remove6", "operator_type": "remove", "error": "remove: field does not exist: body['logging.googleapis.com/trace_sampled']", "action": "send", "entry": {"observed_timestamp":"2024-04-18T14:52:50.317546122Z","timestamp":"2024-04-18T14:52:50.214Z","body":{"_exception":{"stackTrace":""},"_logger":"com.example.demo.SingleController","_thread":"reactor-http-epoll-20","logging.googleapis.com/insertId":"2037","logging.googleapis.com/labels":{"span_id":"5aeb502bb6ef209f","trace_flags":"01","trace_id":"5643ea6d8d99b1b30a6fbab66b1a3ddd"},"logging.googleapis.com/sourceLocation":{"function":"com.example.demo.SingleController.handleSingle"},"message":"Going to sleep for 172"},"attributes":{"log.file.name":"app.log"},"severity_text":"INFO","span_id":"WutQK7bvIJ8=","trace_id":"VkPqbY2ZsbMKb7q2axo93Q==","severity":9,"scope_name":""}}

I'll just let it be for now and reset this file


# parse the trace context fields from GCP attributes
- type: regex_parser
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!--
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">
<!-- [START opentelemetry_instrumentation_setup_logging] -->
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a different region tag than the docs currently use

<!-- [START opentelemetry_instrumentation_logbackxml] -->

So I can update the docs before removing the logback.xml files

<!-- 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>
Loading