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

Commit f450ba3

Browse files
Updated Cosmos Diagnostics samples to use client telemetry config
1 parent 823a505 commit f450ba3

3 files changed

Lines changed: 73 additions & 26 deletions

File tree

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

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
import com.azure.cosmos.CosmosAsyncDatabase;
1010
import com.azure.cosmos.CosmosClientBuilder;
1111
import com.azure.cosmos.CosmosDiagnostics;
12+
import com.azure.cosmos.CosmosDiagnosticsHandler;
13+
import com.azure.cosmos.CosmosDiagnosticsThresholds;
1214
import com.azure.cosmos.CosmosException;
1315
import com.azure.cosmos.examples.common.AccountSettings;
1416
import com.azure.cosmos.examples.common.Family;
17+
import com.azure.cosmos.models.CosmosClientTelemetryConfig;
1518
import com.azure.cosmos.models.CosmosContainerProperties;
1619
import com.azure.cosmos.models.CosmosContainerResponse;
1720
import com.azure.cosmos.models.CosmosDatabaseRequestOptions;
@@ -26,6 +29,7 @@
2629
import org.slf4j.LoggerFactory;
2730
import reactor.core.publisher.Mono;
2831

32+
import java.time.Duration;
2933
import java.util.UUID;
3034

3135
public 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
}

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

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
import com.azure.cosmos.CosmosContainer;
1010
import com.azure.cosmos.CosmosDatabase;
1111
import com.azure.cosmos.CosmosDiagnostics;
12+
import com.azure.cosmos.CosmosDiagnosticsHandler;
13+
import com.azure.cosmos.CosmosDiagnosticsThresholds;
1214
import com.azure.cosmos.CosmosException;
1315
import com.azure.cosmos.examples.common.AccountSettings;
1416
import com.azure.cosmos.examples.common.Family;
17+
import com.azure.cosmos.models.CosmosClientTelemetryConfig;
1518
import com.azure.cosmos.models.CosmosContainerProperties;
1619
import com.azure.cosmos.models.CosmosContainerResponse;
1720
import com.azure.cosmos.models.CosmosDatabaseRequestOptions;
@@ -25,6 +28,7 @@
2528
import org.slf4j.Logger;
2629
import org.slf4j.LoggerFactory;
2730

31+
import java.time.Duration;
2832
import java.util.UUID;
2933

