From d17f07b30b5feddbac7d8afbb79adab1184f5767 Mon Sep 17 00:00:00 2001 From: Annie Liang <64233642+xinlian12@users.noreply.github.com> Date: Wed, 24 Apr 2024 12:02:21 -0700 Subject: [PATCH] addingMetricsSupportInKafkaConnector (#39808) * allow config metrics from system property --------- Co-authored-by: annie-mac --- sdk/cosmos/azure-cosmos-kafka-connect/pom.xml | 11 +- .../src/test/connectorPlugins/build.ps1 | 2 +- .../src/test/connectorPlugins/build.sh | 2 +- .../KafkaCosmosIntegrationTestSuiteBase.java | 6 +- .../azure-cosmos-spark_3_2-12/dev/README.md | 3 +- .../com/azure/cosmos/ClientMetricsTest.java | 56 +++++++++ .../cosmos/implementation/ConfigsTests.java | 36 ++++++ .../azure/cosmos/implementation/Configs.java | 25 +++- .../CosmosMicrometerMetricsConfig.java | 109 ++++++++++++++++++ .../ImplementationBridgeHelpers.java | 3 + .../clienttelemetry/MetricCategory.java | 9 ++ .../clienttelemetry/TagName.java | 9 ++ .../models/CosmosClientTelemetryConfig.java | 17 ++- .../CosmosMicrometerMetricsOptions.java | 57 ++++++++- 14 files changed, 327 insertions(+), 18 deletions(-) create mode 100644 sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/CosmosMicrometerMetricsConfig.java diff --git a/sdk/cosmos/azure-cosmos-kafka-connect/pom.xml b/sdk/cosmos/azure-cosmos-kafka-connect/pom.xml index 95d9dba54c6e9..8684f7c01c23a 100644 --- a/sdk/cosmos/azure-cosmos-kafka-connect/pom.xml +++ b/sdk/cosmos/azure-cosmos-kafka-connect/pom.xml @@ -309,7 +309,6 @@ Licensed under the MIT License. shade - ${project.artifactId}-${project.version}-jar-with-dependencies *:*:*:* @@ -330,6 +329,12 @@ Licensed under the MIT License. META-INF.versions.9.module-info.class + + io.micrometer:micrometer-core + + ** + + @@ -363,10 +368,6 @@ Licensed under the MIT License. com.thoughtworks.paranamer ${shadingPrefix}.com.thoughtworks.paranamer - - io.micrometer - ${shadingPrefix}.io.micrometer - org.HdrHistogram ${shadingPrefix}.org.HdrHistogram diff --git a/sdk/cosmos/azure-cosmos-kafka-connect/src/test/connectorPlugins/build.ps1 b/sdk/cosmos/azure-cosmos-kafka-connect/src/test/connectorPlugins/build.ps1 index 600429c8488ad..b9871e81e434a 100644 --- a/sdk/cosmos/azure-cosmos-kafka-connect/src/test/connectorPlugins/build.ps1 +++ b/sdk/cosmos/azure-cosmos-kafka-connect/src/test/connectorPlugins/build.ps1 @@ -8,7 +8,7 @@ New-Item -Path "$PSScriptRoot/src/test/connectorPlugins" -ItemType "directory" - Write-Host "Rebuilding Cosmos DB connectors..." mvn clean package -DskipTests -Dmaven.javadoc.skip -copy target\*-jar-with-dependencies.jar $PSScriptRoot/src/test/connectorPlugins/connectors +copy target/azure-cosmos-kafka-connect-*.jar $PSScriptRoot/src/test/connectorPlugins/connectors cd $PSScriptRoot/src/test/connectorPlugins Write-Host "Adding custom Insert UUID SMT" diff --git a/sdk/cosmos/azure-cosmos-kafka-connect/src/test/connectorPlugins/build.sh b/sdk/cosmos/azure-cosmos-kafka-connect/src/test/connectorPlugins/build.sh index bda724c262d43..e1e3896d1403e 100755 --- a/sdk/cosmos/azure-cosmos-kafka-connect/src/test/connectorPlugins/build.sh +++ b/sdk/cosmos/azure-cosmos-kafka-connect/src/test/connectorPlugins/build.sh @@ -6,7 +6,7 @@ mkdir src/test/connectorPlugins/connectors echo "Rebuilding Cosmos DB connectors..." mvn clean package -DskipTests=true -Dmaven.javadoc.skip=true -cp target/*-jar-with-dependencies.jar src/test/connectorPlugins/connectors +cp target/azure-cosmos-kafka-connect-*.jar src/test/connectorPlugins/connectors cd src/test/connectorPlugins echo "Adding custom Insert UUID SMT" diff --git a/sdk/cosmos/azure-cosmos-kafka-connect/src/test/java/com/azure/cosmos/kafka/connect/KafkaCosmosIntegrationTestSuiteBase.java b/sdk/cosmos/azure-cosmos-kafka-connect/src/test/java/com/azure/cosmos/kafka/connect/KafkaCosmosIntegrationTestSuiteBase.java index b69a797a328f6..3b476cbe6f27d 100644 --- a/sdk/cosmos/azure-cosmos-kafka-connect/src/test/java/com/azure/cosmos/kafka/connect/KafkaCosmosIntegrationTestSuiteBase.java +++ b/sdk/cosmos/azure-cosmos-kafka-connect/src/test/java/com/azure/cosmos/kafka/connect/KafkaCosmosIntegrationTestSuiteBase.java @@ -137,7 +137,7 @@ private static void createConnectorJar() throws IOException, InterruptedExceptio if (exitCode == 0) { logger.info("Build script completed successfully"); //validate the jar exists - File jarFile = findFile("src/test/connectorPlugins/connectors", "jar-with-dependencies.jar"); + File jarFile = findFile("src/test/connectorPlugins/connectors", "azure-cosmos-kafka-connect"); assertThat(jarFile).isNotNull(); assertThat(jarFile.exists()).isTrue(); @@ -147,13 +147,13 @@ private static void createConnectorJar() throws IOException, InterruptedExceptio } } - private static File findFile(String folder, String filenameFilterEndsWith) { + private static File findFile(String folder, String filenameFilterStartsWith) { File file = new File(folder); if (!file.exists() || !file.isDirectory()) { return null; } return Arrays.stream(file.listFiles()) - .filter(f -> f.getName().endsWith(filenameFilterEndsWith)) + .filter(f -> f.getName().startsWith(filenameFilterStartsWith)) .findFirst().orElse(null); } diff --git a/sdk/cosmos/azure-cosmos-spark_3_2-12/dev/README.md b/sdk/cosmos/azure-cosmos-spark_3_2-12/dev/README.md index 5e7741c9c3562..5686b9022da53 100644 --- a/sdk/cosmos/azure-cosmos-spark_3_2-12/dev/README.md +++ b/sdk/cosmos/azure-cosmos-spark_3_2-12/dev/README.md @@ -42,7 +42,8 @@ The release process is currently manual. Download the spark artifacts or build locally: ```bash -mvn -e -DskipTests -Dgpg.skip -Dmaven.javadoc.skip=true -Dcodesnippet.skip=true -Dspotbugs.skip=true -Dcheckstyle.skip=true -Drevapi.skip=true -pl ,azure-cosmos -am clean install +mvn -e -DskipTests -Dgpg.skip -Dmaven.javadoc.skip=mvn -e -DskipTests -Dgpg.skip -Dmaven.javadoc.skip=true -Dcodesnippet.skip=true -Dspotbugs.skip=true -Dcheckstyle.skip=true -Drevapi.skip=true -pl ,azure-cosmos -am clean install +true -Dcodesnippet.skip=true -Dspotbugs.skip=true -Dcheckstyle.skip=true -Drevapi.skip=true -pl ,azure-cosmos -am clean install mvn -e -DskipTests -Dgpg.skip -Dmaven.javadoc.skip=true -Dcodesnippet.skip=true -Dspotbugs.skip=true -Dcheckstyle.skip=true -Drevapi.skip=true -pl ,azure-cosmos-spark_3-1_2-12 clean install mvn -e -DskipTests -Dgpg.skip -Dmaven.javadoc.skip=true -Dcodesnippet.skip=true -Dspotbugs.skip=true -Dcheckstyle.skip=true -Drevapi.skip=true -pl ,azure-cosmos-spark_3-2_2-12 clean install mvn -e -DskipTests -Dgpg.skip -Dmaven.javadoc.skip=true -Dcodesnippet.skip=true -Dspotbugs.skip=true -Dcheckstyle.skip=true -Drevapi.skip=true -pl ,azure-cosmos-spark_3-3_2-12 clean install diff --git a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/ClientMetricsTest.java b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/ClientMetricsTest.java index 712d15a977f5e..873bf2c7e0172 100644 --- a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/ClientMetricsTest.java +++ b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/ClientMetricsTest.java @@ -1275,6 +1275,62 @@ public void meterNameFromStringConversion() { .isSameAs(CosmosMetricName.DIRECT_REQUEST_SIZE_REQUEST); } + @Test(groups = { "unit" }, timeOut = TIMEOUT) + public void metricConfigsThroughSystemProperty() { + System.setProperty( + "COSMOS.METRICS_CONFIG", + "{\"metricCategories\":\"[OperationDetails]\"," + + "\"tagNames\":\"[PartitionId]\"," + + "\"sampleRate\":0.5," + + "\"percentiles\":[0.90,0.99]," + + "\"enableHistograms\":false," + + "\"applyDiagnosticThresholdsForTransportLevelMeters\":true}"); + + CosmosClientBuilder testClientBuilder = new CosmosClientBuilder(); + CosmosClientTelemetryConfig clientTelemetryConfig = ReflectionUtils.getClientTelemetryConfig(testClientBuilder); + EnumSet effectiveMetricsCategory = ImplementationBridgeHelpers + .CosmosClientTelemetryConfigHelper + .getCosmosClientTelemetryConfigAccessor() + .getMetricCategories(clientTelemetryConfig); + assertThat(effectiveMetricsCategory).containsAll(MetricCategory.MINIMAL_CATEGORIES); + assertThat(effectiveMetricsCategory).contains(MetricCategory.OperationDetails); + + EnumSet effectiveTagNames = + ImplementationBridgeHelpers + .CosmosClientTelemetryConfigHelper + .getCosmosClientTelemetryConfigAccessor() + .getMetricTagNames(clientTelemetryConfig); + assertThat(effectiveTagNames).containsAll(TagName.MINIMUM_TAGS); + assertThat(effectiveTagNames).contains(TagName.PartitionId); + + double sampleRate = ImplementationBridgeHelpers + .CosmosClientTelemetryConfigHelper + .getCosmosClientTelemetryConfigAccessor() + .getSamplingRate(clientTelemetryConfig); + assertThat(sampleRate).isEqualTo(0.5); + + double[] percentiles = + ImplementationBridgeHelpers + .CosmosClientTelemetryConfigHelper + .getCosmosClientTelemetryConfigAccessor() + .getDefaultPercentiles(clientTelemetryConfig); + assertThat(percentiles).contains(0.90, 0.99); + + boolean publishHistograms = + ImplementationBridgeHelpers + .CosmosClientTelemetryConfigHelper + .getCosmosClientTelemetryConfigAccessor() + .shouldPublishHistograms(clientTelemetryConfig); + assertThat(publishHistograms).isFalse(); + + boolean applyDiagnosticThresholdsForTransportLevelMeters = + ImplementationBridgeHelpers + .CosmosClientTelemetryConfigHelper + .getCosmosClientTelemetryConfigAccessor() + .shouldApplyDiagnosticThresholdsForTransportLevelMeters(clientTelemetryConfig); + assertThat(applyDiagnosticThresholdsForTransportLevelMeters).isTrue(); + } + private InternalObjectNode getDocumentDefinition(String documentId) { final String uuid = UUID.randomUUID().toString(); return diff --git a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/ConfigsTests.java b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/ConfigsTests.java index 0248850e1977c..99f1354e34075 100644 --- a/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/ConfigsTests.java +++ b/sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/ConfigsTests.java @@ -3,9 +3,13 @@ package com.azure.cosmos.implementation; +import com.azure.cosmos.implementation.clienttelemetry.MetricCategory; +import com.azure.cosmos.implementation.clienttelemetry.TagName; import com.azure.cosmos.implementation.directconnectivity.Protocol; import org.testng.annotations.Test; +import java.util.EnumSet; + import static org.assertj.core.api.Assertions.assertThat; public class ConfigsTests { @@ -33,4 +37,36 @@ public void getDirectHttpsMaxConnectionLimit() { Configs config = new Configs(); assertThat(config.getDirectHttpsMaxConnectionLimit()).isEqualTo(Runtime.getRuntime().availableProcessors() * 500); } + + @Test(groups = { "unit" }) + public void getMetricsConfig() { + System.clearProperty("COSMOS.METRICS_CONFIG"); + CosmosMicrometerMetricsConfig metricsConfig = Configs.getMetricsConfig(); + assertThat(metricsConfig.getMetricCategories()).isEqualTo(MetricCategory.DEFAULT_CATEGORIES); + assertThat(metricsConfig.getTagNames()).isEqualTo(TagName.DEFAULT_TAGS); + assertThat(metricsConfig.getPercentiles()).contains(0.95, 0.99); + assertThat(metricsConfig.getEnableHistograms()).isTrue(); + assertThat(metricsConfig.getApplyDiagnosticThresholdsForTransportLevelMeters()).isFalse(); + assertThat(metricsConfig.getSampleRate()).isEqualTo(1.0); + + System.setProperty( + "COSMOS.METRICS_CONFIG", + "{\"metricCategories\":\"[OperationSummary, RequestSummary]\"," + + "\"tagNames\":\"[Container, Operation]\"," + + "\"sampleRate\":0.5," + + "\"percentiles\":[0.90,0.99]," + + "\"enableHistograms\":false," + + "\"applyDiagnosticThresholdsForTransportLevelMeters\":true}"); + try { + metricsConfig = Configs.getMetricsConfig(); + assertThat(metricsConfig.getMetricCategories()).isEqualTo(EnumSet.of(MetricCategory.OperationSummary, MetricCategory.RequestSummary)); + assertThat(metricsConfig.getTagNames()).isEqualTo(EnumSet.of(TagName.Container, TagName.Operation)); + assertThat(metricsConfig.getPercentiles()).contains(0.90, 0.99); + assertThat(metricsConfig.getEnableHistograms()).isFalse(); + assertThat(metricsConfig.getApplyDiagnosticThresholdsForTransportLevelMeters()).isTrue(); + assertThat(metricsConfig.getSampleRate()).isEqualTo(0.5); + } finally { + System.clearProperty("COSMOS.METRICS_CONFIG"); + } + } } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Configs.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Configs.java index d6df02e18ee90..da368de5360d8 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Configs.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/Configs.java @@ -11,7 +11,6 @@ import org.slf4j.LoggerFactory; import javax.net.ssl.SSLException; - import java.time.Duration; import java.util.Locale; @@ -175,6 +174,19 @@ public class Configs { public static final String DIAGNOSTICS_PROVIDER_SYSTEM_EXIT_ON_ERROR = "COSMOS.DIAGNOSTICS_PROVIDER_SYSTEM_EXIT_ON_ERROR"; public static final boolean DEFAULT_DIAGNOSTICS_PROVIDER_SYSTEM_EXIT_ON_ERROR = true; + // Metrics + // Samples: + // System.setProperty( + // "COSMOS.METRICS_CONFIG", + // "{\"metricCategories\":\"[OperationSummary, RequestSummary]\"," + // + "\"tagNames\":\"[Container, Operation]\"," + // + "\"sampleRate\":0.5," + // + "\"percentiles\":[0.90,0.99]," + // + "\"enableHistograms\":false," + // + "\"applyDiagnosticThresholdsForTransportLevelMeters\":true}"); + public static final String METRICS_CONFIG = "COSMOS.METRICS_CONFIG"; + public static final String DEFAULT_METRICS_CONFIG = CosmosMicrometerMetricsConfig.DEFAULT.toJson(); + public Configs() { this.sslContext = sslContextInit(); } @@ -509,4 +521,15 @@ public static boolean shouldDiagnosticsProviderSystemExitOnError() { return Boolean.parseBoolean(shouldSystemExit); } + + public static CosmosMicrometerMetricsConfig getMetricsConfig() { + String metricsConfig = + System.getProperty( + METRICS_CONFIG, + firstNonNull( + emptyToNull(System.getenv().get(METRICS_CONFIG)), + DEFAULT_METRICS_CONFIG)); + + return CosmosMicrometerMetricsConfig.fromJsonString(metricsConfig); + } } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/CosmosMicrometerMetricsConfig.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/CosmosMicrometerMetricsConfig.java new file mode 100644 index 0000000000000..15a6632126e63 --- /dev/null +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/CosmosMicrometerMetricsConfig.java @@ -0,0 +1,109 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.cosmos.implementation; + +import com.azure.cosmos.implementation.apachecommons.lang.StringUtils; +import com.azure.cosmos.implementation.clienttelemetry.MetricCategory; +import com.azure.cosmos.implementation.clienttelemetry.TagName; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonSetter; +import com.fasterxml.jackson.annotation.Nulls; +import com.fasterxml.jackson.core.JsonProcessingException; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.EnumSet; +import java.util.List; +import java.util.stream.Collectors; + +public class CosmosMicrometerMetricsConfig { + public static final CosmosMicrometerMetricsConfig DEFAULT = new CosmosMicrometerMetricsConfig(); + + @JsonSetter(nulls = Nulls.SKIP) + @JsonProperty + private String metricCategories = MetricCategory.DEFAULT_CATEGORIES.clone().toString(); + @JsonSetter(nulls = Nulls.SKIP) + @JsonProperty + private String tagNames = TagName.DEFAULT_TAGS.clone().toString(); + @JsonSetter(nulls = Nulls.SKIP) + @JsonProperty + private Double sampleRate = 1.0; + @JsonSetter(nulls = Nulls.SKIP) + @JsonProperty + private double[] percentiles = { 0.95, 0.99 }; + @JsonSetter(nulls = Nulls.SKIP) + @JsonProperty + private Boolean enableHistograms = true; + @JsonSetter(nulls = Nulls.SKIP) + @JsonProperty + private Boolean applyDiagnosticThresholdsForTransportLevelMeters = false; + + public CosmosMicrometerMetricsConfig() {} + + public String toJson() { + try { + return Utils.getSimpleObjectMapper().writeValueAsString(this); + } catch (JsonProcessingException e) { + throw new RuntimeException("Unable to convert to Json String", e); + } + } + + @JsonIgnore + public EnumSet getMetricCategories() { + List metricsCategoryList = convertToList(this.metricCategories); + return EnumSet.copyOf( + metricsCategoryList + .stream() + .map(categoryValue -> MetricCategory.fromValue(categoryValue)) + .collect(Collectors.toList()) + ); + } + + @JsonIgnore + public EnumSet getTagNames() { + List tagNames = convertToList(this.tagNames); + return EnumSet.copyOf( + tagNames + .stream() + .map(tagName -> TagName.fromValue(tagName)) + .collect(Collectors.toList()) + ); + } + + public double[] getPercentiles() { + return this.percentiles; + } + + public double getSampleRate() { + return this.sampleRate; + } + + public Boolean getEnableHistograms() { + return this.enableHistograms; + } + + public Boolean getApplyDiagnosticThresholdsForTransportLevelMeters() { + return applyDiagnosticThresholdsForTransportLevelMeters; + } + + private static List convertToList(String value) { + if (StringUtils.isEmpty(value)) { + return new ArrayList<>(); + } + if (value.startsWith("[") && value.endsWith("]")) { + value = value.substring(1, value.length() - 1); + } + + return Arrays.stream(value.split(",")).map(String::trim).collect(Collectors.toList()); + } + + public static CosmosMicrometerMetricsConfig fromJsonString(String jsonString) { + try { + return Utils.getSimpleObjectMapper().readValue(jsonString, CosmosMicrometerMetricsConfig.class); + } catch (JsonProcessingException e) { + throw new RuntimeException("Unable to convert from Json String", e); + } + } +} diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ImplementationBridgeHelpers.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ImplementationBridgeHelpers.java index 034026a007dac..db2bfd16f8719 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ImplementationBridgeHelpers.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ImplementationBridgeHelpers.java @@ -1488,6 +1488,9 @@ CosmosClientTelemetryConfig createSnapshot( void setUseLegacyTracing(CosmosClientTelemetryConfig config, boolean useLegacyTracing); void setTracer(CosmosClientTelemetryConfig config, Tracer tracer); double getSamplingRate(CosmosClientTelemetryConfig config); + double[] getDefaultPercentiles(CosmosClientTelemetryConfig config); + boolean shouldPublishHistograms(CosmosClientTelemetryConfig config); + boolean shouldApplyDiagnosticThresholdsForTransportLevelMeters(CosmosClientTelemetryConfig config); } } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/MetricCategory.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/MetricCategory.java index e71e32c122977..b6d916315bb10 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/MetricCategory.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/MetricCategory.java @@ -54,4 +54,13 @@ public int value() { MetricCategory.OperationSummary, MetricCategory.System ); + + public static MetricCategory fromValue(String value) { + for (MetricCategory metricCategory : MetricCategory.values()) { + if (metricCategory.toLowerStringValue.equalsIgnoreCase(value)) { + return metricCategory; + } + } + return null; + } } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/TagName.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/TagName.java index 332dd1e945eb3..2de5821cb5df2 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/TagName.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/TagName.java @@ -67,5 +67,14 @@ public int value() { TagName.RequestStatusCode, TagName.RequestOperationType ); + + public static TagName fromValue(String value) { + for (TagName tagName : TagName.values()) { + if (tagName.toLowerStringValue.equalsIgnoreCase(value)) { + return tagName; + } + } + return null; + } } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosClientTelemetryConfig.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosClientTelemetryConfig.java index 4e72488146961..b6d8bd4803965 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosClientTelemetryConfig.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosClientTelemetryConfig.java @@ -89,7 +89,7 @@ public CosmosClientTelemetryConfig() { this.diagnosticHandlers = new CopyOnWriteArrayList<>(); this.tracer = null; this.tracingOptions = null; - this.samplingRate = 1; + this.samplingRate = Configs.getMetricsConfig().getSampleRate(); CosmosMicrometerMetricsOptions defaultMetricsOptions = new CosmosMicrometerMetricsOptions(); this.isClientMetricsEnabled = defaultMetricsOptions.isEnabled(); if (this.isClientMetricsEnabled) { @@ -660,6 +660,21 @@ public void setTracer(CosmosClientTelemetryConfig config, Tracer tracer) { public double getSamplingRate(CosmosClientTelemetryConfig config) { return config.samplingRate; } + + @Override + public double[] getDefaultPercentiles(CosmosClientTelemetryConfig config) { + return config.micrometerMetricsOptions.getDefaultPercentiles(); + } + + @Override + public boolean shouldPublishHistograms(CosmosClientTelemetryConfig config) { + return config.micrometerMetricsOptions.shouldPublishHistograms(); + } + + @Override + public boolean shouldApplyDiagnosticThresholdsForTransportLevelMeters(CosmosClientTelemetryConfig config) { + return config.micrometerMetricsOptions.shouldApplyDiagnosticThresholdsForTransportLevelMeters(); + } }); } diff --git a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosMicrometerMetricsOptions.java b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosMicrometerMetricsOptions.java index 75ca0f5f967f4..83c9b62343632 100644 --- a/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosMicrometerMetricsOptions.java +++ b/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/models/CosmosMicrometerMetricsOptions.java @@ -3,6 +3,8 @@ package com.azure.cosmos.models; import com.azure.core.util.MetricsOptions; +import com.azure.cosmos.implementation.Configs; +import com.azure.cosmos.implementation.CosmosMicrometerMetricsConfig; import com.azure.cosmos.implementation.clienttelemetry.CosmosMeterOptions; import com.azure.cosmos.implementation.clienttelemetry.MetricCategory; import com.azure.cosmos.implementation.clienttelemetry.TagName; @@ -19,18 +21,25 @@ */ public final class CosmosMicrometerMetricsOptions extends MetricsOptions { private MeterRegistry clientMetricRegistry = Metrics.globalRegistry; - private EnumSet metricCategories = MetricCategory.DEFAULT_CATEGORIES.clone(); - private EnumSet defaultTagNames = TagName.DEFAULT_TAGS.clone(); - private double[] defaultPercentiles = { 0.95, 0.99 }; - private boolean defaultShouldPublishHistograms = true; + private EnumSet metricCategories; + private EnumSet defaultTagNames; + private double[] defaultPercentiles; + private boolean defaultShouldPublishHistograms; - private boolean defaultApplyDiagnosticThresholdsForTransportLevelMeters = false; + private boolean defaultApplyDiagnosticThresholdsForTransportLevelMeters; private final ConcurrentHashMap effectiveOptions = new ConcurrentHashMap<>(); /** * Instantiates new Micrometer-specific Azure Cosmos DB SDK metrics options */ public CosmosMicrometerMetricsOptions() { + CosmosMicrometerMetricsConfig metricsConfig = Configs.getMetricsConfig(); + + this.setMetricCategoriesFromConfig(metricsConfig.getMetricCategories()); + this.setTagNamesFromConfig(metricsConfig.getTagNames()); + this.configureDefaultPercentiles(metricsConfig.getPercentiles()); + this.defaultShouldPublishHistograms = metricsConfig.getEnableHistograms(); + this.defaultApplyDiagnosticThresholdsForTransportLevelMeters = metricsConfig.getApplyDiagnosticThresholdsForTransportLevelMeters(); } MeterRegistry getClientMetricRegistry() { @@ -81,6 +90,19 @@ public CosmosMicrometerMetricsOptions configureDefaultTagNames(CosmosMetricTagNa return this; } + CosmosMicrometerMetricsOptions setTagNamesFromConfig(EnumSet tags) { + if (tags == null || tags.size() == 0) { + this.defaultTagNames = TagName.DEFAULT_TAGS.clone(); + } else { + EnumSet newTagNames = TagName.MINIMUM_TAGS.clone(); + newTagNames.addAll(tags); + + this.defaultTagNames = newTagNames; + } + + return this; + } + /** * Sets the default percentiles that should be captured for metrics (where applicable) unless overridden for a * specific meter in its {@link CosmosMicrometerMeterOptions} @@ -186,6 +208,19 @@ public CosmosMicrometerMetricsOptions setMetricCategories(CosmosMetricCategory.. return this; } + CosmosMicrometerMetricsOptions setMetricCategoriesFromConfig(EnumSet categories) { + if (categories == null || categories.size() == 0) { + this.metricCategories = MetricCategory.DEFAULT_CATEGORIES.clone(); + } else { + EnumSet newMetricCategories = MetricCategory.MINIMAL_CATEGORIES.clone(); + newMetricCategories.addAll(categories); + + this.metricCategories = newMetricCategories; + } + + return this; + } + /** * Adds categories of metrics that should be emitted. By default the following categories will be enabled: * OperationSummary (required), RequestSummary, DirectChannels, DirectRequests, System (required) @@ -337,4 +372,16 @@ EnumSet getDefaultTagNames() { void configureDefaultTagNames(EnumSet newTagNames) { this.defaultTagNames = newTagNames; } + + double[] getDefaultPercentiles() { + return defaultPercentiles; + } + + boolean shouldPublishHistograms() { + return defaultShouldPublishHistograms; + } + + boolean shouldApplyDiagnosticThresholdsForTransportLevelMeters() { + return defaultApplyDiagnosticThresholdsForTransportLevelMeters; + } }