Hi jerry_lobenstei
Thanks for sharing your approach.
We have a similar use case where we want to set up alerts for all services that were previously active but are now in an inactive state due to some issue and require a pod restart.
I tried implementing this approach and modified the query by adding namespace and management zone filters. I also updated this field:
const twentyMinutesAgo = new Date(now.getTime() - 72 * 60 * 60 * 1000);
However, I am only seeing 14 records, whereas there should be more in the last 72 hours. Additionally, if I do not use the namespace filter, the data returned is only for a single namespace instead of all namespaces.
Could you please help me figure out what might be causing this issue?
/*
* This function will run in the DYNATRACE JavaScript runtime.
* For information visit https://dt-url.net/functions-help
*/
import { monitoredEntitiesMonitoringStateClient } from "@dynatrace-sdk/client-classic-environment-v2";
import { queryExecutionClient } from "@dynatrace-sdk/client-query";
// **** Dynatrace API ****
async function fetchAllMonitoringStates(pgID) {
const pgID_string = `${pgID.map(id => `"${id}"`).join(",")}`;
let nextPageKey = null;
const monitoringStates = [];
do {
const params = nextPageKey
? { nextPageKey }
: { entitySelector: `entityId(${pgID_string})`};
const result = await monitoredEntitiesMonitoringStateClient.getStates(params);
monitoringStates.push(...result.monitoringStates);
nextPageKey = result.nextPageKey;
} while (nextPageKey);
return monitoringStates
}
export default async function () {
const now = new Date();
const twentyMinutesAgo = new Date(now.getTime() - 72 * 60 * 1000);
const formatDate = (date) => date.toISOString();
const $dt_timeframe_from = formatDate(twentyMinutesAgo);
const $dt_timeframe_to = formatDate(now);
// **** DQL Query ****
const dqlQuery = `
fetch dt.entity.process_group_instance
| fieldsAdd host.id=belongs_to[dt.entity.host], pg_id=instance_of[dt.entity.process_group], cgi_id=(belongs_to[dt.entity.container_group_instance])
| lookup [fetch dt.entity.host], sourceField:host.id, lookupField:id, fields:{host.name=entity.name, host.hostGroupName=hostGroupName}
| lookup [fetch dt.entity.process_group], sourceField:pg_id, lookupField:id, fields:{process.name=entity.name}
| lookup [fetch dt.entity.container_group_instance
| fieldsAdd pod_id=belongs_to[dt.entity.cloud_application_instance], namespace_id=belongs_to[dt.entity.cloud_application_namespace], node_id=belongs_to[dt.entity.host]
| lookup [fetch dt.entity.cloud_application_instance], sourceField:pod_id, lookupField:id, fields:{pod_name=entity.name}
| lookup [fetch dt.entity.cloud_application_namespace], sourceField:namespace_id, lookupField:id, fields:{namespace=entity.name}
| lookup [fetch dt.entity.host], sourceField:node_id, lookupField:id, fields:{node_name=entity.name}], sourceField:cgi_id, lookupField:id, fields:{pod_name, namespace, node_name}
| fields managementZones, host.id, host.name, host.hostGroupName, process.name, pod_name, namespace, node_name, id
| sort process.name desc
| filter namespace == "test-v2"
| filter host.hostGroupName == "TEST.PROD"
// | limit 5000
`;
// **** Execution Performance Limits ****
// const queryExecution = await queryExecutionClient.queryExecute({ body: { query: dqlQuery } });
const queryExecution = await queryExecutionClient.queryExecute({ body: {
query: dqlQuery,
fetchTimeoutSeconds:5000,
defalutScanLimitsGbytes: 50,
maxResultsBytes: 1000000,
maxResultsRecords: 3000000
} });
let dqlResults = [];
while (true) {
const queryPollResult = await queryExecutionClient.queryPoll({ requestToken: queryExecution.requestToken });
if (queryPollResult.state !== "RUNNING") {
dqlResults = queryPollResult.result.records;
break;
}
}
let pgID = dqlResults.map(item => item.id);
const processAllChunks = async () => {
const allMonitoringStates = [];
for (let i = 0; i < pgID.length; i += 50) {
const chunk = pgID.slice(i, i + 50);
const chunkStates = await fetchAllMonitoringStates(chunk);
allMonitoringStates.push(...chunkStates);
}
return allMonitoringStates;
};
const monitoringStates = await processAllChunks();
const mergedResults = dqlResults.map(dqlItem => {
const match = monitoringStates.find(apiItem => apiItem.entityId === dqlItem.id);
return match ? { ...dqlItem, severity: match.severity, state: match.state } : dqlItem;
});
const filteredResults = mergedResults.filter(item =>
item.state && item.state.toLowerCase().includes("restart")
); return filteredResults;
}