Skip to content

Commit 785ff40

Browse files
committed
Update to registry output formats
1 parent 9667fac commit 785ff40

File tree

7 files changed

+80
-74
lines changed

7 files changed

+80
-74
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.lyranthe.prometheus.client
2+
3+
import org.lyranthe.prometheus.client.registry._
4+
5+
trait Registry {
6+
def unsafeRegister(c: MetricFamily): Unit
7+
def collect(): Iterator[RegistryMetrics]
8+
9+
def output(format: RegistryFormat): Iterator[Array[Byte]] =
10+
format.output(collect)
11+
12+
def outputText: String =
13+
TextFormat.output(collect).map(new String(_)).mkString
14+
}

client/src/main/scala/org/lyranthe/prometheus/client/registry/Registry.scala

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package org.lyranthe.prometheus.client.registry
22

33
trait RegistryFormat {
4-
type Out
5-
def output(values: => Iterator[RegistryMetrics]): Iterator[Out]
6-
}
7-
8-
object RegistryFormat {
9-
type Aux[Out0] = RegistryFormat { type Out = Out0 }
4+
def contentType: String
5+
def output(values: => Iterator[RegistryMetrics]): Iterator[Array[Byte]]
106
}

client/src/main/scala/org/lyranthe/prometheus/client/registry/TextFormat.scala

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package org.lyranthe.prometheus.client.registry
33
import org.lyranthe.prometheus.client._
44

