-
Notifications
You must be signed in to change notification settings - Fork 55
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
Changes from 1 commit
930aec4
7b24edf
561d38b
d46e1fe
883a4f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ package org.jitsi.metrics | |
|
||
import io.prometheus.client.CollectorRegistry | ||
import io.prometheus.client.exporter.common.TextFormat | ||
import org.jitsi.utils.logging2.Logger | ||
import org.jitsi.utils.logging2.LoggerImpl | ||
import org.json.simple.JSONObject | ||
import java.io.IOException | ||
import java.io.StringWriter | ||
|
@@ -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) | ||
|
||
/** | ||
* Namespace prefix added to all metrics. | ||
|
@@ -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")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use "when" instead of "if"? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. But make it INFO or DEBUG |
||
true -> name | ||
false -> "${name}_total".also { | ||
logger.warn("Counter '$name' was renamed to '$it' to ensure consistent metric naming.") | ||
} | ||
} | ||
if (metrics.containsKey(newName)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
if (checkForNameConflicts) { | ||
throw RuntimeException("Could not register metric '$name'. A metric with that name already exists.") | ||
throw RuntimeException("Could not register metric '$newName'. A metric with that name already exists.") | ||
} | ||
return metrics[name] as CounterMetric | ||
return metrics[newName] as CounterMetric | ||
} | ||
return CounterMetric(name, help, namespace, initialValue).apply { metrics[name] = register(registry) } | ||
return CounterMetric(newName, help, namespace, initialValue).apply { metrics[newName] = register(registry) } | ||
} | ||
|
||
/** | ||
* Creates and registers a [CounterMetric] with the given [name], [help] string and optional [initialValue]. | ||
* Creates and registers a [LongGaugeMetric] with the given [name], [help] string and optional [initialValue]. | ||
* | ||
* Throws an exception if a metric with the same name but a different type exists. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use createLogger()