Skip to content

Commit

Permalink
added check for metrics' name suffix similar to the one found in prom…
Browse files Browse the repository at this point in the history
…etheus-metrics-core (#340)
  • Loading branch information
dpevunov-cp authored Apr 25, 2024
1 parent 3b1ecf0 commit 7104591
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ lazy val site = project.in(file("site"))
.dependsOn(core)


val prometheusV = "1.1.0"
val prometheusV = "1.2.1"
val catsV = "2.9.0"
val catsEffectV = "3.4.8"
val shapelessV = "2.3.9"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,26 @@ trait NameCommons {

private val reg = "([a-zA-Z_:][a-zA-Z0-9_:]*)".r

private val forbiddenSuffixes = List(
"_total",
"_created",
"_bucket",
"_info",
".total",
".created",
".bucket",
".info"
)

def impl(s: String): Either[IllegalArgumentException, Name] = s match {
case reg(string) => Either.right(new Name(string))
case reg(string) =>
if (forbiddenSuffixes.exists(string.endsWith))
Left[IllegalArgumentException, Name](
new IllegalArgumentException(
s"Input String - $s end with one of the forbidden suffixes(${forbiddenSuffixes.mkString(",")})"
)
)
else Either.right(new Name(string))
case _ => Either.left(
new IllegalArgumentException(
s"Input String - $s does not match regex - ([a-zA-Z_:][a-zA-Z0-9_:]*)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,14 @@ class NameSpec extends munit.CatsEffectSuite {
val goodSuffix = Name.Suffix("0asdfa")
val errors3 = compileErrors("""Name.Suffix("^")""")
assert(errors3.nonEmpty)

assert(compileErrors("""Name("metric_total")""").nonEmpty)
assert(compileErrors("""Name("metric_created")""").nonEmpty)
assert(compileErrors("""Name("metric_bucket")""").nonEmpty)
assert(compileErrors("""Name("metric_info")""").nonEmpty)
assert(compileErrors("""Name("metric.total")""").nonEmpty)
assert(compileErrors("""Name("metric.created")""").nonEmpty)
assert(compileErrors("""Name("metric.bucket")""").nonEmpty)
assert(compileErrors("""Name("metric.info")""").nonEmpty)
}
}
10 changes: 5 additions & 5 deletions docs/docs/05-Counter.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Counter metric, to track counts, running totals, or events.

If your use case can go up or down consider using a `Gauge` instead.
Use the `rate()` function in Prometheus to calculate the rate of increase of a Counter.
By convention, the names of Counters are suffixed by `_total`.
By convention, the names of Counters are suffixed by `_total`. This suffix is added to metric's name by underlying prometheus-metrics library, do not add it yourself.

A `Counter` is a cumulative metric that represents a single monotonically increasing counter whose value can only increase or be reset to zero on restart. For example, you can use a counter to represent the number of requests served, tasks completed, or errors.

Expand All @@ -27,12 +27,12 @@ val noLabelsExample = {
pr <- PrometheusRegistry.build[IO]
successCounter <- Counter.noLabels(
pr,
Name("example_success_total"),
Name("example_success"),
"Example Counter of Success"
)
failureCounter <- Counter.noLabels(
pr,
Name("example_failure_total"),
Name("example_failure"),
"Example Counter of Failure"
)
_ <- IO(println("Action Here")).guaranteeCase{
Expand All @@ -54,7 +54,7 @@ val labelledExample = {
pr <- PrometheusRegistry.build[IO]
counter <- Counter.labelled(
pr,
Name("example_total"),
Name("example"),
"Example Counter",
Sized(Label("foo")),
{s: String => Sized(s)}
Expand Down Expand Up @@ -95,7 +95,7 @@ val fooAgebraExample = {
pr <- PrometheusRegistry.build[IO]
counter <- Counter.labelled(
pr,
Name("example_total"),
Name("example"),
"Example Counter",
Sized(Label("foo")),
fooLabel
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import cats.effect.unsafe.implicits.global
val noLabelsCounterExample = {
for {
pr <- PrometheusRegistry.build[IO]
counter <- Counter.noLabels(pr, Name("counter_total"), "Example Counter")
counter <- Counter.noLabels(pr, Name("counter"), "Example Counter")
_ <- counter.inc
currentMetrics <- pr.write004
} yield currentMetrics
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.6.2
sbt.version=1.9.9

0 comments on commit 7104591

Please sign in to comment.