More efficient way to filter spans by service tags in DQL?

Hi everyone,

I’m currently working with a DQL query that fetches spans and filters them based on a tag applied to the service (or process group) the span belongs to.

In my dashboard, I created a variable that contains a list of tag values. Then, in the DQL, I do the following:

fetch spans
| expand entityAttr(dt.entity.process_group_instance, "tags"), alias: tags2
| filter tags2 == $Service

Functionally, this works exactly as expected:
the spans returned correctly match the selected tag from the variable.

However, the downside is that each execution processes a very large amount of data (many GBs), which makes the query expensive.

My question is:

:backhand_index_pointing_right: Is there a more efficient or recommended way to filter spans by the tags of the service (or related entity) they belong to, without having to expand all tags?

For example:

  • Is there a way to filter earlier in the pipeline using entityAttribute() or similar functions?
  • Any best practices to reduce data processed when filtering spans by entity tags?
  • Or an alternative approach using service-level filtering instead of expanding tags on spans?

Any advice or examples would be greatly appreciated.
Thanks in advance!

Hello @arobayo17,

I would try to avoid expanding tags directly on the span stream.

The query works functionally, but the expensive part is that you first fetch all matching spans for the timeframe, then expand the tags array for each row, and only after that apply the tag filter. On large span volumes this can multiply the number of records processed.

A more efficient approach is usually to resolve the tagged entities first and then filter spans by the entity ID.

For example, if the tag is on the process group instance:

fetch spans
| filter in(
    dt.entity.process_group_instance,
    classicEntitySelector("type(PROCESS_GROUP_INSTANCE),tag(\"YOUR_TAG_VALUE\")")
  )

If the tag is on the service:

fetch spans
| filter in(
    dt.entity.service,
    classicEntitySelector("type(SERVICE),tag(\"YOUR_TAG_VALUE\")")
  )

With a dashboard variable, you can build the entity selector dynamically, for example:

fetch spans
| filter in(
    dt.entity.service,
    classicEntitySelector(concat("type(SERVICE),tag(\"", $Service, "\")"))
  )

This should be preferable to:

fetch spans
| expand entityAttr(dt.entity.process_group_instance, "tags"), alias: tags2
| filter tags2 == $Service

because the filter is based on the entity reference instead of expanding metadata for every span.

So in short: use entity selectors to produce the list of tagged entities, then filter spans by those entity IDs.

I hope it helps 😀