# Java Agent plugin with public api

**URL:** <https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227>\
**Category:** APM\
**Tags:** java\
**Created:** [February 27, 2020, 11:54am UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227 "2020-02-27T11:54:36Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hyhong](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@hyhong](https://discuss.elastic.co/u/hyhong)\
**Post date:** [February 27, 2020, 11:54am UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227/1 "2020-02-27T11:54:37Z")

</div>

I want to customer a java agent plugin,can use public api in java agent plugin?

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [February 27, 2020, 4:23pm UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227/2 "2020-02-27T16:23:12Z")

</div>

Hi, yes you can, the documentation on agent API [is here](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html)

---

<div class="post-metadata">

**Author:** ![hyhong](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@hyhong](https://discuss.elastic.co/u/hyhong)\
**Post date:** [February 28, 2020, 5:05am UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227/3 "2020-02-28T05:05:24Z")

</div>

I hava try,but it doesn't work.Here is my code:  
package co.elastic.apm.agent.ice.client;

import Ice.Communicator;  
import Ice.ImplicitContext;  
import Ice.ObjectPrx;  
import co.elastic.apm.agent.bci.ElasticApmInstrumentation;  
import co.elastic.apm.api.\*;  
import net.bytebuddy.asm.Advice;  
import net.bytebuddy.description.method.MethodDescription;  
import net.bytebuddy.description.type.TypeDescription;  
import net.bytebuddy.matcher.ElementMatcher;

import java.util.Collection;

import static net.bytebuddy.matcher.ElementMatchers.\*;

public class IceClientInstrumentation extends ElasticApmInstrumentation {

```
private static class IceClientAdvice {

    @Advice.OnMethodEnter(suppress = Throwable.class)
    private static void onBeforeExecute(@Advice.Argument(0) ObjectPrx objectPrx) {

            Communicator ic=objectPrx.ice_getCommunicator();
            final ImplicitContext implicitContext=ic.getImplicitContext();
            ic.getImplicitContext().put("test", "123");
            Span span = ElasticApm.currentSpan()
                .startSpan("external", "ice", null)
                .setName("testice");
            try (final Scope scope = span.activate()) {
                span.injectTraceHeaders(new HeaderInjector() {
                    @Override
                    public void addHeader(String headerName, String headerValue) {
                        implicitContext.put(headerName, headerValue);
                    }
                });
            } catch (Exception e) {
                span.captureException(e);
                throw e;
            } finally {
                span.end();
            }
    }
}

@Override
public Class<?> getAdviceClass() {
    return IceClientAdvice.class;
}

@Override
public ElementMatcher<? super TypeDescription> getTypeMatcher() {
    return any();
}

@Override
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
    return named("checkedCast").and(takesArgument(0, named("Ice.ObjectPrx")));
}

@Override
public Collection<String> getInstrumentationGroupNames() {
    return null;
}

```

}

---

<div class="post-metadata">

**Author:** ![hyhong](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@hyhong](https://discuss.elastic.co/u/hyhong)\
**Post date:** [February 28, 2020, 5:23am UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227/4 "2020-02-28T05:23:19Z")

</div>

This is my server side:  
package co.elastic.apm.agent.ice.client;

import Ice.Current;  
import co.elastic.apm.agent.bci.ElasticApmInstrumentation;  
import co.elastic.apm.api.\*;  
import net.bytebuddy.asm.Advice;  
import net.bytebuddy.description.method.MethodDescription;  
import net.bytebuddy.description.type.TypeDescription;  
import net.bytebuddy.matcher.ElementMatcher;

import java.util.Collection;  
import java.util.Map;

import static net.bytebuddy.matcher.ElementMatchers.\*;

public class IceServerInstrumentation extends ElasticApmInstrumentation{

```
private static class IceServerAdvice {

    @Advice.OnMethodEnter(suppress = Throwable.class)
    private static void onBeforeExecute(@Advice.AllArguments Current current) {

       final Map<String, String> map=current.ctx;
        Transaction transaction = ElasticApm.startTransactionWithRemoteParent(new HeaderExtractor() {
            @Override
            public String getFirstHeader(String headerName) {
                return map.get(headerName);
            }
        });
        try (final Scope scope = transaction.activate()) {
            String adapterName = current.adapter.getName();
            String operationName = current.operation;
            transaction.setName(adapterName + "#" + operationName);
            transaction.setType(Transaction.TYPE_REQUEST);
        } catch (Exception e) {
            transaction.captureException(e);
            throw e;
        } finally {
            transaction.end();
        }
    }
}

@Override
public Class<?> getAdviceClass() {
    return IceServerInstrumentation.IceServerAdvice.class;
}

@Override
public ElementMatcher<? super TypeDescription> getTypeMatcher() {
    return any();
}

@Override
public ElementMatcher<? super MethodDescription> getMethodMatcher() {
    return any();
}

@Override
public Collection<String> getInstrumentationGroupNames() {
    return null;
}

```

}

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [March 2, 2020, 2:17pm UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227/5 "2020-03-02T14:17:31Z")

</div>

You cannot use the Public API (`co.elastic.apm.api.*`) when writing an internal plugin. Use the internal API for that (`co.elastic.apm.agent.impl.ElasticApmTracer`).

---

<div class="post-metadata">

**Author:** ![hyhong](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@hyhong](https://discuss.elastic.co/u/hyhong)\
**Post date:** [March 2, 2020, 2:31pm UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227/6 "2020-03-02T14:31:10Z")

</div>

Ok,thanks.I have a question,byte buddy can interctor final class?

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [March 2, 2020, 4:22pm UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227/7 "2020-03-02T16:22:51Z")

</div>

yes.

---

<div class="post-metadata">

**Author:** ![hyhong](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@hyhong](https://discuss.elastic.co/u/hyhong)\
**Post date:** [March 3, 2020, 1:04am UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227/8 "2020-03-03T01:04:12Z")

</div>

Can you provider a example?

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [March 3, 2020, 9:05am UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227/9 "2020-03-03T09:05:31Z")

</div>

Out of curiosity, are you trying to create a custom plugin for [https://doc.zeroc.com/display/Ice35/Java+Mapping](https://doc.zeroc.com/display/Ice35/Java+Mapping) ?

---

<div class="post-metadata">

**Author:** ![hyhong](https://avatars.discourse-cdn.com/v4/letter/h/3bc359/32.png) [@hyhong](https://discuss.elastic.co/u/hyhong)\
**Post date:** [March 3, 2020, 9:20am UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227/10 "2020-03-03T09:20:37Z")

</div>

yes.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 24, 2020, 5:20am UTC](https://discuss.elastic.co/t/java-agent-plugin-with-public-api/221227/11 "2020-03-24T05:20:38Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
