Skip to content

Commit 721d892

Browse files
authored
Fix reading agent version (#2289)
* fix reading agent version * add few tests on agent packaging * update changelog
1 parent 3752548 commit 721d892

File tree

6 files changed

+126
-20
lines changed

6 files changed

+126
-20
lines changed

CHANGELOG.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ endif::[]
2727
===== Bug fixes
2828
* Fixing missing Micrometer metrics in Spring boot due to premature initialization - {pull}2255[#2255]
2929
* Fixing hostname trimming of FQDN too aggressive - {pull}2286[#2286]
30+
* Fixing agent `unknown` version - {pull}2289[#2289]
31+
3032
3133
[[release-notes-1.x]]
3234
=== Java Agent version 1.x

apm-agent-core/src/main/java/co/elastic/apm/agent/configuration/StartupInfo.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,16 @@ public void init(ElasticApmTracer tracer) {
6464
void logConfiguration(ConfigurationRegistry configurationRegistry, Logger logger) {
6565
final String serviceName = configurationRegistry.getConfig(CoreConfiguration.class).getServiceName();
6666
final String serviceVersion = configurationRegistry.getConfig(CoreConfiguration.class).getServiceVersion();
67-
logger.info("Starting Elastic APM {} as {} ({}) on {}", elasticApmVersion, serviceName, serviceVersion, getJvmAndOsVersionString());
67+
68+
StringBuilder serviceNameAndVersion = new StringBuilder(serviceName);
69+
if (serviceVersion != null) {
70+
serviceNameAndVersion.append(" (").append(serviceVersion).append(")");
71+
}
72+
73+
logger.info("Starting Elastic APM {} as {} on {}",
74+
elasticApmVersion,
75+
serviceNameAndVersion,
76+
getJvmAndOsVersionString());
6877
logger.debug("VM Arguments: {}", ManagementFactory.getRuntimeMXBean().getInputArguments());
6978
for (List<ConfigurationOption<?>> options : configurationRegistry.getConfigurationOptionsByCategory().values()) {
7079
for (ConfigurationOption<?> option : options) {

apm-agent-core/src/main/java/co/elastic/apm/agent/util/VersionUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ public final class VersionUtils {
3838
private static final String AGENT_VERSION;
3939

4040
static {
41-
String version = getVersion(VersionUtils.class, "co.elastic.apm", "elastic-apm-agent");
41+
File agentJar = ElasticApmAgent.getAgentJarFile();
42+
String version = getManifestEntry(agentJar, "Implementation-Version");
4243
if (version != null && version.endsWith("SNAPSHOT")) {
43-
String gitRev = getManifestEntry(ElasticApmAgent.getAgentJarFile(), "SCM-Revision");
44+
String gitRev = getManifestEntry(agentJar, "SCM-Revision");
4445
if (gitRev != null) {
4546
version = version + "." + gitRev;
4647
}

apm-agent/pom.xml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
<properties>
2222
<apm-agent-parent.base.dir>${project.basedir}/..</apm-agent-parent.base.dir>
2323

24-
<!-- default value that will be overloaded through commit-id plugin -->
25-
<git.commit.id.abbrev>UNKNOWN</git.commit.id.abbrev>
2624
</properties>
2725

2826
<dependencies>
@@ -445,7 +443,6 @@
445443
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
446444
<manifestEntries>
447445
<Automatic-Module-Name>${project.groupId}.agent</Automatic-Module-Name>
448-
<SCM-Revision>${git.commit.id.abbrev}</SCM-Revision>
449446
</manifestEntries>
450447
</transformer>
451448
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer" />
@@ -477,19 +474,6 @@
477474
</execution>
478475
</executions>
479476
</plugin>
480-
<plugin>
481-
<groupId>pl.project13.maven</groupId>
482-
<artifactId>git-commit-id-plugin</artifactId>
483-
<version>4.0.2</version>
484-
<executions>
485-
<execution>
486-
<id>get-the-git-infos</id>
487-
<goals>
488-
<goal>revision</goal>
489-
</goals>
490-
</execution>
491-
</executions>
492-
</plugin>
493477
</plugins>
494478
</build>
495479

elastic-apm-agent/pom.xml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
<properties>
1515
<apm-agent-parent.base.dir>${project.basedir}/..</apm-agent-parent.base.dir>
16+
17+
<!-- default value that will be overloaded through commit-id plugin -->
18+
<git.commit.id.abbrev>UNKNOWN</git.commit.id.abbrev>
1619
</properties>
1720

1821
<dependencies>
@@ -102,7 +105,6 @@
102105
</executions>
103106
</plugin>
104107
<plugin>
105-
<groupId>org.apache.maven.plugins</groupId>
106108
<artifactId>maven-antrun-plugin</artifactId>
107109
<version>3.0.0</version>
108110
<executions>
@@ -167,6 +169,19 @@
167169
</execution>
168170
</executions>
169171
</plugin>
172+
<plugin>
173+
<groupId>pl.project13.maven</groupId>
174+
<artifactId>git-commit-id-plugin</artifactId>
175+
<version>4.0.2</version>
176+
<executions>
177+
<execution>
178+
<id>get-the-git-infos</id>
179+
<goals>
180+
<goal>revision</goal>
181+
</goals>
182+
</execution>
183+
</executions>
184+
</plugin>
170185
<plugin>
171186
<artifactId>maven-shade-plugin</artifactId>
172187
<executions>
@@ -190,6 +205,7 @@
190205
<manifestEntries>
191206
<Premain-Class>co.elastic.apm.agent.premain.AgentMain</Premain-Class>
192207
<Agent-Class>co.elastic.apm.agent.premain.AgentMain</Agent-Class>
208+
<SCM-Revision>${git.commit.id.abbrev}</SCM-Revision>
193209
<Can-Redefine-Classes>true</Can-Redefine-Classes>
194210
<Can-Retransform-Classes>true</Can-Retransform-Classes>
195211
<Can-Set-Native-Method-Prefix>true</Can-Set-Native-Method-Prefix>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package co.elastic.apm.agent.premain;
20+
21+
import org.junit.jupiter.api.BeforeAll;
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.io.BufferedReader;
25+
import java.io.IOException;
26+
import java.net.URISyntaxException;
27+
import java.net.URL;
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.nio.file.Paths;
31+
import java.util.Properties;
32+
import java.util.jar.Attributes;
33+
import java.util.jar.JarFile;
34+
import java.util.jar.Manifest;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
public class AgentPackagingIT {
39+
40+
private static Path agentJar;
41+
private static String agentVersion;
42+
43+
@BeforeAll
44+
static void before() throws URISyntaxException, IOException {
45+
// find packaged jar location
46+
47+
String classResource = AgentPackagingIT.class.getCanonicalName().replace(".", "/") + ".class";
48+
URL url = AgentPackagingIT.class.getClassLoader().getResource(classResource);
49+
assertThat(url).isNotNull();
50+
51+
Path path = Paths.get(url.toURI());
52+
Path targetFolder = null;
53+
while (path != null && targetFolder == null) {
54+
if (path.endsWith("target")) {
55+
targetFolder = path;
56+
}
57+
path = path.getParent();
58+
}
59+
assertThat(targetFolder).isNotNull();
60+
61+
Path mavenPropertiesFile = targetFolder.resolve("maven-archiver").resolve("pom.properties");
62+
assertThat(mavenPropertiesFile).isRegularFile();
63+
64+
Properties mavenProperties = new Properties();
65+
try (BufferedReader reader = Files.newBufferedReader(mavenPropertiesFile)) {
66+
mavenProperties.load(reader);
67+
}
68+
69+
String artifactId = mavenProperties.getProperty("artifactId");
70+
assertThat(artifactId).isEqualTo("elastic-apm-agent");
71+
72+
agentVersion = mavenProperties.getProperty("version");
73+
assertThat(agentVersion).isNotNull();
74+
75+
agentJar = targetFolder.resolve(String.format("%s-%s.jar", artifactId, agentVersion));
76+
assertThat(agentJar).isRegularFile();
77+
}
78+
79+
@Test
80+
void agentManifest() throws IOException {
81+
82+
JarFile jarFile = new JarFile(agentJar.toFile());
83+
84+
Manifest manifest = jarFile.getManifest();
85+
86+
Attributes attributes = manifest.getMainAttributes();
87+
assertThat(attributes.getValue("Implementation-Version")).isEqualTo(agentVersion);
88+
String agentClass = attributes.getValue("Agent-Class");
89+
assertThat(agentClass).isNotEmpty();
90+
assertThat(attributes.getValue("Premain-Class")).isEqualTo(agentClass);
91+
assertThat(attributes.getValue("SCM-Revision")).isNotEmpty();
92+
}
93+
94+
}

0 commit comments

Comments
 (0)