11 Feb 2024 03:32 PM
Hi,
We have a need to include "old" metrics currently not in grail in new dashboards, however, when returning data based on the timeseries data type current documentation, the dashboard charts are not enabled, so I wonder what is the right way to pass the data.
For example, take a look at the following code:
///endpoint/module being used for metric requests, different when pulling other data
import { metricsClient } from "@dynatrace-sdk/client-classic-environment-v2";
export default async function main() {
//configuration for metric request
let config = {
acceptType: "application/json; charset=utf-8",
metricSelector: "builtin:service.response.server:splitBy(\"dt.entity.service\"):sort(value(auto,descending)):limit(20)",
from: Date.parse($dt_timeframe_from).toString(),
to: Date.parse($dt_timeframe_to).toString(),
// resolution: "10m",
};
//runs the metric request query
const data = await metricsClient.query(config);
//returns nothing if there is not data in the timeframe that matches given filters
if(data.result[0].data.length == 0){
return []
}
// variables used to specify data type to be charted
const data_type = "double";
//variables used for final return
const mappings = {
timestamp: { type: 'timestamp' },
};
const mergedData = <any>[];
//console.log("Before: ", data.result[0].data);
(data.result[0].data).forEach((item) => {
const datapoints_arr = <any>[];
for (let i=0;i<item.timestamps.length;i++) {
datapoints_arr.push({
start: new Date(item.timestamps[i]),
value: item.values[i]
})
}
const output = {
name: [item.dimensions[0]],
datapoints: datapoints_arr
}
mergedData.push(output);
});
console.log("Merged data", mergedData);
return mergedData;
}
Where do I get things wrong?
Gil.
Solved! Go to Solution.
11 Feb 2024 08:37 PM
The recommended way would be to wait for these metrics to be available in Grail (should be and of Q1 afaik).
Recently I used similar approach as you and I've been successful with:
import { metricsClient } from '@dynatrace-sdk/client-classic-environment-v2';
export default async function fetchMetrics() {
// Assuming dt_timeframe_from holds the timeframe from your site
const queryParameters = {
acceptType: "application/json; charset=utf-8",
metricSelector: "builtin:service.response.time:splitBy():avg", //Amend to your Advanced Data Explorer Query
from: $dt_timeframe_from, // Using template literal to interpolate the variable
// resolution: "auto"
// Add other parameters as needed: entitySelector, to, etc.
};
try {
const metricsResponse = await metricsClient.query(queryParameters);
const size = metricsResponse.result[0].data[0].timestamps.length;
return new Array(size).fill(null).map((_, index, array) => {
return {
timestamp: new Date(metricsResponse.result[0].data[0].timestamps[index]).toISOString(),
Something: metricsResponse.result[0].data[0].values[index],
};
});
} catch (error) {
// Handle the error gracefully
console.error("Error fetching metrics:", error);
throw error;
}
}
I'd recommend looking at the raw output and see what's wrong.
12 Feb 2024 11:54 AM
Thanks @Julius_Loman ,
I do understand the recommendation and completely agree with its reasoning. I guess I wasn't clear about my challenge which was how to actually display all dimensions but have managed to solve this.
If anyone else is reading this then just know that for each dimension you should have a property just like the "something", where the name itself is the dimension name and its value is the actual value to be displayed in the chart.