55
object TextFormat extends RegistryFormat {
6-
type Out = String
6+
override val contentType = "text/plain; version=0.0.4"
77

88
def prometheusDoubleFormat(d: Double): String = {
99
if (d == Double.PositiveInfinity)
@@ -16,7 +16,7 @@ object TextFormat extends RegistryFormat {
1616
d.toString
1717
}
1818

19-
def output(values: => Iterator[RegistryMetrics]): Iterator[String] = {
19+
def output(values: => Iterator[RegistryMetrics]): Iterator[Array[Byte]] = {
2020
def labelsToString(labels: List[(LabelName, String)]) = {
2121
if (labels.isEmpty)
2222
""
@@ -48,7 +48,7 @@ object TextFormat extends RegistryFormat {
4848
sb.append(s"${metric.name.name}_sum${labelStr} ${sampleSum}\n")
4949
}
5050
}
51-
sb.toString
51+
sb.toString.getBytes
5252
}
5353
}
5454
}

doc/Guide.md

+49-47
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ To create a simple counter:
8383

8484
```scala
8585
scala> val totalRequests = Counter(metric"total_requests", "Total requests").labels()
86-
totalRequests: org.lyranthe.prometheus.client.internal.counter.Counter0 = Counter(MetricName(total_requests))()
86+
totalRequests: org.lyranthe.prometheus.client.counter.Counter0 = Counter(MetricName(total_requests))()
8787
```
8888

8989
The resulting counter has a metric name `total_requests`, a help message with the contents,
@@ -102,7 +102,7 @@ the `.labels` method:
102102

103103
```scala
104104
scala> val totalErrors = Counter(metric"total_errors", "Total errors").labels(label"code")
105-
totalErrors: org.lyranthe.prometheus.client.internal.counter.Counter1 = Counter1(MetricName(total_errors),Total errors,List(LabelName(code)))
105+
totalErrors: org.lyranthe.prometheus.client.counter.Counter1 = Counter1(MetricName(total_errors),Total errors,List(LabelName(code)))
106106
```
107107

108108
To increment a counter with an error code of "404", one might
@@ -119,7 +119,7 @@ number of values when using this counter:
119119

120120
```scala
121121
scala> totalErrors.labelValues("404", "/path").inc()
122-
<console>:17: error: too many arguments for method labelValues: (labelValue1: String)org.lyranthe.prometheus.client.internal.counter.LabelledCounter
122+
<console>:17: error: too many arguments for method labelValues: (labelValue1: String)org.lyranthe.prometheus.client.counter.LabelledCounter
123123
totalErrors.labelValues("404", "/path").inc()
124124
^
125125
```
@@ -134,30 +134,36 @@ You can create a registry with a default implementation with:
134134

135135
```scala
136136
scala> implicit val defaultRegistry = DefaultRegistry()
137-
defaultRegistry: org.lyranthe.prometheus.client.DefaultRegistry =
137+
defaultRegistry: org.lyranthe.prometheus.client.DefaultRegistry = org.lyranthe.prometheus.client.DefaultRegistry@252a4792
138138
```
139139

140140
```scala
141141
scala> implicit val histogramBuckets = HistogramBuckets(1, 2, 5, 10, 20, 50, 100)
142142
histogramBuckets: org.lyranthe.prometheus.client.HistogramBuckets{val buckets: List[Double]} = HistogramBuckets(1.0,2.0,5.0,10.0,20.0,50.0,100.0,Infinity)
143143

144144
scala> val activeRequests = Gauge(metric"active_requests", "Active requests").labels().unsafeRegister
145-
activeRequests: org.lyranthe.prometheus.client.internal.gauge.Gauge0 = Counter(MetricName(active_requests))()
145+
activeRequests: org.lyranthe.prometheus.client.gauge.Gauge0 = Counter(MetricName(active_requests))()
146146

147147
scala> val numErrors = Counter(metric"num_errors", "Total errors").labels().unsafeRegister
148-
numErrors: org.lyranthe.prometheus.client.internal.counter.Counter0 = Counter(MetricName(num_errors))()
148+
numErrors: org.lyranthe.prometheus.client.counter.Counter0 = Counter(MetricName(num_errors))()
149149

150150
scala> val requestLatency = Histogram(metric"request_latency", "Request latency").labels(label"path").unsafeRegister
151-
requestLatency: org.lyranthe.prometheus.client.internal.histogram.Histogram1 = Histogram1(MetricName(request_latency),Request latency,List(LabelName(path)),List(1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, Infinity))
151+
requestLatency: org.lyranthe.prometheus.client.histogram.Histogram1 = Histogram1(MetricName(request_latency),Request latency,List(LabelName(path)),List(1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, Infinity))
152152

153153
scala> activeRequests.set(50)
154154

155155
scala> numErrors.inc
156156

157157
scala> requestLatency.labelValues("/home").observe(17)
158158

159-
scala> implicitly[Registry]
160-
res10: org.lyranthe.prometheus.client.Registry =
159+
scala> implicitly[Registry].outputText
160+
res10: String =
161+
"# HELP active_requests Active requests
162+
# TYPE active_requests gauge
163+
active_requests 50.0
164+
# HELP num_errors Total errors
165+
# TYPE num_errors counter
166+
num_errors 1.0
161167
# HELP request_latency Request latency
162168
# TYPE request_latency histogram
163169
request_latency_bucket{le="1.0",path="/home"} 0
@@ -170,12 +176,7 @@ request_latency_bucket{le="100.0",path="/home"} 1
170176
request_latency_bucket{le="+Inf",path="/home"} 1
171177
request_latency_count{path="/home"} 1
172178
request_latency_sum{path="/home"} 17.0
173-
# HELP num_errors Total errors
174-
# TYPE num_errors counter
175-
num_errors 1.0
176-
# HELP active_requests Active requests
177-
# TYPE active_requests gauge
178-
active_requests 50.0
179+
"
179180
```
180181

181182
## Using with FS2 Task (WIP)
@@ -198,14 +199,14 @@ import org.lyranthe.prometheus.client.fs2_syntax._
198199
Then the method `timeSuccess` can be used to capture the duration of the task (in seconds):
199200

200201
```scala
201-
implicit val registry = DefaultRegistry()
202+
implicit val defaultRegistry = DefaultRegistry()
202203
```
203204
```scala
204205
scala> implicit val histogramBuckets = HistogramBuckets(0.02, 0.05, 0.1, 0.2, 0.5, 1.0)
205206
histogramBuckets: org.lyranthe.prometheus.client.HistogramBuckets{val buckets: List[Double]} = HistogramBuckets(0.02,0.05,0.1,0.2,0.5,1.0,Infinity)
206207

207208
scala> val requestLatency = Histogram(metric"request_latency", "Request latency").labels(label"path").unsafeRegister
208-
requestLatency: org.lyranthe.prometheus.client.internal.histogram.Histogram1 = Histogram1(MetricName(request_latency),Request latency,List(LabelName(path)),List(0.02, 0.05, 0.1, 0.2, 0.5, 1.0, Infinity))
209+
requestLatency: org.lyranthe.prometheus.client.histogram.Histogram1 = Histogram1(MetricName(request_latency),Request latency,List(LabelName(path)),List(0.02, 0.05, 0.1, 0.2, 0.5, 1.0, Infinity))
209210

210211
scala> val mySleepyTask = Task.delay(Thread.sleep(scala.util.Random.nextInt(800)))
211212
mySleepyTask: fs2.Task[Unit] = Task
@@ -215,27 +216,28 @@ myTimedSleepyTask: fs2.Task[Unit] = Task
215216

216217
scala> for (i <- Range(1, 10)) myTimedSleepyTask.unsafeRun
217218

218-
scala> implicitly[Registry]
219-
res1: org.lyranthe.prometheus.client.Registry =
220-
# HELP request_latency Request latency
219+
scala> implicitly[Registry].outputText
220+
res1: String =
221+
"# HELP request_latency Request latency
221222
# TYPE request_latency histogram
222223
request_latency_bucket{le="0.02",path="/home"} 0
223224
request_latency_bucket{le="0.05",path="/home"} 0
224225
request_latency_bucket{le="0.1",path="/home"} 0
225-
request_latency_bucket{le="0.2",path="/home"} 4
226-
request_latency_bucket{le="0.5",path="/home"} 6
226+
request_latency_bucket{le="0.2",path="/home"} 1
227+
request_latency_bucket{le="0.5",path="/home"} 5
227228
request_latency_bucket{le="1.0",path="/home"} 9
228229
request_latency_bucket{le="+Inf",path="/home"} 9
229230
request_latency_count{path="/home"} 9
230-
request_latency_sum{path="/home"} 3.398811496
231+
request_latency_sum{path="/home"} 3.8517136149999995
232+
"
231233
```
232234

233235
## Exposing JMX Statistics
234236

235237
Some JVM statistics can be exposed with:
236238

237239
```scala
238-
implicit val registry = DefaultRegistry()
240+
implicit val defaultRegistry = DefaultRegistry()
239241
```
240242
```scala
241243
scala> import fs2._
@@ -246,34 +248,34 @@ import org.lyranthe.prometheus.client._
246248

247249
scala> jmx.unsafeRegister
248250

249-
scala> println(implicitly[Registry])
250-
# HELP jvm_threads JVM Thread Information
251-
# TYPE jvm_threads gauge
252-
jvm_threads{type="non-daemon"} 11.0
253-
jvm_threads{type="daemon"} 4.0
254-
# HELP jvm_start_time JVM Start Time
255-
# TYPE jvm_start_time gauge
256-
jvm_start_time 1.477244444947E9
251+
scala> println(implicitly[Registry].outputText)
252+
# HELP jvm_classloader JVM Classloader statistics
253+
# TYPE jvm_classloader gauge
254+
jvm_classloader{classloader="loaded"} 15479.0
255+
jvm_classloader{classloader="total-loaded"} 15554.0
256+
jvm_classloader{classloader="unloaded"} 75.0
257+
# HELP jvm_gc_stats JVM Garbage Collector Statistics
258+
# TYPE jvm_gc_stats gauge
259+
jvm_gc_stats{name="PS Scavenge",type="count"} 10.0
260+
jvm_gc_stats{name="PS Scavenge",type="time"} 0.217
261+
jvm_gc_stats{name="PS MarkSweep",type="count"} 5.0
262+
jvm_gc_stats{name="PS MarkSweep",type="time"} 0.406
257263
# HELP jvm_memory_usage JVM Memory Usage
258264
# TYPE jvm_memory_usage gauge
259-
jvm_memory_usage{region="heap",type="committed"} 1.038614528E9
265+
jvm_memory_usage{region="heap",type="committed"} 1.042808832E9
260266
jvm_memory_usage{region="heap",type="init"} 5.36870912E8
261267
jvm_memory_usage{region="heap",type="max"} 1.431830528E9
262-
jvm_memory_usage{region="heap",type="used"} 2.84849344E8
263-
jvm_memory_usage{region="non-heap",type="committed"} 1.48914176E8
268+
jvm_memory_usage{region="heap",type="used"} 4.70523048E8
269+
jvm_memory_usage{region="non-heap",type="committed"} 1.67591936E8
264270
jvm_memory_usage{region="non-heap",type="init"} 2555904.0
265271
jvm_memory_usage{region="non-heap",type="max"} -1.0
266-
jvm_memory_usage{region="non-heap",type="used"} 1.47149728E8
267-
# HELP jvm_gc_stats JVM Garbage Collector Statistics
268-
# TYPE jvm_gc_stats gauge
269-
jvm_gc_stats{name="PS Scavenge",type="count"} 9.0
270-
jvm_gc_stats{name="PS Scavenge",type="time"} 0.176
271-
jvm_gc_stats{name="PS MarkSweep",type="count"} 5.0
272-
jvm_gc_stats{name="PS MarkSweep",type="time"} 0.325
273-
# HELP jvm_classloader JVM Classloader statistics
274-
# TYPE jvm_classloader gauge
275-
jvm_classloader{classloader="loaded"} 15230.0
276-
jvm_classloader{classloader="total-loaded"} 15305.0
277-
jvm_classloader{classloader="unloaded"} 75.0
272+
jvm_memory_usage{region="non-heap",type="used"} 1.65366072E8
273+
# HELP jvm_start_time JVM Start Time
274+
# TYPE jvm_start_time gauge
275+
jvm_start_time 1.477262878978E9
276+
# HELP jvm_threads JVM Thread Information
277+
# TYPE jvm_threads gauge
278+
jvm_threads{type="non-daemon"} 11.0
279+
jvm_threads{type="daemon"} 4.0
278280

279281
```

doc/src/Guide.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ val requestLatency = Histogram(metric"request_latency", "Request latency").label
127127
activeRequests.set(50)
128128
numErrors.inc
129129
requestLatency.labelValues("/home").observe(17)
130-
defaultRegistry
130+
implicitly[Registry].outputText
131131
```
132132

133133
## Using with FS2 Task (WIP)
@@ -145,7 +145,7 @@ import org.lyranthe.prometheus.client.fs2_syntax._
145145
Then the method `timeSuccess` can be used to capture the duration of the task (in seconds):
146146

147147
```tut:silent
148-
implicit val registry = DefaultRegistry()
148+
implicit val defaultRegistry = DefaultRegistry()
149149
```
150150
```tut
151151
implicit val histogramBuckets = HistogramBuckets(0.02, 0.05, 0.1, 0.2, 0.5, 1.0)
@@ -156,7 +156,7 @@ val myTimedSleepyTask = mySleepyTask.timeSuccess(requestLatency.labelValues("/ho
156156
157157
for (i <- Range(1, 10)) myTimedSleepyTask.unsafeRun
158158
159-
registry
159+
implicitly[Registry].outputText
160160
```
161161

162162
## Exposing JMX Statistics
@@ -172,5 +172,5 @@ import org.lyranthe.prometheus.client._
172172
173173
jmx.unsafeRegister
174174
175-
println(defaultRegistry)
175+
println(implicitly[Registry].outputText)
176176
```

protobuf/src/main/scala/Protobuf.scala protobuf/src/main/scala/ProtoFormat.scala

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package org.lyranthe.prometheus.client
1+
package org.lyranthe.prometheus.client.registry
22

33
import com.google.protobuf.CodedOutputStream
44
import io.prometheus.client.{Metrics => PB}
5-
import org.lyranthe.prometheus.client.registry._
5+
import org.lyranthe.prometheus.client._
66

7-
object Protobuf {
7+
object ProtoFormat extends RegistryFormat {
8+
override val contentType =
9+
"application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited"
810

911
def labelPairs(labels: List[(LabelName, String)]): List[PB.LabelPair] = {
1012
labels.map(lp =>
@@ -48,10 +50,11 @@ object Protobuf {
4850
}
4951
}
5052

51-
def collectProtobuf(implicit registry: Registry): Iterator[Array[Byte]] = {
53+
override def output(
54+
values: => Iterator[RegistryMetrics]): Iterator[Array[Byte]] = {
5255
import scala.collection.JavaConverters._
5356

54-
registry.collect() map { metric =>
57+
values map { metric =>
5558
val proto = PB.MetricFamily.newBuilder
5659
.setName(metric.name.name)
5760
.setHelp(metric.help)

0 commit comments

Comments
 (0)