-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathMetrics.java
More file actions
128 lines (113 loc) · 5.08 KB
/
Metrics.java
File metadata and controls
128 lines (113 loc) · 5.08 KB
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
127
128
package dev.openfga.sdk.telemetry;
import dev.openfga.sdk.api.configuration.Configuration;
import dev.openfga.sdk.api.configuration.TelemetryConfiguration;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.metrics.DoubleHistogram;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.Meter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* The Metrics class provides methods for creating and publishing metrics using OpenTelemetry.
*/
public class Metrics {
private final Meter meter;
private final Map<String, LongCounter> counters;
private final Map<String, DoubleHistogram> histograms;
private final Configuration configuration;
public Metrics() {
this(new Configuration());
}
public Metrics(Configuration configuration) {
this.meter = GlobalOpenTelemetry.get().getMeterProvider().get("openfga-sdk");
this.counters = new ConcurrentHashMap<>();
this.histograms = new ConcurrentHashMap<>();
this.configuration = configuration;
if (this.configuration.getTelemetryConfiguration() == null) {
this.configuration.telemetryConfiguration(new TelemetryConfiguration());
}
}
/**
* Returns the Meter associated with this Metrics session.
*
* @return The Meter object.
*/
public Meter getMeter() {
return meter;
}
/**
* Returns a LongCounter metric instance.
*
* @param counter The Counter enum representing the metric.
* @param value The value to be added to the counter.
* @param attributes A map of attributes associated with the metric.
*
* @return The LongCounter metric instance, if the counter was configured. Otherwise, null.
*/
public LongCounter getCounter(Counter counter, Long value, Map<Attribute, String> attributes) {
if (configuration.getTelemetryConfiguration().metrics() == null
|| !configuration.getTelemetryConfiguration().metrics().containsKey(counter)) {
return null;
}
LongCounter counterInstance = counters.computeIfAbsent(counter.getName(), name -> meter.counterBuilder(name)
.setDescription(counter.getDescription())
.build());
if (value != null) {
counterInstance.add(value, Attributes.prepare(attributes, counter, configuration));
}
return counterInstance;
}
/**
* Returns a DoubleHistogram metric instance.
*
* @param histogram The Histogram enum representing the metric.
* @param value The value to be recorded in the histogram.
* @param attributes A map of attributes associated with the metric.
*
* @return the DoubleHistogram instance, if the histogram was configured. Otherwise, null.
*/
public DoubleHistogram getHistogram(Histogram histogram, Double value, Map<Attribute, String> attributes) {
if (configuration.getTelemetryConfiguration().metrics() == null
|| !configuration.getTelemetryConfiguration().metrics().containsKey(histogram)) {
return null;
}
DoubleHistogram histogramInstance =
histograms.computeIfAbsent(histogram.getName(), name -> meter.histogramBuilder(name)
.setDescription(histogram.getDescription())
.setUnit(histogram.getUnit())
.build());
if (value != null) {
histogramInstance.record(value, Attributes.prepare(attributes, histogram, configuration));
}
return histogramInstance;
}
/**
* Returns a LongCounter counter for tracking the number of times an access token is requested through ClientCredentials.
*
* @param value The value to be added to the counter.
* @param attributes A map of attributes associated with the metric.
*
* @return The LongCounter metric instance for credentials request.
*/
public LongCounter credentialsRequest(Long value, Map<Attribute, String> attributes) {
return getCounter(Counters.CREDENTIALS_REQUEST, value, attributes);
}
/**
* Returns a DoubleHistogram histogram for measuring the total roundtrip time it took to process a request, including the time it took to send the request and receive the response.
*
* @param value The value to be recorded in the histogram.
* @param attributes A map of attributes associated with the metric.
*/
public DoubleHistogram requestDuration(Double value, Map<Attribute, String> attributes) {
return getHistogram(Histograms.REQUEST_DURATION, value, attributes);
}
/**
* Returns a DoubleHistogram for measuring how long the FGA server took to process and evaluate a request.
*
* @param value The value to be recorded in the histogram.
* @param attributes A map of attributes associated with the metric.
*/
public DoubleHistogram queryDuration(Double value, Map<Attribute, String> attributes) {
return getHistogram(Histograms.QUERY_DURATION, value, attributes);
}
}