Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ exclude known static files in all applications might be changed to `/app/static/
===== Bug fixes
* Fix k8s metadata discovery for containerd-cri envs - {pull}2126[#2126]
* Fixing/reducing startup delays related to `ensureInstrumented` - {pull}2150[#2150]
* Fix runtime attach when bytebuddy is in application classpath - {pull}2116[#2116]

[float]
===== Refactorings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
*/
package co.elastic.apm.attach;

import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.agent.VirtualMachine;
import co.elastic.apm.attach.UserRegistry.CommandOutput;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Properties;

import co.elastic.apm.attach.UserRegistry.CommandOutput;

public class GetAgentProperties {

/**
Expand All @@ -46,11 +43,11 @@ public class GetAgentProperties {
* @throws Exception
*/
public static void main(String[] args) throws Exception {
getAgentAndSystemPropertiesCurrentUser(args[0]).store(System.out, null);
JvmAttachUtils.getAgentAndSystemProperties(args[0]).store(System.out, null);
}

/**
* Attaches to the VM with the given pid and gets the {@link VirtualMachine#getAgentProperties()} and {@link VirtualMachine#getSystemProperties()}.
* Attaches to the VM with the given pid and gets the agent and system properties.
*
* @param pid The pid of the target VM, If it is current VM, this method will fork a VM for self-attachment.
* @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.
Expand All @@ -59,13 +56,13 @@ public static void main(String[] args) throws Exception {
*/
public static Properties getAgentAndSystemProperties(String pid, UserRegistry.User user) throws Exception {
if (user.isCurrentUser() && !JvmInfo.CURRENT_PID.equals(pid)) {
return getAgentAndSystemPropertiesCurrentUser(pid);
return JvmAttachUtils.getAgentAndSystemProperties(pid);
} else {
return getAgentAndSystemPropertiesSwitchUser(pid, user);
}
}

static Properties getAgentAndSystemPropertiesSwitchUser(String pid, UserRegistry.User user) throws IOException, InterruptedException {
static Properties getAgentAndSystemPropertiesSwitchUser(String pid, UserRegistry.User user) throws IOException {
CommandOutput output = user.executeAsUserWithCurrentClassPath(GetAgentProperties.class, Arrays.asList(pid, user.getUsername()));
if (output.getExitCode() == 0) {
Properties properties = new Properties();
Expand All @@ -76,27 +73,4 @@ static Properties getAgentAndSystemPropertiesSwitchUser(String pid, UserRegistry
}
}

static Properties getAgentAndSystemPropertiesCurrentUser(String pid) {
ByteBuddyAgent.AttachmentProvider.Accessor accessor = ElasticAttachmentProvider.get().attempt();
if (!accessor.isAvailable()) {
throw new IllegalStateException("No compatible attachment provider is available");
}

try {
Class<?> vm = accessor.getVirtualMachineType();
Object virtualMachineInstance = vm
.getMethod("attach", String.class)
.invoke(null, pid);
try {
Properties agentProperties = (Properties) vm.getMethod("getAgentProperties").invoke(virtualMachineInstance);
Properties systemProperties = (Properties) vm.getMethod("getSystemProperties").invoke(virtualMachineInstance);
systemProperties.putAll(agentProperties);
return systemProperties;
} finally {
vm.getMethod("detach").invoke(virtualMachineInstance);
}
} catch (Exception exception) {
throw new IllegalStateException("Error during attachment using: " + accessor, exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
*/
package co.elastic.apm.attach;

import net.bytebuddy.agent.ByteBuddyAgent;

import java.util.Locale;
import java.util.Properties;

class JvmInfo {
public static final String CURRENT_PID = ByteBuddyAgent.ProcessProvider.ForCurrentVm.INSTANCE.resolve();
public static final String CURRENT_PID = JvmAttachUtils.CURRENT_PID;

private final String pid;
private final String userName;
Expand Down
47 changes: 46 additions & 1 deletion apm-agent-attach/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>${version.byte-buddy}</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
Expand Down Expand Up @@ -111,6 +110,52 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<id>shade-dependencies</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<keepDependenciesWithProvidedScope>true</keepDependenciesWithProvidedScope>
<createSourcesJar>true</createSourcesJar>
<shadeSourcesContent>true</shadeSourcesContent>
<shadedArtifactId>${project.artifactId}</shadedArtifactId>
<filters>
<filter>
<artifact>net.bytebuddy:*</artifact>
<excludes>
<exclude>META-INF/MANIFEST.MF</exclude>
<exclude>META-INF/NOTICE</exclude>
</excludes>
</filter>
</filters>
<artifactSet>
<excludes>
<exclude>net.java.dev.jna:*</exclude>
</excludes>
</artifactSet>
<relocations>
<relocation>
<pattern>net.bytebuddy</pattern>
<shadedPattern>co.elastic.apm.attach.bytebuddy</shadedPattern>
</relocation>
</relocations>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheNoticeResourceTransformer" />
<transformer implementation="org.apache.maven.plugins.shade.resource.IncludeResourceTransformer">
<resource>META-INF/LICENSE</resource>
<file>${apm-agent-parent.base.dir}/LICENSE</file>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -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.attach;

import net.bytebuddy.agent.ByteBuddyAgent;

import java.util.Properties;

/**
* Allows convenient access to current JVM utilities provided by bytebuddy which is relocated on packaging.
*/
public class JvmAttachUtils {

public static final String CURRENT_PID = ByteBuddyAgent.ProcessProvider.ForCurrentVm.INSTANCE.resolve();

public static Properties getAgentAndSystemProperties(String pid) {
ByteBuddyAgent.AttachmentProvider.Accessor accessor = ElasticAttachmentProvider.get().attempt();
if (!accessor.isAvailable()) {
throw new IllegalStateException("No compatible attachment provider is available");
}

try {
Class<?> vm = accessor.getVirtualMachineType();
Object virtualMachineInstance = vm
.getMethod("attach", String.class)
.invoke(null, pid);
try {
Properties agentProperties = (Properties) vm.getMethod("getAgentProperties").invoke(virtualMachineInstance);
Properties systemProperties = (Properties) vm.getMethod("getSystemProperties").invoke(virtualMachineInstance);
systemProperties.putAll(agentProperties);
return systemProperties;
} finally {
vm.getMethod("detach").invoke(virtualMachineInstance);
}
} catch (Exception e) {
throw new IllegalStateException("Error during attachment using: " + accessor, e);
}
}

}
1 change: 0 additions & 1 deletion apm-agent-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>${version.byte-buddy}</version>
</dependency>
<dependency>
<groupId>org.agrona</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* 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.bci;

import java.util.Collection;
Expand Down
1 change: 0 additions & 1 deletion apm-agent-plugin-sdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-dep</artifactId>
<version>${version.byte-buddy}</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* 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.concurrent;

import co.elastic.apm.agent.bci.PluginClassLoaderRootPackageCustomizer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* 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.log4j1;

import co.elastic.apm.agent.bci.PluginClassLoaderRootPackageCustomizer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* 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.log4j2;

import co.elastic.apm.agent.bci.PluginClassLoaderRootPackageCustomizer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* 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.logback;

import co.elastic.apm.agent.bci.PluginClassLoaderRootPackageCustomizer;
Expand Down
1 change: 1 addition & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<module>soap-test</module>
<module>cdi-app</module>
<module>external-plugin-test</module>
<module>runtime-attach</module>
</modules>

<properties>
Expand Down
26 changes: 26 additions & 0 deletions integration-tests/runtime-attach/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>integration-tests</artifactId>
<groupId>co.elastic.apm</groupId>
<version>1.26.1-SNAPSHOT</version>
</parent>

<artifactId>runtime-attach</artifactId>
<packaging>pom</packaging>

<name>${project.groupId}:${project.artifactId}</name>

<properties>
<apm-agent-parent.base.dir>${project.basedir}/../..</apm-agent-parent.base.dir>
</properties>

<modules>
<module>runtime-attach-app</module>
<module>runtime-attach-test</module>
</modules>

</project>
Loading