# How to query for spans that has an exception event?

**URL:** <https://community.dynatrace.com/t/how-to-query-for-spans-that-has-an-exception-event/47692>\
**Category:** DQL\
**Tags:** dql\
**Created:** [September 16, 2024, 6:59am UTC](https://community.dynatrace.com/t/how-to-query-for-spans-that-has-an-exception-event/47692 "2024-09-16T06:59:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bren](https://avatars.discourse-cdn.com/v4/letter/b/50afbb/32.png) [@bren](https://community.dynatrace.com/u/bren)\
**Post date:** [September 16, 2024, 6:59am UTC](https://community.dynatrace.com/t/how-to-query-for-spans-that-has-an-exception-event/47692/1 "2024-09-16T06:59:57Z")

</div>

Hi,

When I do a simple DQL query on the spans, I can see some spans have exceptions like below. The exceptions are wrapped as records in an array under “span.events” field.

How do I write a DQL query to pick out spans with certain exceptions, for example “java.sql.BatchUpdateException”?  
It looks like I should use “iAny”. However the example from&nbsp;[DQL operators — Dynatrace Docs](https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/operators#iAny)&nbsp;is only for simple type, not records.

Thanks!

```markup
{
      "trace.id": "674367267460f48738534bff81e3718f",
      "span.id": "f0a1bfa933cad913",
      "start_time": "2024-09-16T09:13:09.991600000+10:00",
      "end_time": "2024-09-16T09:13:59.999816000+10:00",
      "span.parent_id": "f68ff88792574c09",
      "duration": "50008216000",
      ... ...
      "span.events": [
        {
          "span_event.name": "exception",
          "exception.message": "Lock wait timeout exceeded; try restarting transaction",
          "exception.type": "java.sql.BatchUpdateException",
          "exception.id": "9d6064b5304e83f1",
          "exception.escaped": true,
          "exception.is_caused_by_root": true,
          "exception.file.full": "SQLError.java",
          "exception.line_number": "223",
          "exception.stack_trace": "... ..."
          "exception.caused_by_id": "50fe50882eeef674"
        },
        {
          "span_event.name": "exception",
          "exception.message": "Lock wait timeout exceeded; try restarting transaction",
          "exception.type": "com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException",
          "exception.id": "50fe50882eeef674",
          "exception.escaped": false,
          "exception.is_caused_by_root": false,
          "exception.file.full": "SQLError.java",
          "exception.line_number": "124",
          "exception.stack_trace": "... ..."
        }
      ],
      "dt.retain.size": "22598"
    }

```

---

<div class="post-metadata">

**Author:** ![bren](https://avatars.discourse-cdn.com/v4/letter/b/50afbb/32.png) [@bren](https://community.dynatrace.com/u/bren)\
**Post date:** [September 16, 2024, 7:48am UTC](https://community.dynatrace.com/t/how-to-query-for-spans-that-has-an-exception-event/47692/2 "2024-09-16T07:48:37Z")

</div>

Ah I found the answer myself. Do something like below.

```markup
fetch spans
| filter 
  like( lower(db.query.text), "%update%") and
  iAny(span.events[][span_event.name] == "exception") and
  iAny(like(span.events[][exception.type], "%BatchUpdateException"))

```
