22
33import static org .junit .jupiter .api .Assertions .*;
44import static org .mockito .Mockito .mock ;
5+ import static org .mockito .Mockito .when ;
56
67import dev .openfga .sdk .api .configuration .Configuration ;
8+ import dev .openfga .sdk .api .configuration .TelemetryConfiguration ;
79import io .opentelemetry .api .metrics .DoubleHistogram ;
810import io .opentelemetry .api .metrics .LongCounter ;
911import io .opentelemetry .api .metrics .Meter ;
1012import java .util .HashMap ;
1113import java .util .Map ;
14+ import java .util .Optional ;
1215import org .junit .jupiter .api .BeforeEach ;
1316import org .junit .jupiter .api .Test ;
1417
@@ -21,6 +24,8 @@ class MetricsTest {
2124 void setUp () {
2225 configuration = mock (Configuration .class );
2326 metrics = new Metrics (configuration );
27+
28+ when (configuration .getTelemetryConfiguration ()).thenReturn (new TelemetryConfiguration ());
2429 }
2530
2631 @ Test
@@ -116,4 +121,96 @@ void testQueryDuration() {
116121 // Assert
117122 assertNotNull (doubleHistogram , "The DoubleHistogram object should not be null." );
118123 }
124+
125+ @ Test
126+ void testMetricsNotSentIfNotConfigured () {
127+ Map <Attribute , Optional <Object >> attributes = new HashMap <>();
128+ attributes .put (Attributes .FGA_CLIENT_REQUEST_METHOD , Optional .empty ());
129+ Map <Metric , Map <Attribute , Optional <Object >>> metrics = new HashMap <>();
130+ metrics .put (Histograms .QUERY_DURATION , attributes );
131+ TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration (metrics );
132+
133+ Configuration config = new Configuration ();
134+ config .telemetryConfiguration (telemetryConfiguration );
135+
136+ Metrics configuredMetrics = new Metrics (config );
137+
138+ DoubleHistogram requestDuration =
139+ configuredMetrics .getHistogram (Histograms .REQUEST_DURATION , 10.0 , new HashMap <>());
140+ assertNull (requestDuration , "Unconfigured histograms should not be sent." );
141+
142+ DoubleHistogram queryDuration =
143+ configuredMetrics .getHistogram (Histograms .QUERY_DURATION , 10.0 , new HashMap <>());
144+ assertNotNull (queryDuration , "Configured histograms should be sent." );
145+
146+ LongCounter credsRequestCounter =
147+ configuredMetrics .getCounter (Counters .CREDENTIALS_REQUEST , 10L , new HashMap <>());
148+ assertNull (credsRequestCounter , "Unconfigured counters should not be sent." );
149+ }
150+
151+ @ Test
152+ void testCountersNotSentIfNotConfigured () {
153+ Map <Attribute , Optional <Object >> attributes = new HashMap <>();
154+ attributes .put (Attributes .FGA_CLIENT_REQUEST_METHOD , Optional .empty ());
155+ Map <Metric , Map <Attribute , Optional <Object >>> metrics = new HashMap <>();
156+ metrics .put (Counters .CREDENTIALS_REQUEST , attributes );
157+ TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration (metrics );
158+
159+ Configuration config = new Configuration ();
160+ config .telemetryConfiguration (telemetryConfiguration );
161+
162+ Metrics configuredMetrics = new Metrics (config );
163+
164+ DoubleHistogram requestDuration =
165+ configuredMetrics .getHistogram (Histograms .REQUEST_DURATION , 10.0 , new HashMap <>());
166+ assertNull (requestDuration , "Unconfigured histograms should not be sent." );
167+
168+ DoubleHistogram queryDuration =
169+ configuredMetrics .getHistogram (Histograms .QUERY_DURATION , 10.0 , new HashMap <>());
170+ assertNull (queryDuration , "Unconfigured histograms should not be sent." );
171+
172+ LongCounter credsCounter = configuredMetrics .getCounter (Counters .CREDENTIALS_REQUEST , 10L , new HashMap <>());
173+ assertNotNull (credsCounter , "Configured counter should be sent." );
174+ }
175+
176+ @ Test
177+ void testDefaultMetricsEnabled () {
178+ // Arrange
179+ Configuration config = new Configuration ();
180+
181+ // Act
182+ Metrics metrics = new Metrics (config );
183+
184+ // Assert
185+ assertNotNull (
186+ metrics .getCounter (Counters .CREDENTIALS_REQUEST , 10L , new HashMap <>()), "The counter should be sent." );
187+ assertNotNull (
188+ metrics .getHistogram (Histograms .QUERY_DURATION , 10.0 , new HashMap <>()),
189+ "The query duration histogram should be sent." );
190+ assertNotNull (
191+ metrics .getHistogram (Histograms .REQUEST_DURATION , 10.0 , new HashMap <>()),
192+ "The request duration histogram should be sent." );
193+ }
194+
195+ @ Test
196+ void testMetricsWithNullMetricsConfig () {
197+ // Arrange
198+ TelemetryConfiguration telemetryConfiguration = new TelemetryConfiguration (null );
199+ Configuration config = new Configuration ();
200+ config .telemetryConfiguration (telemetryConfiguration );
201+
202+ // Act
203+ Metrics metrics = new Metrics (config );
204+
205+ // Assert
206+ assertNull (
207+ metrics .getCounter (Counters .CREDENTIALS_REQUEST , 10L , new HashMap <>()),
208+ "The counter should not be sent." );
209+ assertNull (
210+ metrics .getHistogram (Histograms .QUERY_DURATION , 10.0 , new HashMap <>()),
211+ "The query duration histogram should not be sent." );
212+ assertNull (
213+ metrics .getHistogram (Histograms .REQUEST_DURATION , 10.0 , new HashMap <>()),
214+ "The request duration histogram should not be sent." );
215+ }
119216}
0 commit comments