# How to derive a field in DQL

**URL:** <https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984>\
**Category:** DQL\
**Tags:** dql\
**Created:** [February 5, 2024, 5:05pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984 "2024-02-05T17:05:00Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![susmita\_k](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@susmita\_k](https://community.dynatrace.com/u/susmita_k)\
**Post date:** [February 5, 2024, 5:05pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/1 "2024-02-05T17:05:00Z")

</div>

I have a problem where i need to check log content for job status. If the job status is STARTED and not ENDED yet then need to show as Running, and if the Job status is started and ended ok, then Completed, else if job status is Started and ended not ok then failed. Any help is appreciated. Thank you

---

<div class="post-metadata">

**Author:** ![dannemca](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/dannemca/32/7215_2.png) [@dannemca](https://community.dynatrace.com/u/dannemca)\
**Post date:** [February 5, 2024, 5:31pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/2 "2024-02-05T17:31:25Z")

</div>

You should be able to achieve this with the Conditional IF:

> **[if - Conditional functions](https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/functions/conditional-functions#if)**
>
> Functions that return a conditional result. | A list of DQL conditional functions.

---

<div class="post-metadata">

**Author:** ![susmita\_k](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@susmita\_k](https://community.dynatrace.com/u/susmita_k)\
**Post date:** [February 6, 2024, 5:25pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/3 "2024-02-06T17:25:24Z")

</div>

I have tried below, it is only working for the first condition, but when its NOT OK, its not returning as Failed. Is it not how it should be done ?

fetch logs //, scanLimitGBytes: 500, samplingRatio: 1000  
| filter matchesValue(log.source, “/opt/controlm/ctmagent/ctm/dailylog/daily\_ctmag\_\*”)  
and matchesphrase(jobstatus, “STARTED”)  
|filter in(job,array($jobname))  
|parse content , “TIMESTAMP(‘MMdd hhmmss’):Starttime”  
|fields job, orderid, runno,Starttime,host.name  
| join [fetch logs  
| filter matchesValue(log.source, “/opt/controlm/ctmagent/ctm/dailylog/daily\_ctmag\_\*”)  
| filter matchesphrase(jobstatus, “ENDED OK”) OR matchesphrase(jobstatus, “ENDED NOTOK”)  
|filter in(job,array($jobname))  
|parse content , “TIMESTAMP(‘MMdd hhmmss’):EndTime”  
|parse content , “LD ‘elapsed -’, LD:RunTime ‘cpu’”

|fieldsadd Current\_status =if(matchesphrase(jobstatus, “ENDED OK”), “Completed”),if(matchesphrase(jobstatus, “ENDED NOTOK”),“Failed”,else: “Running”)  
|fields orderid,runno,jobstatus,Current\_status,EndTime,RunTime

] , on: {orderid,runno}  
|fields host.name, job, orderid, runno,right.jobstatus, right.Current\_status, Starttime,right.EndTime, right.RunTime

---

<div class="post-metadata">

**Author:** ![dannemca](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/dannemca/32/7215_2.png) [@dannemca](https://community.dynatrace.com/u/dannemca)\
**Post date:** [February 6, 2024, 6:01pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/4 "2024-02-06T18:01:36Z")

</div>

Try this If condition:

```markup
data record(jobstatus="ENDED OK"), record(jobstatus="ENDED NOTOK"), record(jobstatus="Danne")
| fieldsadd Current_status =if((not matchesphrase(jobstatus, "ENDED OK")) and (not matchesphrase(jobstatus, "ENDED NOTOK")), "Running", else: (if(matchesphrase(jobstatus, "ENDED OK"), "Completed", else: "Failed")))

```

Results:

 ![image.png](https://global.discourse-cdn.com/dynatrace/original/2X/2/22ff15f2bfbfe8242867c9b0e51c9ac1e3f1374c.png)

Regards.

---

<div class="post-metadata">

**Author:** ![susmita\_k](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@susmita\_k](https://community.dynatrace.com/u/susmita_k)\
**Post date:** [February 6, 2024, 6:52pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/5 "2024-02-06T18:52:55Z")

</div>

Thank you! it works as expected.

I’m trying to add an alert for the jobs with ‘Running’ status, to see if the job is running more than certain (for e.g 30 mins) time, then send an alert saying this job is long running. I’m not sure how can I achieve this. Any pointer would be very helpful.

---

<div class="post-metadata">

**Author:** ![dannemca](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/dannemca/32/7215_2.png) [@dannemca](https://community.dynatrace.com/u/dannemca)\
**Post date:** [February 6, 2024, 6:58pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/6 "2024-02-06T18:58:01Z")

</div>

First you will need to create a metric, following this procedure:

> **[Log metrics](https://docs.dynatrace.com/docs/analyze-explore-automate/logs/lma-log-processing/lma-log-metrics)**
>
> Create metrics based on log data and use them throughout Dynatrace like any other metric.

Then, once the metric starts to receive data points, you can create a metric event for alerting:

> **[Metric events](https://docs.dynatrace.com/docs/dynatrace-intelligence/anomaly-detection-classic/metric-events)**
>
> Learn about metric events in Dynatrace

making sure to adjust the interval to 30 mins.

---

<div class="post-metadata">

**Author:** ![susmita\_k](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@susmita\_k](https://community.dynatrace.com/u/susmita_k)\
**Post date:** [February 6, 2024, 8:40pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/7 "2024-02-06T20:40:48Z")

</div>

[@dannemca](https://community.dynatrace.com/user/18264)&nbsp;Thanks for the pointer, however, I need to do join to get the job run time for running jobs, and below is my query&nbsp; -

| filter matchesValue(log.source, “/opt/controlm/ctmagent/ctm/dailylog/daily\_ctmag\_\*”) and matchesphrase(jobstatus, “STARTED”)  
|parse content , “TIMESTAMP(‘MMdd hhmmss’):startts”  
|fields startts,job, orderid, runno  
| join [fetch logs  
| filter matchesValue(log.source, “/opt/controlm/ctmagent/ctm/dailylog/daily\_ctmag\_\*”)  
| filter matchesphrase(jobstatus, “ENDED OK”) OR matchesphrase(jobstatus, “ENDED NOTOK”)  
|fieldsadd Current\_status =if((not matchesphrase(jobstatus, “ENDED OK”)) and (not matchesphrase(jobstatus, “ENDED NOTOK”)), “Running”)  
|fieldsadd duration= if (jobstatus == “Running”, (unixSecondsFromTimestamp(now()) - unixSecondsFromTimestamp(startts)))  
| fields orderid,runno,jobstatus,duration

] , on:{orderid, runno}  
| fields orderid,runno,job,right.jobstatus,right.duration

However, I am not able to do the join condition in processor, so not able to get this duration for the log metrics. Is there another place where I can run this on a scheduled time to identify long running job and alert ?

---

<div class="post-metadata">

**Author:** ![dannemca](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/dannemca/32/7215_2.png) [@dannemca](https://community.dynatrace.com/u/dannemca)\
**Post date:** [February 6, 2024, 8:55pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/8 "2024-02-06T20:55:10Z")

</div>

We are not able to create metrics directly from raw DQL ([there is an idea for that](https://community.dynatrace.com/post/233074)).

In this case, you want to be notified if a job is still running, means jobstatus is not “ENDED OK” nor “ENDED NOTOK”, right? So you can just set these conditions in the matcher, like:

```markup
matchesValue(log.source, "/opt/controlm/ctmagent/ctm/dailylog/daily_ctmag_*") and (matchesphrase(jobstatus, "ENDED OK") OR matchesphrase(jobstatus, "ENDED NOTOK"))

```

Then you can set some dimensions like timestamp, job, orderid ,runno and jobstatus.

You can use these dimensions to work on the metric selector and define the alerts rules by it.

Try and let us know.

---

<div class="post-metadata">

**Author:** ![susmita\_k](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@susmita\_k](https://community.dynatrace.com/u/susmita_k)\
**Post date:** [February 6, 2024, 10:28pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/9 "2024-02-06T22:28:47Z")

</div>

Unfortunately only matcher is not giving the intended result as we need to identify the duration from start time to now(). We are looking for a solution where we would run this dql in certain time and then get the job run time and if any of them have run time more than 30 mins (not ended yet) then send an alert. Would workflow be appropriate for this kind of scenario ?

---

<div class="post-metadata">

**Author:** ![dannemca](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/dannemca/32/7215_2.png) [@dannemca](https://community.dynatrace.com/u/dannemca)\
**Post date:** [February 7, 2024, 12:21pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/10 "2024-02-07T12:21:55Z")

</div>

Yep, that would be my next suggestion. You can create a workflow to run in a crontab way, that execute your DQL and then take some action based on its results.

---

<div class="post-metadata">

**Author:** ![susmita\_k](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@susmita\_k](https://community.dynatrace.com/u/susmita_k)\
**Post date:** [February 9, 2024, 5:49pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/11 "2024-02-09T17:49:49Z")

</div>

Thanks&nbsp;[@dannemca](https://community.dynatrace.com/user/18264),&nbsp;workflow solution is working as expected.&nbsp; On a different log content, I am tryin gto parse the content like “REPLACE\_WEB\_SERVICE\_UI=TRUE” with below parse dql :&nbsp;PARSE content, " DATA:propkey ‘=’, DATA:propValue EOL;"

However, it does not seem to work. is there another way to find out the value from = to end of line ?

---

<div class="post-metadata">

**Author:** ![dannemca](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/dannemca/32/7215_2.png) [@dannemca](https://community.dynatrace.com/u/dannemca)\
**Post date:** [February 9, 2024, 5:57pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/12 "2024-02-09T17:57:11Z")

</div>

Try this:

```markup
data record(content="REPLACE_WEB_SERVICE_UI=TRUE")
| parse content, "LD:key '=' LD:value"

```

Result:

![image (15).png](https://global.discourse-cdn.com/dynatrace/original/3X/a/7/a7d598c9c6c8f5a33865cf969c79e9d912a3a499.png)

Regards.

---

<div class="post-metadata">

**Author:** ![susmita\_k](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@susmita\_k](https://community.dynatrace.com/u/susmita_k)\
**Post date:** [February 9, 2024, 6:15pm UTC](https://community.dynatrace.com/t/how-to-derive-a-field-in-dql/43984/13 "2024-02-09T18:15:32Z")

</div>

Thanks so much! works like a charm.
