-
Notifications
You must be signed in to change notification settings - Fork 2
/
Counter.scala
126 lines (93 loc) · 4.31 KB
/
Counter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright 2022 Permutive
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package prometheus4cats
import java.util.regex.Pattern
import cats.{Applicative, Contravariant, ~>}
sealed abstract class Counter[F[_], -A] extends Metric[A] { self =>
def inc: F[Unit]
def inc(n: A): F[Unit]
def contramap[B](f: B => A): Counter[F, B] = new Counter[F, B] {
override def inc: F[Unit] = self.inc
override def inc(n: B): F[Unit] = self.inc(f(n))
}
final def mapK[G[_]](fk: F ~> G): Counter[G, A] = new Counter[G, A] {
override def inc: G[Unit] = fk(self.inc)
override def inc(n: A): G[Unit] = fk(self.inc(n))
}
}
object Counter {
/** Refined value class for a counter name that has been parsed from a string
*/
final class Name private (val value: String) extends AnyVal with internal.Refined.Value[String] {
override def toString: String = s"""Counter.Name("$value")"""
}
object Name extends internal.Refined.StringRegexRefinement[Name] with internal.CounterNameFromStringLiteral {
override protected val regex: Pattern = "^[a-zA-Z_:][a-zA-Z0-9_:]*_total$".r.pattern
override protected def make(a: String): Name = new Name(a)
}
implicit def catsInstances[F[_]]: Contravariant[Counter[F, *]] = new Contravariant[Counter[F, *]] {
override def contramap[A, B](fa: Counter[F, A])(f: B => A): Counter[F, B] = fa.contramap(f)
}
def make[F[_], A](default: A, _inc: A => F[Unit]): Counter[F, A] = new Counter[F, A] {
override def inc: F[Unit] = inc(default)
override def inc(n: A): F[Unit] = _inc(n)
}
def make[F[_], A](_inc: A => F[Unit])(implicit A: Numeric[A]): Counter[F, A] = make(A.one, _inc)
def noop[F[_]: Applicative, A]: Counter[F, A] = new Counter[F, A] {
override def inc: F[Unit] = Applicative[F].unit
override def inc(n: A): F[Unit] = Applicative[F].unit
}
sealed abstract class Labelled[F[_], -A, -B] extends Metric[A] with Metric.Labelled[B] {
self =>
def inc(labels: B): F[Unit]
def inc(n: A, labels: B): F[Unit]
def contramap[C](f: C => A): Labelled[F, C, B] = new Labelled[F, C, B] {
override def inc(labels: B): F[Unit] = self.inc(labels)
override def inc(n: C, labels: B): F[Unit] = self.inc(f(n), labels)
}
def contramapLabels[C](f: C => B): Labelled[F, A, C] = new Labelled[F, A, C] {
override def inc(labels: C): F[Unit] = self.inc(f(labels))
override def inc(n: A, labels: C): F[Unit] = self.inc(n, f(labels))
}
final def mapK[G[_]](fk: F ~> G): Counter.Labelled[G, A, B] =
new Labelled[G, A, B] {
override def inc(labels: B): G[Unit] = fk(self.inc(labels))
override def inc(n: A, labels: B): G[Unit] = fk(
self.inc(n, labels)
)
}
}
object Labelled {
implicit def catsInstances[F[_], C]: Contravariant[Labelled[F, *, C]] =
new Contravariant[Labelled[F, *, C]] {
override def contramap[A, B](fa: Labelled[F, A, C])(f: B => A): Labelled[F, B, C] = fa.contramap(f)
}
implicit def labelsContravariant[F[_], C]: LabelsContravariant[Labelled[F, C, *]] =
new LabelsContravariant[Labelled[F, C, *]] {
override def contramapLabels[A, B](fa: Labelled[F, C, A])(f: B => A): Labelled[F, C, B] = fa.contramapLabels(f)
}
def make[F[_], A, B](default: A, _inc: (A, B) => F[Unit]): Labelled[F, A, B] =
new Labelled[F, A, B] {
override def inc(labels: B): F[Unit] = inc(default, labels)
override def inc(n: A, labels: B): F[Unit] = _inc(n, labels)
}
def make[F[_], A, B](_inc: (A, B) => F[Unit])(implicit A: Numeric[A]): Labelled[F, A, B] = make(A.one, _inc)
def noop[F[_]: Applicative, A, B]: Labelled[F, A, B] = new Labelled[F, A, B] {
override def inc(labels: B): F[Unit] = Applicative[F].unit
override def inc(n: A, labels: B): F[Unit] = Applicative[F].unit
}
}
}