# Java APM agent does override default method, causing no-op tracing

**URL:** <https://discuss.elastic.co/t/java-apm-agent-does-override-default-method-causing-no-op-tracing/352637>\
**Category:** APM\
**Tags:** java, open-telemetry\
**Created:** [February 6, 2024, 2:07pm UTC](https://discuss.elastic.co/t/java-apm-agent-does-override-default-method-causing-no-op-tracing/352637 "2024-02-06T14:07:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bebeDobreRano](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bebeDobreRano](https://discuss.elastic.co/u/bebeDobreRano)\
**Post date:** [February 6, 2024, 2:07pm UTC](https://discuss.elastic.co/t/java-apm-agent-does-override-default-method-causing-no-op-tracing/352637/1 "2024-02-06T14:07:53Z")

</div>

**Elasticsearch Java client version**  
8.11.1

**Kibana version** :  
8.9.1

**APM Elasticsearch version** :  
8.9.1

**APM Server version** :  
8.9.1

**APM Agent language and version** :  
Java, 1.46.0

Hello,  
I originally wanted to create github issue, but your guidelines recommend creating post here first, so here we go:

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**:

Java Elastic APM agent does not record OpenTelemetry spans for Elasticsearch client.

**Prelude**  
Java Elasticsearch client \>= v8.10 comes with its own instrumentation. Here I am talking specifically about `ElasticsearchTransportBase`.

When no instrumentation is specified via constructor, then `OpenTelemetryForElasticsearch.getDefault()` is called, which in turn calls `GlobalOpenTelemetry.get()`, which Java Elastic APM agent hooks and makes the method return `OpenTelemetry` object of implementation `co.elastic.apm.agent.opentelemetry.global.ElasticOpenTelemetryWithMetrics`.  
Finally, the the `OpenTelemetry` instance is passed into `OpenTelemetryForElasticsearch` constructor, which attempts to create `Tracer` object via `openTelemetry.tracerBuilder("elasticsearch-api")...build()` call. Therein lies a bug.

**The bug - Actual behaviour**  
The `TracerBuilder`, that is provided by calling `getTracerProvider().tracerBuilder(instrumentationScopeName)` in the `OpenTelemetry#tracerBuilder`, is instance of `DefaultTracerBuilder`, because the `co.elastic.apm.agent.opentelemetry.tracing.OTelTracerProvider` does not override method - `io.opentelemetry.api.trace.TracerProvider#tracerBuilder` - which by default returns no-op instance. This I think causes the problem of Elasticsearch client spans not being recorded, since the `OpenTelemetryForElasticsearch#tracer` ends up being no-op.

Here is a picture of the resulting types from mentioned methods.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/5/059ad9dc2f9fa5bc84cacfc907806e7fde214042.png)

**Expected behaviour**  
`ElasticOpenTelemetryWithMetrics#tracerBuilder(String)` should not produce no-op builder.

Thank you

---

<div class="post-metadata">

**Author:** ![bebeDobreRano](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bebeDobreRano](https://discuss.elastic.co/u/bebeDobreRano)\
**Post date:** [February 6, 2024, 3:36pm UTC](https://discuss.elastic.co/t/java-apm-agent-does-override-default-method-causing-no-op-tracing/352637/2 "2024-02-06T15:36:38Z")

</div>

For now, I have made a workaround, where I put instance `Tracer` of type `OTelTracer` into the `OpenTelemetryForElasticsearch#tracer` field, that is being used in the `ElasticsearchTransportBase` instance, via reflection.

The tracer is created as follows:

```auto
    private Tracer getTracer() {
        OpenTelemetry openTelemetry = GlobalOpenTelemetry.get();
        Version version = Version.VERSION;

        return openTelemetry.getTracer("elasticsearch-api", version == null ? "unknown" : version.toString());
    }

```

After this workaround, spans created by the Elasticsearch Java client are correctly correlated to the current APM transaction and arrive to the APM server

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [February 12, 2024, 5:52pm UTC](https://discuss.elastic.co/t/java-apm-agent-does-override-default-method-causing-no-op-tracing/352637/3 "2024-02-12T17:52:29Z")

</div>

Thanks for reporting this, the team will look into it as soon as we can!

---

<div class="post-metadata">

**Author:** ![bebeDobreRano](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bebeDobreRano](https://discuss.elastic.co/u/bebeDobreRano)\
**Post date:** [February 13, 2024, 7:28am UTC](https://discuss.elastic.co/t/java-apm-agent-does-override-default-method-causing-no-op-tracing/352637/4 "2024-02-13T07:28:03Z")

</div>

Thank you 👍

---

<div class="post-metadata">

**Author:** ![Jonas\_Kunz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jonas_kunz/32/110740_2.png) [@Jonas\_Kunz](https://discuss.elastic.co/u/Jonas_Kunz)\
**Post date:** [February 20, 2024, 9:19am UTC](https://discuss.elastic.co/t/java-apm-agent-does-override-default-method-causing-no-op-tracing/352637/5 "2024-02-20T09:19:12Z")

</div>

Thanks again for the detailed report @bebeDobreRano , you can follow [this issue](https://github.com/elastic/apm-agent-java/issues/3534) to get notified when this is fixed.

---

<div class="post-metadata">

**Author:** ![bebeDobreRano](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@bebeDobreRano](https://discuss.elastic.co/u/bebeDobreRano)\
**Post date:** [February 20, 2024, 2:38pm UTC](https://discuss.elastic.co/t/java-apm-agent-does-override-default-method-causing-no-op-tracing/352637/6 "2024-02-20T14:38:09Z")

</div>

Will follow. Thank you for the fix 👍

---

<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 19, 2024, 2:38pm UTC](https://discuss.elastic.co/t/java-apm-agent-does-override-default-method-causing-no-op-tracing/352637/7 "2024-03-19T14:38:36Z")

</div>

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