private void recordGatewayStatistics()

in sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/clienttelemetry/ClientTelemetryMetrics.java [1152:1348]


        private void recordGatewayStatistics(
            CosmosDiagnosticsContext ctx,
            CosmosAsyncClient client,
            Duration latency,
            List<ClientSideRequestStatistics.GatewayStatistics> gatewayStatisticsList,
            int requestPayloadSizeInBytes,
            int actualItemCount,
            long opCountPerEvaluation,
            long opRetriedCountPerEvaluation,
            long globalOpCount,
            int targetMaxMicroBatchSize) {

            if (gatewayStatisticsList == null
                || gatewayStatisticsList.size() == 0
                || !this.metricCategories.contains(MetricCategory.RequestSummary)) {
                return;
            }

            EnumSet<TagName> metricTagNamesForGateway = metricTagNames.clone();
            metricTagNamesForGateway.remove(TagName.RegionName);
            metricTagNamesForGateway.remove(TagName.ServiceAddress);
            metricTagNamesForGateway.remove(TagName.ServiceEndpoint);
            metricTagNamesForGateway.remove(TagName.PartitionId);
            metricTagNamesForGateway.remove(TagName.ReplicaId);

            for (ClientSideRequestStatistics.GatewayStatistics gatewayStats : gatewayStatisticsList) {
                Tags requestTags = operationTags.and(
                    createRequestTags(
                        metricTagNamesForGateway,
                        gatewayStats.getPartitionKeyRangeId(),
                        gatewayStats.getStatusCode(),
                        gatewayStats.getSubStatusCode(),
                        gatewayStats.getResourceType().toString(),
                        gatewayStats.getOperationType().toString(),
                        null,
                        null,
                        null)
                );

                recordRequestPayloadSizes(
                    ctx,
                    client,
                    requestPayloadSizeInBytes,
                    gatewayStats.getResponsePayloadSizeInBytes()
                );

                CosmosMeterOptions reqOptions = clientAccessor.getMeterOptions(
                    client,
                    CosmosMetricName.REQUEST_SUMMARY_GATEWAY_REQUESTS);
                if (reqOptions.isEnabled() &&
                    (!reqOptions.isDiagnosticThresholdsFilteringEnabled() || ctx.isThresholdViolated())) {
                    Counter requestCounter = Counter
                        .builder(reqOptions.getMeterName().toString())
                        .baseUnit("requests")
                        .description("Gateway requests")
                        .tags(getEffectiveTags(requestTags, reqOptions))
                        .register(compositeRegistry);
                    requestCounter.increment();
                }

                CosmosMeterOptions ruOptions = clientAccessor.getMeterOptions(
                    client,
                    CosmosMetricName.REQUEST_SUMMARY_GATEWAY_REQUEST_CHARGE);
                if (ruOptions.isEnabled() &&
                    (!ruOptions.isDiagnosticThresholdsFilteringEnabled() || ctx.isThresholdViolated())) {
                    double requestCharge = gatewayStats.getRequestCharge();
                    DistributionSummary requestChargeMeter = DistributionSummary
                        .builder(ruOptions.getMeterName().toString())
                        .baseUnit("RU (request unit)")
                        .description("Gateway Request RU charge")
                        .maximumExpectedValue(100_000d)
                        .publishPercentiles(ruOptions.getPercentiles())
                        .publishPercentileHistogram(ruOptions.isHistogramPublishingEnabled())
                        .tags(getEffectiveTags(requestTags, ruOptions))
                        .register(compositeRegistry);
                    requestChargeMeter.record(Math.min(requestCharge, 100_000d));
                }

                if (latency != null) {
                    CosmosMeterOptions latencyOptions = clientAccessor.getMeterOptions(
                        client,
                        CosmosMetricName.REQUEST_SUMMARY_GATEWAY_LATENCY);
                    if (latencyOptions.isEnabled() &&
                        (!latencyOptions.isDiagnosticThresholdsFilteringEnabled() || ctx.isThresholdViolated())) {
                        Timer requestLatencyMeter = Timer
                            .builder(latencyOptions.getMeterName().toString())
                            .description("Gateway Request latency")
                            .maximumExpectedValue(Duration.ofSeconds(300))
                            .publishPercentiles(latencyOptions.getPercentiles())
                            .publishPercentileHistogram(latencyOptions.isHistogramPublishingEnabled())
                            .tags(getEffectiveTags(requestTags, latencyOptions))
                            .register(compositeRegistry);
                        requestLatencyMeter.record(latency);
                    }
                }

                CosmosMeterOptions actualItemCountOptions = clientAccessor.getMeterOptions(
                    client,
                    CosmosMetricName.REQUEST_SUMMARY_GATEWAY_ACTUAL_ITEM_COUNT);

                if (actualItemCountOptions.isEnabled()
                    && (!actualItemCountOptions.isDiagnosticThresholdsFilteringEnabled() || ctx.isThresholdViolated())) {
                    DistributionSummary actualItemCountMeter = DistributionSummary
                        .builder(actualItemCountOptions.getMeterName().toString())
                        .baseUnit("item count")
                        .description("Gateway response actual item count")
                        .maximumExpectedValue(100_000d)
                        .publishPercentiles()
                        .publishPercentileHistogram(false)
                        .tags(getEffectiveTags(requestTags, actualItemCountOptions))
                        .register(compositeRegistry);
                    actualItemCountMeter.record(Math.max(0, Math.min(actualItemCount, 100_000d)));
                }

                CosmosMeterOptions opCountPerEvaluationOptions = clientAccessor.getMeterOptions(
                    client,
                    CosmosMetricName.REQUEST_SUMMARY_GATEWAY_BULK_OP_COUNT_PER_EVALUATION
                );

                if (opCountPerEvaluationOptions.isEnabled()
                    && (!opCountPerEvaluationOptions.isDiagnosticThresholdsFilteringEnabled() || ctx.isThresholdViolated())) {
                    DistributionSummary opCountPerEvaluationMeter = DistributionSummary
                        .builder(opCountPerEvaluationOptions.getMeterName().toString())
                        .baseUnit("item count")
                        .description("Operation count per evaluation")
                        .maximumExpectedValue(Double.MAX_VALUE)
                        .publishPercentiles()
                        .publishPercentileHistogram(false)
                        .tags(getEffectiveTags(requestTags, opCountPerEvaluationOptions))
                        .register(compositeRegistry);
                    opCountPerEvaluationMeter.record(Math.max(0, Math.min(opCountPerEvaluation, Double.MAX_VALUE)));
                }

                CosmosMeterOptions opRetriedCountPerEvaluationOptions = clientAccessor.getMeterOptions(
                    client,
                    CosmosMetricName.REQUEST_SUMMARY_GATEWAY_BULK_OP_RETRIED_COUNT_PER_EVALUATION
                );

                if (opRetriedCountPerEvaluationOptions.isEnabled()
                    && (!opRetriedCountPerEvaluationOptions.isDiagnosticThresholdsFilteringEnabled() || ctx.isThresholdViolated())) {
                    DistributionSummary opRetriedCountPerEvaluationMeter = DistributionSummary
                        .builder(opRetriedCountPerEvaluationOptions.getMeterName().toString())
                        .baseUnit("item count")
                        .description("Operation retried count per evaluation")
                        .maximumExpectedValue(Double.MAX_VALUE)
                        .publishPercentiles()
                        .publishPercentileHistogram(false)
                        .tags(getEffectiveTags(requestTags, opRetriedCountPerEvaluationOptions))
                        .register(compositeRegistry);
                    opRetriedCountPerEvaluationMeter.record(Math.max(0, Math.min(opRetriedCountPerEvaluation, Double.MAX_VALUE)));
                }

                CosmosMeterOptions globalOpCountOptions = clientAccessor.getMeterOptions(
                    client,
                    CosmosMetricName.REQUEST_SUMMARY_GATEWAY_BULK_GLOBAL_OP_COUNT
                );

                if (globalOpCountOptions.isEnabled()
                    && (!globalOpCountOptions.isDiagnosticThresholdsFilteringEnabled() || ctx.isThresholdViolated())) {
                    DistributionSummary globalOpCountMeter = DistributionSummary
                        .builder(globalOpCountOptions.getMeterName().toString())
                        .baseUnit("item count")
                        .description("Global operation count")
                        .maximumExpectedValue(Double.MAX_VALUE)
                        .publishPercentiles()
                        .publishPercentileHistogram(false)
                        .tags(getEffectiveTags(requestTags, globalOpCountOptions))
                        .register(compositeRegistry);
                    globalOpCountMeter.record(Math.max(0, Math.min(globalOpCount, Double.MAX_VALUE)));
                }

                CosmosMeterOptions targetMaxMicroBatchSizeOptions = clientAccessor.getMeterOptions(
                    client,
                    CosmosMetricName.REQUEST_SUMMARY_GATEWAY_BULK_TARGET_MAX_MICRO_BATCH_SIZE
                );

                if (targetMaxMicroBatchSizeOptions.isEnabled()
                    && (!targetMaxMicroBatchSizeOptions.isDiagnosticThresholdsFilteringEnabled() || ctx.isThresholdViolated())) {
                    DistributionSummary targetMaxMicroBatchSizeMeter = DistributionSummary
                        .builder(targetMaxMicroBatchSizeOptions.getMeterName().toString())
                        .baseUnit("item count")
                        .description("Target max micro batch size")
                        .maximumExpectedValue(101d)
                        .publishPercentiles()
                        .publishPercentileHistogram(false)
                        .tags(getEffectiveTags(requestTags, targetMaxMicroBatchSizeOptions))
                        .register(compositeRegistry);
                    targetMaxMicroBatchSizeMeter.record(Math.max(0, Math.min(targetMaxMicroBatchSize, 101d)));
                }

                recordRequestTimeline(
                    ctx,
                    client,
                    CosmosMetricName.REQUEST_DETAILS_GATEWAY_TIMELINE,
                    gatewayStats.getRequestTimeline(), requestTags);
            }
        }