From f69f7078a37d17431d123410e086380f6ef7d7d4 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 13:54:59 +0200 Subject: [PATCH 01/18] add common code for environment --- .../agent/ecs_logging/EcsLoggingUtils.java | 20 ++++++++ .../ecs_logging/EcsServiceCorrelationIT.java | 6 +++ .../EcsServiceEnvironmentTest.java | 51 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/EcsLoggingUtils.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/EcsLoggingUtils.java index 9e5fa5e948..bf7b4090ef 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/EcsLoggingUtils.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/EcsLoggingUtils.java @@ -36,6 +36,7 @@ public class EcsLoggingUtils { private static final WeakSet nameChecked = WeakConcurrent.buildSet(); private static final WeakSet versionChecked = WeakConcurrent.buildSet(); + private static final WeakSet environmentChecked = WeakConcurrent.buildSet(); private static final ElasticApmTracer tracer = GlobalTracer.get().require(ElasticApmTracer.class); @@ -53,6 +54,12 @@ public static String getServiceVersion() { return serviceInfo != null ? serviceInfo.getServiceVersion() : configuredServiceVersion; } + @Nullable + public static String getServiceEnvironment() { + return tracer.getConfig(CoreConfiguration.class).getEnvironment(); + } + + private static void warnIfMisConfigured(String key, @Nullable String configuredValue, @Nullable String agentValue) { if (!Objects.equals(agentValue, configuredValue)) { log.warn("configuration values differ for '{}': ecs-logging='{}', agent='{}', traces and logs might not correlate properly", key, configuredValue, agentValue); @@ -84,4 +91,17 @@ public static String getOrWarnServiceName(Object target, @Nullable String value) return value; } } + + @Nullable + public static String getOrWarnServiceEnvironment(Object target, @Nullable String value) { + if (!environmentChecked.add(target)) { + return value; + } + if (value == null) { + return getServiceEnvironment(); + } else { + warnIfMisConfigured("environment", value, getServiceEnvironment()); + return value; + } + } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceCorrelationIT.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceCorrelationIT.java index 3af21c1759..47168b85b5 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceCorrelationIT.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceCorrelationIT.java @@ -32,6 +32,8 @@ public abstract class EcsServiceCorrelationIT { protected abstract String getServiceVersionTestClass(); + protected abstract String getServiceEnvironmentTestClass(); + @ParameterizedTest(name = "ecs-logging {0}, supports version = {1}, supports environment = {2}") @CsvSource(delimiter = '|', value = { "1.3.2 | false | false", // 1.3.2 only supports service name @@ -45,5 +47,9 @@ void testVersion(String version, boolean serviceVersionSupported, boolean servic if (serviceVersionSupported) { new TestClassWithDependencyRunner(List.of(dependency), getServiceVersionTestClass()).run(); } + + if (serviceEnvironmentSupported) { + new TestClassWithDependencyRunner(List.of(dependency), getServiceEnvironmentTestClass()).run(); + } } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java new file mode 100644 index 0000000000..7abff9b8f5 --- /dev/null +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java @@ -0,0 +1,51 @@ +/* + * 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.configuration.CoreConfiguration; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.doReturn; + +public abstract class EcsServiceEnvironmentTest extends EcsLoggingTest { + + @BeforeEach + public void setUp() { + doReturn("test").when(tracer.getConfig(CoreConfiguration.class)).getEnvironment(); + } + + @Test + public void testBuildWithNoServiceNameSet() { + initFormatterWithoutServiceEnvironmentSet(); + assertThat(getJson(createLogMsg(), "service.environment")).isEqualTo("test-env"); + } + + protected abstract void initFormatterWithoutServiceEnvironmentSet(); + + @Test + public void testBuildWithServiceNameSet() { + // this should also issue a warning as the value configured in ecs-logging differs from the agent + initFormatterWithServiceEnvironment("prod"); + assertThat(getJson(createLogMsg(), "service.environment")).isEqualTo("prod"); + } + + protected abstract void initFormatterWithServiceEnvironment(String environment); +} From 5cf4c0a62ee4c546e91adc1faa7733dae70ba8f1 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 13:57:04 +0200 Subject: [PATCH 02/18] add environment for JUL --- .../jul/JulServiceCorrelationIT.java | 5 ++ ...ServiceEnvironmentInstrumentationTest.java | 52 +++++++++++++++++++ .../JulServiceNameInstrumentationTest.java | 12 +---- .../JulServiceVersionInstrumentationTest.java | 13 +---- .../jul/LogManagerTestInstrumentation.java | 14 ++++- 5 files changed, 74 insertions(+), 22 deletions(-) create mode 100644 apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceEnvironmentInstrumentationTest.java diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceCorrelationIT.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceCorrelationIT.java index 20772ff408..45bae66f1e 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceCorrelationIT.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceCorrelationIT.java @@ -36,4 +36,9 @@ protected String getServiceNameTestClass() { protected String getServiceVersionTestClass() { return "co.elastic.apm.agent.ecs_logging.jul.JulServiceVersionInstrumentationTest"; } + + @Override + protected String getServiceEnvironmentTestClass() { + return "co.elastic.apm.agent.ecs_logging.jul.JulServiceEnvironmentInstrumentationTest"; + } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceEnvironmentInstrumentationTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceEnvironmentInstrumentationTest.java new file mode 100644 index 0000000000..ba9c533575 --- /dev/null +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceEnvironmentInstrumentationTest.java @@ -0,0 +1,52 @@ +/* + * 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.jul; + +import co.elastic.apm.agent.ecs_logging.EcsServiceEnvironmentTest; +import co.elastic.apm.agent.ecs_logging.EcsServiceNameTest; +import co.elastic.apm.agent.testutils.TestClassWithDependencyRunner; +import co.elastic.logging.jul.EcsFormatter; + +import java.util.Collections; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.LogRecord; + +// must be tested in isolation, either used in agent or has side effects +@TestClassWithDependencyRunner.DisableOutsideOfRunner +public class JulServiceEnvironmentInstrumentationTest extends EcsServiceEnvironmentTest { + + private EcsFormatter formatter; + + @Override + protected String createLogMsg() { + return formatter.format(new LogRecord(Level.INFO, "msg")); + } + + @Override + protected void initFormatterWithoutServiceEnvironmentSet() { + formatter = LogManagerTestInstrumentation.createFormatter(Collections.emptyMap()); + } + + @Override + protected void initFormatterWithServiceEnvironment(String environment) { + formatter = LogManagerTestInstrumentation.createFormatter(Map.of("co.elastic.logging.jul.EcsFormatter.serviceEnvironment", environment)); + } + +} diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceNameInstrumentationTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceNameInstrumentationTest.java index 24c39b3783..508213d28f 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceNameInstrumentationTest.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceNameInstrumentationTest.java @@ -40,21 +40,13 @@ protected String createLogMsg() { @Override protected void initFormatterWithoutServiceNameSet() { - formatter = createFormatter(Collections.emptyMap()); + formatter = LogManagerTestInstrumentation.createFormatter(Collections.emptyMap()); } @Override protected void initFormatterWithServiceName(String name) { - formatter = createFormatter(Map.of("co.elastic.logging.jul.EcsFormatter.serviceName", name)); + formatter = LogManagerTestInstrumentation.createFormatter(Map.of("co.elastic.logging.jul.EcsFormatter.serviceName", name)); } - private static EcsFormatter createFormatter(Map map) { - try { - LogManagerTestInstrumentation.JulProperties.override(map); - return new EcsFormatter(); - } finally { - LogManagerTestInstrumentation.JulProperties.restore(); - } - } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceVersionInstrumentationTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceVersionInstrumentationTest.java index b75e488765..0b5705cff7 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceVersionInstrumentationTest.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceVersionInstrumentationTest.java @@ -40,20 +40,11 @@ protected String createLogMsg() { @Override protected void initFormatterWithoutServiceVersionSet() { - formatter = createFormatter(Collections.emptyMap()); + formatter = LogManagerTestInstrumentation.createFormatter(Collections.emptyMap()); } @Override protected void initFormatterWithServiceVersion(String version) { - formatter = createFormatter(Map.of("co.elastic.logging.jul.EcsFormatter.serviceVersion", version)); - } - - private static EcsFormatter createFormatter(Map map) { - try { - LogManagerTestInstrumentation.JulProperties.override(map); - return new EcsFormatter(); - } finally { - LogManagerTestInstrumentation.JulProperties.restore(); - } + formatter = LogManagerTestInstrumentation.createFormatter(Map.of("co.elastic.logging.jul.EcsFormatter.serviceVersion", version)); } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java index 35b909f079..cdd3aad6e0 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java @@ -20,12 +20,14 @@ import co.elastic.apm.agent.sdk.ElasticApmInstrumentation; import co.elastic.apm.agent.sdk.state.GlobalState; +import co.elastic.logging.jul.EcsFormatter; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.NamedElement; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; +import javax.annotation.Nullable; import java.util.Collection; import java.util.Collections; import java.util.Map; @@ -74,8 +76,9 @@ public static String onExit(@Advice.Argument(0) String key, @Advice.Return Strin } @GlobalState - public static class JulProperties { + private static class JulProperties { + @Nullable public static Properties overridenProperties = null; public static void override(Map map) { @@ -88,4 +91,13 @@ public static void restore() { } } + static EcsFormatter createFormatter(Map map) { + try { + JulProperties.override(map); + return new EcsFormatter(); + } finally { + JulProperties.restore(); + } + } + } From e17b869c986fcad06450c2c3ff55869bd081b67d Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 13:57:56 +0200 Subject: [PATCH 03/18] add jboss logging --- .../JbossServiceCorrelationIT.java | 4 ++ ...ServiceEnvironmentInstrumentationTest.java | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossServiceEnvironmentInstrumentationTest.java diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossServiceCorrelationIT.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossServiceCorrelationIT.java index 66d966c06f..186999ac92 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossServiceCorrelationIT.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossServiceCorrelationIT.java @@ -37,4 +37,8 @@ protected String getServiceVersionTestClass() { return "co.elastic.apm.agent.ecs_logging.jbosslogging.JbossServiceVersionInstrumentationTest"; } + @Override + protected String getServiceEnvironmentTestClass() { + return "co.elastic.apm.agent.ecs_logging.jbosslogging.JbossServiceEnvironmentInstrumentationTest"; + } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossServiceEnvironmentInstrumentationTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossServiceEnvironmentInstrumentationTest.java new file mode 100644 index 0000000000..c6fa41c0a4 --- /dev/null +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossServiceEnvironmentInstrumentationTest.java @@ -0,0 +1,49 @@ +/* + * 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.jbosslogging; + +import co.elastic.apm.agent.ecs_logging.EcsServiceEnvironmentTest; +import co.elastic.apm.agent.testutils.TestClassWithDependencyRunner; +import co.elastic.logging.jboss.logmanager.EcsFormatter; +import org.jboss.logmanager.ExtLogRecord; +import org.jboss.logmanager.Level; + +// must be tested in isolation, either used in agent or has side effects +@TestClassWithDependencyRunner.DisableOutsideOfRunner +public class JbossServiceEnvironmentInstrumentationTest extends EcsServiceEnvironmentTest { + + EcsFormatter formatter; + + @Override + protected String createLogMsg() { + ExtLogRecord record = new ExtLogRecord(Level.INFO, "Example Message", "ExampleLoggerClass"); + return formatter.format(record); + } + + @Override + protected void initFormatterWithoutServiceEnvironmentSet() { + formatter = new EcsFormatter(); + } + + @Override + protected void initFormatterWithServiceEnvironment(String environment) { + formatter = new EcsFormatter(); + formatter.setServiceEnvironment(environment); + } +} From daee9c3a6f56fa034be2b85e9eb31fbfa8099dbe Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 14:00:33 +0200 Subject: [PATCH 04/18] add tests for log4j1 --- .../log4j1/Log4jServiceCorrelationIT.java | 5 ++ ...ServiceEnvironmentInstrumentationTest.java | 53 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jServiceEnvironmentInstrumentationTest.java diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jServiceCorrelationIT.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jServiceCorrelationIT.java index 7c3ce90fc3..c2a786630d 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jServiceCorrelationIT.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jServiceCorrelationIT.java @@ -37,4 +37,9 @@ protected String getServiceVersionTestClass() { return "co.elastic.apm.agent.ecs_logging.log4j1.Log4jServiceVersionInstrumentationTest"; } + @Override + protected String getServiceEnvironmentTestClass() { + return "co.elastic.apm.agent.ecs_logging.log4j1.Log4jServiceEnvironmentInstrumentationTest"; + } + } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jServiceEnvironmentInstrumentationTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jServiceEnvironmentInstrumentationTest.java new file mode 100644 index 0000000000..fbb49a6368 --- /dev/null +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jServiceEnvironmentInstrumentationTest.java @@ -0,0 +1,53 @@ +/* + * 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.log4j1; + +import co.elastic.apm.agent.ecs_logging.EcsServiceEnvironmentTest; +import co.elastic.apm.agent.testutils.TestClassWithDependencyRunner; +import co.elastic.logging.log4j.EcsLayout; +import org.apache.log4j.Category; +import org.apache.log4j.Level; +import org.apache.log4j.spi.LoggingEvent; +import org.apache.log4j.spi.RootLogger; + +// must be tested in isolation, either used in agent or has side effects +@TestClassWithDependencyRunner.DisableOutsideOfRunner +public class Log4jServiceEnvironmentInstrumentationTest extends EcsServiceEnvironmentTest { + + private EcsLayout ecsLayout; + + @Override + protected void initFormatterWithoutServiceEnvironmentSet() { + ecsLayout = new EcsLayout(); + } + + @Override + protected void initFormatterWithServiceEnvironment(String environment) { + ecsLayout = new EcsLayout(); + ecsLayout.setServiceEnvironment(environment); + } + + @Override + protected String createLogMsg() { + Category logger = new RootLogger(Level.ALL); + LoggingEvent event = new LoggingEvent("", logger, System.currentTimeMillis(), Level.INFO, "msg", null); + return ecsLayout.format(event); + } + +} From 1454f089de81d465c72be711080b6660b3ce5a3f Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 14:04:36 +0200 Subject: [PATCH 05/18] add tests for log4j2 --- .../log4j2/Log4j2ServiceCorrelationIT.java | 4 ++ ...ServiceEnvironmentInstrumentationTest.java | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceEnvironmentInstrumentationTest.java diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceCorrelationIT.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceCorrelationIT.java index 63d337dc99..53561b4796 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceCorrelationIT.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceCorrelationIT.java @@ -37,4 +37,8 @@ protected String getServiceVersionTestClass() { return "co.elastic.apm.agent.ecs_logging.log4j2.Log4j2ServiceVersionInstrumentationTest"; } + @Override + protected String getServiceEnvironmentTestClass() { + return "co.elastic.apm.agent.ecs_logging.log4j2.Log4j2ServiceEnvironmentInstrumentationTest"; + } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceEnvironmentInstrumentationTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceEnvironmentInstrumentationTest.java new file mode 100644 index 0000000000..a8291d28cd --- /dev/null +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceEnvironmentInstrumentationTest.java @@ -0,0 +1,48 @@ +/* + * 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.log4j2; + +import co.elastic.apm.agent.ecs_logging.EcsServiceEnvironmentTest; +import co.elastic.apm.agent.testutils.TestClassWithDependencyRunner; +import co.elastic.logging.log4j2.EcsLayout; +import org.apache.logging.log4j.core.impl.Log4jLogEvent; +import org.apache.logging.log4j.message.SimpleMessage; + +// must be tested in isolation, either used in agent or has side effects +@TestClassWithDependencyRunner.DisableOutsideOfRunner +public class Log4j2ServiceEnvironmentInstrumentationTest extends EcsServiceEnvironmentTest { + + private EcsLayout ecsLayout; + + @Override + protected void initFormatterWithoutServiceEnvironmentSet() { + ecsLayout = EcsLayout.newBuilder().build(); + } + + @Override + protected void initFormatterWithServiceEnvironment(String environment) { + ecsLayout = EcsLayout.newBuilder().setServiceEnvironment(environment).build(); + } + + @Override + protected String createLogMsg() { + Log4jLogEvent event = new Log4jLogEvent("", null, "", null, new SimpleMessage(), null, null); + return ecsLayout.toSerializable(event); + } +} From b629870f5c7e3e6ddcc84554dc1ab4109ce59220 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 14:05:47 +0200 Subject: [PATCH 06/18] add tests for logback --- .../logback/LogbackServiceCorrelationIT.java | 5 ++ ...ServiceEnvironmentInstrumentationTest.java | 56 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/logback/LogbackServiceEnvironmentInstrumentationTest.java diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/logback/LogbackServiceCorrelationIT.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/logback/LogbackServiceCorrelationIT.java index 7297ea45a6..b4795840cf 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/logback/LogbackServiceCorrelationIT.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/logback/LogbackServiceCorrelationIT.java @@ -36,4 +36,9 @@ protected String getServiceNameTestClass() { protected String getServiceVersionTestClass() { return "co.elastic.apm.agent.ecs_logging.logback.LogbackServiceVersionInstrumentationTest"; } + + @Override + protected String getServiceEnvironmentTestClass() { + return "co.elastic.apm.agent.ecs_logging.logback.LogbackServiceEnvironmentInstrumentationTest"; + } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/logback/LogbackServiceEnvironmentInstrumentationTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/logback/LogbackServiceEnvironmentInstrumentationTest.java new file mode 100644 index 0000000000..75b45a95bc --- /dev/null +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/logback/LogbackServiceEnvironmentInstrumentationTest.java @@ -0,0 +1,56 @@ +/* + * 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.logback; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.classic.spi.LoggingEvent; +import co.elastic.apm.agent.ecs_logging.EcsServiceEnvironmentTest; +import co.elastic.apm.agent.testutils.TestClassWithDependencyRunner; +import co.elastic.logging.logback.EcsEncoder; + +// must be tested in isolation, either used in agent or has side effects +@TestClassWithDependencyRunner.DisableOutsideOfRunner +public class LogbackServiceEnvironmentInstrumentationTest extends EcsServiceEnvironmentTest { + + private EcsEncoder ecsEncoder; + + @Override + protected String createLogMsg() { + LoggerContext loggerContext = new LoggerContext(); + Logger logger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); + ILoggingEvent event = new LoggingEvent("co.elastic.apm.agent.ecs_logging.logback.LogbackServiceNameInstrumentationTest", logger, Level.INFO, "msg", null, null); + return new String(ecsEncoder.encode(event)); + } + + @Override + protected void initFormatterWithoutServiceEnvironmentSet() { + ecsEncoder = new EcsEncoder(); + ecsEncoder.start(); + } + + @Override + protected void initFormatterWithServiceEnvironment(String environment) { + ecsEncoder = new EcsEncoder(); + ecsEncoder.start(); + ecsEncoder.setServiceEnvironment(environment); + } +} From 995a7100af1db1b63b67438291eefe18ca7e96bd Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 14:10:13 +0200 Subject: [PATCH 07/18] remove leftover --- .../logback/LogBackServiceInstrumentation.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java index e834a6b681..896c0ec157 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java @@ -85,16 +85,4 @@ public static String onExit(@Advice.This Object encoder, } - public static class VersionAdvice { - - @Nullable - @Advice.AssignReturned.ToFields(@ToField("serviceVersion")) - @Advice.OnMethodExit(suppress = Throwable.class, inline = false) - public static String onExit(@Advice.This Object encoder, - @Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) { - - return EcsLoggingUtils.getOrWarnServiceVersion(encoder, serviceVersion); - } - } - } From 81eed31847a8f763a043e0bcc8eecdcbefeb22a2 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 14:21:09 +0200 Subject: [PATCH 08/18] implement for jboss logging --- .../JbossEcsServiceInstrumentation.java | 24 +++++++++++++++++++ ...ic.apm.agent.sdk.ElasticApmInstrumentation | 1 + .../EcsServiceEnvironmentTest.java | 2 +- .../jul/LogManagerTestInstrumentation.java | 2 +- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java index 9f66b68dcd..85d1441459 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java @@ -87,4 +87,28 @@ public static String onEnter(@Advice.This Object formatter, } + public static class Environment extends JbossEcsServiceInstrumentation { + + @Override + public ElementMatcher.Junction getTypeMatcher() { + return super.getTypeMatcher() + // setServiceEnvironment introduced in 1.5.0 + .and(declaresMethod(named("setServiceEnvironment"))); + } + + public static class AdviceClass { + + @Nullable + @Advice.AssignReturned.ToFields(@ToField(value = "serviceEnvironment", typing = Assigner.Typing.DYNAMIC)) + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + public static String onEnter(@Advice.This Object formatter, + @Advice.FieldValue("serviceEnvironment") @Nullable String serviceEnvironment) { + + return EcsLoggingUtils.getOrWarnServiceEnvironment(formatter, serviceEnvironment); + } + } + + } + + } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation index 8fd72c819e..3cdac625e0 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation @@ -18,3 +18,4 @@ co.elastic.apm.agent.ecs_logging.logback.LogBackServiceInstrumentation$Version # JBoss co.elastic.apm.agent.ecs_logging.jbosslogging.JbossEcsServiceInstrumentation$Name co.elastic.apm.agent.ecs_logging.jbosslogging.JbossEcsServiceInstrumentation$Version +co.elastic.apm.agent.ecs_logging.jbosslogging.JbossEcsServiceInstrumentation$Environment diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java index 7abff9b8f5..377d4e9bd6 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java @@ -35,7 +35,7 @@ public void setUp() { @Test public void testBuildWithNoServiceNameSet() { initFormatterWithoutServiceEnvironmentSet(); - assertThat(getJson(createLogMsg(), "service.environment")).isEqualTo("test-env"); + assertThat(getJson(createLogMsg(), "service.environment")).isEqualTo("test"); } protected abstract void initFormatterWithoutServiceEnvironmentSet(); diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java index cdd3aad6e0..5da3b944c0 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java @@ -76,7 +76,7 @@ public static String onExit(@Advice.Argument(0) String key, @Advice.Return Strin } @GlobalState - private static class JulProperties { + public static class JulProperties { @Nullable public static Properties overridenProperties = null; From 0f967f9c7858f682af1eeb1a3e51ba544f0c9e0e Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 14:32:23 +0200 Subject: [PATCH 09/18] implement for JUL + fix tests --- .../jul/JulEcsServiceInstrumentation.java | 24 ++++++++++++++++++- ...ic.apm.agent.sdk.ElasticApmInstrumentation | 1 + .../EcsServiceEnvironmentTest.java | 4 ++-- ...ServiceEnvironmentInstrumentationTest.java | 13 ++++++++-- .../JulServiceNameInstrumentationTest.java | 12 ++++++++-- .../JulServiceVersionInstrumentationTest.java | 13 ++++++++-- .../jul/LogManagerTestInstrumentation.java | 9 ------- 7 files changed, 58 insertions(+), 18 deletions(-) diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java index cf86934637..29a68dfa43 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java @@ -20,7 +20,6 @@ import co.elastic.apm.agent.ecs_logging.EcsLoggingInstrumentation; import co.elastic.apm.agent.ecs_logging.EcsLoggingUtils; -import co.elastic.logging.jul.EcsFormatter; import net.bytebuddy.asm.Advice; import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; import net.bytebuddy.description.method.MethodDescription; @@ -88,5 +87,28 @@ public static String onEnter(@Advice.This Object formatter, } + public static class Environment extends JulEcsServiceInstrumentation { + + @Override + public ElementMatcher.Junction getTypeMatcher() { + return super.getTypeMatcher() + // setServiceVersion introduced in 1.5.0 + .and(declaresMethod(named("setServiceEnvironment"))); + } + + public static class AdviceClass { + + @Nullable + @Advice.AssignReturned.ToFields(@ToField(value = "serviceEnvironment")) + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + public static String onEnter(@Advice.This Object formatter, + @Advice.FieldValue("serviceEnvironment") @Nullable String serviceEnvironment) { + + return EcsLoggingUtils.getOrWarnServiceEnvironment(formatter, serviceEnvironment); + } + } + + } + } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation index 3cdac625e0..108fe16c54 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation @@ -10,6 +10,7 @@ co.elastic.apm.agent.ecs_logging.log4j2.Log4j2ServiceInstrumentation$Version co.elastic.apm.agent.ecs_logging.jul.JulEcsMdcInstrumentation co.elastic.apm.agent.ecs_logging.jul.JulEcsServiceInstrumentation$Name co.elastic.apm.agent.ecs_logging.jul.JulEcsServiceInstrumentation$Version +co.elastic.apm.agent.ecs_logging.jul.JulEcsServiceInstrumentation$Environment # Logback co.elastic.apm.agent.ecs_logging.logback.LogBackServiceInstrumentation$Name diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java index 377d4e9bd6..915975ebac 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/EcsServiceEnvironmentTest.java @@ -33,7 +33,7 @@ public void setUp() { } @Test - public void testBuildWithNoServiceNameSet() { + public void testBuildWithNoServiceEnvironmentSet() { initFormatterWithoutServiceEnvironmentSet(); assertThat(getJson(createLogMsg(), "service.environment")).isEqualTo("test"); } @@ -41,7 +41,7 @@ public void testBuildWithNoServiceNameSet() { protected abstract void initFormatterWithoutServiceEnvironmentSet(); @Test - public void testBuildWithServiceNameSet() { + public void testBuildWithServiceEnvironmentSet() { // this should also issue a warning as the value configured in ecs-logging differs from the agent initFormatterWithServiceEnvironment("prod"); assertThat(getJson(createLogMsg(), "service.environment")).isEqualTo("prod"); diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceEnvironmentInstrumentationTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceEnvironmentInstrumentationTest.java index ba9c533575..c8585b5968 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceEnvironmentInstrumentationTest.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceEnvironmentInstrumentationTest.java @@ -41,12 +41,21 @@ protected String createLogMsg() { @Override protected void initFormatterWithoutServiceEnvironmentSet() { - formatter = LogManagerTestInstrumentation.createFormatter(Collections.emptyMap()); + formatter = createFormatter(Collections.emptyMap()); } @Override protected void initFormatterWithServiceEnvironment(String environment) { - formatter = LogManagerTestInstrumentation.createFormatter(Map.of("co.elastic.logging.jul.EcsFormatter.serviceEnvironment", environment)); + formatter = createFormatter(Map.of("co.elastic.logging.jul.EcsFormatter.serviceEnvironment", environment)); + } + + private static EcsFormatter createFormatter(Map map) { + try { + LogManagerTestInstrumentation.JulProperties.override(map); + return new EcsFormatter(); + } finally { + LogManagerTestInstrumentation.JulProperties.restore(); + } } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceNameInstrumentationTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceNameInstrumentationTest.java index 508213d28f..24c39b3783 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceNameInstrumentationTest.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceNameInstrumentationTest.java @@ -40,13 +40,21 @@ protected String createLogMsg() { @Override protected void initFormatterWithoutServiceNameSet() { - formatter = LogManagerTestInstrumentation.createFormatter(Collections.emptyMap()); + formatter = createFormatter(Collections.emptyMap()); } @Override protected void initFormatterWithServiceName(String name) { - formatter = LogManagerTestInstrumentation.createFormatter(Map.of("co.elastic.logging.jul.EcsFormatter.serviceName", name)); + formatter = createFormatter(Map.of("co.elastic.logging.jul.EcsFormatter.serviceName", name)); } + private static EcsFormatter createFormatter(Map map) { + try { + LogManagerTestInstrumentation.JulProperties.override(map); + return new EcsFormatter(); + } finally { + LogManagerTestInstrumentation.JulProperties.restore(); + } + } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceVersionInstrumentationTest.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceVersionInstrumentationTest.java index 0b5705cff7..b75e488765 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceVersionInstrumentationTest.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/JulServiceVersionInstrumentationTest.java @@ -40,11 +40,20 @@ protected String createLogMsg() { @Override protected void initFormatterWithoutServiceVersionSet() { - formatter = LogManagerTestInstrumentation.createFormatter(Collections.emptyMap()); + formatter = createFormatter(Collections.emptyMap()); } @Override protected void initFormatterWithServiceVersion(String version) { - formatter = LogManagerTestInstrumentation.createFormatter(Map.of("co.elastic.logging.jul.EcsFormatter.serviceVersion", version)); + formatter = createFormatter(Map.of("co.elastic.logging.jul.EcsFormatter.serviceVersion", version)); + } + + private static EcsFormatter createFormatter(Map map) { + try { + LogManagerTestInstrumentation.JulProperties.override(map); + return new EcsFormatter(); + } finally { + LogManagerTestInstrumentation.JulProperties.restore(); + } } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java index 5da3b944c0..5fdc74747e 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/test/java/co/elastic/apm/agent/ecs_logging/jul/LogManagerTestInstrumentation.java @@ -91,13 +91,4 @@ public static void restore() { } } - static EcsFormatter createFormatter(Map map) { - try { - JulProperties.override(map); - return new EcsFormatter(); - } finally { - JulProperties.restore(); - } - } - } From b6c2075332b8e9d292aa8dd657d0c8a90730d3f8 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 14:34:55 +0200 Subject: [PATCH 10/18] logback + log4j2 --- .../log4j2/Log4j2ServiceInstrumentation.java | 23 ++++++++++++++++++ .../LogBackServiceInstrumentation.java | 24 +++++++++++++++++++ ...ic.apm.agent.sdk.ElasticApmInstrumentation | 2 ++ 3 files changed, 49 insertions(+) diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceInstrumentation.java index 68a13b34b8..732405fc96 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j2/Log4j2ServiceInstrumentation.java @@ -84,4 +84,27 @@ public static String onEnter(@Advice.This Object builder, } } + public static class Environment extends Log4j2ServiceInstrumentation { + + @Override + public ElementMatcher.Junction getTypeMatcher() { + return named("co.elastic.logging.log4j2.EcsLayout$Builder") + // serviceEnvironment introduced in 1.5.0 + .and(declaresField(named("serviceEnvironment"))); + } + + public static class AdviceClass { + + @Nullable + @Advice.AssignReturned.ToFields(@ToField(value = "serviceEnvironment")) + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + public static String onEnter(@Advice.This Object formatter, + @Advice.FieldValue("serviceEnvironment") @Nullable String serviceEnvironment) { + + return EcsLoggingUtils.getOrWarnServiceEnvironment(formatter, serviceEnvironment); + } + } + + } + } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java index 896c0ec157..f162d5e244 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/logback/LogBackServiceInstrumentation.java @@ -30,6 +30,7 @@ import javax.annotation.Nullable; import static net.bytebuddy.matcher.ElementMatchers.declaresField; +import static net.bytebuddy.matcher.ElementMatchers.declaresMethod; import static net.bytebuddy.matcher.ElementMatchers.named; /** @@ -85,4 +86,27 @@ public static String onExit(@Advice.This Object encoder, } + public static class Environment extends LogBackServiceInstrumentation { + + @Override + public ElementMatcher.Junction getTypeMatcher() { + return super.getTypeMatcher() + // setServiceVersion introduced in 1.5.0 + .and(declaresMethod(named("setServiceEnvironment"))); + } + + public static class AdviceClass { + + @Nullable + @Advice.AssignReturned.ToFields(@ToField(value = "serviceEnvironment")) + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + public static String onEnter(@Advice.This Object formatter, + @Advice.FieldValue("serviceEnvironment") @Nullable String serviceEnvironment) { + + return EcsLoggingUtils.getOrWarnServiceEnvironment(formatter, serviceEnvironment); + } + } + + } + } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation index 108fe16c54..9ca7348ce8 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation @@ -5,6 +5,7 @@ co.elastic.apm.agent.ecs_logging.log4j1.Log4jEcsServiceInstrumentation$Version # log4j 2.x co.elastic.apm.agent.ecs_logging.log4j2.Log4j2ServiceInstrumentation$Name co.elastic.apm.agent.ecs_logging.log4j2.Log4j2ServiceInstrumentation$Version +co.elastic.apm.agent.ecs_logging.log4j2.Log4j2ServiceInstrumentation$Environment # JUL co.elastic.apm.agent.ecs_logging.jul.JulEcsMdcInstrumentation @@ -15,6 +16,7 @@ co.elastic.apm.agent.ecs_logging.jul.JulEcsServiceInstrumentation$Environment # Logback co.elastic.apm.agent.ecs_logging.logback.LogBackServiceInstrumentation$Name co.elastic.apm.agent.ecs_logging.logback.LogBackServiceInstrumentation$Version +co.elastic.apm.agent.ecs_logging.logback.LogBackServiceInstrumentation$Environment # JBoss co.elastic.apm.agent.ecs_logging.jbosslogging.JbossEcsServiceInstrumentation$Name From 35ac015af9a8c8443d868656e2d110605325d9d6 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 14:36:56 +0200 Subject: [PATCH 11/18] add log4j1 --- .../Log4jEcsServiceInstrumentation.java | 23 +++++++++++++++++++ ...ic.apm.agent.sdk.ElasticApmInstrumentation | 1 + 2 files changed, 24 insertions(+) diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jEcsServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jEcsServiceInstrumentation.java index abc9860a5f..283846ff08 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jEcsServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/log4j1/Log4jEcsServiceInstrumentation.java @@ -86,4 +86,27 @@ public static String onEnter(@Advice.This Object layout, } } + + public static class Environment extends Log4jEcsServiceInstrumentation { + + @Override + public ElementMatcher.Junction getTypeMatcher() { + return super.getTypeMatcher() + // setServiceEnvironment introduced in 1.5.0 + .and(declaresMethod(named("setServiceEnvironment"))); + } + + public static class AdviceClass { + + @Nullable + @Advice.AssignReturned.ToFields(@ToField("serviceEnvironment")) + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + public static String onEnter(@Advice.This Object layout, + @Advice.FieldValue("serviceEnvironment") @Nullable String serviceEnvironment) { + + return EcsLoggingUtils.getOrWarnServiceEnvironment(layout, serviceEnvironment); + } + } + + } } diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation index 9ca7348ce8..f25ba23436 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/resources/META-INF/services/co.elastic.apm.agent.sdk.ElasticApmInstrumentation @@ -1,6 +1,7 @@ # log4j 1.x co.elastic.apm.agent.ecs_logging.log4j1.Log4jEcsServiceInstrumentation$Name co.elastic.apm.agent.ecs_logging.log4j1.Log4jEcsServiceInstrumentation$Version +co.elastic.apm.agent.ecs_logging.log4j1.Log4jEcsServiceInstrumentation$Environment # log4j 2.x co.elastic.apm.agent.ecs_logging.log4j2.Log4j2ServiceInstrumentation$Name From 3e36f5d8b3a065417e283e6d112652d74e40638f Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 14:48:45 +0200 Subject: [PATCH 12/18] removing useless explicit typing --- .../jbosslogging/JbossEcsServiceInstrumentation.java | 7 +++---- .../ecs_logging/jul/JulEcsServiceInstrumentation.java | 5 ++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java index 85d1441459..f3abe05e53 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jbosslogging/JbossEcsServiceInstrumentation.java @@ -25,7 +25,6 @@ import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.implementation.bytecode.assign.Assigner; import net.bytebuddy.matcher.ElementMatcher; import javax.annotation.Nullable; @@ -53,7 +52,7 @@ public static class Name extends JbossEcsServiceInstrumentation { public static class AdviceClass { @Nullable - @Advice.AssignReturned.ToFields(@ToField(value = "serviceName", typing = Assigner.Typing.DYNAMIC)) + @Advice.AssignReturned.ToFields(@ToField(value = "serviceName")) @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static String onEnter(@Advice.This Object formatter, @Advice.FieldValue("serviceName") @Nullable String serviceName) { @@ -76,7 +75,7 @@ public ElementMatcher.Junction getTypeMatcher() { public static class AdviceClass { @Nullable - @Advice.AssignReturned.ToFields(@ToField(value = "serviceVersion", typing = Assigner.Typing.DYNAMIC)) + @Advice.AssignReturned.ToFields(@ToField(value = "serviceVersion")) @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static String onEnter(@Advice.This Object formatter, @Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) { @@ -99,7 +98,7 @@ public ElementMatcher.Junction getTypeMatcher() { public static class AdviceClass { @Nullable - @Advice.AssignReturned.ToFields(@ToField(value = "serviceEnvironment", typing = Assigner.Typing.DYNAMIC)) + @Advice.AssignReturned.ToFields(@ToField(value = "serviceEnvironment")) @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static String onEnter(@Advice.This Object formatter, @Advice.FieldValue("serviceEnvironment") @Nullable String serviceEnvironment) { diff --git a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java index 29a68dfa43..779a908931 100644 --- a/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java +++ b/apm-agent-plugins/apm-ecs-logging-plugin/src/main/java/co/elastic/apm/agent/ecs_logging/jul/JulEcsServiceInstrumentation.java @@ -24,7 +24,6 @@ import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; import net.bytebuddy.description.method.MethodDescription; import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.implementation.bytecode.assign.Assigner; import net.bytebuddy.matcher.ElementMatcher; import javax.annotation.Nullable; @@ -52,7 +51,7 @@ public static class Name extends JulEcsServiceInstrumentation { public static class AdviceClass { @Nullable - @Advice.AssignReturned.ToFields(@ToField(value = "serviceName", typing = Assigner.Typing.DYNAMIC)) + @Advice.AssignReturned.ToFields(@ToField(value = "serviceName")) @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static String onEnter(@Advice.This Object formatter, @Advice.FieldValue("serviceName") @Nullable String serviceName) { @@ -76,7 +75,7 @@ public ElementMatcher.Junction getTypeMatcher() { public static class AdviceClass { @Nullable - @Advice.AssignReturned.ToFields(@ToField(value = "serviceVersion", typing = Assigner.Typing.DYNAMIC)) + @Advice.AssignReturned.ToFields(@ToField(value = "serviceVersion")) @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) public static String onEnter(@Advice.This Object formatter, @Advice.FieldValue("serviceVersion") @Nullable String serviceVersion) { From 84338daa3d951924c58b6765afb31143be4d2535 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 17:53:29 +0200 Subject: [PATCH 13/18] reformat logs add env. test --- .../AbstractJulEcsReformattingHelper.java | 11 +++++++--- .../Log4J1EcsReformattingHelper.java | 9 ++++++-- .../Log4J2EcsReformattingHelper.java | 8 +++++-- .../LogbackEcsReformattingHelper.java | 5 ++++- .../AbstractEcsReformattingHelper.java | 22 +++++++++++++++---- .../loginstr/LoggingInstrumentationTest.java | 5 ++++- 6 files changed, 47 insertions(+), 13 deletions(-) diff --git a/apm-agent-plugins/apm-logging-plugin/apm-jul-plugin/src/main/java/co/elastic/apm/agent/jul/reformatting/AbstractJulEcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-jul-plugin/src/main/java/co/elastic/apm/agent/jul/reformatting/AbstractJulEcsReformattingHelper.java index 12e07a9a1d..ba10807374 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-jul-plugin/src/main/java/co/elastic/apm/agent/jul/reformatting/AbstractJulEcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-jul-plugin/src/main/java/co/elastic/apm/agent/jul/reformatting/AbstractJulEcsReformattingHelper.java @@ -65,9 +65,14 @@ protected void closeShadeAppender(T handler) { @Nullable @Override - public Formatter createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion, - @Nullable String serviceNodeName, @Nullable Map additionalFields, - Formatter originalFormatter) { + public Formatter createEcsFormatter(String eventDataset, + @Nullable String serviceName, + @Nullable String serviceVersion, + @Nullable String serviceEnvironment, + @Nullable String serviceNodeName, + @Nullable Map additionalFields, + @Nullable Formatter originalFormatter) { + EcsFormatter ecsFormatter = new EcsFormatter() { @Override protected Map getMdcEntries() { diff --git a/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java index a1ca4c0de4..fc91d5b8ba 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java @@ -60,9 +60,14 @@ protected String getAppenderName(WriterAppender appender) { } @Override - protected Layout createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion, - @Nullable String serviceNodeName, @Nullable Map additionalFields, + protected Layout createEcsFormatter(String eventDataset, + @Nullable String serviceName, + @Nullable String serviceVersion, + @Nullable String serviceEnvironment, + @Nullable String serviceNodeName, + @Nullable Map additionalFields, @Nullable Layout originalFormatter) { + EcsLayout ecsLayout = new EcsLayout(); ecsLayout.setServiceName(serviceName); ecsLayout.setServiceVersion(serviceVersion); diff --git a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java index 35d3960fa4..2055934395 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java @@ -66,8 +66,12 @@ protected String getAppenderName(Appender appender) { } @Override - protected Layout createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion, - @Nullable String serviceNodeName, @Nullable Map additionalFields, + protected Layout createEcsFormatter(String eventDataset, + @Nullable String serviceName, + @Nullable String serviceVersion, + @Nullable String serviceEnvironment, + @Nullable String serviceNodeName, + @Nullable Map additionalFields, @Nullable Layout originalFormatter) { EcsLayout.Builder builder = EcsLayout.newBuilder() .setServiceName(serviceName) diff --git a/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java index 093e7ac480..559f09c91f 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java @@ -79,7 +79,10 @@ protected String getAppenderName(OutputStreamAppender appender) { } @Override - protected Encoder createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion, + protected Encoder createEcsFormatter(String eventDataset, + @Nullable String serviceName, + @Nullable String serviceVersion, + @Nullable String serviceEnvironment, @Nullable String serviceNodeName, @Nullable Map additionalFields, @Nullable Encoder originalFormatter) { diff --git a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java index 27028daa6b..efeeef4d16 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/main/java/co/elastic/apm/agent/loginstr/reformatting/AbstractEcsReformattingHelper.java @@ -164,6 +164,9 @@ public abstract class AbstractEcsReformattingHelper { @Nullable private final String configuredServiceNodeName; + @Nullable + private final String environment; + @Nullable private final Map additionalFields; private final Reporter reporter; @@ -184,6 +187,7 @@ public AbstractEcsReformattingHelper() { } else { configuredServiceNodeName = null; } + environment = service.getEnvironment(); reporter = tracer.require(ElasticApmTracer.class).getReporter(); } @@ -367,11 +371,17 @@ private Object createAndMapShadeAppenderFor(final A originalAppender) { } } + @Nullable private F createEcsFormatter(A originalAppender) { String serviceName = getServiceName(); return createEcsFormatter( - getEventDataset(originalAppender, serviceName), serviceName, getServiceVersion(), - configuredServiceNodeName, additionalFields, getFormatterFrom(originalAppender) + getEventDataset(originalAppender, serviceName), + serviceName, + getServiceVersion(), + environment, + configuredServiceNodeName, + additionalFields, + getFormatterFrom(originalAppender) ); } @@ -466,8 +476,12 @@ private boolean isEcsFormatter(F formatter) { protected abstract A createAndStartEcsAppender(A originalAppender, String ecsAppenderName, F ecsFormatter); @Nullable - protected abstract F createEcsFormatter(String eventDataset, @Nullable String serviceName, @Nullable String serviceVersion, - @Nullable String serviceNodeName, @Nullable Map additionalFields, + protected abstract F createEcsFormatter(String eventDataset, + @Nullable String serviceName, + @Nullable String serviceVersion, + @Nullable String serviceEnvironment, + @Nullable String serviceNodeName, + @Nullable Map additionalFields, @Nullable F originalFormatter); /** diff --git a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/test/java/co/elastic/apm/agent/loginstr/LoggingInstrumentationTest.java b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/test/java/co/elastic/apm/agent/loginstr/LoggingInstrumentationTest.java index b588182fb5..63e2ee3e9d 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/test/java/co/elastic/apm/agent/loginstr/LoggingInstrumentationTest.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-logging-plugin-common/src/test/java/co/elastic/apm/agent/loginstr/LoggingInstrumentationTest.java @@ -68,6 +68,7 @@ public abstract class LoggingInstrumentationTest extends AbstractInstrumentation private static final String SERVICE_NODE_NAME = "my-service-node"; private static final Map ADDITIONAL_FIELDS = Map.of("some.field", "some-value", "another.field", "another-value"); + private static final String ENVIRONMENT = "my-env"; private final LoggerFacade logger; private final ObjectMapper objectMapper; @@ -91,6 +92,7 @@ public LoggingInstrumentationTest() { @BeforeEach public void setup() throws Exception { doReturn(SERVICE_VERSION).when(config.getConfig(CoreConfiguration.class)).getServiceVersion(); + doReturn(ENVIRONMENT).when(config.getConfig(CoreConfiguration.class)).getEnvironment(); doReturn(SERVICE_NODE_NAME).when(config.getConfig(CoreConfiguration.class)).getServiceNodeName(); loggingConfig = config.getConfig(LoggingConfiguration.class); @@ -342,7 +344,8 @@ private void verifyTracingMetadata(JsonNode ecsLogLineTree) { assertThat(ecsLogLineTree.get("service.name").textValue()).isEqualTo(serviceName); assertThat(ecsLogLineTree.get("service.node.name").textValue()).isEqualTo(SERVICE_NODE_NAME); assertThat(ecsLogLineTree.get("event.dataset").textValue()).isEqualTo(serviceName + ".FILE"); - assertThat(ecsLogLineTree.get("service.version").textValue()).isEqualTo("v42"); + assertThat(ecsLogLineTree.get("service.version").textValue()).isEqualTo(SERVICE_VERSION); + assertThat(ecsLogLineTree.get("service.environment").textValue()).isEqualTo(ENVIRONMENT); assertThat(ecsLogLineTree.get("some.field").textValue()).isEqualTo("some-value"); assertThat(ecsLogLineTree.get("another.field").textValue()).isEqualTo("another-value"); } From 57994c4f55309bd025913ba1629b6fbebfb47502 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Tue, 18 Apr 2023 18:00:58 +0200 Subject: [PATCH 14/18] set environment for reformatting --- .../agent/jul/reformatting/AbstractJulEcsReformattingHelper.java | 1 + .../agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java | 1 + .../agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java | 1 + .../agent/logback/reformatting/LogbackEcsReformattingHelper.java | 1 + 4 files changed, 4 insertions(+) diff --git a/apm-agent-plugins/apm-logging-plugin/apm-jul-plugin/src/main/java/co/elastic/apm/agent/jul/reformatting/AbstractJulEcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-jul-plugin/src/main/java/co/elastic/apm/agent/jul/reformatting/AbstractJulEcsReformattingHelper.java index ba10807374..c53dc5fbd3 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-jul-plugin/src/main/java/co/elastic/apm/agent/jul/reformatting/AbstractJulEcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-jul-plugin/src/main/java/co/elastic/apm/agent/jul/reformatting/AbstractJulEcsReformattingHelper.java @@ -82,6 +82,7 @@ protected Map getMdcEntries() { }; ecsFormatter.setServiceName(serviceName); ecsFormatter.setServiceVersion(serviceVersion); + ecsFormatter.setServiceEnvironment(serviceEnvironment); ecsFormatter.setServiceNodeName(serviceNodeName); ecsFormatter.setEventDataset(eventDataset); if (additionalFields != null && !additionalFields.isEmpty()) { diff --git a/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java index fc91d5b8ba..f70bbd550b 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-log4j1-plugin/src/main/java/co/elastic/apm/agent/log4j1/reformatting/Log4J1EcsReformattingHelper.java @@ -71,6 +71,7 @@ protected Layout createEcsFormatter(String eventDataset, EcsLayout ecsLayout = new EcsLayout(); ecsLayout.setServiceName(serviceName); ecsLayout.setServiceVersion(serviceVersion); + ecsLayout.setServiceEnvironment(serviceEnvironment); ecsLayout.setServiceNodeName(serviceNodeName); ecsLayout.setEventDataset(eventDataset); if (additionalFields != null) { diff --git a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java index 2055934395..a9c7f519bc 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-log4j2-plugin/src/main/java/co/elastic/apm/agent/log4j2/reformatting/Log4J2EcsReformattingHelper.java @@ -76,6 +76,7 @@ protected Layout createEcsFormatter(String eventDataset, EcsLayout.Builder builder = EcsLayout.newBuilder() .setServiceName(serviceName) .setServiceVersion(serviceVersion) + .setServiceEnvironment(serviceEnvironment) .setServiceNodeName(serviceNodeName) .setEventDataset(eventDataset) .setIncludeMarkers(true) diff --git a/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java b/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java index 559f09c91f..6ebb056352 100644 --- a/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java +++ b/apm-agent-plugins/apm-logging-plugin/apm-logback-plugin/apm-logback-plugin-impl/src/main/java/co/elastic/apm/agent/logback/reformatting/LogbackEcsReformattingHelper.java @@ -89,6 +89,7 @@ protected Encoder createEcsFormatter(String eventDataset, EcsEncoder ecsEncoder = new EcsEncoder(); ecsEncoder.setServiceName(serviceName); ecsEncoder.setServiceVersion(serviceVersion); + ecsEncoder.setServiceEnvironment(serviceEnvironment); ecsEncoder.setServiceNodeName(serviceNodeName); ecsEncoder.setEventDataset(eventDataset); ecsEncoder.setIncludeMarkers(true); From a21fe387ac6b1ed66b81424af4ffaec099104e92 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Wed, 19 Apr 2023 10:11:39 +0200 Subject: [PATCH 15/18] document service-level log correlation --- docs/logs.asciidoc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/docs/logs.asciidoc b/docs/logs.asciidoc index 273a412afc..b2fcb09d3f 100644 --- a/docs/logs.asciidoc +++ b/docs/logs.asciidoc @@ -26,7 +26,7 @@ NOTE: Starting in APM agent version 1.30.0, log correlation is enabled by defaul In previous versions, log correlation must be explicitly enabled by setting the `enable_log_correlation` configuration variable to `true`. -In order to correlate logs from your application with transactions captured by the Elastic APM Java Agent, +In order to correlate logs from your application with traces and errors captured by the Elastic APM Java Agent, the agent injects the following IDs into https://www.slf4j.org/api/org/slf4j/MDC.html[slf4j-MDC]-equivalents of <>: @@ -40,6 +40,13 @@ with <>. For plain text logs, the pattern layout of your logging configuration needs to be modified to write the MDC values into log files. If you are using Logback or log4j, add `%X` to the format to log all MDC values or `%X{trace.id}` to only log the trace id. +When using ECS-formatted logs, with `ecs-logging-java` library or <>, the agent +also injects into ECS formatted logs the following identifiers for service-level correlation. + +* {ecs-ref}/ecs-service.html[`service.name`] +* {ecs-ref}/ecs-service.html[`service.version`] +* {ecs-ref}/ecs-service.html[`service.environment`] + [float] [[log-reformatting]] === Log reformatting (experimental) From abf9a2668c9c7e646132849cacf9f28a3e3da22d Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Wed, 19 Apr 2023 10:44:45 +0200 Subject: [PATCH 16/18] doc: single correlation paragraph --- docs/logs.asciidoc | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/docs/logs.asciidoc b/docs/logs.asciidoc index b2fcb09d3f..7bff448a6a 100644 --- a/docs/logs.asciidoc +++ b/docs/logs.asciidoc @@ -40,12 +40,13 @@ with <>. For plain text logs, the pattern layout of your logging configuration needs to be modified to write the MDC values into log files. If you are using Logback or log4j, add `%X` to the format to log all MDC values or `%X{trace.id}` to only log the trace id. -When using ECS-formatted logs, with `ecs-logging-java` library or <>, the agent -also injects into ECS formatted logs the following identifiers for service-level correlation. +When the application is logging in ECS format (by using `ecs-logging-java` or <>) + but does not provide the service fields, then the agent will automatically provide fallback values from its own configuration +to provide service-level correlation: -* {ecs-ref}/ecs-service.html[`service.name`] -* {ecs-ref}/ecs-service.html[`service.version`] -* {ecs-ref}/ecs-service.html[`service.environment`] +- {ecs-ref}/ecs-service.html[`service.name`] value provided <> in agent config. +- {ecs-ref}/ecs-service.html[`service.version`] value provided by <> in agent config. +- {ecs-ref}/ecs-service.html[`service.environment`] value provided by <> in agent config. [float] [[log-reformatting]] @@ -57,6 +58,8 @@ change to the application. Log reformatting is controlled by the <> configuration option, and is disabled by default. +The reformatted logs will include both the <> IDs. + [float] [[log-error-capturing]] === Error capturing @@ -77,13 +80,3 @@ As a result, when an exception is reported to the logger: The agent can automatically capture and send logs directly to APM Server, which allows to ingest log events without relying on filebeat. Log sending is controlled by the <> configuration option and is disabled by default. - -[float] -[[ecs-logging-service-fields]] -=== ECS Logging Service fields - -When the application is using `ecs-logging-java` but does not provide the service fields, then the agent will -automatically provide fallback values from its own configuration: - -- `service.name` value will be populated by <> in agent config. -- `service.version` value will be populated by <> in agent config. From adaff135764125fbfc979af90c4a15b94dc319c9 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Wed, 19 Apr 2023 10:47:56 +0200 Subject: [PATCH 17/18] update & simplify supported technologies --- docs/supported-technologies.asciidoc | 60 ++++++++++++++++------------ 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/docs/supported-technologies.asciidoc b/docs/supported-technologies.asciidoc index 6155823b1b..9a7c4442c8 100644 --- a/docs/supported-technologies.asciidoc +++ b/docs/supported-technologies.asciidoc @@ -550,19 +550,17 @@ NOTE: only classes from the quartz-jobs dependency will be instrumented automati There are multiple log-related features in the agent and their support depend on the logging framework: -- *<>*: +- *<>*: The agent automatically injects `trace.id`, `transaction.id` and `error.id` into the MDC implementation (see below for framework specific MDC implementations used. MDC = Mapped Diagnostic Context, a standard way to enrich log messages with additional information). + For service correlation, the agent sets values for `service.name`, `service.version` and `service.environment`, using ECS log format is required (`ecs-logging-java` or reformatting). -- *<>*: +- *<>*: Automatically captures exceptions for calls like `logger.error("message", exception)`. -- *<>*: +- *<>*: When <> is enabled, logs will be automatically reformatted into ECS-compatible format. -- *<>*: For users of `ecs-logging-java`, the agent sets the service name and version for the `EcsLayout` if not provided explicitly by the -application. - |=== |Framework |Supported versions | Description | Since @@ -572,31 +570,33 @@ application. |Error capturing - 1.10.0 |log4j2 -|Trace correlation - 2.0+ +| Correlation - 2.0+ -ECS Reformatting - 2.6+ +Reformatting - 2.6+ |https://logging.apache.org/log4j/2.x/manual/thread-context.html[`org.apache.logging.log4j.ThreadContext`] is used for correlation. -|Trace correlation - 1.13.0 +|Correlation (traces) - 1.13.0 -Error capturing - 1.10.0 +Correlation (service) - 1.29.0 -ECS Service Name - 1.29.0 +Error capturing - 1.10.0 -ECS Reformatting - 1.22.0 +Reformatting - 1.22.0 |log4j1 -|Trace correlation and error capture - 1.x +|Correlation & error capture - 1.x -ECS Reformatting - 1.2.17 +Reformatting - 1.2.17 |https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html[`org.apache.log4j.MDC`] is used for correlation. -|Trace correlation - 1.13.0 +|Correlation (traces) - 1.13.0 -ECS Reformatting - 1.22.0 +Correlation (service) - 1.38.0 + +Reformatting - 1.22.0 Error capturing - 1.30.0 @@ -604,36 +604,46 @@ Error capturing - 1.30.0 |1.1.0+ |https://www.slf4j.org/api/org/slf4j/MDC.html[`org.slf4j.MDC`] is used for correlation. -|Trace correlation - 1.0.0 +|Correlation (traces) - 1.0.0 + +Correlation (service) - 1.38.0 ECS Reformatting - 1.22.0 |JBoss Logging |3.0.0+ | http://javadox.com/org.jboss.logging/jboss-logging/3.3.0.Final/org/jboss/logging/MDC.html[`org.jboss.logging.MDC`] is used for correlation. -|Trace correlation - 1.23.0 (LogManager 1.30.0) +|Correlation (traces) - 1.23.0 (LogManager 1.30.0) + +Correlation (service) - 1.38.0 + +Reformatting - 1.31.0 |JUL - `java.util.logging` |All supported Java versions -|Trace correlation is only supported when used ECS logging library or with ECS Reformatting as JUL does +|Correlation is only supported with ECS logging (library or reformatting) as JUL does not provide any MDC implementation. -|Trace correlation - 1.35.0 (only when using ECS format as there is no MDC in JUL) +|Correlation (traces) - 1.35.0 (requires ECS logging) -ECS Reformatting - 1.31.0 +Correlation (service) - 1.38.0 (requires ECS logging) + +Reformatting - 1.31.0 Error capturing - 1.31.0 | Tomcat JULI | 7.0.0+ -|Trace correlation is only supported when used ECS logging library or with ECS Reformatting as JUL does +|Trace correlation is only supported with ECS logging (library or reformatting) as JUL does not provide any MDC implementation. Tomcat access logs are not supported. -|Trace correlation - 1.35.0 (only when using ECS format as there is no MDC in JUL) +|Correlation (traces) - 1.35.0 (requires ECS logging) + +Correlation (service) - 1.38.0 (requires ECS logging) -ECS Reformatting - 1.35.0 +Reformatting - 1.35.0 -Error capturing - 1.35.0 (through JUL) +Error capturing - 1.35.0 |=== From 35dae0f0ebda0b5ae34aa6c08fe04e03632a49a8 Mon Sep 17 00:00:00 2001 From: Sylvain Juge Date: Wed, 19 Apr 2023 10:52:36 +0200 Subject: [PATCH 18/18] update changelog --- CHANGELOG.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 99c4b45acf..99d729cee8 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -27,6 +27,7 @@ endif::[] ===== Features * Added tests for Quarkus / RestEasy, adjusted vert.x router transaction name priority - {pull}1765[#1765] * Added support for Spring WebMVC 6.x and Spring Boot 3.x - {pull}3094[#3094] +* Added `service.environment` to logs for service correlation - {pull}3115[#3115] [float] ===== Bug fixes