# Fetch Spans to Calculate Duration in Distributed Traces Using DQL

**URL:** <https://community.dynatrace.com/t/fetch-spans-to-calculate-duration-in-distributed-traces-using-dql/51307>\
**Category:** DQL\
**Tags:** dql, spans\
**Created:** [April 21, 2025, 7:26pm UTC](https://community.dynatrace.com/t/fetch-spans-to-calculate-duration-in-distributed-traces-using-dql/51307 "2025-04-21T19:26:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![StrangerThing](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/strangerthing/32/12182_2.png) [@StrangerThing](https://community.dynatrace.com/u/StrangerThing)\
**Post date:** [April 21, 2025, 7:26pm UTC](https://community.dynatrace.com/t/fetch-spans-to-calculate-duration-in-distributed-traces-using-dql/51307/1 "2025-04-21T19:26:29Z")

</div>

I’m trying to mimic the way duration of a trace is shown in the Distributed Tracing apps. I’m getting very, very close but seem to be missing something. I realize this may be slightly different depending on the application, but I’d like to know if anyone has had any success with this or if anyone from Dynatrace would like to chime in and describe how it’s calculated.

My trouble is some of the spans seem to be duplicated to represent code functions and if I don’t exclude them from my query they will contribute to the overall sum of the durations.

The reason this is important is because I’d like to calculate duration of a trace, but without certain specific spans and their sub-spans. I’m trying to recreate the duration in DQL as a start.

---

<div class="post-metadata">

**Author:** ![marco\_irmer](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/marco_irmer/32/3038_2.png) [@marco\_irmer](https://community.dynatrace.com/u/marco_irmer)\
**Post date:** [April 21, 2025, 11:05pm UTC](https://community.dynatrace.com/t/fetch-spans-to-calculate-duration-in-distributed-traces-using-dql/51307/2 "2025-04-21T23:05:31Z")

</div>

I think you can get the main trace if you look for spans where the span.parent\_id value is not present (null).

In its simplest form and without any additional filters, the DQL might look like this:

```javascript
fetch spans
| filter isNull(span.parent_id)
| fields start_time, trace.id, dt.entity.service, endpoint.name, duration

```

---

<div class="post-metadata">

**Author:** ![StrangerThing](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/strangerthing/32/12182_2.png) [@StrangerThing](https://community.dynatrace.com/u/StrangerThing)\
**Post date:** [April 22, 2025, 1:54pm UTC](https://community.dynatrace.com/t/fetch-spans-to-calculate-duration-in-distributed-traces-using-dql/51307/3 "2025-04-22T13:54:21Z")

</div>

That doesn’t seem to work because my beginning span also has a span.parent\_id

---

<div class="post-metadata">

**Author:** ![izabelarokita](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/izabelarokita/32/41812_2.png) [@izabelarokita](https://community.dynatrace.com/u/izabelarokita)\
**Post date:** [December 18, 2025, 10:54am UTC](https://community.dynatrace.com/t/fetch-spans-to-calculate-duration-in-distributed-traces-using-dql/51307/4 "2025-12-18T10:54:31Z")

</div>

Hey&nbsp;[@StrangerThing](https://community.dynatrace.com/user/53298)&nbsp;,  
I just wanted to check in and see if you still need help with this. If so, I’d be happy to look into it for you!&nbsp;:blush:  
Please let me know what works best for you.

---

<div class="post-metadata">

**Author:** ![StrangerThing](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/strangerthing/32/12182_2.png) [@StrangerThing](https://community.dynatrace.com/u/StrangerThing)\
**Post date:** [December 18, 2025, 2:03pm UTC](https://community.dynatrace.com/t/fetch-spans-to-calculate-duration-in-distributed-traces-using-dql/51307/5 "2025-12-18T14:03:28Z")

</div>

I’ve built out something that works, however I’d still like a way to differentiate between async and sync spans
