From d28f376d2c5d5a01742dc0f2250ede43a4ee4997 Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Fri, 16 Jul 2021 08:15:53 +0200 Subject: [PATCH 1/7] Use Implementation-Version as default service version if it is available --- .../configuration/CoreConfiguration.java | 1 + .../configuration/ServiceVersionUtil.java | 61 +++++++++++++++++++ docs/configuration.asciidoc | 4 +- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceVersionUtil.java diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java index fc977552b5..a3d0c12f69 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java @@ -142,6 +142,7 @@ public class CoreConfiguration extends ConfigurationOptionProvider { .description("A version string for the currently deployed version of the service. If you don’t version your deployments, " + "the recommended value for this field is the commit identifier of the deployed revision, " + "e.g. the output of git rev-parse HEAD.") + .defaultValue(ServiceVersionUtil.getDefaultServiceVersion()) .build(); private final ConfigurationOption hostname = ConfigurationOption.stringOption() diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceVersionUtil.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceVersionUtil.java new file mode 100644 index 0000000000..3a257b5ea8 --- /dev/null +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceVersionUtil.java @@ -0,0 +1,61 @@ +/* + * 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.configuration; + +import javax.annotation.Nullable; +import java.util.jar.Attributes; +import java.util.jar.JarFile; + +public class ServiceVersionUtil { + + public static String getDefaultServiceVersion() { + return getDefaultServiceVersion(System.getProperty("sun.java.command")); + } + + private static String getDefaultServiceVersion(@Nullable String command) { + if (command == null || !command.contains(".jar") || isContainerCommand(command)) { + return null; + } + + for (String commandPart : command.split(" ")) { + if (commandPart.endsWith(".jar")) { + try (JarFile jarFile = new JarFile(commandPart)) { + String serviceVersion = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION); + if (serviceVersion != null && !serviceVersion.isEmpty()) { + return serviceVersion; + } + } catch (Exception ignored) { + } + + break; + } + } + + return null; + } + + private static boolean isContainerCommand(String command) { + return command.startsWith("org.apache.catalina.startup.Bootstrap") + || command.startsWith("org.eclipse.jetty") + || command.startsWith("com.sun.enterprise.glassfish") + || command.contains("ws-server.jar") + || command.contains("jboss-modules.jar") + || command.contains("weblogic"); + } +} diff --git a/docs/configuration.asciidoc b/docs/configuration.asciidoc index d90d68d19c..c0a5caaead 100644 --- a/docs/configuration.asciidoc +++ b/docs/configuration.asciidoc @@ -549,7 +549,9 @@ A version string for the currently deployed version of the service. If you don [options="header"] |============ | Default | Type | Dynamic -| `` | String | false +| `Implementation-Version` in the `META-INF/MANIFEST.MF` of the main jar file. +If the service version is set explicitly, it overrides the above. +| String | false |============ From 4ac6672cf6c08de3a50bc4d5a27331668cbee13a Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Fri, 16 Jul 2021 09:06:09 +0200 Subject: [PATCH 2/7] Added #1922 to the changelog --- CHANGELOG.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index c6bd54e726..872161917e 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -59,6 +59,7 @@ service map and downstream service in the dependencies table - {pull}1898[#1898] * Basic support for com.sun.net.httpserver.HttpServer - {pull}1854[#1854] * Update to async-profiler 1.8.6 {pull}1907[#1907] * Added support for setting the framework using the public api (#1908) - {pull}1909[#1909] +* When the MANIFEST.MF of the main jar contains the Implementation-Version attribute, it is used as the default service version (except for application servers) - {pull}1922[#1922] [float] ===== Bug fixes From e462aa43e47d4930945296f68bafa3d0612fc96d Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Wed, 24 Nov 2021 18:46:11 +0100 Subject: [PATCH 3/7] Fixed merge --- CHANGELOG.asciidoc | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 39bc8f6045..5451a85932 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -25,7 +25,6 @@ endif::[] [float] ===== Features -* Add support to Jakarta EE for JSF - {pull}2254[#2254] * When the MANIFEST.MF of the main jar contains the Implementation-Version attribute, it is used as the default service version (except for application servers) - {pull}1922[#1922] [float] From de857a2016378857258234b34721cc5ef2898c70 Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Thu, 2 Dec 2021 15:51:17 +0100 Subject: [PATCH 4/7] Updated configuration.asciidoc --- docs/configuration.asciidoc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/configuration.asciidoc b/docs/configuration.asciidoc index b6fd8013bc..1094882f2b 100644 --- a/docs/configuration.asciidoc +++ b/docs/configuration.asciidoc @@ -553,9 +553,7 @@ A version string for the currently deployed version of the service. If you don [options="header"] |============ | Default | Type | Dynamic -| `Implementation-Version` in the `META-INF/MANIFEST.MF` of the main jar file. -If the service version is set explicitly, it overrides the above. -| String | false +| `` | String | false |============ From c25ffbdc1b772c56c4fb7aa74dec77b1f2934222 Mon Sep 17 00:00:00 2001 From: Tobias Stadler Date: Wed, 26 Jan 2022 13:17:31 +0100 Subject: [PATCH 5/7] Merged ServiceNameUtil and ServiceVersionUtil as suggested by @feliybarney --- .../configuration/CoreConfiguration.java | 4 +- ...{ServiceNameUtil.java => ServiceInfo.java} | 97 ++++++++++++------- .../configuration/ServiceVersionUtil.java | 61 ------------ .../apm/agent/impl/ElasticApmTracer.java | 4 +- .../logging/Log4j2ConfigurationFactory.java | 4 +- ...NameUtilTest.java => ServiceInfoTest.java} | 14 ++- .../apm/agent/impl/ElasticApmTracerTest.java | 4 +- 7 files changed, 80 insertions(+), 108 deletions(-) rename apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/{ServiceNameUtil.java => ServiceInfo.java} (58%) delete mode 100644 apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceVersionUtil.java rename apm-agent-core/src/test/java/co/elastic/apm/agent/configuration/{ServiceNameUtilTest.java => ServiceInfoTest.java} (93%) diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java index cbe3646fcf..ce01159f2e 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java @@ -110,7 +110,7 @@ public class CoreConfiguration extends ConfigurationOptionProvider { "NOTE: Service name auto discovery mechanisms require APM Server 7.0+.") .addValidator(RegexValidator.of("^[a-zA-Z0-9 _-]+$", "Your service name \"{0}\" must only contain characters " + "from the ASCII alphabet, numbers, dashes, underscores and spaces")) - .buildWithDefault(ServiceNameUtil.getDefaultServiceName()); + .buildWithDefault(ServiceInfo.DEFAULT.getServiceName()); private final ConfigurationOption serviceNodeName = ConfigurationOption.stringOption() .key(SERVICE_NODE_NAME) @@ -145,7 +145,7 @@ public class CoreConfiguration extends ConfigurationOptionProvider { .description("A version string for the currently deployed version of the service. If you don’t version your deployments, " + "the recommended value for this field is the commit identifier of the deployed revision, " + "e.g. the output of git rev-parse HEAD.") - .defaultValue(ServiceVersionUtil.getDefaultServiceVersion()) + .defaultValue(ServiceInfo.DEFAULT.getServiceVersion()) .build(); private final ConfigurationOption hostname = ConfigurationOption.stringOption() diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceNameUtil.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceInfo.java similarity index 58% rename from apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceNameUtil.java rename to apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceInfo.java index c394c33eba..cf53ef8a92 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceNameUtil.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceInfo.java @@ -18,51 +18,77 @@ */ package co.elastic.apm.agent.configuration; -import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Properties; import java.util.jar.Attributes; import java.util.jar.JarFile; -public class ServiceNameUtil { +public class ServiceInfo { + + public static final ServiceInfo DEFAULT = createDefault(); private static final String JAR_VERSION_SUFFIX = "-(\\d+\\.)+(\\d+)(.*)?$"; - public static String getDefaultServiceName() { - String lambdaFunctionName = System.getenv("AWS_LAMBDA_FUNCTION_NAME"); - if (null != lambdaFunctionName) { - return lambdaFunctionName; - } else { - return getDefaultServiceName(System.getProperty("sun.java.command")); - } + private final String serviceName; + private final String serviceVersion; + + private ServiceInfo() { + this(null, null); } - static String getDefaultServiceName(@Nullable String sunJavaCommand) { - String serviceName = parseSunJavaCommand(sunJavaCommand); - if (serviceName != null) { - serviceName = replaceDisallowedChars(serviceName); - serviceName = serviceName.trim(); - } - if (serviceName == null || serviceName.isEmpty()) { - serviceName = "unknown-java-service"; - } + public ServiceInfo(@Nullable String serviceName) { + this(serviceName, null); + } + + public ServiceInfo(@Nullable String serviceName, @Nullable String serviceVersion) { + this.serviceName = serviceName != null && !serviceName.trim().isEmpty() ? replaceDisallowedServiceNameChars(serviceName).trim() : "unknown-java-service"; + this.serviceVersion = serviceVersion; + } + + public String getServiceName() { return serviceName; } @Nullable - private static String parseSunJavaCommand(@Nullable String command) { + public String getServiceVersion() { + return serviceVersion; + } + + public static String replaceDisallowedServiceNameChars(String serviceName) { + return serviceName.replaceAll("[^a-zA-Z0-9 _-]", "-"); + } + + public static ServiceInfo createDefault() { + return createDefault(System.getProperties()); + } + + static ServiceInfo createDefault(Properties properties) { + String lambdaFunctionName = System.getenv("AWS_LAMBDA_FUNCTION_NAME"); + if (lambdaFunctionName != null) { + return new ServiceInfo(lambdaFunctionName, null); + } else { + ServiceInfo serviceInfo = createFromSunJavaCommand(properties.getProperty("sun.java.command")); + if (serviceInfo != null) { + return serviceInfo; + } + return new ServiceInfo(); + } + } + + @Nullable + private static ServiceInfo createFromSunJavaCommand(@Nullable String command) { if (command == null) { return null; } command = command.trim(); String serviceName = getContainerServiceName(command); if (serviceName != null) { - return serviceName; + return new ServiceInfo(serviceName); } if (command.contains(".jar")) { - serviceName = parseJarCommand(command); + return parseJarCommand(command); } else { - serviceName = parseMainClass(command); + return parseMainClass(command); } - return serviceName; } @Nullable @@ -83,31 +109,28 @@ private static String getContainerServiceName(String command) { return null; } - public static String replaceDisallowedChars(String serviceName) { - return serviceName.replaceAll("[^a-zA-Z0-9 _-]", "-"); - } - - @Nullable - private static String parseJarCommand(String command) { + private static ServiceInfo parseJarCommand(String command) { final String[] commandParts = command.split(" "); - String result = null; + String serviceName = null; + String serviceVersion = null; for (String commandPart : commandParts) { if (commandPart.endsWith(".jar")) { try (JarFile jarFile = new JarFile(commandPart)) { - result = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_TITLE); + Attributes mainAttributes = jarFile.getManifest().getMainAttributes(); + serviceName = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE); + serviceVersion = mainAttributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION); } catch (Exception ignored) { } - if (result == null || result.isEmpty()) { - result = removeVersionFromJar(removePath(removeJarExtension(commandPart))); + if (serviceName == null || serviceName.isEmpty()) { + serviceName = removeVersionFromJar(removePath(removeJarExtension(commandPart))); } break; } } - return result; + return new ServiceInfo(serviceName, serviceVersion); } - @Nonnull private static String removeJarExtension(String commandPart) { return commandPart.substring(0, commandPart.indexOf(".jar")); } @@ -120,7 +143,7 @@ private static String removeVersionFromJar(String jarFileName) { return jarFileName.replaceFirst(JAR_VERSION_SUFFIX, ""); } - private static String parseMainClass(String command) { + private static ServiceInfo parseMainClass(String command) { final String mainClassName; int indexOfSpace = command.indexOf(' '); if (indexOfSpace != -1) { @@ -128,6 +151,6 @@ private static String parseMainClass(String command) { } else { mainClassName = command; } - return mainClassName.substring(mainClassName.lastIndexOf('.') + 1); + return new ServiceInfo(mainClassName.substring(mainClassName.lastIndexOf('.') + 1)); } } diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceVersionUtil.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceVersionUtil.java deleted file mode 100644 index 3a257b5ea8..0000000000 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceVersionUtil.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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.configuration; - -import javax.annotation.Nullable; -import java.util.jar.Attributes; -import java.util.jar.JarFile; - -public class ServiceVersionUtil { - - public static String getDefaultServiceVersion() { - return getDefaultServiceVersion(System.getProperty("sun.java.command")); - } - - private static String getDefaultServiceVersion(@Nullable String command) { - if (command == null || !command.contains(".jar") || isContainerCommand(command)) { - return null; - } - - for (String commandPart : command.split(" ")) { - if (commandPart.endsWith(".jar")) { - try (JarFile jarFile = new JarFile(commandPart)) { - String serviceVersion = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_VERSION); - if (serviceVersion != null && !serviceVersion.isEmpty()) { - return serviceVersion; - } - } catch (Exception ignored) { - } - - break; - } - } - - return null; - } - - private static boolean isContainerCommand(String command) { - return command.startsWith("org.apache.catalina.startup.Bootstrap") - || command.startsWith("org.eclipse.jetty") - || command.startsWith("com.sun.enterprise.glassfish") - || command.contains("ws-server.jar") - || command.contains("jboss-modules.jar") - || command.contains("weblogic"); - } -} diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java index 244c0112b7..4e129f63a3 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/impl/ElasticApmTracer.java @@ -20,7 +20,7 @@ import co.elastic.apm.agent.common.JvmRuntimeInfo; import co.elastic.apm.agent.configuration.CoreConfiguration; -import co.elastic.apm.agent.configuration.ServiceNameUtil; +import co.elastic.apm.agent.configuration.ServiceInfo; import co.elastic.apm.agent.context.ClosableLifecycleListenerAdapter; import co.elastic.apm.agent.context.LifecycleListener; import co.elastic.apm.agent.impl.error.ErrorCapture; @@ -753,7 +753,7 @@ public void overrideServiceNameForClassLoader(@Nullable ClassLoader classLoader, return; } - String sanitizedServiceName = ServiceNameUtil.replaceDisallowedChars(serviceName); + String sanitizedServiceName = ServiceInfo.replaceDisallowedServiceNameChars(serviceName); logger.debug("Using `{}` as the service name for class loader [{}]", sanitizedServiceName, classLoader); if (!serviceNameByClassLoader.containsKey(classLoader)) { serviceNameByClassLoader.putIfAbsent(classLoader, sanitizedServiceName); diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/Log4j2ConfigurationFactory.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/Log4j2ConfigurationFactory.java index 9e66869dd4..bace079945 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/Log4j2ConfigurationFactory.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/Log4j2ConfigurationFactory.java @@ -20,7 +20,7 @@ import co.elastic.apm.agent.bci.ElasticApmAgent; import co.elastic.apm.agent.configuration.CoreConfiguration; -import co.elastic.apm.agent.configuration.ServiceNameUtil; +import co.elastic.apm.agent.configuration.ServiceInfo; import co.elastic.apm.agent.configuration.converter.ByteValue; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.LoggerContext; @@ -176,7 +176,7 @@ private LayoutComponentBuilder createLayout(ConfigurationBuilder Date: Wed, 26 Jan 2022 16:56:02 +0100 Subject: [PATCH 6/7] Polishing --- .../configuration/CoreConfiguration.java | 4 ++-- .../apm/agent/configuration/ServiceInfo.java | 22 ++++++++++--------- .../logging/Log4j2ConfigurationFactory.java | 2 +- .../agent/configuration/ServiceInfoTest.java | 2 +- .../apm/agent/impl/ElasticApmTracerTest.java | 2 +- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java index ce01159f2e..d6679b1491 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/CoreConfiguration.java @@ -110,7 +110,7 @@ public class CoreConfiguration extends ConfigurationOptionProvider { "NOTE: Service name auto discovery mechanisms require APM Server 7.0+.") .addValidator(RegexValidator.of("^[a-zA-Z0-9 _-]+$", "Your service name \"{0}\" must only contain characters " + "from the ASCII alphabet, numbers, dashes, underscores and spaces")) - .buildWithDefault(ServiceInfo.DEFAULT.getServiceName()); + .buildWithDefault(ServiceInfo.autoDetected().getServiceName()); private final ConfigurationOption serviceNodeName = ConfigurationOption.stringOption() .key(SERVICE_NODE_NAME) @@ -145,7 +145,7 @@ public class CoreConfiguration extends ConfigurationOptionProvider { .description("A version string for the currently deployed version of the service. If you don’t version your deployments, " + "the recommended value for this field is the commit identifier of the deployed revision, " + "e.g. the output of git rev-parse HEAD.") - .defaultValue(ServiceInfo.DEFAULT.getServiceVersion()) + .defaultValue(ServiceInfo.autoDetected().getServiceVersion()) .build(); private final ConfigurationOption hostname = ConfigurationOption.stringOption() diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceInfo.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceInfo.java index cf53ef8a92..6d5d371d88 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceInfo.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceInfo.java @@ -25,22 +25,24 @@ public class ServiceInfo { - public static final ServiceInfo DEFAULT = createDefault(); private static final String JAR_VERSION_SUFFIX = "-(\\d+\\.)+(\\d+)(.*)?$"; + private static final String DEFAULT_SERVICE_NAME = "unknown-java-service"; + private static final ServiceInfo AUTO_DETECTED = autoDetect(System.getProperties()); private final String serviceName; + @Nullable private final String serviceVersion; - private ServiceInfo() { - this(null, null); - } - public ServiceInfo(@Nullable String serviceName) { this(serviceName, null); } public ServiceInfo(@Nullable String serviceName, @Nullable String serviceVersion) { - this.serviceName = serviceName != null && !serviceName.trim().isEmpty() ? replaceDisallowedServiceNameChars(serviceName).trim() : "unknown-java-service"; + if (serviceName == null || serviceName.trim().isEmpty()) { + this.serviceName = DEFAULT_SERVICE_NAME; + } else { + this.serviceName = replaceDisallowedServiceNameChars(serviceName).trim(); + } this.serviceVersion = serviceVersion; } @@ -57,11 +59,11 @@ public static String replaceDisallowedServiceNameChars(String serviceName) { return serviceName.replaceAll("[^a-zA-Z0-9 _-]", "-"); } - public static ServiceInfo createDefault() { - return createDefault(System.getProperties()); + public static ServiceInfo autoDetected() { + return AUTO_DETECTED; } - static ServiceInfo createDefault(Properties properties) { + static ServiceInfo autoDetect(Properties properties) { String lambdaFunctionName = System.getenv("AWS_LAMBDA_FUNCTION_NAME"); if (lambdaFunctionName != null) { return new ServiceInfo(lambdaFunctionName, null); @@ -70,7 +72,7 @@ static ServiceInfo createDefault(Properties properties) { if (serviceInfo != null) { return serviceInfo; } - return new ServiceInfo(); + return new ServiceInfo(null); } } diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/Log4j2ConfigurationFactory.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/Log4j2ConfigurationFactory.java index bace079945..8f5d91ca5c 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/Log4j2ConfigurationFactory.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/logging/Log4j2ConfigurationFactory.java @@ -176,7 +176,7 @@ private LayoutComponentBuilder createLayout(ConfigurationBuilder Date: Wed, 26 Jan 2022 17:49:16 +0100 Subject: [PATCH 7/7] Fixed test --- .../java/co/elastic/apm/agent/configuration/ServiceInfo.java | 2 +- .../java/co/elastic/apm/agent/impl/ElasticApmTracerTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceInfo.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceInfo.java index 6d5d371d88..fafb9f41ad 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceInfo.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/ServiceInfo.java @@ -63,7 +63,7 @@ public static ServiceInfo autoDetected() { return AUTO_DETECTED; } - static ServiceInfo autoDetect(Properties properties) { + public static ServiceInfo autoDetect(Properties properties) { String lambdaFunctionName = System.getenv("AWS_LAMBDA_FUNCTION_NAME"); if (lambdaFunctionName != null) { return new ServiceInfo(lambdaFunctionName, null); diff --git a/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/ElasticApmTracerTest.java b/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/ElasticApmTracerTest.java index 3387ab0b95..5689160390 100644 --- a/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/ElasticApmTracerTest.java +++ b/apm-agent-core/src/test/java/co/elastic/apm/agent/impl/ElasticApmTracerTest.java @@ -471,7 +471,7 @@ void testNotOverrideServiceNameWhenDefaultServiceNameConfigured() { startTestRootTransaction().end(); CoreConfiguration coreConfig = localConfig.getConfig(CoreConfiguration.class); - assertThat(ServiceInfo.autoDetected().getServiceName()).isEqualTo(coreConfig.getServiceName()); + assertThat(ServiceInfo.autoDetect(System.getProperties()).getServiceName()).isEqualTo(coreConfig.getServiceName()); assertThat(reporter.getFirstTransaction().getTraceContext().getServiceName()).isNull(); if (command != null) { System.setProperty("sun.java.command", command);