diff --git a/src/main/java/com/azure/cosmos/examples/diagnostics/async/CosmosDiagnosticsQuickStartAsync.java b/src/main/java/com/azure/cosmos/examples/diagnostics/async/CosmosDiagnosticsQuickStartAsync.java index a1a8484..800d891 100644 --- a/src/main/java/com/azure/cosmos/examples/diagnostics/async/CosmosDiagnosticsQuickStartAsync.java +++ b/src/main/java/com/azure/cosmos/examples/diagnostics/async/CosmosDiagnosticsQuickStartAsync.java @@ -3,15 +3,20 @@ package com.azure.cosmos.examples.diagnostics.async; +import com.azure.core.util.Context; import com.azure.cosmos.ConsistencyLevel; import com.azure.cosmos.CosmosAsyncClient; import com.azure.cosmos.CosmosAsyncContainer; import com.azure.cosmos.CosmosAsyncDatabase; import com.azure.cosmos.CosmosClientBuilder; import com.azure.cosmos.CosmosDiagnostics; +import com.azure.cosmos.CosmosDiagnosticsContext; +import com.azure.cosmos.CosmosDiagnosticsHandler; +import com.azure.cosmos.CosmosDiagnosticsThresholds; import com.azure.cosmos.CosmosException; import com.azure.cosmos.examples.common.AccountSettings; import com.azure.cosmos.examples.common.Family; +import com.azure.cosmos.models.CosmosClientTelemetryConfig; import com.azure.cosmos.models.CosmosContainerProperties; import com.azure.cosmos.models.CosmosContainerResponse; import com.azure.cosmos.models.CosmosDatabaseRequestOptions; @@ -26,6 +31,7 @@ import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; +import java.time.Duration; import java.util.UUID; public class CosmosDiagnosticsQuickStartAsync { @@ -65,12 +71,40 @@ private void diagnosticsDemo() throws Exception { logger.info("Using Azure Cosmos DB endpoint: {}", AccountSettings.HOST); - // Create sync client + // Create diagnostics threshold + CosmosDiagnosticsThresholds cosmosDiagnosticsThresholds = new CosmosDiagnosticsThresholds(); + // These thresholds are for demo purposes + // NOTE: Do not use the same thresholds for production + cosmosDiagnosticsThresholds.setPayloadSizeThreshold(100_00); + cosmosDiagnosticsThresholds.setPointOperationLatencyThreshold(Duration.ofSeconds(1)); + cosmosDiagnosticsThresholds.setNonPointOperationLatencyThreshold(Duration.ofSeconds(5)); + cosmosDiagnosticsThresholds.setRequestChargeThreshold(100f); + + // By default, DEFAULT_LOGGING_HANDLER can be used + CosmosDiagnosticsHandler cosmosDiagnosticsHandler = CosmosDiagnosticsHandler.DEFAULT_LOGGING_HANDLER; + + // App developers can also define their own diagnostics handler + cosmosDiagnosticsHandler = new CosmosDiagnosticsHandler() { + @Override + public void handleDiagnostics(CosmosDiagnosticsContext diagnosticsContext, Context traceContext) { + logger.info("This is custom diagnostics handler: {}", diagnosticsContext.toJson()); + } + }; + + + // Create Client Telemetry Config + CosmosClientTelemetryConfig cosmosClientTelemetryConfig = + new CosmosClientTelemetryConfig(); + cosmosClientTelemetryConfig.diagnosticsHandler(cosmosDiagnosticsHandler); + cosmosClientTelemetryConfig.diagnosticsThresholds(cosmosDiagnosticsThresholds); + + // Create async client client = new CosmosClientBuilder() .endpoint(AccountSettings.HOST) .key(AccountSettings.MASTER_KEY) .consistencyLevel(ConsistencyLevel.EVENTUAL) .contentResponseOnWriteEnabled(true) + .clientTelemetryConfig(cosmosClientTelemetryConfig) .buildAsyncClient(); diff --git a/src/main/java/com/azure/cosmos/examples/diagnostics/sync/CosmosDiagnosticsQuickStart.java b/src/main/java/com/azure/cosmos/examples/diagnostics/sync/CosmosDiagnosticsQuickStart.java index 95f433d..b255cdc 100644 --- a/src/main/java/com/azure/cosmos/examples/diagnostics/sync/CosmosDiagnosticsQuickStart.java +++ b/src/main/java/com/azure/cosmos/examples/diagnostics/sync/CosmosDiagnosticsQuickStart.java @@ -3,15 +3,20 @@ package com.azure.cosmos.examples.diagnostics.sync; +import com.azure.core.util.Context; import com.azure.cosmos.ConsistencyLevel; import com.azure.cosmos.CosmosClient; import com.azure.cosmos.CosmosClientBuilder; import com.azure.cosmos.CosmosContainer; import com.azure.cosmos.CosmosDatabase; import com.azure.cosmos.CosmosDiagnostics; +import com.azure.cosmos.CosmosDiagnosticsContext; +import com.azure.cosmos.CosmosDiagnosticsHandler; +import com.azure.cosmos.CosmosDiagnosticsThresholds; import com.azure.cosmos.CosmosException; import com.azure.cosmos.examples.common.AccountSettings; import com.azure.cosmos.examples.common.Family; +import com.azure.cosmos.models.CosmosClientTelemetryConfig; import com.azure.cosmos.models.CosmosContainerProperties; import com.azure.cosmos.models.CosmosContainerResponse; import com.azure.cosmos.models.CosmosDatabaseRequestOptions; @@ -25,6 +30,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.time.Duration; import java.util.UUID; public class CosmosDiagnosticsQuickStart { @@ -64,12 +70,41 @@ private void diagnosticsDemo() throws Exception { logger.info("Using Azure Cosmos DB endpoint: {}", AccountSettings.HOST); + // Create diagnostics threshold + CosmosDiagnosticsThresholds cosmosDiagnosticsThresholds = new CosmosDiagnosticsThresholds(); + // These thresholds are for demo purposes + // NOTE: Do not use the same thresholds for production + cosmosDiagnosticsThresholds.setPayloadSizeThreshold(100_00); + cosmosDiagnosticsThresholds.setPointOperationLatencyThreshold(Duration.ofSeconds(1)); + cosmosDiagnosticsThresholds.setNonPointOperationLatencyThreshold(Duration.ofSeconds(5)); + cosmosDiagnosticsThresholds.setRequestChargeThreshold(100f); + + // By default, DEFAULT_LOGGING_HANDLER can be used + CosmosDiagnosticsHandler cosmosDiagnosticsHandler = CosmosDiagnosticsHandler.DEFAULT_LOGGING_HANDLER; + + // App developers can also define their own diagnostics handler + cosmosDiagnosticsHandler = new CosmosDiagnosticsHandler() { + @Override + public void handleDiagnostics(CosmosDiagnosticsContext diagnosticsContext, Context traceContext) { + logger.info("This is custom diagnostics handler: {}", diagnosticsContext.toJson()); + } + }; + + + // Create Client Telemetry Config + CosmosClientTelemetryConfig cosmosClientTelemetryConfig = + new CosmosClientTelemetryConfig(); + cosmosClientTelemetryConfig.diagnosticsHandler(cosmosDiagnosticsHandler); + cosmosClientTelemetryConfig.diagnosticsThresholds(cosmosDiagnosticsThresholds); + + // Create sync client client = new CosmosClientBuilder() .endpoint(AccountSettings.HOST) .key(AccountSettings.MASTER_KEY) .consistencyLevel(ConsistencyLevel.EVENTUAL) .contentResponseOnWriteEnabled(true) + .clientTelemetryConfig(cosmosClientTelemetryConfig) .buildClient(); diff --git a/src/main/resources/log4j2.properties b/src/main/resources/log4j2.properties index 4877704..e27566b 100644 --- a/src/main/resources/log4j2.properties +++ b/src/main/resources/log4j2.properties @@ -5,7 +5,7 @@ rootLogger.appenderRef.stdout.ref=STDOUT logger.netty.name=io.netty logger.netty.level=OFF logger.cosmos.name=com.azure.cosmos -logger.cosmos.level=warn +logger.cosmos.level=info # STDOUT is a ConsoleAppender and uses PatternLayout. appender.console.name=STDOUT appender.console.type=Console