你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

适用于 Java 的 Micrometer 指标

适用范围: NoSQL

适用于 Azure Cosmos DB 的 Java SDK 使用 Micrometer 工具在常用的可观测性系统(如 Prometheus)中实现客户端指标。 本文包含用于将指标抓取到 Prometheus 中的说明和代码片段,取自此示例此处介绍了由 SDK 提供的指标的完整列表。 如果客户端部署在 Azure Kubernetes 服务 (AKS) 上,还可以将适用于 Prometheus 的 Azure Monitor 托管服务与自定义抓取配合使用,请参阅此处的文档。

使用来自 Prometheus 的指标

可以从此处下载 prometheus。 若要在适用于 Azure Cosmos DB 的 Java SDK 中通过 Prometheus 使用 Micrometer 指标,请首先确保已导入注册表和客户端所需的库:

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.6.6</version>
</dependency>

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_httpserver</artifactId>
    <version>0.5.0</version>
</dependency>

在应用程序中,向遥测配置提供 prometheus 注册表。请注意,可以设置各种诊断阈值,这有助于将使用的指标限制为最感兴趣的指标:

//prometheus meter registry
PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);

//provide the prometheus registry to the telemetry config
CosmosClientTelemetryConfig telemetryConfig = new CosmosClientTelemetryConfig()
        .diagnosticsThresholds(
                new CosmosDiagnosticsThresholds()
                        // Any requests that violate (are lower than) any of the below thresholds that are set
                        // will not appear in "request-level" metrics (those with "rntbd" or "gw" in their name).
                        // The "operation-level" metrics (those with "ops" in their name) will still be collected.
                        // Use this to reduce noise in the amount of metrics collected.
                        .setRequestChargeThreshold(10)
                        .setNonPointOperationLatencyThreshold(Duration.ofDays(10))
                        .setPointOperationLatencyThreshold(Duration.ofDays(10))
        )
        // Uncomment below to apply sampling to help further tune client-side resource consumption related to metrics.
        // The sampling rate can be modified after Azure Cosmos DB Client initialization – so the sampling rate can be
        // modified without any restarts being necessary.
        //.sampleDiagnostics(0.25)
        .clientCorrelationId("samplePrometheusMetrics001")
        .metricsOptions(new CosmosMicrometerMetricsOptions().meterRegistry(prometheusRegistry)
                //.configureDefaultTagNames(CosmosMetricTagName.PARTITION_KEY_RANGE_ID)
                .applyDiagnosticThresholdsForTransportLevelMeters(true)
        );

启动本地 HttpServer 服务器,以向 Prometheus 公开计量注册表指标:

try {
    HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
    server.createContext("/metrics", httpExchange -> {
        String response = prometheusRegistry.scrape();
        int i = 1;
        httpExchange.sendResponseHeaders(200, response.getBytes().length);
        try (OutputStream os = httpExchange.getResponseBody()) {
            os.write(response.getBytes());
        }
    });
    new Thread(server::start).start();
} catch (IOException e) {
    throw new RuntimeException(e);
}

确保在创建 CosmosClient 时传递 clientTelemetryConfig

//  Create async client
client = new CosmosClientBuilder()
    .endpoint(AccountSettings.HOST)
    .key(AccountSettings.MASTER_KEY)
    .clientTelemetryConfig(telemetryConfig)
    .consistencyLevel(ConsistencyLevel.SESSION) //make sure we can read our own writes
    .contentResponseOnWriteEnabled(true)
    .buildAsyncClient();

将应用程序客户端的终结点添加到 prometheus.yml 时,在“targets”中添加域名和端口。 例如,如果 prometheus 与应用客户端运行在同一服务器上,则可以按如下所示添加 localhost:8080targets

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090", "localhost:8080"]

现在,可以使用来自 Prometheus 的指标:

Screenshot of metrics graph in Prometheus explorer.

后续步骤