Creating log-based metric to count unique field values in logs

Hey guys, I have some logs stored in loki, and the log lines have the following fields / labels:

  • node_id
  • block_height
  • block_id

The logs are aggregated coming from multiple instances of a service (node_id is the instance id).

For example, if I make the following query in the log browser:

{app="execution",network="mainnet16"} | json | block_height="24439205"

then I might get the following results:

2022-02-19 17:50:37	
2022-02-19 17:50:35	
2022-02-19 17:50:35	
2022-02-19 17:50:35	

I’d like to achieve the following:

  • For a given block_height, compute the number of distinct block_id values. In the example above, the value would be 1 because all four log lines have the same block_id value.
  • Create a dashboard for this value, where block_height is on the x-axis, and the value as computed above is the y-axis.

Can anyone point me in the right direction on how to do this, or know if this is even possible? At the moment, I’m just trying to simply compute the number of distinct block_id values for a single block_height, and I can’t seem to even get that working. Thank you very much for the help.