# How can I group Prometheus metrics based on dynamic time windows?

**URL:** <https://community.grafana.com/t/how-can-i-group-prometheus-metrics-based-on-dynamic-time-windows/75741>\
**Category:** Prometheus\
**Tags:** templating\
**Created:** [November 9, 2022, 5:50pm UTC](https://community.grafana.com/t/how-can-i-group-prometheus-metrics-based-on-dynamic-time-windows/75741 "2022-11-09T17:50:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackcarey](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/jackcarey/32/27406_2.png) [@jackcarey](https://community.grafana.com/u/jackcarey)\
**Post date:** [November 9, 2022, 5:50pm UTC](https://community.grafana.com/t/how-can-i-group-prometheus-metrics-based-on-dynamic-time-windows/75741/1 "2022-11-09T17:50:30Z")

</div>

Hello, I’m new to Grafana so bare with me while I explain this as I think it’s a complicated ask.

- **What Grafana version and what operating system are you using?**  
9.2.4
- **What are you trying to achieve?**  
I am recording the CPU and memory usage metrics of a Node.js instance to Prometheus and the timestamps of when some events start and end. The timestamp events currently look like this in Prometheus (though they don’t always appear in order):

```javascript
{node_hook_event_total{eventType="start", instance="localhost:9464", job="NodeJS", type="foo", pid="10292", timestamp="1468003781002"}
{node_hook_event_total{eventType="finish", instance="localhost:9464", job="NodeJS", type="foo", pid="10292", timestamp="1468003799009"}
{node_hook_event_total{eventType="start", instance="localhost:9464", job="NodeJS", type="bar", pid="10292", timestamp="1568003781002"}
{node_hook_event_total{eventType="finish", instance="localhost:9464", job="NodeJS", type="bar", pid="10292", timestamp="1568003799009"}
{node_hook_event_total{eventType="start", instance="localhost:9464", job="NodeJS", type="foo", pid="10292", timestamp="1668003781002"}
{node_hook_event_total{eventType="finish", instance="localhost:9464", job="NodeJS", type="foo", pid="10292", timestamp="1668003799009"}

```

and examples of the CPU & memory events look like this:

```javascript
process_cpu_user_seconds_total{instance="localhost:8000", job="NodeJS"}

```

```javascript
nodejs_external_memory_bytes{instance="localhost:8000", job="NodeRED"} | 2022-11-09 20:38:57 | 85170246

```

```javascript
nodejs_heap_space_size_used_bytes{instance="localhost:8000", job="NodeJS", space="code"}
nodejs_heap_space_size_used_bytes{instance="localhost:8000", job="NodeJS", space="code_large_object"}
nodejs_heap_space_size_used_bytes{instance="localhost:8000", job="NodeJS", space="large_object"}
nodejs_heap_space_size_used_bytes{instance="localhost:8000", job="NodeJS", space="map"}
nodejs_heap_space_size_used_bytes{instance="localhost:8000", job="NodeJS", space="new"}
nodejs_heap_space_size_used_bytes{instance="localhost:8000", job="NodeJS", space="new_large_object"}
nodejs_heap_space_size_used_bytes{instance="localhost:8000", job="NodeJS", space="old"}
nodejs_heap_space_size_used_bytes{instance="localhost:8000", job="NodeJS", space="read_only"}

```

I’m looking to create a window of time that I can use in other queries for grouping the CPU and memory metrics, so in the example above, there would be three windows of time to represent three “executions” (1 for `bar`, 2 for `foo`), with CPU and memory values occurring within each. My intention is then to be able to extract information about each execution on it’s own, or about all executions for a given `eventType`.

- **How are you trying to achieve it?**

So far, I’m using the ‘Transformations \> Group By’ to cluster the timestamp events.

- **What happened?**

- I’m unsure how to bring the heap and CPU measures into the graph.
- I’ve looked at query variables, and I could get a maximum and minimum for the `start` and `finish` timestamps, but I don’t know how to get the _next_ `finish` event after a given `start`.

- **Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.**

No.

- **Did you follow any online instructions? If so, what is the URL?**

- [Query and transform data | Grafana documentation](https://grafana.com/docs/grafana/latest/panels-visualizations/query-transform-data/)
- [Add a query variable | Grafana documentation](https://grafana.com/docs/grafana/v9.0/variables/variable-types/add-query-variable/)

I’m not expecting anyone to give me the ins and outs of Grafana, but I’ve struggled to find examples of this so anything to nudge me in the right direction would be very much appreciated! Thanks