3034
public class CosmosDiagnosticsQuickStart {
@@ -64,12 +68,32 @@ private void diagnosticsDemo() throws Exception {
6468

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

71+
// Create diagnostics threshold
72+
CosmosDiagnosticsThresholds cosmosDiagnosticsThresholds = new CosmosDiagnosticsThresholds();
73+
cosmosDiagnosticsThresholds.setPayloadSizeThreshold(10000);
74+
// For demo purposes, we will reduce the threshold so to log all diagnostics
75+
// NOTE: Do not use the same thresholds for production
76+
cosmosDiagnosticsThresholds.setPointOperationLatencyThreshold(Duration.ofMillis(10));
77+
cosmosDiagnosticsThresholds.setNonPointOperationLatencyThreshold(Duration.ofMillis(10));
78+
cosmosDiagnosticsThresholds.setRequestChargeThreshold(5f);
79+
80+
CosmosDiagnosticsHandler cosmosDiagnosticsHandler = CosmosDiagnosticsHandler.DEFAULT_LOGGING_HANDLER;
81+
82+
83+
// Create Client Telemetry Config
84+
CosmosClientTelemetryConfig cosmosClientTelemetryConfig =
85+
new CosmosClientTelemetryConfig();
86+
cosmosClientTelemetryConfig.diagnosticsHandler(cosmosDiagnosticsHandler);
87+
cosmosClientTelemetryConfig.diagnosticsThresholds(cosmosDiagnosticsThresholds);
88+
89+
6790
// Create sync client
6891
client = new CosmosClientBuilder()
6992
.endpoint(AccountSettings.HOST)
7093
.key(AccountSettings.MASTER_KEY)
7194
.consistencyLevel(ConsistencyLevel.EVENTUAL)
7295
.contentResponseOnWriteEnabled(true)
96+
.clientTelemetryConfig(cosmosClientTelemetryConfig)
7397
.buildClient();
7498

7599

@@ -91,7 +115,7 @@ private void createDatabaseIfNotExists() throws Exception {
91115
// Create database if not exists
92116
CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists(databaseName);
93117
CosmosDiagnostics diagnostics = databaseResponse.getDiagnostics();
94-
logger.info("Create database diagnostics : {}", diagnostics);
118+
// logger.info("Create database diagnostics : {}", diagnostics);
95119
database = client.getDatabase(databaseResponse.getProperties().getId());
96120

97121
logger.info("Done.");
@@ -112,7 +136,7 @@ private void createContainerIfNotExists() throws Exception {
112136
CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties,
113137
throughputProperties);
114138
CosmosDiagnostics diagnostics = containerResponse.getDiagnostics();
115-
logger.info("Create container diagnostics : {}", diagnostics);
139+
// logger.info("Create container diagnostics : {}", diagnostics);
116140
container = database.getContainer(containerResponse.getProperties().getId());
117141

118142
logger.info("Done.");
@@ -133,7 +157,7 @@ private void createDocument() throws Exception {
133157
new CosmosItemRequestOptions());
134158

135159
CosmosDiagnostics diagnostics = item.getDiagnostics();
136-
logger.info("Create item diagnostics : {}", diagnostics);
160+
// logger.info("Create item diagnostics : {}", diagnostics);
137161

138162
logger.info("Done.");
139163
}
@@ -147,7 +171,7 @@ private void readDocumentById() throws Exception {
147171
new PartitionKey(documentLastName), Family.class);
148172

149173
CosmosDiagnostics diagnostics = familyCosmosItemResponse.getDiagnostics();
150-
logger.info("Read item diagnostics : {}", diagnostics);
174+
// logger.info("Read item diagnostics : {}", diagnostics);
151175

152176
Family family = familyCosmosItemResponse.getItem();
153177

@@ -167,7 +191,7 @@ private void readDocumentDoesntExist() throws Exception {
167191
new PartitionKey("bad-lastName"), Family.class);
168192
} catch (CosmosException cosmosException) {
169193
CosmosDiagnostics diagnostics = cosmosException.getDiagnostics();
170-
logger.info("Read item exception diagnostics : {}", diagnostics);
194+
// logger.info("Read item exception diagnostics : {}", diagnostics);
171195
}
172196

173197
logger.info("Done.");
@@ -183,13 +207,13 @@ private void queryDocuments() throws Exception {
183207

184208
// Add handler to capture diagnostics
185209
filteredFamilies = filteredFamilies.handle(familyFeedResponse -> {
186-
logger.info("Query Item diagnostics through handler : {}", familyFeedResponse.getCosmosDiagnostics());
210+
// logger.info("Query Item diagnostics through handler : {}", familyFeedResponse.getCosmosDiagnostics());
187211
});
188212

189213
// Or capture diagnostics through iterableByPage() APIs.
190214
filteredFamilies.iterableByPage().forEach(familyFeedResponse -> {
191-
logger.info("Query item diagnostics through iterableByPage : {}",
192-
familyFeedResponse.getCosmosDiagnostics());
215+
// logger.info("Query item diagnostics through iterableByPage : {}", familyFeedResponse
216+
// .getCosmosDiagnostics());
193217
});
194218

195219
logger.info("Done.");
@@ -209,7 +233,7 @@ private void replaceDocument() throws Exception {
209233
new CosmosItemRequestOptions());
210234

211235
CosmosDiagnostics diagnostics = itemResponse.getDiagnostics();
212-
logger.info("Replace item diagnostics : {}", diagnostics);
236+
// logger.info("Replace item diagnostics : {}", diagnostics);
213237

214238
logger.info("Request charge of replace operation: {} RU", itemResponse.getRequestCharge());
215239

@@ -229,7 +253,7 @@ private void upsertDocument() throws Exception {
229253
container.upsertItem(family, new CosmosItemRequestOptions());
230254

231255
CosmosDiagnostics diagnostics = itemResponse.getDiagnostics();
232-
logger.info("Upsert item diagnostics : {}", diagnostics);
256+
// logger.info("Upsert item diagnostics : {}", diagnostics);
233257

234258
logger.info("Done.");
235259
}
@@ -243,7 +267,7 @@ private void deleteDocument() throws Exception {
243267
new PartitionKey(documentLastName), new CosmosItemRequestOptions());
244268

245269
CosmosDiagnostics diagnostics = itemResponse.getDiagnostics();
246-
logger.info("Delete item diagnostics : {}", diagnostics);
270+
// logger.info("Delete item diagnostics : {}", diagnostics);
247271

248272
logger.info("Done.");
249273
}
@@ -254,7 +278,7 @@ private void deleteDatabase() throws Exception {
254278

255279
// Delete database
256280
CosmosDatabaseResponse dbResp = client.getDatabase(databaseName).delete(new CosmosDatabaseRequestOptions());
257-
logger.info("Status code for database delete: {}", dbResp.getStatusCode());
281+
// logger.info("Status code for database delete: {}", dbResp.getStatusCode());
258282

259283
logger.info("Done.");
260284
}

src/main/resources/log4j2.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ rootLogger.appenderRef.stdout.ref=STDOUT
55
logger.netty.name=io.netty
66
logger.netty.level=OFF
77
logger.cosmos.name=com.azure.cosmos
8-
logger.cosmos.level=warn
8+
logger.cosmos.level=info
99
# STDOUT is a ConsoleAppender and uses PatternLayout.
1010
appender.console.name=STDOUT
1111
appender.console.type=Console

0 commit comments

Comments
 (0)