-
Notifications
You must be signed in to change notification settings - Fork 144
Add Log4j2 support for logging to active spans #392
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Copyright © 2017-2026 The OpenTracing Authors | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
| package io.opentracing.contrib.spring.cloud.log; | ||
|
|
||
| import io.opentracing.Tracer; | ||
| import io.opentracing.contrib.spring.tracer.configuration.TracerAutoConfiguration; | ||
| import javax.annotation.PostConstruct; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.core.LoggerContext; | ||
| import org.apache.logging.log4j.core.config.LoggerConfig; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| @Configuration | ||
| @ConditionalOnBean(Tracer.class) | ||
| @AutoConfigureAfter(TracerAutoConfiguration.class) | ||
| @ConditionalOnClass(name = "org.apache.logging.log4j.core.LoggerContext") | ||
| @ConditionalOnProperty(name = "opentracing.spring.cloud.log.enabled", havingValue = "true", matchIfMissing = true) | ||
| public class Log4j2LoggingAutoConfiguration { | ||
|
|
||
| @Autowired | ||
| private Tracer tracer; | ||
|
|
||
| @PostConstruct | ||
| public void postConstruct() { | ||
| LoggerContext context = (LoggerContext) LogManager.getContext(false); | ||
| LoggerConfig rootLogger = context.getConfiguration().getRootLogger(); | ||
| Log4j2SpanLogsAppender appender = new Log4j2SpanLogsAppender(tracer); | ||
| appender.start(); | ||
| rootLogger.addAppender(appender, null, null); | ||
| context.updateLoggers(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,61 @@ | ||||||
| /* | ||||||
| * Copyright © 2017-2026 The OpenTracing Authors | ||||||
| * | ||||||
| * 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 | ||||||
| * | ||||||
| * http://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. | ||||||
| */ | ||||||
| package io.opentracing.contrib.spring.cloud.log; | ||||||
|
|
||||||
| import io.opentracing.Span; | ||||||
| import io.opentracing.Tracer; | ||||||
| import io.opentracing.tag.Tags; | ||||||
| import java.util.HashMap; | ||||||
| import java.util.Map; | ||||||
| import java.util.concurrent.TimeUnit; | ||||||
| import org.apache.logging.log4j.Level; | ||||||
| import org.apache.logging.log4j.core.LogEvent; | ||||||
| import org.apache.logging.log4j.core.appender.AbstractAppender; | ||||||
| import org.apache.logging.log4j.core.config.Property; | ||||||
|
|
||||||
| public class Log4j2SpanLogsAppender extends AbstractAppender { | ||||||
|
|
||||||
| private final Tracer tracer; | ||||||
|
|
||||||
| public Log4j2SpanLogsAppender(Tracer tracer) { | ||||||
| super(Log4j2SpanLogsAppender.class.getSimpleName(), null, null, true, Property.EMPTY_ARRAY); | ||||||
| this.tracer = tracer; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void append(LogEvent event) { | ||||||
| Span span = tracer.activeSpan(); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift Record span logs before asynchronous logger dispatch. When applications use Record the span log on the emitting thread before the asynchronous boundary. Add an integration test that emits through an asynchronous logger; direct 🤖 Prompt for AI Agents |
||||||
| if (span == null) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| Map<String, Object> logs = new HashMap<>(6); | ||||||
| logs.put("logger", event.getLoggerName()); | ||||||
| logs.put("level", event.getLevel().toString()); | ||||||
| logs.put("thread", event.getThreadName()); | ||||||
| logs.put("message", event.getMessage().getFormattedMessage()); | ||||||
|
|
||||||
| if (Level.ERROR.equals(event.getLevel())) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Include FATAL events in error tagging.
Proposed fix- if (Level.ERROR.equals(event.getLevel())) {
+ if (event.getLevel().isMoreSpecificThan(Level.ERROR)) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| Tags.ERROR.set(span, Boolean.TRUE); | ||||||
| logs.put("event", Tags.ERROR.getKey()); | ||||||
| } | ||||||
|
|
||||||
| if (event.getThrown() != null) { | ||||||
| logs.put("error.object", event.getThrown()); | ||||||
| } | ||||||
| span.log(TimeUnit.MICROSECONDS.convert(event.getTimeMillis(), TimeUnit.MILLISECONDS), logs); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright © 2017-2026 The OpenTracing Authors | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
| package io.opentracing.contrib.spring.cloud.log; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertSame; | ||
|
|
||
| import io.opentracing.mock.MockSpan; | ||
| import io.opentracing.mock.MockTracer; | ||
| import io.opentracing.tag.Tags; | ||
| import org.apache.logging.log4j.Level; | ||
| import org.apache.logging.log4j.core.impl.Log4jLogEvent; | ||
| import org.apache.logging.log4j.message.SimpleMessage; | ||
| import org.junit.Test; | ||
|
|
||
| public class Log4j2SpanLogsAppenderTest { | ||
|
|
||
| @Test | ||
| public void appendsLogEventToActiveSpan() { | ||
| MockTracer tracer = new MockTracer(); | ||
| MockSpan span = tracer.buildSpan("request").start(); | ||
| Log4j2SpanLogsAppender appender = new Log4j2SpanLogsAppender(tracer); | ||
| appender.start(); | ||
|
|
||
| try (io.opentracing.Scope ignored = tracer.scopeManager().activate(span)) { | ||
| appender.append(Log4jLogEvent.newBuilder() | ||
| .setLoggerName("example.Controller") | ||
| .setLevel(Level.ERROR) | ||
| .setThreadName("request-thread") | ||
| .setMessage(new SimpleMessage("request failed")) | ||
| .setTimeMillis(1000L) | ||
| .setThrown(new IllegalStateException("failure")) | ||
| .build()); | ||
| } | ||
|
|
||
| assertEquals(1, span.logEntries().size()); | ||
| MockSpan.LogEntry entry = span.logEntries().get(0); | ||
| assertEquals("example.Controller", entry.fields().get("logger")); | ||
| assertEquals("ERROR", entry.fields().get("level")); | ||
| assertEquals("request-thread", entry.fields().get("thread")); | ||
| assertEquals("request failed", entry.fields().get("message")); | ||
| assertEquals(1000000L, entry.timestampMicros()); | ||
| assertSame(IllegalStateException.class, ((Throwable) entry.fields().get("error.object")).getClass()); | ||
| assertEquals(Tags.ERROR.getKey(), entry.fields().get("event")); | ||
| assertEquals(Boolean.TRUE, span.tags().get(Tags.ERROR.getKey())); | ||
| span.finish(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright © 2017-2026 The OpenTracing Authors | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
| package io.opentracing.contrib.spring.cloud.feign; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import feign.Client; | ||
| import feign.Request; | ||
| import feign.Response; | ||
| import feign.opentracing.TracingClient; | ||
| import io.opentracing.mock.MockTracer; | ||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import org.junit.Test; | ||
|
|
||
| public class FeignContextBeanPostProcessorTest { | ||
|
|
||
| @Test | ||
| public void wrapsFinalFeignClientWithoutSubclassProxy() throws IOException { | ||
| FeignContextBeanPostProcessor postProcessor = new FeignContextBeanPostProcessor( | ||
| new MockTracer(), Collections.emptyList()); | ||
|
|
||
| Object client = postProcessor.postProcessAfterInitialization(new FinalClient(), "client"); | ||
|
|
||
| assertTrue(client instanceof TracingClient); | ||
| } | ||
|
|
||
| private static final class FinalClient implements Client { | ||
| @Override | ||
| public Response execute(Request request, Request.Options options) { | ||
| return null; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright © 2017-2026 The OpenTracing Authors | ||
| * | ||
| * 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 | ||
| * | ||
| * http://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. | ||
| */ | ||
| package io.opentracing.contrib.spring.cloud.feign; | ||
|
|
||
| import org.springframework.test.context.TestPropertySource; | ||
|
|
||
| @TestPropertySource(properties = "feign.okhttp.enabled=true") | ||
| public class FeignManualOkhttpClientTest extends FeignManualTest { | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
Check the selected logging backend before casting.
When
log4j-coreis present butlog4j-to-slf4jis the selected provider,LogManager.getContext(false)returnsSLF4JLoggerContext. That class is not a CoreLoggerContext. The classpath condition still passes, and this cast throwsClassCastExceptionduring Spring initialization. (logging.apache.org)Skip this configuration when the selected context is not a Core
LoggerContext.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents