The code you provided retrieves the value of `metric.label.store` from...

August 7, 2025 at 06:24 PM

if my json contains {"metric.label.store" : "BLAH"} which itself is inside another {} and so on and i want to do js code to retrieve the answer how do i og about that - context.panel.data. what? { "state": "Done", "series": [ { "refId": "A", "meta": { "typeVersion": [ 0, 0 ], "custom": { "labels": { "metric.label.bucket": "", "metric.label.file_name": "CSS-3727-250807112348051-D-J-R.txt.pgp", "metric.label.file_name_short": "3727-250807112348051-D-J-R", "metric.label.folder": "RIF", "metric.label.log": "soft-log", "metric.label.message": "Successfully uploaded file CSS-3727-250807112348051-D-J-R.txt.pgp to folder RIF in bucket sb-lt-pvt-cps-cpak-prd after 0 retries", "metric.label.rif_file_name": "CSS-3727-250807112348051-D-J-R.txt.pgp", "metric.label.store": "3727", "metric.label.timestamp": "2025-08-07 11:24:08.515", "metric.name": "value.falcon_CPAK_rif_folder_count_files", "resource.label.bucket_id": "cpak-log", "resource.label.location": "northamerica-northeast1", "resource.label.monitored_resource_type": "global", "resource.label.project_id": "phc-store-apps", "resource.label.source_resource_container": "projects/phc-store-apps" } } }, i need to get metric.label.store const storeSet = new Set(); context.panel.data.series.forEach((seriesObj) => { storeSet.add(seriesObj.meta.custom.labels["metric.label.store"]); console.log(seriesObj.meta.custom.labels["metric.label.store"]); }) console.log(storeSet); how do iuse the set to populate a variable in grafana

The code you provided retrieves the value of metric.label.store from a nested JSON structure within Grafana's context.panel.data object. Here’s a breakdown of what the code does:


1. Iterating through series objects

The code loops through the series array in context.panel.data.series, which presumably holds the data you are working with. Each series object in this array contains metadata, custom attributes, and labels.

context.panel.data.series.forEach((seriesObj) => {
  storeSet.add(seriesObj.meta.custom.labels["metric.label.store"]);
  console.log(seriesObj.meta.custom.labels["metric.label.store"]);
});
  • seriesObj is each item in the series array.
  • It accesses seriesObj.meta.custom.labels, where the key "metric.label.store" exists.
  • The value corresponding to "metric.label.store" is retrieved and added to a Set (storeSet), ensuring there are no duplicate values.
  • Each retrieved value is also logged to the console.

2. What does the Set store?

Set is a JavaScript data structure that holds unique values. After iterating through the series array, storeSet will contain all unique values for "metric.label.store" across all series objects.

console.log(storeSet);
  • Logs the final Set to the console. For example, if there are multiple series and each has "metric.label.store": "BLAH", the output could look like:
    Set { "3727", "3749", "3750" }
    

3. Populating a Variable in Grafana

Grafana allows JavaScript to be executed in its panels for advanced use cases (such as in the JavaScript interface, usually for "Data Manipulation/Transform"). The next steps depend on what you want to do with the retrieved value of storeSet:

Option 1: Use It Inside the Panel

If this is used in a JavaScript panel, you could assign storeSet's values to a variable to manipulate or render it inside the panel:

const uniqueStores = Array.from(storeSet); // Convert Set to an Array
console.log(uniqueStores); 
// You can use uniqueStores here as required, e.g., render them in HTML or tables

Option 2: Store It as a Template Variable

If you want to assign these values to Grafana's template variable, you cannot directly manipulate variables in Grafana through JavaScript code. Instead, you would need to:

  • Update your data source query or script so that the retrieved values (e.g., the Set) are returned as a result of the query.
  • Use those values for a template variable in Grafana.

Option 3: Render It in a Text Box or Table

Use the storeSet in a text panel or table to display the values, e.g.:

document.getElementById("result").innerHTML = [...storeSet].join(", ");

This requires using some HTML to create a container for displaying the result.


Summary

  • The given JavaScript retrieves all unique values of metric.label.store from context.panel.data and logs them.
  • The Set ensures the values are distinct.
  • To use storeSet values in Grafana:
    • Either modify the panel's rendering logic to display or utilize the data (e.g., render it in HTML).
    • Or ensure that your query delivery mechanism implements the logic so the Grafana UI can consume the value dynamically.
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node