Skip to content
This repository was archived by the owner on Jul 13, 2026. It is now read-only.

Commit 4737fc8

Browse files
Added custom diagnostics handler code
1 parent 6ea5682 commit 4737fc8

2 files changed

Lines changed: 34 additions & 12 deletions

File tree

src/main/java/com/azure/cosmos/examples/diagnostics/async/CosmosDiagnosticsQuickStartAsync.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
package com.azure.cosmos.examples.diagnostics.async;
55

6+
import com.azure.core.util.Context;
67
import com.azure.cosmos.ConsistencyLevel;
78
import com.azure.cosmos.CosmosAsyncClient;
89
import com.azure.cosmos.CosmosAsyncContainer;
910
import com.azure.cosmos.CosmosAsyncDatabase;
1011
import com.azure.cosmos.CosmosClientBuilder;
1112
import com.azure.cosmos.CosmosDiagnostics;
13+
import com.azure.cosmos.CosmosDiagnosticsContext;
1214
import com.azure.cosmos.CosmosDiagnosticsHandler;
1315
import com.azure.cosmos.CosmosDiagnosticsThresholds;
1416
import com.azure.cosmos.CosmosException;
@@ -69,7 +71,7 @@ private void diagnosticsDemo() throws Exception {
6971

7072
logger.info("Using Azure Cosmos DB endpoint: {}", AccountSettings.HOST);
7173

72-
// Create Diagnostics threshold
74+
// Create diagnostics threshold
7375
CosmosDiagnosticsThresholds cosmosDiagnosticsThresholds = new CosmosDiagnosticsThresholds();
7476
// For demo purposes, we will reduce the threshold so to log all diagnostics
7577
// NOTE: Do not use the same thresholds for production
@@ -78,8 +80,17 @@ private void diagnosticsDemo() throws Exception {
7880
cosmosDiagnosticsThresholds.setNonPointOperationLatencyThreshold(Duration.ofMillis(10));
7981
cosmosDiagnosticsThresholds.setRequestChargeThreshold(5f);
8082

83+
// By default, DEFAULT_LOGGING_HANDLER can be used
8184
CosmosDiagnosticsHandler cosmosDiagnosticsHandler = CosmosDiagnosticsHandler.DEFAULT_LOGGING_HANDLER;
8285

86+
// App developers can also define their own diagnostics handler
87+
cosmosDiagnosticsHandler = new CosmosDiagnosticsHandler() {
88+
@Override
89+
public void handleDiagnostics(CosmosDiagnosticsContext diagnosticsContext, Context traceContext) {
90+
logger.info("This is custom diagnostics handler: {}", diagnosticsContext.toJson());
91+
}
92+
};
93+
8394

8495
// Create Client Telemetry Config
8596
CosmosClientTelemetryConfig cosmosClientTelemetryConfig =

src/main/java/com/azure/cosmos/examples/diagnostics/sync/CosmosDiagnosticsQuickStart.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
package com.azure.cosmos.examples.diagnostics.sync;
55

6+
import com.azure.core.util.Context;
67
import com.azure.cosmos.ConsistencyLevel;
78
import com.azure.cosmos.CosmosClient;
89
import com.azure.cosmos.CosmosClientBuilder;
910
import com.azure.cosmos.CosmosContainer;
1011
import com.azure.cosmos.CosmosDatabase;
1112
import com.azure.cosmos.CosmosDiagnostics;
13+
import com.azure.cosmos.CosmosDiagnosticsContext;
1214
import com.azure.cosmos.CosmosDiagnosticsHandler;
1315
import com.azure.cosmos.CosmosDiagnosticsThresholds;
1416
import 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

Comments
 (0)