# Manually capturing database statement

**URL:** <https://discuss.elastic.co/t/manually-capturing-database-statement/349593>\
**Category:** APM\
**Tags:** java\
**Created:** [December 18, 2023, 7:16pm UTC](https://discuss.elastic.co/t/manually-capturing-database-statement/349593 "2023-12-18T19:16:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![georgms](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/georgms/32/124425_2.png) [@georgms](https://discuss.elastic.co/u/georgms)\
**Post date:** [December 18, 2023, 7:16pm UTC](https://discuss.elastic.co/t/manually-capturing-database-statement/349593/1 "2023-12-18T19:16:23Z")

</div>

The Elastic Java agent is able to automatically capture "database" statement e.g. when firing a request to Elastic Search:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/d/d/dde140fdbd509f79316ae930ecd9421ee6df4e31.png)

This happens in the [Elastic Java agent instrumentation using the `span.db.statement` field](https://github.com/elastic/apm-agent-java/blob/4e60e0a997a844bde499801a4b889b5ee7484f6f/apm-agent-plugins/apm-es-restclient-plugin/apm-es-restclient-plugin-common/src/main/java/co/elastic/apm/agent/esrestclient/ElasticsearchRestClientInstrumentationHelper.java#L125). The main benefit over context fields is that this is nicely highlighted and more visible.

We're using our own GraphQL server implementation so no automatic instrumentation. While manually instrumenting our application I can only access transaction and span labels and context fields.

Is there any way I can set the `span.db.statement` field when manually instrumenting?

---

<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:** [December 19, 2023, 10:56am UTC](https://discuss.elastic.co/t/manually-capturing-database-statement/349593/2 "2023-12-19T10:56:29Z")

</div>

Hi,

I see two ways to implement that:

- with Elastic agent API: create a span and store it as a context field, then use an [ingest pipeline](https://www.elastic.co/guide/en/apm/guide/current/ingest-pipelines.html) to modify the stored document with the appropriate `span.db.statement` field value.

- with OpenTelemetry API: create a span and follow the [semantic conventions for databases](https://opentelemetry.io/docs/specs/semconv/database/database-spans/), use the `db.statement` attribute to store the query.

Disclamer: I haven't tried it myself, and some experimentation might be needed here.

---

<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:** [January 16, 2024, 10:57am UTC](https://discuss.elastic.co/t/manually-capturing-database-statement/349593/3 "2024-01-16T10:57:03Z")

</div>

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