3333import java .time .Instant ;
3434import java .util .List ;
3535import java .util .Map ;
36- import java .util .HashMap ;
3736
3837import static org .opensearch .dataprepper .logging .DataPrepperMarkers .NOISY ;
3938import static org .opensearch .dataprepper .plugins .source .microsoft_office365 .utils .Constants .CONTENT_TYPES ;
39+ import static org .opensearch .dataprepper .plugins .source .microsoft_office365 .utils .MetricsHelper .getErrorTypeMetricCounterMap ;
40+ import static org .opensearch .dataprepper .plugins .source .microsoft_office365 .utils .MetricsHelper .publishErrorTypeMetricCounter ;
4041
4142/**
4243 * REST client for interacting with Office 365 Management API.
@@ -55,11 +56,6 @@ public class Office365RestClient {
5556 private static final String SEARCH_REQUESTS_SUCCESS = "searchRequestsSuccess" ;
5657 private static final String SEARCH_REQUESTS_FAILED = "searchRequestsFailed" ;
5758
58- // specific retryable/non-retryable metric names
59- private static final String ACCESS_DENIED_FAILED = "accessDenied" ;
60- private static final String THROTTLING_FAILED = "throttling" ;
61- private static final String RESOURCE_NOT_FOUND_FAILED = "resourceNotFound" ;
62-
6359 private static final String MANAGEMENT_API_BASE_URL = "https://manage.office.com/api/v1.0/" ;
6460
6561 private final RestTemplate restTemplate = new RestTemplate ();
@@ -90,7 +86,7 @@ public Office365RestClient(final Office365AuthenticationInterface authConfig,
9086 this .auditLogResponseSizeSummary = pluginMetrics .summary (AUDIT_LOG_RESPONSE_SIZE );
9187 this .searchResponseSizeSummary = pluginMetrics .summary (SEARCH_RESPONSE_SIZE );
9288
93- this .initializeErrorTypeMetricCounterMap (pluginMetrics );
89+ this .errorTypeMetricCounterMap = getErrorTypeMetricCounterMap (pluginMetrics );
9490 }
9591
9692 /**
@@ -148,14 +144,14 @@ public void startSubscriptions() {
148144 }
149145 } catch (HttpClientErrorException | HttpServerErrorException e ) {
150146 HttpStatus statusCode = e .getStatusCode ();
151- publishErrorTypeMetricCounter (statusCode .getReasonPhrase ());
147+ publishErrorTypeMetricCounter (statusCode .getReasonPhrase (), this . errorTypeMetricCounterMap );
152148 log .error (NOISY , "Failed to initialize subscriptions with status code {}: {}" ,
153149 statusCode , e .getMessage ());
154150 throw new RuntimeException ("Failed to initialize subscriptions: " + e .getMessage (), e );
155151 } catch (Exception e ) {
156152 // FORBIDDEN throws SecurityException in RetryHandler
157153 if (e instanceof SecurityException ) {
158- publishErrorTypeMetricCounter (HttpStatus .FORBIDDEN .getReasonPhrase ());
154+ publishErrorTypeMetricCounter (HttpStatus .FORBIDDEN .getReasonPhrase (), this . errorTypeMetricCounterMap );
159155 }
160156 log .error (NOISY , "Failed to initialize subscriptions" , e );
161157 throw new RuntimeException ("Failed to initialize subscriptions: " + e .getMessage (), e );
@@ -220,14 +216,13 @@ public AuditLogsResponse searchAuditLogs(final String contentType,
220216 );
221217 } catch (HttpClientErrorException | HttpServerErrorException e ) {
222218 HttpStatus statusCode = e .getStatusCode ();
223- publishErrorTypeMetricCounter (statusCode .getReasonPhrase ());
224- log .error (NOISY , "Error while fetching audit logs with status code {}: {}" ,
225- statusCode , e .getMessage ());
219+ publishErrorTypeMetricCounter (statusCode .getReasonPhrase (), this .errorTypeMetricCounterMap );
220+ log .error (NOISY , "Error while fetching audit logs for content type {}" , contentType , e );
226221 throw new RuntimeException ("Failed to fetch audit logs" , e );
227222 } catch (Exception e ) {
228223 // FORBIDDEN throws SecurityException in RetryHandler
229224 if (e instanceof SecurityException ) {
230- publishErrorTypeMetricCounter (HttpStatus .FORBIDDEN .getReasonPhrase ());
225+ publishErrorTypeMetricCounter (HttpStatus .FORBIDDEN .getReasonPhrase (), this . errorTypeMetricCounterMap );
231226 }
232227 log .error (NOISY , "Error while fetching audit logs for content type {}" , contentType , e );
233228 throw new RuntimeException ("Failed to fetch audit logs" , e );
@@ -272,47 +267,17 @@ public String getAuditLog(String contentUri) {
272267 return response ;
273268 } catch (HttpClientErrorException | HttpServerErrorException e ) {
274269 HttpStatus statusCode = e .getStatusCode ();
275- publishErrorTypeMetricCounter (statusCode .getReasonPhrase ());
276- log .error (NOISY , "Error while getting audit log with status code {}: {}" ,
277- statusCode , e .getMessage ());
270+ publishErrorTypeMetricCounter (statusCode .getReasonPhrase (), this .errorTypeMetricCounterMap );
271+ log .error (NOISY , "Error while fetching audit log content from URI: {}" , contentUri , e );
278272 throw new RuntimeException ("Failed to fetch audit log" , e );
279273 } catch (Exception e ) {
280274 // FORBIDDEN throws SecurityException in RetryHandler
281275 if (e instanceof SecurityException ) {
282- publishErrorTypeMetricCounter (HttpStatus .FORBIDDEN .getReasonPhrase ());
276+ publishErrorTypeMetricCounter (HttpStatus .FORBIDDEN .getReasonPhrase (), this . errorTypeMetricCounterMap );
283277 }
284278 log .error (NOISY , "Error while fetching audit log content from URI: {}" , contentUri , e );
285279 throw new RuntimeException ("Failed to fetch audit log" , e );
286280 }
287281 });
288282 }
289-
290- /**
291- * Initialize the metric counter map for specific errorType
292- * FORBIDDEN/UNAUTHORIZED = accessDenied
293- * TOO_MANY_REQUESTS = throttling
294- * NOT_FOUND = resourceNotFound
295- */
296- private void initializeErrorTypeMetricCounterMap (PluginMetrics pluginMetrics ) {
297- // TODO: should move this over to a common class later
298- this .errorTypeMetricCounterMap = new HashMap <>();
299- this .errorTypeMetricCounterMap .put (HttpStatus .FORBIDDEN .getReasonPhrase (), pluginMetrics .counter (ACCESS_DENIED_FAILED ));
300- this .errorTypeMetricCounterMap .put (HttpStatus .UNAUTHORIZED .getReasonPhrase (), pluginMetrics .counter (ACCESS_DENIED_FAILED ));
301- this .errorTypeMetricCounterMap .put (HttpStatus .TOO_MANY_REQUESTS .getReasonPhrase (), pluginMetrics .counter (THROTTLING_FAILED ));
302- this .errorTypeMetricCounterMap .put (HttpStatus .NOT_FOUND .getReasonPhrase (), pluginMetrics .counter (RESOURCE_NOT_FOUND_FAILED ));
303- }
304-
305- /**
306- * Increment the errorType metric if it exists in errorTypeMetricCounterMap
307- * Should only be the following:
308- * FORBIDDEN/UNAUTHORIZED = accessDenied
309- * TOO_MANY_REQUESTS = throttling
310- * NOT_FOUND = resourceNotFound
311- */
312- private void publishErrorTypeMetricCounter (String errorType ) {
313- if (this .errorTypeMetricCounterMap != null && this .errorTypeMetricCounterMap .containsKey (errorType )) {
314- errorTypeMetricCounterMap .get (errorType ).increment ();
315- }
316- }
317-
318283}
0 commit comments