# How can I instrument APM agent with a core JAVA service?

**URL:** <https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459>\
**Category:** APM\
**Tags:** java\
**Created:** [July 19, 2019, 11:23pm UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459 "2019-07-19T23:23:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parth](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Parth](https://discuss.elastic.co/u/Parth)\
**Post date:** [July 19, 2019, 11:23pm UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459/1 "2019-07-19T23:23:20Z")

</div>

I have successfully instrumented JAVA APM agent with my web application that runs on Tomcat but I am not sure on how to instrument it with my core java service (that does not run on any server)?

Any suggestions would help!

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 20, 2019, 4:17am UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459/2 "2019-07-20T04:17:10Z")

</div>

Take a look at the public API...

[https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html](https://www.elastic.co/guide/en/apm/agent/java/current/public-api.html)

This tells you how to create your own transactions and spans.

Then depending on the communication protocols you'll probably have to figure out how to pass the parents transaction IDs if you through some form of headers or the message itself or use opentracing bridge if you want to see distributed tracing.

Here is a sample the TCP example to demonstrate.

> **[bvader/pipelineapmexample](https://github.com/bvader/pipelineapmexample)**
>
> Example of pipeline that uses APM pefrormance metrics - bvader/pipelineapmexample

---

<div class="post-metadata">

**Author:** ![Parth](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Parth](https://discuss.elastic.co/u/Parth)\
**Post date:** [July 22, 2019, 6:08pm UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459/3 "2019-07-22T18:08:16Z")

</div>

So does it mean that if we use elastic-apm-agent.jar then we can only get hold of default transactions?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 22, 2019, 7:03pm UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459/4 "2019-07-22T19:03:57Z")

</div>

If you just use the agent without the Public API you will automatically get transactions for [the supported frameworks.](https://www.elastic.co/guide/en/apm/agent/java/current/supported-technologies-details.html)

If your app does not use one of the supported frameworks you would use the agent plus the Public API.

That example I gave uses the agent + the Public API since it was just raw tcp i.e. not a supported framework.

---

<div class="post-metadata">

**Author:** ![Parth](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Parth](https://discuss.elastic.co/u/Parth)\
**Post date:** [July 22, 2019, 10:30pm UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459/5 "2019-07-22T22:30:34Z")

</div>

Thanks Stephen. It does make sense now. But what do you think would be a preferred way of using APM agent then, would it be using Public API or programmatic API setup to self attach?

Especially when you have to provide a generalized solution across all different Java micro-services [micro-services with different Java framework implementation] running on your platform?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 23, 2019, 2:01pm UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459/6 "2019-07-23T14:01:59Z")

</div>

Well _It Depends_ ...

It depends on many things how your code is organized, how it is built your CI CD pipelines, the different Java frameworks and messaging systems involved. I would certainly take advantage of any of the automatic instrumentation for the frameworks that are supported. I don't think there's a simple answer to this and it's going to take some discussion, thought and it'll be a bit of a journey for sure.

I think I would approach it in an iterative manner take a look at some candidate and/or your most important services, or the set of services that are most common with the largest footprint (depends on your goals) and instrument them first and prepare that you will evolve / iterate how you do it.

That said the very first one I would do would be a small Canary project meaning a very small service with low-impact for the first one I'd roll into production to make sure you understand impact and how to configure in production.

If there are a bunch of different development groups it'll be difficult to push that on all of them. What I have found works in the past is get something working well and then other developers will want to take advantage of that.

If I sound like an engineering manager its because I was for many years .... good luck on your journey.

Keep coming back and asking questions and let us know how it's going.

---

<div class="post-metadata">

**Author:** ![Parth](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Parth](https://discuss.elastic.co/u/Parth)\
**Post date:** [July 23, 2019, 5:32pm UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459/7 "2019-07-23T17:32:53Z")

</div>

Thanks a lot Stephen, this definitely helps.

---

<div class="post-metadata">

**Author:** ![Parth](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Parth](https://discuss.elastic.co/u/Parth)\
**Post date:** [July 23, 2019, 10:19pm UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459/8 "2019-07-23T22:19:25Z")

</div>

One more help Stephen, When I am trying to instrument Elastic Java Agent to an appplication which uses spring boot [with Tomcat], I am getting the following error:

```
2019-07-23 22:10:07.893 [apm-reporter] ERROR co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Failed to handle event of type METRICS with this error: apm-server-service
2019-07-23 22:10:07.893 [apm-reporter] INFO co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Backing off for 1 seconds (+/-10%)
2019-07-23 22:10:37.757 [apm-reporter] ERROR co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Failed to handle event of type METRICS with this error: apm-server-service
2019-07-23 22:10:37.757 [apm-reporter] INFO co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Backing off for 4 seconds (+/-10%)
2019-07-23 22:11:07.773 [apm-reporter] ERROR co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Failed to handle event of type METRICS with this error: apm-server-service
2019-07-23 22:11:07.773 [apm-reporter] INFO co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Backing off for 9 seconds (+/-10%)
2019-07-23 22:11:37.758 [apm-reporter] ERROR co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Failed to handle event of type METRICS with this error: apm-server-service
2019-07-23 22:11:37.758 [apm-reporter] INFO co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Backing off for 16 seconds (+/-10%)

```

I am not sure If I did something wrong: Following is the configurational detail on how I achieved this:

Used -javaagent along with other -Delastic parameters in order to deploy the application. Also added setenv.sh with the same system properties under /bin on the container while opening permission to execute by doing chmod +x /bin/setenv.sh

Looking forward to get more insight on this.

Thanks,  
PARTH

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [July 24, 2019, 2:52pm UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459/9 "2019-07-24T14:52:15Z")

</div>

Can you get the simple petclinc work in tomcat?

the reason I asked if you looked at this thread at the very end they finally figure it out they weren't using a supported framework. Are you using some other framework with your spring boot?

> [@APM agent showing timeout Error (Connection Refused)](https://discuss.elastic.co/t/apm-agent-showing-timeout-error-connection-refused/171768/12):
>
> Not sure if that's what you meant by "I made sure to add in the objects and it found 16." but did you Load Kibana objects? See [https://www.elastic.co/guide/en/kibana/6.6/apm-getting-started.html](https://www.elastic.co/guide/en/kibana/6.6/apm-getting-started.html)? If it still does not work, could you execute this query in the Kibana Dev Tools console: GET /apm\*/\_search { "query": { "match\_all": {} } }

---

<div class="post-metadata">

**Author:** ![Parth](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Parth](https://discuss.elastic.co/u/Parth)\
**Post date:** [July 26, 2019, 6:01pm UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459/10 "2019-07-26T18:01:15Z")

</div>

Thanks a lot Stephen, I think it was my bad while configuring Kubernetes External Name Service [DNS]. Figured out & resolved the issue.

---

<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:** [August 16, 2019, 2:01pm UTC](https://discuss.elastic.co/t/how-can-i-instrument-apm-agent-with-a-core-java-service/191459/11 "2019-08-16T14:01:20Z")

</div>

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