# Span-level Custom Context/Metadata

**URL:** <https://discuss.elastic.co/t/span-level-custom-context-metadata/323868>\
**Category:** APM\
**Created:** [January 24, 2023, 7:59pm UTC](https://discuss.elastic.co/t/span-level-custom-context-metadata/323868 "2023-01-24T19:59:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LightSky](https://avatars.discourse-cdn.com/v4/letter/l/8edcca/32.png) [@LightSky](https://discuss.elastic.co/u/LightSky)\
**Post date:** [January 24, 2023, 7:59pm UTC](https://discuss.elastic.co/t/span-level-custom-context-metadata/323868/1 "2023-01-24T19:59:41Z")

</div>

Setup:  
Elastic Cloud - Running version 8.6

I am looking for a way to add non-indexed metadata to an individual span instead of just on the transaction itself.  
(See [Metadata | APM User Guide [8.6] | Elastic](https://www.elastic.co/guide/en/apm/guide/current/data-model-metadata.html#data-model-custom))  
In certain scenarios I want to be able to log larger json data for triaging issues, however the standard Span.Label is indexed and truncates the text after 1024 characters.  
It would be possible to have a singular transaction with the custom content for each action, but that is not ideal since I could be dealing with multiple actions in each transaction and I would like to isolate the custom metadata to the span itself.

---

<div class="post-metadata">

**Author:** ![LightSky](https://avatars.discourse-cdn.com/v4/letter/l/8edcca/32.png) [@LightSky](https://discuss.elastic.co/u/LightSky)\
**Post date:** [January 31, 2023, 4:03pm UTC](https://discuss.elastic.co/t/span-level-custom-context-metadata/323868/2 "2023-01-31T16:03:02Z")

</div>

Bump.

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [February 1, 2023, 10:15am UTC](https://discuss.elastic.co/t/span-level-custom-context-metadata/323868/3 "2023-02-01T10:15:26Z")

</div>

Hi @LightSky, welcome to our forum! 👋  
I opened an [issue](https://github.com/elastic/apm/issues/752) to discuss the options.

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [February 6, 2023, 4:25pm UTC](https://discuss.elastic.co/t/span-level-custom-context-metadata/323868/4 "2023-02-06T16:25:53Z")

</div>

@LightSky Are you able to provide some details on what you are using this for? The use case can help inform whether/what we can do to support this in the APM system. Commenting here or on Eyal's issue is fine. Thanks.

---

<div class="post-metadata">

**Author:** ![LightSky](https://avatars.discourse-cdn.com/v4/letter/l/8edcca/32.png) [@LightSky](https://discuss.elastic.co/u/LightSky)\
**Post date:** [February 6, 2023, 5:12pm UTC](https://discuss.elastic.co/t/span-level-custom-context-metadata/323868/5 "2023-02-06T17:12:17Z")

</div>

Our application is required to log individual actions throughout a given Transaction for each user in our system and the ActionData is a JSON string which can easily exceed 1024 characters, but is _usually_ under 8k or so.  
We have other Spans which encompass other logged data aside from the ActionData which we keep inside the Transaction too.  
The only workaround we see at the moment is to have a separate Transaction for every single log, which seems like overkill. Or doing a Span.SetLabel with an index (Ex: ActionData[0], ActionData[1]), but that could cause another set of issues since we didn't want to abuse the Elastic Index, and the approach seems kind of _hacky_.  
Finally we saw that Transaction has CustomData support and thought to see if there was a similar approach for Span since we don't need the field indexed whatsoever.

From ElasticAPM's point of view it would look like something similar to this:

- Transaction
- 
  - Span (1)

- 
  - 
    - ActionData: JSON

- 
  - Span (2)

- 
  - 
    - Misc Log

- 
  - Span (3)

- 
  - 
    - ActionData: JSON

- 
  - ....

- 
  - Span (8)

- 
  - 
    - ActionData: JSON

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [February 6, 2023, 6:52pm UTC](https://discuss.elastic.co/t/span-level-custom-context-metadata/323868/6 "2023-02-06T18:52:12Z")

</div>

@LightSky Thanks. That's very helpful.

How do you use that captured data? Are you using the Kibana APM app to click around and look at the `ActionData` and other data in the browser UI? Or are you querying the trace indices in Elasticsearch with your own separate tooling?

I'm asking because another possible work-around could be to setup structured log ingest for your applications and use [log correlation](https://www.elastic.co/guide/en/apm/agent/java/current/logs.html#log-correlation-ids) by ensuring trace.id, span.id, transaction.id fields get added to log records. (I've linked to the Java docs for this. I'm not sure what language(s) are relevant for your usage.) This might not be appealing to you, because it may be more setup/change than you'd like. Also the data linking in the Kibana APM app may not be as convenient.

---

<div class="post-metadata">

**Author:** ![LightSky](https://avatars.discourse-cdn.com/v4/letter/l/8edcca/32.png) [@LightSky](https://discuss.elastic.co/u/LightSky)\
**Post date:** [February 6, 2023, 7:16pm UTC](https://discuss.elastic.co/t/span-level-custom-context-metadata/323868/7 "2023-02-06T19:16:08Z")

</div>

@trentm  
We are currently using C#.  
We currently use the data via Kibana APM to monitor the data in the browser.  
We do however have plans in the future to add support for consuming the data from separate tooling.

---

<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 6, 2023, 7:16pm UTC](https://discuss.elastic.co/t/span-level-custom-context-metadata/323868/8 "2023-03-06T19:16:42Z")

</div>

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