Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append missing _total suffix when registering Counter metrics. #177

Merged
merged 5 commits into from
Sep 13, 2022

Conversation

alexcsf
Copy link
Contributor

@alexcsf alexcsf commented Aug 26, 2022

As discussed in this jitsi-videobridge PR.

This ensures consistent metric names across the Prometheus registry,
the MetricsContainer and across the JSON and Prometheus exposition
formats available at /metrics. Also adds basic logging and fixes docs.

See: jitsi/jitsi-videobridge#1942
Fixes JSON output showing up in text/html when querying via browser.
@codecov
Copy link

codecov bot commented Aug 26, 2022

Codecov Report

Merging #177 (883a4f6) into master (1e384d9) will increase coverage by 0.45%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #177      +/-   ##
============================================
+ Coverage     22.78%   23.24%   +0.45%     
- Complexity      122      124       +2     
============================================
  Files            32       32              
  Lines          1119     1123       +4     
  Branches        101      102       +1     
============================================
+ Hits            255      261       +6     
+ Misses          846      845       -1     
+ Partials         18       17       -1     
Impacted Files Coverage Δ
...ain/java/org/jitsi/rest/prometheus/Prometheus.java 100.00% <ø> (ø)
.../main/kotlin/org/jitsi/metrics/MetricsContainer.kt 86.66% <100.00%> (+6.17%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1e384d9...883a4f6. Read the comment docs.

@@ -28,6 +30,7 @@ open class MetricsContainer @JvmOverloads constructor(
/** the registry used to register metrics */
val registry: CollectorRegistry = CollectorRegistry.defaultRegistry
) {
private val logger: Logger = LoggerImpl(javaClass.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use createLogger()

@@ -110,17 +113,23 @@ open class MetricsContainer @JvmOverloads constructor(
/** the optional initial value of the metric */
initialValue: Long = 0
): CounterMetric {
if (metrics.containsKey(name)) {
val newName = when (name.endsWith("_total")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use "when" instead of "if"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I was playing around and forgot to revert it to "if". Should I go ahead and change it to this?

val newName = if (name.endsWith("_total")) name else "${name}_total".also {
    logger.warn("Counter '$name' was renamed to '$it' to ensure consistent metric naming.")
}

Also, it currently logs whenever a metric is renamed (which currently applies to all of our CounterMetric). I think it's better to log this behavior even though it's the most common, as opposed to logging whenever a metric name is unchanged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. But make it INFO or DEBUG

logger.warn("Counter '$name' was renamed to '$it' to ensure consistent metric naming.")
}
}
if (metrics.containsKey(newName)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is another metric with "name" the Prometheus lib will throw, right? What do you think about handling that case here, so it's clear that it's expected and the warning message is clear

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it throws an IllegalArgumentException. So were you thinking of trying to return a newly-registered metric, and catch the exception and deal with it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I was thinking of catching the case and throwing our own exception. Just a suggestion I'm not sure this is a good idea or worth doing.

@bgrozev bgrozev marked this pull request as ready for review September 9, 2022 21:42
@bgrozev bgrozev merged commit e02029b into jitsi:master Sep 13, 2022
bgrozev added a commit to bgrozev/jitsi-videobridge that referenced this pull request Sep 15, 2022
* Append missing _total suffix when registering Counter metrics.
  (jitsi/jicoco#177)
* Disable all HTTP TRACE requests. (jitsi/jicoco#179)
bgrozev added a commit to bgrozev/jicofo that referenced this pull request Sep 15, 2022
bgrozev added a commit to jitsi/jitsi-videobridge that referenced this pull request Sep 15, 2022
* Append missing _total suffix when registering Counter metrics.
  (jitsi/jicoco#177)
* Disable all HTTP TRACE requests. (jitsi/jicoco#179)
bgrozev added a commit to jitsi/jicofo that referenced this pull request Sep 15, 2022
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.

2 participants