diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 80b5e6937d..3057b7ffa1 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -29,6 +29,8 @@ endif::[] * Replaced `authorization` in the default value of `sanitize_field_names` with `*auth*` - {pull}2326[#2326] * Unsampled transactions are dropped and not sent to the APM-Server if the APM-Server version is 8.0+ - {pull}2329[#2329] * Adding agent logging capabilities to our SDK, making it available for external plugins - {pull}2390[#2390] +* When the `MANIFEST.MF` of the main jar contains the `Implementation-Title` attribute, it is used as the default service name - {pull}1921[#1921] + Note: this may change your service names if you relied on the auto-discovery that uses the name of the jar file. If that jar file also contains an `Implementation-Title` attribute in the `MANIFEST.MF` file, the latter will take precedence. [float] ===== Bug fixes 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/ServiceNameUtil.java index 2d592bbac6..c394c33eba 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/ServiceNameUtil.java @@ -20,6 +20,8 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.jar.Attributes; +import java.util.jar.JarFile; public class ServiceNameUtil { private static final String JAR_VERSION_SUFFIX = "-(\\d+\\.)+(\\d+)(.*)?$"; @@ -91,7 +93,14 @@ private static String parseJarCommand(String command) { String result = null; for (String commandPart : commandParts) { if (commandPart.endsWith(".jar")) { - result = removeVersionFromJar(removePath(removeJarExtension(commandPart))); + try (JarFile jarFile = new JarFile(commandPart)) { + result = jarFile.getManifest().getMainAttributes().getValue(Attributes.Name.IMPLEMENTATION_TITLE); + } catch (Exception ignored) { + } + + if (result == null || result.isEmpty()) { + result = removeVersionFromJar(removePath(removeJarExtension(commandPart))); + } break; } } diff --git a/apm-agent/src/test/resources/configuration.asciidoc.ftl b/apm-agent/src/test/resources/configuration.asciidoc.ftl index 2a1428716a..6de0111723 100644 --- a/apm-agent/src/test/resources/configuration.asciidoc.ftl +++ b/apm-agent/src/test/resources/configuration.asciidoc.ftl @@ -83,6 +83,7 @@ ELASTIC_APM_SERVER_URL=http://localhost:8200 For Spring-based application, uses the `spring.application.name` property, if set. For Servlet-based applications, uses the `display-name` of the `web.xml`, if available. Falls back to the servlet context path the application is mapped to (unless mapped to the root context). +Falls back to the `Implementation-Title` in the `MANIFEST.MF` file. Falls back to the name of the main class or jar file. If the service name is set explicitly, it overrides all of the above. diff --git a/docs/configuration.asciidoc b/docs/configuration.asciidoc index 6db66d6ef9..b21e9232b0 100644 --- a/docs/configuration.asciidoc +++ b/docs/configuration.asciidoc @@ -497,6 +497,7 @@ NOTE: Service name auto discovery mechanisms require APM Server 7.0+. | For Spring-based application, uses the `spring.application.name` property, if set. For Servlet-based applications, uses the `display-name` of the `web.xml`, if available. Falls back to the servlet context path the application is mapped to (unless mapped to the root context). +Falls back to the `Implementation-Title` in the `MANIFEST.MF` file. Falls back to the name of the main class or jar file. If the service name is set explicitly, it overrides all of the above. | String | false @@ -2891,6 +2892,7 @@ The default unit for this option is `ms`. # Default value: For Spring-based application, uses the `spring.application.name` property, if set. # For Servlet-based applications, uses the `display-name` of the `web.xml`, if available. # Falls back to the servlet context path the application is mapped to (unless mapped to the root context). +# Falls back to the `Implementation-Title` in the `MANIFEST.MF` file. # Falls back to the name of the main class or jar file. # If the service name is set explicitly, it overrides all of the above. #