@@ -11,5 +11,111 @@ There are two roles in the Metrics API:
11
11
* Metrics backends: this is infrastructure code that implements the metrics APIs used by metrics
12
12
producers. Our current only backend is Prometheus. Backends can be in-memory only (for testing
13
13
and development) or they can integrate with a metrics service.
14
-
14
+
15
+ ## How to Use
16
+
17
+ The ` misk-metrics ` module provides a simple interface for creating and using metrics in your application. You can create counters, gauges, and histograms.
18
+
19
+ ``` kotlin
20
+ class MyService @Inject constructor(private val metrics : Metrics ) {
21
+ // Create a counter with no labels
22
+ private val simpleCounter = metrics.counter(
23
+ name = " my_simple_counter" ,
24
+ help = " Counts the number of operations performed"
25
+ )
26
+
27
+ // Create a counter with labels
28
+ private val labeledCounter = metrics.counter(
29
+ name = " my_labeled_counter" ,
30
+ help = " Counts operations by type and status" ,
31
+ labelNames = listOf (" type" , " status" )
32
+ )
33
+
34
+ // Create a gauge
35
+ private val gauge = metrics.gauge(
36
+ name = " my_gauge" ,
37
+ help = " Shows the current number of active operations" ,
38
+ labelNames = listOf (" type" )
39
+ )
40
+
41
+ // Create a histogram
42
+ private val histogram = metrics.histogram(
43
+ name = " my_operation_duration" ,
44
+ help = " Measures the duration of operations in milliseconds" ,
45
+ labelNames = listOf (" operation_type" )
46
+ )
47
+
48
+ fun performOperation (type : String ) {
49
+ // Increment a simple counter
50
+ simpleCounter.inc()
51
+
52
+ // Increment a labeled counter
53
+ labeledCounter.labels(type, " success" ).inc()
54
+
55
+ // Set a gauge value
56
+ gauge.labels(type).set(42.0 )
57
+
58
+ // Record a histogram value
59
+ val startTime = System .currentTimeMillis()
60
+ try {
61
+ // Do work...
62
+ } finally {
63
+ val duration = System .currentTimeMillis() - startTime
64
+ histogram.labels(type).observe(duration.toDouble())
65
+ }
66
+ }
67
+ }
68
+ ```
69
+
70
+ ### How to Test
71
+
72
+ To test your metrics, add the following dependency to your Gradle build file:
73
+
74
+ ``` kotlin
75
+ testImplementation(testFixtures(libs.miskMetrics))
76
+ ```
77
+
78
+ In your tests, you can inject the ` CollectorRegistry ` to verify that your metrics are being recorded correctly:
79
+
80
+ ``` kotlin
81
+ @MiskTest
82
+ class MyServiceTest {
83
+
84
+ @MiskTestModule val module = MiskTestingServiceModule (installFakeMetrics = true )
85
+
86
+ @Inject private lateinit var collectorRegistry: CollectorRegistry
87
+ @Inject private lateinit var myService: MyService
88
+
89
+ @Test
90
+ fun testMetrics () {
91
+ // Perform operations that should record metrics
92
+ myService.performOperation(" read" )
93
+
94
+ // Test counter values
95
+ assertThat(collectorRegistry.get(" my_simple_counter" )).isEqualTo(1.0 )
96
+ assertThat(collectorRegistry.get(" my_labeled_counter" , " type" to " read" , " status" to " success" )).isEqualTo(1.0 )
97
+
98
+ // Test gauge values
99
+ assertThat(collectorRegistry.get(" my_gauge" , " type" to " read" )).isEqualTo(42.0 )
100
+
101
+ // Test histogram values
102
+ // Check count of observations
103
+ assertThat(collectorRegistry.summaryCount(" my_operation_duration" , " operation_type" to " read" )).isEqualTo(1.0 )
104
+
105
+ // For histograms, you can also check percentiles
106
+ val p99 = collectorRegistry.summaryP99(" my_operation_duration" , " operation_type" to " read" )
107
+ assertThat(p99).isNotNull()
108
+ }
109
+ }
110
+ ```
111
+
112
+ The ` CollectorRegistry ` provides several extension methods for testing different types of metrics:
113
+
114
+ - ` get(name, vararg labels) ` : Returns the value of a counter or gauge
115
+ - ` summaryCount(name, vararg labels) ` : Returns the number of observations recorded in a histogram
116
+ - ` summaryP99(name, vararg labels) ` : Returns the 99th percentile value of a histogram
117
+ - ` summaryMean(name, vararg labels) ` : Returns the mean value of a histogram
118
+ - ` summaryP50(name, vararg labels) ` : Returns the median (50th percentile) value of a histogram
119
+
120
+ These extension methods make it easy to test that your metrics are being recorded correctly.
15
121
0 commit comments