- 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
JUL log correlation #2724
Changes from all commits
e5f78b0 0721aaa 2f53689 761fc4d 95ff860 893a254 6d8105f b49edc0 1905234 338aa2d 5ee3c1c 180e790 230a900 705335f c6c5a6b 272a067 09602a0 208904a 89d8a7b b89c337 2486d0e f05eb48 d4cf2f3 72f997b ddd9f4c de590a4 348504f 8f0d7bf 3d29bd7 76af93f f316120 b6ddf62 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 |
|---|---|---|
| | @@ -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> | ||
| | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * 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.bci.bytebuddy.CustomElementMatchers; | ||
| 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; | ||
| import static net.bytebuddy.matcher.ElementMatchers.not; | ||
| | ||
| /** | ||
| * 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 ElementMatcher.Junction<ClassLoader> getClassLoaderMatcher() { | ||
| // ECS formatter that is loaded within the agent should not be instrumented | ||
| return not(CustomElementMatchers.isInternalPluginClassLoader()); | ||
| 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] this filtering means that the agent copy of | ||
| } | ||
| | ||
| @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.AssignReturned.ToReturned | ||
| @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class, inline = false) | ||
| public static Map<String, String> onExit() { | ||
| return CorrelationIdMapAdapter.get(); | ||
| } | ||
| | ||
| } | ||
| | ||
| | ||
| } | ||
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * 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.Test; | ||
| | ||
| 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); | ||
| } | ||
| } | ||
| } |
| 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; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.