# Elastic APM Java Annotation API

**URL:** <https://discuss.elastic.co/t/elastic-apm-java-annotation-api/157009>\
**Category:** APM\
**Created:** [November 16, 2018, 8:45am UTC](https://discuss.elastic.co/t/elastic-apm-java-annotation-api/157009 "2018-11-16T08:45:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![digitalron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/digitalron/32/38224_2.png) [@digitalron](https://discuss.elastic.co/u/digitalron)\
**Post date:** [November 16, 2018, 8:45am UTC](https://discuss.elastic.co/t/elastic-apm-java-annotation-api/157009/1 "2018-11-16T08:45:15Z")

</div>

I'm trying to test the Annotation API on a Spring application but I get a compile-time error "cannot find symbol" related to the annotations for both @CaptureScan() and @CaptureTransaction().

I've already configured application\_packages and am using agent version 1.0.0.

Without the annotations, the other public APIs (like createSpan(), addTag(), etc) work perfectly, able to create child spans and propagate across in the methods they are embedded in.

As I couldn't find any example annotations use nor more in-depth documentation on this, I'm wondering if I am missing other dependencies or configuration settings.

Thanks!

---

<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:** [November 16, 2018, 9:21am UTC](https://discuss.elastic.co/t/elastic-apm-java-annotation-api/157009/2 "2018-11-16T09:21:18Z")

</div>

Hi and thanks for giving the Java agent a try!

The annotation can be used like this

```auto
    @CaptureSpan
    private void testSpan() {
    }

```

Don't forget to import `co.elastic.apm.api.CaptureSpan` and make sure you are using the [`apm-agent-api`](https://search.maven.org/artifact/co.elastic.apm/apm-agent-api/1.0.1/jar) artifact.

See [https://github.com/elastic/opbeans-java/pull/20/files](https://github.com/elastic/opbeans-java/pull/20/files) for an example.

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![digitalron](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/digitalron/32/38224_2.png) [@digitalron](https://discuss.elastic.co/u/digitalron)\
**Post date:** [November 16, 2018, 1:28pm UTC](https://discuss.elastic.co/t/elastic-apm-java-annotation-api/157009/3 "2018-11-16T13:28:10Z")

</div>

Thank you very much Felix, we got it working now.

We have a follow up question on @CaptureSpan.

When we are using @CaptureSpan, we notice that each child span graph extends all the way to the end of the transaction, even if we explicitly call end() for each annotated method.

If we are using ElasticApm.createSpan() from the parent span, each method we have a trace on correctly captures the span graph as a waterfall diagram.

It's something like this:

Annotation

 ![31](https://us1.discourse-cdn.com/elastic/original/3X/1/0/105dbb653fb2635a184006e74e93babf056e6f73.png)

Code-only

 ![16](https://us1.discourse-cdn.com/elastic/original/3X/a/2/a2cb35123b1fc601e69b638e5f43dfba1bc3240b.png)

Is this expected behaviour?

---

<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:** [November 16, 2018, 2:36pm UTC](https://discuss.elastic.co/t/elastic-apm-java-annotation-api/157009/4 "2018-11-16T14:36:25Z")

</div>

Calling end on the span created with `@CaptureSpan` is not supported and leads to undefined behavior. You could create three different methods like this:

```auto
@CaptureSpan
void doSteps() {
    step1();
    step2();
    step3();
}

@CaptureSpan
void step1() {
}

@CaptureSpan
void step2() {
}

@CaptureSpan
void step3() {
}

```

---

<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:** [December 7, 2018, 10:36am UTC](https://discuss.elastic.co/t/elastic-apm-java-annotation-api/157009/5 "2018-12-07T10:36:25Z")

</div>

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