cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Workflow for creating custom metric from DQL Query Result

36Krazyfists
Helper

Before I go down the rabbit hole of trying to do this from scratch, I'm hoping someone out there has already done this (Dynatrace really needs a sharing site where users can share their custom creations...).

 

What I need is relatively simple.  I have a DQL Query that spits out some timeseries metrics.  I'd like to take those and create a new custom metric in Dynatrace using the metrics ingest API.

 

I'm really hoping someone has done this already and can share their code with us?

 

(I also wish Dynatrace would create an integration for that, just like they have integrations for creating/editing ownership, SRG, and Davis Anomaly Detectors)

5 REPLIES 5

haris
Dynatrace Helper
Dynatrace Helper

Hi 36Krazyfists,

 

can you pass more details for your use case? What data does the DQL query fetch, and can you share an example query? 

Peter_Youssef
Champion

Hi @36Krazyfists 

We would appreciate providing sufficient details to enable community team give the required support.

Regards,

Peter

christian_kreuz
Dynatrace Advisor
Dynatrace Advisor

Hi,

assuming you have a DQL Task like this one, called "get_data"

timeseries p95 = percentile(dt.service.request.response_time, 95)
| fields response_time_95 = (arrayAvg(p95))

 

You can ingest a metric using the docs provided here: https://developer.dynatrace.com/develop/data/ingest-data/#metrics

Example, using a "Run JavaScript" task:

// optional import of sdk modules
import { execution } from '@dynatrace-sdk/automation-utils';
import { metricsClient } from '@dynatrace-sdk/client-classic-environment-v2';

export default async function ({ executionId }) {
  // your code goes here
  // e.g. get the current execution
  const ex = await execution(executionId);

  const dqlResult = await ex.result('get_data');
  // dqlResult = { records: [...], metaData: ..., types: ... }
  // get response_time_95 from records of dqlREsult
  const response_time_95 = dqlResult.records[0].response_time_95;

  console.log(`Going to ingest ${response_time_95}`);

  // ingest metric called my.response_time_95 using metricsClient
  return await metricsClient
    .ingest({ body: `my.response_time_95 ${response_time_95}` })
    .then((response) => {
      console.log(response);
     })
    .catch((e) => {
      console.error(e);
    });
}

christian_kreuz_0-1733476901698.png

You can run this Workflow based on a schedule. I've attached a Workflow Template as an example (zipped, as .yaml is not allowed).

You can observe the result using a Notebook or a Dashboard, accessing the metric you ingested:

christian_kreuz_1-1733476962249.png

Note: Please ensure that your User and the Workflow Actor have appropriate permissions to read and write data from/to Grail, specifically metrics.

christian_kreuz_3-1733477075582.png

 

 

Hope this helps!

Really great stuff @christian_kreuz !  Exactly what I was looking for!  I still contend that Dynatrace should provide an integration (like Owners and Anomaly Detection) for creating custom metrics (and even integrations for setting maintenance periods on entities and other common stuff that people will use Workflows for within Dynatrace.  Or, at least setup a sharing site where users can post and share creations they've made with each other).

 

Thanks so much!

I agree, built-in actions that make certain-uses cases easier, essentially a no-code solution for the end-user, would be a great addition. I will forward this feedback internally!

Featured Posts