99import com .azure .cosmos .CosmosAsyncDatabase ;
1010import com .azure .cosmos .CosmosClientBuilder ;
1111import com .azure .cosmos .CosmosDiagnostics ;
12+ import com .azure .cosmos .CosmosDiagnosticsHandler ;
13+ import com .azure .cosmos .CosmosDiagnosticsThresholds ;
1214import com .azure .cosmos .CosmosException ;
1315import com .azure .cosmos .examples .common .AccountSettings ;
1416import com .azure .cosmos .examples .common .Family ;
17+ import com .azure .cosmos .models .CosmosClientTelemetryConfig ;
1518import com .azure .cosmos .models .CosmosContainerProperties ;
1619import com .azure .cosmos .models .CosmosContainerResponse ;
1720import com .azure .cosmos .models .CosmosDatabaseRequestOptions ;
2629import org .slf4j .LoggerFactory ;
2730import reactor .core .publisher .Mono ;
2831
32+ import java .time .Duration ;
2933import java .util .UUID ;
3034
3135public class CosmosDiagnosticsQuickStartAsync {
@@ -65,12 +69,31 @@ private void diagnosticsDemo() throws Exception {
6569
6670 logger .info ("Using Azure Cosmos DB endpoint: {}" , AccountSettings .HOST );
6771
72+ // Create diagnostics threshold
73+ CosmosDiagnosticsThresholds cosmosDiagnosticsThresholds = new CosmosDiagnosticsThresholds ();
74+ cosmosDiagnosticsThresholds .setPayloadSizeThreshold (10000 );
75+ // For demo purposes, we will reduce the threshold so to log all diagnostics
76+ // NOTE: Do not use the same thresholds for production
77+ cosmosDiagnosticsThresholds .setPointOperationLatencyThreshold (Duration .ofMillis (10 ));
78+ cosmosDiagnosticsThresholds .setNonPointOperationLatencyThreshold (Duration .ofMillis (10 ));
79+ cosmosDiagnosticsThresholds .setRequestChargeThreshold (5f );
80+
81+ CosmosDiagnosticsHandler cosmosDiagnosticsHandler = CosmosDiagnosticsHandler .DEFAULT_LOGGING_HANDLER ;
82+
83+
84+ // Create Client Telemetry Config
85+ CosmosClientTelemetryConfig cosmosClientTelemetryConfig =
86+ new CosmosClientTelemetryConfig ();
87+ cosmosClientTelemetryConfig .diagnosticsHandler (cosmosDiagnosticsHandler );
88+ cosmosClientTelemetryConfig .diagnosticsThresholds (cosmosDiagnosticsThresholds );
89+
6890 // Create sync client
6991 client = new CosmosClientBuilder ()
7092 .endpoint (AccountSettings .HOST )
7193 .key (AccountSettings .MASTER_KEY )
7294 .consistencyLevel (ConsistencyLevel .EVENTUAL )
7395 .contentResponseOnWriteEnabled (true )
96+ .clientTelemetryConfig (cosmosClientTelemetryConfig )
7497 .buildAsyncClient ();
7598
7699
@@ -94,7 +117,7 @@ private void createDatabaseIfNotExists() throws Exception {
94117 CosmosDatabaseResponse cosmosDatabaseResponse = databaseResponseMono .block ();
95118
96119 CosmosDiagnostics diagnostics = cosmosDatabaseResponse .getDiagnostics ();
97- logger .info ("Create database diagnostics : {}" , diagnostics );
120+ // logger.info("Create database diagnostics : {}", diagnostics);
98121
99122 database = client .getDatabase (cosmosDatabaseResponse .getProperties ().getId ());
100123
@@ -117,7 +140,7 @@ private void createContainerIfNotExists() throws Exception {
117140 throughputProperties );
118141 CosmosContainerResponse cosmosContainerResponse = containerResponseMono .block ();
119142 CosmosDiagnostics diagnostics = cosmosContainerResponse .getDiagnostics ();
120- logger .info ("Create container diagnostics : {}" , diagnostics );
143+ // logger.info("Create container diagnostics : {}", diagnostics);
121144 container = database .getContainer (cosmosContainerResponse .getProperties ().getId ());
122145
123146 logger .info ("Done." );
@@ -140,7 +163,7 @@ private void createDocument() throws Exception {
140163
141164 CosmosItemResponse <Family > itemResponse = itemResponseMono .block ();
142165 CosmosDiagnostics diagnostics = itemResponse .getDiagnostics ();
143- logger .info ("Create item diagnostics : {}" , diagnostics );
166+ // logger.info("Create item diagnostics : {}", diagnostics);
144167
145168 logger .info ("Done." );
146169 }
@@ -155,7 +178,7 @@ private void readDocumentById() throws Exception {
155178
156179 CosmosItemResponse <Family > familyCosmosItemResponse = itemResponseMono .block ();
157180 CosmosDiagnostics diagnostics = familyCosmosItemResponse .getDiagnostics ();
158- logger .info ("Read item diagnostics : {}" , diagnostics );
181+ // logger.info("Read item diagnostics : {}", diagnostics);
159182
160183 Family family = familyCosmosItemResponse .getItem ();
161184
@@ -175,7 +198,7 @@ private void readDocumentDoesntExist() throws Exception {
175198 new PartitionKey ("bad-lastName" ), Family .class ).block ();
176199 } catch (CosmosException cosmosException ) {
177200 CosmosDiagnostics diagnostics = cosmosException .getDiagnostics ();
178- logger .info ("Read item exception diagnostics : {}" , diagnostics );
201+ // logger.info("Read item exception diagnostics : {}", diagnostics);
179202 }
180203
181204 logger .info ("Done." );
@@ -191,13 +214,13 @@ private void queryDocuments() throws Exception {
191214
192215 // Add handler to capture diagnostics
193216 filteredFamilies = filteredFamilies .handle (familyFeedResponse -> {
194- logger .info ("Query Item diagnostics through handler : {}" , familyFeedResponse .getCosmosDiagnostics ());
217+ // logger.info("Query Item diagnostics through handler : {}", familyFeedResponse.getCosmosDiagnostics());
195218 });
196219
197220 // Or capture diagnostics through byPage() APIs.
198221 filteredFamilies .byPage ().toIterable ().forEach (familyFeedResponse -> {
199- logger .info ("Query item diagnostics through iterableByPage : {}" ,
200- familyFeedResponse .getCosmosDiagnostics ());
222+ // logger.info("Query item diagnostics through iterableByPage : {}", familyFeedResponse
223+ // .getCosmosDiagnostics());
201224 });
202225
203226 logger .info ("Done." );
@@ -218,8 +241,8 @@ private void replaceDocument() throws Exception {
218241
219242 CosmosItemResponse <Family > itemResponse = itemResponseMono .block ();
220243 CosmosDiagnostics diagnostics = itemResponse .getDiagnostics ();
221- logger .info ("Replace item diagnostics : {}" , diagnostics );
222- logger .info ("Request charge of replace operation: {} RU" , itemResponse .getRequestCharge ());
244+ // logger.info("Replace item diagnostics : {}", diagnostics);
245+ // logger.info("Request charge of replace operation: {} RU", itemResponse.getRequestCharge());
223246
224247 logger .info ("Done." );
225248 }
@@ -238,7 +261,7 @@ private void upsertDocument() throws Exception {
238261
239262 CosmosItemResponse <Family > itemResponse = itemResponseMono .block ();
240263 CosmosDiagnostics diagnostics = itemResponse .getDiagnostics ();
241- logger .info ("Upsert item diagnostics : {}" , diagnostics );
264+ // logger.info("Upsert item diagnostics : {}", diagnostics);
242265
243266 logger .info ("Done." );
244267 }
@@ -253,7 +276,7 @@ private void deleteDocument() throws Exception {
253276
254277 CosmosItemResponse <Object > itemResponse = itemResponseMono .block ();
255278 CosmosDiagnostics diagnostics = itemResponse .getDiagnostics ();
256- logger .info ("Delete item diagnostics : {}" , diagnostics );
279+ // logger.info("Delete item diagnostics : {}", diagnostics);
257280
258281 logger .info ("Done." );
259282 }
@@ -265,7 +288,7 @@ private void deleteDatabase() throws Exception {
265288 // Delete database
266289 CosmosDatabaseResponse dbResp =
267290 client .getDatabase (databaseName ).delete (new CosmosDatabaseRequestOptions ()).block ();
268- logger .info ("Status code for database delete: {}" , dbResp .getStatusCode ());
291+ // logger.info("Status code for database delete: {}", dbResp.getStatusCode());
269292
270293 logger .info ("Done." );
271294 }
0 commit comments