- Notifications
You must be signed in to change notification settings - Fork 327
JUL log correlation #2724
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
Merged
Merged
JUL log correlation #2724
Changes from 9 commits
Commits
Show all changes
32 commits Select commit Hold shift + click to select a range
e5f78b0 first working draft
SylvainJuge 0721aaa use static methods
SylvainJuge 2f53689 use single ecs logging version
SylvainJuge 761fc4d enhance javadoc
SylvainJuge 95ff860 small refator + only capture SEVERE with JUL
SylvainJuge 893a254 Update all the JulMdc from the agent
SylvainJuge 6d8105f minor cleanup
SylvainJuge b49edc0 improved and simpler instrumentation
SylvainJuge 1905234 maybe the final simplification
SylvainJuge 338aa2d Update pom.xml
eyalkoren 5ee3c1c ignore plugin classes for instrumentation
SylvainJuge 180e790 override Jul formatter for MDC reading
SylvainJuge 230a900 remove unused code
SylvainJuge 705335f simplify JUL Mdc instrumentation
SylvainJuge c6c5a6b remove unnecesary deps + pkg access
SylvainJuge 272a067 slightly better wording
SylvainJuge 09602a0 reformat
SylvainJuge 208904a post review changes
SylvainJuge 89d8a7b Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge b89c337 do not ignore plugin CL for now
SylvainJuge 2486d0e update doc
SylvainJuge f05eb48 ignore only in ECS instrumentation for now
SylvainJuge d4cf2f3 update documentation
SylvainJuge 72f997b Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge ddd9f4c clarify JUL support doc
SylvainJuge de590a4 Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge 348504f Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge 8f0d7bf cleanup
SylvainJuge 3d29bd7 minor format fixes
SylvainJuge 76af93f Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge f316120 Merge branch 'main' of github.com:elastic/apm-agent-java into jul-log…
SylvainJuge b6ddf62 fix changelog
SylvainJuge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -77,7 +77,7 @@ | |
| <dependency> | ||
| <groupId>co.elastic.logging</groupId> | ||
| <artifactId>log4j2-ecs-layout</artifactId> | ||
| <version>${version.log4j2-ecs-layout}</version> | ||
| <version>${version.ecs.logging}</version> | ||
| Member Author 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. [for reviewer] we now have a single version to target | ||
| </dependency> | ||
| | ||
| <dependency> | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions 23 ...va/co/elastic/apm/agent/ecs_logging/EcsLoggingPluginClassLoaderRootPackageCustomizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.ecs_logging; | ||
| | ||
| import co.elastic.apm.agent.loginstr.LoggingPluginClassLoaderRootPackageCustomizer; | ||
| | ||
| public class EcsLoggingPluginClassLoaderRootPackageCustomizer extends LoggingPluginClassLoaderRootPackageCustomizer {} | ||
felixbarny marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
74 changes: 74 additions & 0 deletions 74 ...plugin/src/main/java/co/elastic/apm/agent/ecs_logging/JulEcsFormatterInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.ecs_logging; | ||
| | ||
| import co.elastic.apm.agent.bci.TracerAwareInstrumentation; | ||
| import co.elastic.apm.agent.loginstr.correlation.CorrelationIdMapAdapter; | ||
| import co.elastic.logging.jul.EcsFormatter; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.method.MethodDescription; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
| | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.Map; | ||
| | ||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
| | ||
| /** | ||
| * Instruments {@link EcsFormatter#getMdcEntries()} to provide correlation IDs at runtime. | ||
| * Application(s) copies of ecs-logging and the one in the agent will be instrumented, hence providing log correlation | ||
| * for all of them. | ||
| */ | ||
| @SuppressWarnings("JavadocReference") | ||
| public class JulEcsFormatterInstrumentation extends TracerAwareInstrumentation { | ||
| | ||
| @Override | ||
| public Collection<String> getInstrumentationGroupNames() { | ||
| return Arrays.asList("logging", "jul-ecs"); | ||
| } | ||
| | ||
| @Override | ||
| public ElementMatcher<? super TypeDescription> getTypeMatcher() { | ||
| return named("co.elastic.logging.jul.EcsFormatter"); | ||
| } | ||
| | ||
| @Override | ||
| public ElementMatcher<? super MethodDescription> getMethodMatcher() { | ||
| return named("getMdcEntries"); | ||
| } | ||
| | ||
| public static class AdviceClass { | ||
| | ||
| @Advice.OnMethodEnter(suppress = Throwable.class, inline = false, skipOn = Advice.OnNonDefaultValue.class) | ||
| public static boolean onEnter() { | ||
| return true; | ||
| } | ||
SylvainJuge marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| @Advice.AssignReturned.ToReturned | ||
| @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) | ||
| public static Map<String, String> onExit() { | ||
| return CorrelationIdMapAdapter.get(); | ||
| } | ||
| | ||
| } | ||
| | ||
| | ||
| } | ||
1 change: 1 addition & 0 deletions 1 ...sources/META-INF/services/co.elastic.apm.agent.bci.PluginClassLoaderRootPackageCustomizer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| co.elastic.apm.agent.ecs_logging.EcsLoggingPluginClassLoaderRootPackageCustomizer | ||
felixbarny marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
2 changes: 2 additions & 0 deletions 2 ...n/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| co.elastic.apm.agent.ecs_logging.Log4j2ServiceNameInstrumentation | ||
| co.elastic.apm.agent.ecs_logging.Log4j2ServiceVersionInstrumentation | ||
| | ||
| co.elastic.apm.agent.ecs_logging.JulEcsFormatterInstrumentation |
84 changes: 84 additions & 0 deletions 84 ...in/src/test/java/co/elastic/apm/agent/ecs_logging/JulEcsFormatterInstrumentationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.ecs_logging; | ||
| | ||
| import co.elastic.apm.agent.AbstractInstrumentationTest; | ||
| import co.elastic.apm.agent.impl.error.ErrorCapture; | ||
| import co.elastic.apm.agent.impl.transaction.Transaction; | ||
| import co.elastic.logging.jul.EcsFormatter; | ||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Test; | ||
| | ||
| import javax.annotation.Nullable; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.LogRecord; | ||
| | ||
| import static co.elastic.apm.agent.testutils.assertions.Assertions.assertThat; | ||
| | ||
| public class JulEcsFormatterInstrumentationTest extends AbstractInstrumentationTest { | ||
| | ||
| @Test | ||
| void testNoCorrelation() { | ||
| JsonNode logLine = logSomething(); | ||
| | ||
| assertThat(logLine.get("transaction.id")).isNull(); | ||
| assertThat(logLine.get("trace.id")).isNull(); | ||
| } | ||
| | ||
| @Test | ||
| void testActiveTransaction() { | ||
| Transaction transaction = startTestRootTransaction("log"); | ||
| try { | ||
| JsonNode logLine = logSomething(); | ||
| | ||
| assertThat(logLine.get("transaction.id").textValue()).isEqualTo(transaction.getTraceContext().getTransactionId().toString()); | ||
| assertThat(logLine.get("trace.id").textValue()).isEqualTo(transaction.getTraceContext().getTraceId().toString()); | ||
| } finally { | ||
| transaction.deactivate().end(); | ||
| } | ||
| } | ||
| | ||
| @Test | ||
| void testActiveError() { | ||
| ErrorCapture error = new ErrorCapture(tracer); | ||
| | ||
| error.activate(); | ||
| try { | ||
| JsonNode logLine = logSomething(); | ||
| | ||
| assertThat(logLine.get("error.id").textValue()).isEqualTo(error.getTraceContext().getId().toString()); | ||
| } finally { | ||
| error.deactivate(); | ||
| } | ||
| | ||
| } | ||
| | ||
| private static JsonNode logSomething() { | ||
| EcsFormatter formatter = new EcsFormatter(); | ||
| LogRecord record = new LogRecord(Level.INFO, "msg"); | ||
| try { | ||
| return new ObjectMapper().readTree(formatter.format(record)); | ||
| } catch (JsonProcessingException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions 33 ...ging-plugin/apm-jul-plugin/src/main/java/co/elastic/apm/agent/jul/JulInstrumentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Licensed to Elasticsearch B.V. under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch B.V. licenses this file to you 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 co.elastic.apm.agent.jul; | ||
| | ||
| import co.elastic.apm.agent.loginstr.AbstractLogIntegrationInstrumentation; | ||
| | ||
| import java.util.Collection; | ||
| | ||
| public abstract class JulInstrumentation extends AbstractLogIntegrationInstrumentation { | ||
| | ||
| @Override | ||
| public Collection<String> getInstrumentationGroupNames() { | ||
| Collection<String> ret = super.getInstrumentationGroupNames(); | ||
| ret.add("jul-ecs"); | ||
| return ret; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.