|
3 | 3 |
|
4 | 4 | package com.azure.cosmos.examples.diagnostics.async; |
5 | 5 |
|
| 6 | +import com.azure.core.util.Context; |
6 | 7 | import com.azure.cosmos.ConsistencyLevel; |
7 | 8 | import com.azure.cosmos.CosmosAsyncClient; |
8 | 9 | import com.azure.cosmos.CosmosAsyncContainer; |
9 | 10 | import com.azure.cosmos.CosmosAsyncDatabase; |
10 | 11 | import com.azure.cosmos.CosmosClientBuilder; |
11 | 12 | import com.azure.cosmos.CosmosDiagnostics; |
| 13 | +import com.azure.cosmos.CosmosDiagnosticsContext; |
| 14 | +import com.azure.cosmos.CosmosDiagnosticsHandler; |
| 15 | +import com.azure.cosmos.CosmosDiagnosticsThresholds; |
12 | 16 | import com.azure.cosmos.CosmosException; |
13 | 17 | import com.azure.cosmos.examples.common.AccountSettings; |
14 | 18 | import com.azure.cosmos.examples.common.Family; |
| 19 | +import com.azure.cosmos.models.CosmosClientTelemetryConfig; |
15 | 20 | import com.azure.cosmos.models.CosmosContainerProperties; |
16 | 21 | import com.azure.cosmos.models.CosmosContainerResponse; |
17 | 22 | import com.azure.cosmos.models.CosmosDatabaseRequestOptions; |
|
26 | 31 | import org.slf4j.LoggerFactory; |
27 | 32 | import reactor.core.publisher.Mono; |
28 | 33 |
|
| 34 | +import java.time.Duration; |
29 | 35 | import java.util.UUID; |
30 | 36 |
|
31 | 37 | public class CosmosDiagnosticsQuickStartAsync { |
@@ -65,12 +71,40 @@ private void diagnosticsDemo() throws Exception { |
65 | 71 |
|
66 | 72 | logger.info("Using Azure Cosmos DB endpoint: {}", AccountSettings.HOST); |
67 | 73 |
|
68 | | - // Create sync client |
| 74 | + // Create diagnostics threshold |
| 75 | + CosmosDiagnosticsThresholds cosmosDiagnosticsThresholds = new CosmosDiagnosticsThresholds(); |
| 76 | + // These thresholds are for demo purposes |
| 77 | + // NOTE: Do not use the same thresholds for production |
| 78 | + cosmosDiagnosticsThresholds.setPayloadSizeThreshold(100_00); |
| 79 | + cosmosDiagnosticsThresholds.setPointOperationLatencyThreshold(Duration.ofSeconds(1)); |
| 80 | + cosmosDiagnosticsThresholds.setNonPointOperationLatencyThreshold(Duration.ofSeconds(5)); |
| 81 | + cosmosDiagnosticsThresholds.setRequestChargeThreshold(100f); |
| 82 | + |
| 83 | + // By default, DEFAULT_LOGGING_HANDLER can be used |
| 84 | + CosmosDiagnosticsHandler cosmosDiagnosticsHandler = CosmosDiagnosticsHandler.DEFAULT_LOGGING_HANDLER; |
| 85 | + |
| 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 | + |
| 94 | + |
| 95 | + // Create Client Telemetry Config |
| 96 | + CosmosClientTelemetryConfig cosmosClientTelemetryConfig = |
| 97 | + new CosmosClientTelemetryConfig(); |
| 98 | + cosmosClientTelemetryConfig.diagnosticsHandler(cosmosDiagnosticsHandler); |
| 99 | + cosmosClientTelemetryConfig.diagnosticsThresholds(cosmosDiagnosticsThresholds); |
| 100 | + |
| 101 | + // Create async client |
69 | 102 | client = new CosmosClientBuilder() |
70 | 103 | .endpoint(AccountSettings.HOST) |
71 | 104 | .key(AccountSettings.MASTER_KEY) |
72 | 105 | .consistencyLevel(ConsistencyLevel.EVENTUAL) |
73 | 106 | .contentResponseOnWriteEnabled(true) |
| 107 | + .clientTelemetryConfig(cosmosClientTelemetryConfig) |
74 | 108 | .buildAsyncClient(); |
75 | 109 |
|
76 | 110 |
|
|
0 commit comments