# Custom extension for slow queries

**URL:** <https://community.dynatrace.com/t/custom-extension-for-slow-queries/45639>\
**Category:** Extensions\
**Tags:** databases, oracle, extensions\
**Created:** [May 17, 2024, 3:19am UTC](https://community.dynatrace.com/t/custom-extension-for-slow-queries/45639 "2024-05-17T03:19:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikhil](https://avatars.discourse-cdn.com/v4/letter/n/b782af/32.png) [@nikhil](https://community.dynatrace.com/u/nikhil)\
**Post date:** [May 17, 2024, 3:19am UTC](https://community.dynatrace.com/t/custom-extension-for-slow-queries/45639/1 "2024-05-17T03:19:32Z")

</div>

Hi all,

I am trying to build an extension to monitor top 20 slow queries with time taken to execute

Following is my YAML file. in this I am unable to fetch top queries, only I can get response time. Can someone please help me out where I am wrong.

metrics:&nbsp; - key: oracle.queries.extension.oracle-top-queries&nbsp; &nbsp; metadata:&nbsp; &nbsp; &nbsp; displayName: Top queries&nbsp; &nbsp; &nbsp; unit: Unspecified  
&nbsp; - key: oracle.queries.extension.oracle-top-queries-response&nbsp; &nbsp; metadata:&nbsp; &nbsp; &nbsp; displayName: Response times&nbsp; &nbsp; &nbsp; unit: MicroSecond  
sqlOracle:&nbsp; - group: Top queries&nbsp; &nbsp; featureSet: “Top queries”&nbsp; &nbsp; interval:&nbsp; &nbsp; &nbsp; minutes: 1&nbsp; &nbsp; ingest: metrics&nbsp; &nbsp; query: \>&nbsp; &nbsp; &nbsp; SELECT&nbsp; &nbsp; &nbsp; \*&nbsp; &nbsp; &nbsp; FROM&nbsp; &nbsp; &nbsp; (SELECT sql\_text AS “Queries”, elapsed\_time/1000000 AS “Response” FROM sys.v\_$sqlarea ORDER BY 2 DESC)&nbsp; &nbsp; &nbsp; WHERE ROWNUM \< 21&nbsp; &nbsp; metrics:&nbsp; &nbsp; &nbsp; - key: oracle.queries.extension.oracle-top-queries&nbsp; &nbsp; &nbsp; &nbsp; value: col:Queries&nbsp; &nbsp; &nbsp; - key: oracle.queries.extension.oracle-top-queries-response&nbsp; &nbsp; &nbsp; &nbsp; value: col:Response

---

<div class="post-metadata">

**Author:** ![PacoPorro](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/pacoporro/32/36087_2.png) [@PacoPorro](https://community.dynatrace.com/u/PacoPorro)\
**Post date:** [May 17, 2024, 6:50am UTC](https://community.dynatrace.com/t/custom-extension-for-slow-queries/45639/2 "2024-05-17T06:50:34Z")

</div>

Queries should end in 10s by default. Otherwise, the query is killed.  
If you check the Oracle extension, in the vars section you have this

```php
vars:
id: long-running-query-timeout
displayName: Heavy query timeout
description: "Default is 10 seconds. Requires ActiveGate 1.275 or newer."
type: text

```

In the needed queries, the timeout parameter is also added.

```php
- subgroup: topN Queries
        interval:
          minutes: 5
        featureSet: TopN
        ingest: log
        query: >
          SELECT sqlarea.sql_id,
                sqlarea.elapsed_time,
                sqlarea.cpu_time,
                sqlarea.sql_fulltext,
                sqlarea.parsing_schema_name,
                sqlarea.disk_reads,
                sqlarea.direct_writes,
                sqlarea.executions,
                sqlarea.parse_calls,
                sqlarea.buffer_gets,
                sqlarea.rows_processed,
                sqlarea.user_io_wait_time,
                sqlarea.cluster_wait_time,
                sqlarea.concurrency_wait_time,
                sqlarea.application_wait_time,
                sys_context('USERENV', 'INSTANCE_NAME') as INSTANCE_NAME
          FROM v$sqlarea sqlarea
                  JOIN (SELECT sql_id, current_con_id
                        FROM (SELECT area.sql_id, current_con_id
                              FROM v$sqlarea area
                                        JOIN (SELECT SYS_CONTEXT('USERENV', 'CON_ID') as current_con_id from DUAL)
                                            ON area.con_id = current_con_id
                              WHERE (area.last_active_time >= (SYSDATE - NUMTODSINTERVAL(300, 'SECOND'))
                                  OR (area.last_active_time IS NULL
                                      AND area.last_load_time >= (SYSDATE - NUMTODSINTERVAL(300, 'SECOND'))))
                              ORDER BY elapsed_time DESC)
                        WHERE rownum <= 100) sqlarea_int
                        ON sqlarea.sql_id = sqlarea_int.sql_id AND sqlarea.con_id = current_con_id
        timeout: var:long-running-query-timeout

```

By setting the query duration parameter, you can control the maximum duration of the query execution. For example, if you set it to 30seconds, the query is killed after that time. The code snippets are from the Oracle extension, which demonstrates how this functionality is implemented there.

> **[Oracle Database monitoring & observability | Dynatrace Hub](https://www.dynatrace.com/hub/detail/oracle-database/?query=oracle&filter=all#release-notes)**
>
> Observe, analyze and optimize the usage, health and performance of your database. Start your free trial today!

---

<div class="post-metadata">

**Author:** ![dprint](https://avatars.discourse-cdn.com/v4/letter/d/919ad9/32.png) [@dprint](https://community.dynatrace.com/u/dprint)\
**Post date:** [October 23, 2024, 3:09pm UTC](https://community.dynatrace.com/t/custom-extension-for-slow-queries/45639/3 "2024-10-23T15:09:30Z")

</div>

Hi - I’m following this -&nbsp;  
```

vars:- id: long-running-query-timeoutdisplayName: Heavy query timeoutdescription: "Default is 10 seconds. Requires ActiveGate 1.275 or newer."type: text```

and when referencing it :

- group: some groupfeatureSet: Latestinterval:minutes: 30ingest: metricsquery: |SOME QUERYtimeout: var:long-running-query-timeout

I receive the below warns and error messages:  
1:&nbsp;A variable is referenced but is not defined.  
2:&nbsp;This variable is defined but is not used within this extension

Could you please share a working example?
