33
44package com .azure .cosmos .examples .diagnostics .sync ;
55
6+ import com .azure .core .util .Context ;
67import com .azure .cosmos .ConsistencyLevel ;
78import com .azure .cosmos .CosmosClient ;
89import com .azure .cosmos .CosmosClientBuilder ;
910import com .azure .cosmos .CosmosContainer ;
1011import com .azure .cosmos .CosmosDatabase ;
1112import com .azure .cosmos .CosmosDiagnostics ;
13+ import com .azure .cosmos .CosmosDiagnosticsContext ;
1214import com .azure .cosmos .CosmosDiagnosticsHandler ;
1315import com .azure .cosmos .CosmosDiagnosticsThresholds ;
1416import com .azure .cosmos .CosmosException ;
@@ -77,8 +79,17 @@ private void diagnosticsDemo() throws Exception {
7779 cosmosDiagnosticsThresholds .setNonPointOperationLatencyThreshold (Duration .ofMillis (10 ));
7880 cosmosDiagnosticsThresholds .setRequestChargeThreshold (5f );
7981
82+ // By default, DEFAULT_LOGGING_HANDLER can be used
8083 CosmosDiagnosticsHandler cosmosDiagnosticsHandler = CosmosDiagnosticsHandler .DEFAULT_LOGGING_HANDLER ;
8184
85+ // App developers can also define their own diagnostics handler
86+ cosmosDiagnosticsHandler = new CosmosDiagnosticsHandler () {
87+ @ Override
88+ public void handleDiagnostics (CosmosDiagnosticsContext diagnosticsContext , Context traceContext ) {
89+ logger .info ("This is custom diagnostics handler: {}" , diagnosticsContext .toJson ());
90+ }
91+ };
92+
8293
8394 // Create Client Telemetry Config
8495 CosmosClientTelemetryConfig cosmosClientTelemetryConfig =
@@ -115,7 +126,7 @@ private void createDatabaseIfNotExists() throws Exception {
115126 // Create database if not exists
116127 CosmosDatabaseResponse databaseResponse = client .createDatabaseIfNotExists (databaseName );
117128 CosmosDiagnostics diagnostics = databaseResponse .getDiagnostics ();
118- // logger.info("Create database diagnostics : {}", diagnostics);
129+ logger .info ("Create database diagnostics : {}" , diagnostics );
119130 database = client .getDatabase (databaseResponse .getProperties ().getId ());
120131
121132 logger .info ("Done." );
@@ -136,7 +147,7 @@ private void createContainerIfNotExists() throws Exception {
136147 CosmosContainerResponse containerResponse = database .createContainerIfNotExists (containerProperties ,
137148 throughputProperties );
138149 CosmosDiagnostics diagnostics = containerResponse .getDiagnostics ();
139- // logger.info("Create container diagnostics : {}", diagnostics);
150+ logger .info ("Create container diagnostics : {}" , diagnostics );
140151 container = database .getContainer (containerResponse .getProperties ().getId ());
141152
142153 logger .info ("Done." );
@@ -157,7 +168,7 @@ private void createDocument() throws Exception {
157168 new CosmosItemRequestOptions ());
158169
159170 CosmosDiagnostics diagnostics = item .getDiagnostics ();
160- // logger.info("Create item diagnostics : {}", diagnostics);
171+ logger .info ("Create item diagnostics : {}" , diagnostics );
161172
162173 logger .info ("Done." );
163174 }
@@ -171,7 +182,7 @@ private void readDocumentById() throws Exception {
171182 new PartitionKey (documentLastName ), Family .class );
172183
173184 CosmosDiagnostics diagnostics = familyCosmosItemResponse .getDiagnostics ();
174- // logger.info("Read item diagnostics : {}", diagnostics);
185+ logger .info ("Read item diagnostics : {}" , diagnostics );
175186
176187 Family family = familyCosmosItemResponse .getItem ();
177188
@@ -207,13 +218,13 @@ private void queryDocuments() throws Exception {
207218
208219 // Add handler to capture diagnostics
209220 filteredFamilies = filteredFamilies .handle (familyFeedResponse -> {
210- // logger.info("Query Item diagnostics through handler : {}", familyFeedResponse.getCosmosDiagnostics());
221+ logger .info ("Query Item diagnostics through handler : {}" , familyFeedResponse .getCosmosDiagnostics ());
211222 });
212223
213224 // Or capture diagnostics through iterableByPage() APIs.
214225 filteredFamilies .iterableByPage ().forEach (familyFeedResponse -> {
215- // logger.info("Query item diagnostics through iterableByPage : {}", familyFeedResponse
216- // .getCosmosDiagnostics());
226+ logger .info ("Query item diagnostics through iterableByPage : {}" ,
227+ familyFeedResponse .getCosmosDiagnostics ());
217228 });
218229
219230 logger .info ("Done." );
@@ -233,7 +244,7 @@ private void replaceDocument() throws Exception {
233244 new CosmosItemRequestOptions ());
234245
235246 CosmosDiagnostics diagnostics = itemResponse .getDiagnostics ();
236- // logger.info("Replace item diagnostics : {}", diagnostics);
247+ logger .info ("Replace item diagnostics : {}" , diagnostics );
237248
238249 logger .info ("Request charge of replace operation: {} RU" , itemResponse .getRequestCharge ());
239250
@@ -253,7 +264,7 @@ private void upsertDocument() throws Exception {
253264 container .upsertItem (family , new CosmosItemRequestOptions ());
254265
255266 CosmosDiagnostics diagnostics = itemResponse .getDiagnostics ();
256- // logger.info("Upsert item diagnostics : {}", diagnostics);
267+ logger .info ("Upsert item diagnostics : {}" , diagnostics );
257268
258269 logger .info ("Done." );
259270 }
@@ -267,7 +278,7 @@ private void deleteDocument() throws Exception {
267278 new PartitionKey (documentLastName ), new CosmosItemRequestOptions ());
268279
269280 CosmosDiagnostics diagnostics = itemResponse .getDiagnostics ();
270- // logger.info("Delete item diagnostics : {}", diagnostics);
281+ logger .info ("Delete item diagnostics : {}" , diagnostics );
271282
272283 logger .info ("Done." );
273284 }
@@ -278,7 +289,7 @@ private void deleteDatabase() throws Exception {
278289
279290 // Delete database
280291 CosmosDatabaseResponse dbResp = client .getDatabase (databaseName ).delete (new CosmosDatabaseRequestOptions ());
281- // logger.info("Status code for database delete: {}", dbResp.getStatusCode());
292+ logger .info ("Status code for database delete: {}" , dbResp .getStatusCode ());
282293
283294 logger .info ("Done." );
284295 }
0 commit comments