Skip to content

Commit 60b0608

Browse files
committed
Merge branch 'master' of github.com:elastic/apm-agent-java into user-agent-spec
2 parents e379822 + 9f36a55 commit 60b0608

File tree

31 files changed

+933
-68
lines changed

31 files changed

+933
-68
lines changed

CHANGELOG.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ exclude known static files in all applications might be changed to `/app/static/
3838
===== Features
3939
* Improved capturing of logged exceptions when using Log4j2 - {pull}2139[#2139]
4040
* Update to async-profiler 1.8.7 and set configured `safemode` at load time though a new system property - {pull}2165[#2165]
41+
* Added support to capture `context.message.routing-key` in rabbitmq, spring amqp instrumentations - {pull}1767[#1767]
4142
4243
[float]
4344
===== Performance improvements
@@ -47,6 +48,7 @@ exclude known static files in all applications might be changed to `/app/static/
4748
===== Bug fixes
4849
* Fix k8s metadata discovery for containerd-cri envs - {pull}2126[#2126]
4950
* Fixing/reducing startup delays related to `ensureInstrumented` - {pull}2150[#2150]
51+
* Fix runtime attach when bytebuddy is in application classpath - {pull}2116[#2116]
5052
5153
[float]
5254
===== Refactorings

apm-agent-attach-cli/pom.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@
160160
must be loaded through a dedicated class loader
161161
-->
162162
<exclude>co/elastic/apm/attach/bouncycastle/BouncyCastleVerifier.class</exclude>
163+
<!--
164+
Eliminating exposure to the log4j2 vulnerability related to the SMTP appender -
165+
https://nvd.nist.gov/vuln/detail/CVE-2020-9488#vulnCurrentDescriptionTitle
166+
-->
167+
<exclude>org/apache/logging/log4j/core/appender/SmtpAppender.class</exclude>
163168
</excludes>
164169
</filter>
165170
</filters>
@@ -199,15 +204,10 @@
199204
must be loaded through a dedicated class loader
200205
-->
201206
<exclude>co/elastic/apm/attach/bouncycastle/BouncyCastleVerifier.class</exclude>
202-
</excludes>
203-
</filter>
204-
<filter>
205-
<artifact>org.apache.logging.log4j:log4j-core</artifact>
206-
<!--
207-
Eliminating exposure to the log4j2 vulnerability related to the SMTP appender -
208-
https://nvd.nist.gov/vuln/detail/CVE-2020-9488#vulnCurrentDescriptionTitle
209-
-->
210-
<excludes>
207+
<!--
208+
Eliminating exposure to the log4j2 vulnerability related to the SMTP appender -
209+
https://nvd.nist.gov/vuln/detail/CVE-2020-9488#vulnCurrentDescriptionTitle
210+
-->
211211
<exclude>org/apache/logging/log4j/core/appender/SmtpAppender.class</exclude>
212212
</excludes>
213213
</filter>

apm-agent-attach-cli/src/main/java/co/elastic/apm/attach/GetAgentProperties.java

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@
1818
*/
1919
package co.elastic.apm.attach;
2020

21-
import net.bytebuddy.agent.ByteBuddyAgent;
22-
import net.bytebuddy.agent.VirtualMachine;
21+
import co.elastic.apm.attach.UserRegistry.CommandOutput;
2322

2423
import java.io.IOException;
2524
import java.io.InputStream;
2625
import java.io.StringReader;
2726
import java.util.Arrays;
2827
import java.util.Properties;
2928

30-
import co.elastic.apm.attach.UserRegistry.CommandOutput;
31-
3229
public class GetAgentProperties {
3330

3431
/**
@@ -46,11 +43,11 @@ public class GetAgentProperties {
4643
* @throws Exception
4744
*/
4845
public static void main(String[] args) throws Exception {
49-
getAgentAndSystemPropertiesCurrentUser(args[0]).store(System.out, null);
46+
JvmAttachUtils.getAgentAndSystemProperties(args[0]).store(System.out, null);
5047
}
5148

5249
/**
53-
* Attaches to the VM with the given pid and gets the {@link VirtualMachine#getAgentProperties()} and {@link VirtualMachine#getSystemProperties()}.
50+
* Attaches to the VM with the given pid and gets the agent and system properties.
5451
*
5552
* @param pid The pid of the target VM, If it is current VM, this method will fork a VM for self-attachment.
5653
* @param user The user that runs the target VM. If this is not the current user, this method will fork a VM that runs under this user.
@@ -59,13 +56,13 @@ public static void main(String[] args) throws Exception {
5956
*/
6057
public static Properties getAgentAndSystemProperties(String pid, UserRegistry.User user) throws Exception {
6158
if (user.isCurrentUser() && !JvmInfo.CURRENT_PID.equals(pid)) {
62-
return getAgentAndSystemPropertiesCurrentUser(pid);
59+
return JvmAttachUtils.getAgentAndSystemProperties(pid);
6360
} else {
6461
return getAgentAndSystemPropertiesSwitchUser(pid, user);
6562
}
6663
}
6764

68-
static Properties getAgentAndSystemPropertiesSwitchUser(String pid, UserRegistry.User user) throws IOException, InterruptedException {
65+
static Properties getAgentAndSystemPropertiesSwitchUser(String pid, UserRegistry.User user) throws IOException {
6966
CommandOutput output = user.executeAsUserWithCurrentClassPath(GetAgentProperties.class, Arrays.asList(pid, user.getUsername()));
7067
if (output.getExitCode() == 0) {
7168
Properties properties = new Properties();
@@ -76,27 +73,4 @@ static Properties getAgentAndSystemPropertiesSwitchUser(String pid, UserRegistry
7673
}
7774
}
7875

79-
static Properties getAgentAndSystemPropertiesCurrentUser(String pid) {
80-
ByteBuddyAgent.AttachmentProvider.Accessor accessor = ElasticAttachmentProvider.get().attempt();
81-
if (!accessor.isAvailable()) {
82-
throw new IllegalStateException("No compatible attachment provider is available");
83-
}
84-
85-
try {
86-
Class<?> vm = accessor.getVirtualMachineType();
87-
Object virtualMachineInstance = vm
88-
.getMethod("attach", String.class)
89-
.invoke(null, pid);
90-
try {
91-
Properties agentProperties = (Properties) vm.getMethod("getAgentProperties").invoke(virtualMachineInstance);
92-
Properties systemProperties = (Properties) vm.getMethod("getSystemProperties").invoke(virtualMachineInstance);
93-
systemProperties.putAll(agentProperties);
94-
return systemProperties;
95-
} finally {
96-
vm.getMethod("detach").invoke(virtualMachineInstance);
97-
}
98-
} catch (Exception exception) {
99-
throw new IllegalStateException("Error during attachment using: " + accessor, exception);
100-
}
101-
}
10276
}

apm-agent-attach-cli/src/main/java/co/elastic/apm/attach/JvmInfo.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@
1818
*/
1919
package co.elastic.apm.attach;
2020

21-
import net.bytebuddy.agent.ByteBuddyAgent;
22-
2321
import java.util.Locale;
2422
import java.util.Properties;
2523

2624
class JvmInfo {
27-
public static final String CURRENT_PID = ByteBuddyAgent.ProcessProvider.ForCurrentVm.INSTANCE.resolve();
25+
public static final String CURRENT_PID = JvmAttachUtils.CURRENT_PID;
2826

2927
private final String pid;
3028
private final String userName;

apm-agent-attach/pom.xml

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
<dependency>
3333
<groupId>net.bytebuddy</groupId>
3434
<artifactId>byte-buddy-agent</artifactId>
35-
<version>${version.byte-buddy}</version>
3635
</dependency>
3736
<dependency>
3837
<groupId>net.java.dev.jna</groupId>
@@ -111,6 +110,52 @@
111110
</execution>
112111
</executions>
113112
</plugin>
113+
<plugin>
114+
<artifactId>maven-shade-plugin</artifactId>
115+
<executions>
116+
<execution>
117+
<id>shade-dependencies</id>
118+
<phase>package</phase>
119+
<goals>
120+
<goal>shade</goal>
121+
</goals>
122+
<configuration>
123+
<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
124+
<createSourcesJar>true</createSourcesJar>
125+
<shadeSourcesContent>true</shadeSourcesContent>
126+
<shadedArtifactId>${project.artifactId}</shadedArtifactId>
127+
<filters>
128+
<filter>
129+
<artifact>net.bytebuddy:*</artifact>
130+
<excludes>
131+
<exclude>META-INF/MANIFEST.MF</exclude>
132+
<exclude>META-INF/NOTICE</exclude>
133+
</excludes>
134+
</filter>
135+
</filters>
136+
<artifactSet>
137+
<excludes>
138+
<exclude>net.java.dev.jna:*</exclude>
139+
</excludes>
140+
</artifactSet>
141+
<relocations>
142+
<relocation>
143+
<pattern>net.bytebuddy</pattern>
144+
<shadedPattern>co.elastic.apm.attach.bytebuddy</shadedPattern>
145+
</relocation>
146+
</relocations>
147+
<transformers>
148+
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
149+
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer" />
150+
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
151+
<resource>META-INF/LICENSE</resource>
152+
<file>${apm-agent-parent.base.dir}/LICENSE</file>
153+
</transformer>
154+
</transformers>
155+
</configuration>
156+
</execution>
157+
</executions>
158+
</plugin>
114159
</plugins>
115160
</build>
116161

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.attach;
20+
21+
import net.bytebuddy.agent.ByteBuddyAgent;
22+
23+
import java.util.Properties;
24+
25+
/**
26+
* Allows convenient access to current JVM utilities provided by bytebuddy which is relocated on packaging.
27+
*/
28+
public class JvmAttachUtils {
29+
30+
public static final String CURRENT_PID = ByteBuddyAgent.ProcessProvider.ForCurrentVm.INSTANCE.resolve();
31+
32+
public static Properties getAgentAndSystemProperties(String pid) {
33+
ByteBuddyAgent.AttachmentProvider.Accessor accessor = ElasticAttachmentProvider.get().attempt();
34+
if (!accessor.isAvailable()) {
35+
throw new IllegalStateException("No compatible attachment provider is available");
36+
}
37+
38+
try {
39+
Class<?> vm = accessor.getVirtualMachineType();
40+
Object virtualMachineInstance = vm
41+
.getMethod("attach", String.class)
42+
.invoke(null, pid);
43+
try {
44+
Properties agentProperties = (Properties) vm.getMethod("getAgentProperties").invoke(virtualMachineInstance);
45+
Properties systemProperties = (Properties) vm.getMethod("getSystemProperties").invoke(virtualMachineInstance);
46+
systemProperties.putAll(agentProperties);
47+
return systemProperties;
48+
} finally {
49+
vm.getMethod("detach").invoke(virtualMachineInstance);
50+
}
51+
} catch (Exception e) {
52+
throw new IllegalStateException("Error during attachment using: " + accessor, e);
53+
}
54+
}
55+
56+
}

apm-agent-benchmarks/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@
122122
<dependency>
123123
<groupId>net.bytebuddy</groupId>
124124
<artifactId>byte-buddy-agent</artifactId>
125-
<version>${version.byte-buddy}</version>
126125
</dependency>
127126
<dependency>
128127
<groupId>org.agrona</groupId>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public void init(ElasticApmTracer tracer) {
6363

6464
void logConfiguration(ConfigurationRegistry configurationRegistry, Logger logger) {
6565
final String serviceName = configurationRegistry.getConfig(CoreConfiguration.class).getServiceName();
66-
logger.info("Starting Elastic APM {} as {} on {}", elasticApmVersion, serviceName, getJvmAndOsVersionString());
66+
final String serviceVersion = configurationRegistry.getConfig(CoreConfiguration.class).getServiceVersion();
67+
logger.info("Starting Elastic APM {} as {} ({}) on {}", elasticApmVersion, serviceName, serviceVersion, getJvmAndOsVersionString());
6768
logger.debug("VM Arguments: {}", ManagementFactory.getRuntimeMXBean().getInputArguments());
6869
for (List<ConfigurationOption<?>> options : configurationRegistry.getConfigurationOptionsByCategory().values()) {
6970
for (ConfigurationOption<?> option : options) {

apm-agent-core/src/main/java/co/elastic/apm/agent/impl/context/Message.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public void recycle(StringBuilder object) {
5151
@Nullable
5252
private StringBuilder body;
5353

54+
@Nullable
55+
private String routingKey;
56+
5457
/**
5558
* Represents the message age in milliseconds. Since 0 is a valid value (can occur due to clock skews between
5659
* sender and receiver) - a negative value represents invalid or unavailable age.
@@ -72,6 +75,16 @@ public Message withQueue(String queueName) {
7275
return this;
7376
}
7477

78+
public Message withRoutingKey(String routingKey) {
79+
this.routingKey = routingKey;
80+
return this;
81+
}
82+
83+
@Nullable
84+
public String getRoutingKey() {
85+
return routingKey;
86+
}
87+
7588
/**
7689
* Gets a body StringBuilder to write content to. If this message's body is not initializes, this method will
7790
* initialize from the StringBuilder pool.
@@ -149,6 +162,7 @@ public void resetState() {
149162
stringBuilderPool.recycle(body);
150163
body = null;
151164
}
165+
routingKey = null;
152166
}
153167

154168
public void copyFrom(Message other) {
@@ -159,5 +173,6 @@ public void copyFrom(Message other) {
159173
}
160174
this.headers.copyFrom(other.getHeaders());
161175
this.age = other.getAge();
176+
this.routingKey = other.getRoutingKey();
162177
}
163178
}

apm-agent-core/src/main/java/co/elastic/apm/agent/report/serialize/DslJsonSerializer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,9 @@ private void serializeMessageContext(final Message message) {
902902
jw.writeByte(OBJECT_END);
903903
jw.writeByte(COMMA);
904904
}
905-
905+
if (message.getRoutingKey() != null && !message.getRoutingKey().isEmpty()) {
906+
writeField("routing_key", message.getRoutingKey());
907+
}
906908
writeFieldName("queue");
907909
jw.writeByte(OBJECT_START);
908910
writeLastField("name", message.getQueueName());

0 commit comments

Comments
 (0)