-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManualTests.scala
110 lines (94 loc) · 3.36 KB
/
ManualTests.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
/**
* Copyright 2021 Peter Nerg
*
* 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 org.dmonix.prometheus
import io.prometheus.client.{Counter, Gauge, Histogram, Summary}
import io.prometheus.client.exporter.HTTPServer
import java.util.concurrent.TimeUnit
import scala.concurrent.Future
import scala.concurrent.duration.DurationInt
/**
* Some manual tests to play around with metrics and see the results
*/
object ManualTests extends App with Implicits {
metricExample()
histogramExample()
gaugeExample()
summaryExample()
new HTTPServer(9095)
println("Go check metrics at http://localhost:9095")
Thread.currentThread().join()
private def metricExample(): Unit = {
import scala.concurrent.ExecutionContext.Implicits.global
val counter = Counter.build("counter", "Measures received jobs").register()
counter.incAfter{
//increases the counter immediately
"some result"
}
counter.incAfterAsync{
Future {
//fake some async job resulting in the counter being updated once the Future completes
Thread.sleep(5000)
"some result"
}
}
}
private def gaugeExample(): Unit = {
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
val gauge = Gauge.build("gauge", "Measuring ongoing jobs").register()
def worker():Runnable = new Runnable {
override def run(): Unit = {
gauge.measure {
//fake some job
Thread.sleep(5000)
}
}
}
val scheduler = Executors.newScheduledThreadPool(2)
scheduler.scheduleWithFixedDelay(worker(), 1, 1, TimeUnit.SECONDS)
scheduler.scheduleWithFixedDelay(worker(), 1, 4, TimeUnit.SECONDS)
}
private def histogramExample(): Unit = {
import org.dmonix.prometheus.TimeUnitImplicits.MILLISECONDS
import scala.concurrent.ExecutionContext.Implicits.global
val latencyHistogram = Histogram.build("latency_histogram", "Measuring some random latency").unit(TimeUnit.MILLISECONDS).exponentialBuckets(5, 3, 8).register()
latencyHistogram.record(5.millis)
latencyHistogram.record(10.millis)
latencyHistogram.record(100.millis)
latencyHistogram.record(1000.millis)
latencyHistogram.record(1.seconds)
latencyHistogram.measureAsync{
Future{
Thread.sleep(123)
}
}
}
private def summaryExample(): Unit = {
import org.dmonix.prometheus.TimeUnitImplicits.MILLISECONDS
import scala.concurrent.ExecutionContext.Implicits.global
val latencySummary = Summary.build("latency_summary", "Measuring some random latency").unit(TimeUnit.MILLISECONDS).register()
latencySummary.record(5.millis)
latencySummary.record(10.millis)
latencySummary.record(100.millis)
latencySummary.record(1000.millis)
latencySummary.record(1.seconds)
latencySummary.measureAsync{
Future{
Thread.sleep(123)
}
}
}
}