Skip to content

Add support for dimensions to metrics#1447

Merged
tj-wazei merged 16 commits intodevelopfrom
metric-dimensions
Mar 26, 2026
Merged

Add support for dimensions to metrics#1447
tj-wazei merged 16 commits intodevelopfrom
metric-dimensions

Conversation

@tj-wazei
Copy link
Contributor

@tj-wazei tj-wazei commented Mar 23, 2026

Add custom dimension support to the Metrics class.

Changes:

  • New migration V17 adds a nullable dimensions TEXT column to metric_events & migrates all the postfixed metrics with dimensions
  • New count(String event, Map<String, String> dimensions) overload which serializes dimensions as JSON into the new dimensions column
  • Existing count(String event) is unchanged (delegates to the new overload with an empty map)

Example usage:

metrics.count("tag", Map.of(
  "method", "use", 
  "id", "learn"
)) ;

Migration:

local database before:
image

local database after:
image

@tj-wazei tj-wazei marked this pull request as ready for review March 23, 2026 20:17
@tj-wazei tj-wazei requested a review from a team as a code owner March 23, 2026 20:17
@christolis
Copy link
Member

christolis commented Mar 23, 2026 via email

@SquidXTV
Copy link
Member

Looking good at a first glance. What I am about to ask shouldn't really affect the functionality of what you created, but wouldn't the term "metadata" be a more fitting name than "dimensions"? It took me some time to figure out what "dimensions" stands for here, though this could just be me that has never seen it named that way. Just my two cents. ~Chris

See CloudWatch for example, it also uses the term dimension for metrics:
https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html#Dimension

@Zabuzard
Copy link
Member

its the idiomatic name. ive worked with google analytics and matomo for tracking stuff, they all called it dimensions.

but yeah, the code should have some javadoc to explain what it is, such as "to track additional info, e.g. metadata"

@christolis
Copy link
Member

christolis commented Mar 23, 2026 via email

christolis
christolis previously approved these changes Mar 23, 2026
@tj-wazei tj-wazei marked this pull request as draft March 23, 2026 20:40
@tj-wazei tj-wazei marked this pull request as ready for review March 24, 2026 11:02
@tj-wazei
Copy link
Contributor Author

tj-wazei commented Mar 24, 2026

Hi @Zabuzard, this is ready for another review. I have tested including tested in Grafana and we'll need to update the queries for some of the graphs e.g.

for the /tag (Total) graph, the query goes from:

Before:

SELECT
  substr(event, length('tag-') + 1) AS tag,
  COUNT(*) AS count
FROM metric_events
WHERE event LIKE 'tag-%'
GROUP BY tag
ORDER BY count DESC;

After:

SELECT
  json_extract(dimensions, '$.id') AS tag,
  COUNT(*) AS count
FROM metric_events
WHERE event = 'tag'
GROUP BY tag
ORDER BY count DESC;

for the Slash Commands (Total):

Before:

SELECT
  substr(event, length('slash-') + 1) AS slash,
  COUNT(*) AS count
FROM metric_events
WHERE event LIKE 'slash-%'
GROUP BY slash
ORDER BY count DESC;

After:

SELECT
  json_extract(dimension, '$.name') AS slash,
  COUNT(*) AS count
FROM metric_events
WHERE event = 'slash'
GROUP BY json_extract(dimension, '$.name')
ORDER BY count DESC;

The key thing here is that we're just using json_extract(dimension, '$.key') to extract the value which was previously postfixed. Super simple stuff

@sonarqubecloud
Copy link

@tj-wazei tj-wazei merged commit 0682838 into develop Mar 26, 2026
11 checks passed
@tj-wazei tj-wazei deleted the metric-dimensions branch March 26, 2026 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants