99
1010package org .opensearch .dataprepper .plugins .source .microsoft_office365 ;
1111
12- import io .micrometer .core .instrument .Counter ;
1312import lombok .extern .slf4j .Slf4j ;
14- import org .opensearch .dataprepper .metrics .PluginMetrics ;
1513import org .opensearch .dataprepper .plugins .source .microsoft_office365 .auth .Office365AuthenticationInterface ;
1614import org .opensearch .dataprepper .plugins .source .source_crawler .exception .SaaSCrawlerException ;
1715import org .opensearch .dataprepper .plugins .source .microsoft_office365 .models .AuditLogsResponse ;
4543@ Named
4644public class Office365RestClient {
4745 private static final String MANAGEMENT_API_BASE_URL = "https://manage.office.com/api/v1.0/" ;
48- private static final String API_CALLS = "apiCalls" ;
49-
5046 private final RestTemplate restTemplate = new RestTemplate ();
5147 private final RetryHandler retryHandler ;
5248 private final Office365AuthenticationInterface authConfig ;
5349 private final VendorAPIMetricsRecorder metricsRecorder ;
54- private final Counter apiCallsCounter ;
5550
5651 public Office365RestClient (final Office365AuthenticationInterface authConfig ,
57- final PluginMetrics pluginMetrics ,
5852 final VendorAPIMetricsRecorder metricsRecorder ) {
5953 this .authConfig = authConfig ;
6054 this .metricsRecorder = metricsRecorder ;
61- this .apiCallsCounter = pluginMetrics .counter (API_CALLS );
6255 this .retryHandler = new RetryHandler (
6356 new DefaultRetryStrategy (),
6457 new DefaultStatusCodeHandler ());
@@ -69,59 +62,66 @@ public Office365RestClient(final Office365AuthenticationInterface authConfig,
6962 */
7063 public void startSubscriptions () {
7164 log .info ("Starting Office 365 subscriptions for audit logs" );
72- try {
73- HttpHeaders headers = new HttpHeaders ();
74- headers .setContentType (MediaType .APPLICATION_JSON );
65+
66+ metricsRecorder .recordSubscriptionLatency (() -> {
67+ try {
68+ HttpHeaders headers = new HttpHeaders ();
69+ headers .setContentType (MediaType .APPLICATION_JSON );
7570
76- // TODO: Only start the subscriptions only if the call commented
77- // out below doesn't return all the audit log types
78- // Check current subscriptions
79- // final String SUBSCRIPTION_LIST_URL = MANAGEMENT_API_BASE_URL + "%s/activity/feed/subscriptions/list";
80- // String listUrl = String.format(SUBSCRIPTION_LIST_URL, authConfig.getTenantId());
81- //
82- // ResponseEntity<String> listResponse = restTemplate.exchange(
83- // listUrl,
84- // HttpMethod.GET,
85- // new HttpEntity<>(headers),
86- // String.class
87- // );
88- // log.debug("Current subscriptions: {}", listResponse.getBody());
71+ // TODO: Only start the subscriptions only if the call commented
72+ // out below doesn't return all the audit log types
73+ // Check current subscriptions
74+ // final String SUBSCRIPTION_LIST_URL = MANAGEMENT_API_BASE_URL + "%s/activity/feed/subscriptions/list";
75+ // String listUrl = String.format(SUBSCRIPTION_LIST_URL, authConfig.getTenantId());
76+ //
77+ // ResponseEntity<String> listResponse = restTemplate.exchange(
78+ // listUrl,
79+ // HttpMethod.GET,
80+ // new HttpEntity<>(headers),
81+ // String.class
82+ // );
83+ // log.debug("Current subscriptions: {}", listResponse.getBody());
8984
90- // Start subscriptions for each content type
91- headers .setContentLength (0 );
85+ // Start subscriptions for each content type
86+ headers .setContentLength (0 );
9287
93- for (String contentType : CONTENT_TYPES ) {
94- final String SUBSCRIPTION_START_URL = MANAGEMENT_API_BASE_URL + "%s/activity/feed/subscriptions/start?contentType=%s" ;
95- String url = String .format (SUBSCRIPTION_START_URL ,
96- authConfig .getTenantId (),
97- contentType );
88+ for (String contentType : CONTENT_TYPES ) {
89+ final String SUBSCRIPTION_START_URL = MANAGEMENT_API_BASE_URL + "%s/activity/feed/subscriptions/start?contentType=%s" ;
90+ String url = String .format (SUBSCRIPTION_START_URL ,
91+ authConfig .getTenantId (),
92+ contentType );
9893
99- retryHandler .executeWithRetry (() -> {
100- try {
101- headers .setBearerAuth (authConfig .getAccessToken ());
102- apiCallsCounter .increment ();
103- ResponseEntity <String > response = restTemplate .exchange (
104- url ,
105- HttpMethod .POST ,
106- new HttpEntity <>(headers ),
107- String .class
108- );
109- log .debug ("Started subscription for {}: {}" , contentType , response .getBody ());
110- return response ;
111- } catch (HttpClientErrorException | HttpServerErrorException e ) {
112- if (e .getResponseBodyAsString ().contains ("AF20024" )) {
113- log .debug ("Subscription for {} is already enabled" , contentType );
114- return null ;
94+ retryHandler .executeWithRetry (() -> {
95+ try {
96+ headers .setBearerAuth (authConfig .getAccessToken ());
97+ metricsRecorder .recordSubscriptionCall ();
98+
99+ ResponseEntity <String > response = restTemplate .exchange (
100+ url ,
101+ HttpMethod .POST ,
102+ new HttpEntity <>(headers ),
103+ String .class
104+ );
105+ log .debug ("Started subscription for {}: {}" , contentType , response .getBody ());
106+ return response ;
107+ } catch (HttpClientErrorException | HttpServerErrorException e ) {
108+ if (e .getResponseBodyAsString ().contains ("AF20024" )) {
109+ log .debug ("Subscription for {} is already enabled" , contentType );
110+ return null ;
111+ }
112+ throw e ;
115113 }
116- throw e ;
117- }
118- }, authConfig ::renewCredentials );
114+ }, authConfig ::renewCredentials , metricsRecorder ::recordSubscriptionFailure );
115+ }
116+
117+ metricsRecorder .recordSubscriptionSuccess ();
118+ return null ;
119+ } catch (Exception e ) {
120+ metricsRecorder .recordError (e );
121+ log .error (NOISY , "Failed to initialize subscriptions" , e );
122+ throw new SaaSCrawlerException ("Failed to initialize subscriptions: " + e .getMessage (), e , true );
119123 }
120- } catch (Exception e ) {
121- metricsRecorder .recordError (e );
122- log .error (NOISY , "Failed to initialize subscriptions" , e );
123- throw new SaaSCrawlerException ("Failed to initialize subscriptions: " + e .getMessage (), e , true );
124- }
124+ });
125125 }
126126
127127 /**
0 commit comments