Jetty

本文档介绍如何配置 Google Kubernetes Engine 部署,以便使用 Google Cloud Managed Service for Prometheus 从 Jetty 收集指标。本页面介绍如何完成以下任务:

  • 设置导出器以报告指标。
  • 为 Managed Service for Prometheus 配置 PodMonitoring 资源以收集导出的指标。
  • 在 Cloud Monitoring 中访问信息中心以查看指标。
  • 配置提醒规则以监控指标。

以下说明仅在您将代管式收集功能与 Managed Service for Prometheus 搭配使用时适用。 如果您使用的是自行部署的收集功能,请参阅 JMX 导出器的源代码库以了解安装信息。

这些说明仅作为示例提供,应该适用于大多数 Kubernetes 环境。如果由于安全或组织政策的限制,您在安装应用或导出器时遇到问题,我们建议您查看开源文档以获取支持。

如需了解 Jetty 的信息,请参阅 Jetty

前提条件

如需使用 Managed Service for Prometheus 和代管式收集功能从 Jetty 收集指标,您的部署必须满足以下要求:

  • 您的集群必须运行 Google Kubernetes Engine 1.21.4-gke.300 或更高版本。
  • 您必须运行 Managed Service for Prometheus,并启用代管式收集功能。如需了解详情,请参阅代管式收集功能使用入门

  • 如需使用 Cloud Monitoring 中提供的信息中心进行 Jetty 集成,您必须使用 jmx-exporter 0.17.0 版或更高版本。

    如需详细了解可用的信息中心,请参阅查看信息中心

安装 Jetty 导出器

我们建议您将 Jetty 导出器 jmx-exporter 作为边车的 Jetty 工作负载进行安装。如需了解如何使用 Sidecar,请参阅具有多容器 pod 的 Kubernetes 上的扩展应用

如需将 jmx-exporter 作为边车安装到 Jetty,请修改 Jetty 配置,如以下示例所示:

# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
  name: jetty-exporter
data:
  config.yaml: |
    hostPort: 127.0.0.1:1099
    rules:
    - pattern: "org.eclipse.jetty.deploy<(.*)><>(.*)"
      name: "jetty_deploy_$2"
      labels:
        labelList: "$1"
    - pattern: "org.eclipse.jetty.deploy.providers<type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_deploy_providers_$3"
      labels:
        id: "$2"
        type: "$1"
    - pattern: "org.eclipse.jetty.io<context=(.*), type=(.*), id=(\\d+)><>(\\w+)"
      name: "jetty_io_$4"
      labels:
        id: "$3"
        type: "$2"
        context: "$1"
    - pattern: "org.eclipse.jetty.io<type=(.*), id=(\\d+)><>(\\w+)"
      name: "jetty_io_$3"
      labels:
        id: "$2"
        type: "$1"
    - pattern: "org.eclipse.jetty.jmx<type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_jmx_$3"
      labels:
        id: "$2"
        type: "$1"
    - pattern: "org.eclipse.jetty.logging<type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_logging_$3"
      labels:
        id: "$2"
        type: "$1"
    - pattern: "org.eclipse.jetty.server<type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_server_$3"
      labels:
        id: "$2"
        type: "$1"
    - pattern: "org.eclipse.jetty.server<context=(.*), type=(.*), id=(\\d+)><>(\\w+)"
      name: "jetty_server_$4"
      labels:
        id: "$3"
        type: "$2"
        context: "$1"
    - pattern: "org.eclipse.jetty.server.handler<type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_server_handler_$3"
      labels:
        id: "$2"
        type: "$1"
    - pattern: "org.eclipse.jetty.server.session<type=(.*), id=(\\d+)><>(\\w+)"
      name: "jetty_server_session_$3"
      labels:
        id: "$2"
        type: "$1"
    - pattern: "org.eclipse.jetty.server.session<context=(.*), type=(.*), id=(\\d+)><>(\\w+)"
      name: "jetty_server_session_$4"
      labels:
        id: "$3"
        type: "$2"
        context: "$1"
    - pattern: "org.eclipse.jetty.util<type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_util_$3"
      labels:
        id: "$2"
        type: "$1"
    - pattern: "org.eclipse.jetty.util.component<type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_util_component_$3"
      labels:
        id: "$2"
        type: "$1"
    - pattern: "org.eclipse.jetty.servlet<context=(.*), type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_servlet_$4"
      labels:
        id: "$3"
        type: "$2"
        context: "$1"
    - pattern: "org.eclipse.jetty.servlet<context=(.*), type=(\\w+), name=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_servlet_$5"
      labels:
        id: "$4"
        type: "$2"
        name: "$3"
        context: "$1"
    - pattern: "org.eclipse.jetty.util.thread<type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_util_thread_$3"
      labels:
        id: "$2"
        type: "$1"
    - pattern: "org.eclipse.jetty.util.thread.strategy<context=(.*), type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_util_thread_strategy_$4"
      labels:
        id: "$3"
        type: "$2"
        context: "$1"
    - pattern: "org.eclipse.jetty.webapp<context=(.*), type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_webapp_$4"
      labels:
        id: "$3"
        type: "$2"
        context: "$1"
    - pattern: "org.eclipse.jetty.websocket.common<context=(.*), type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_websocket_jakarta_common_$4"
      labels:
        id: "$3"
        type: "$2"
        context: "$1"
    - pattern: "org.eclipse.jetty.websocket.jakarta.common<context=(.*), type=(\\w+), id=(\\d+)><>(\\w+)"
      name: "jetty_websocket_jakarta_common_$4"
      labels:
        id: "$3"
        type: "$2"
        context: "$1"
    - pattern: "org.eclipse.jetty.(.*)"
      name: jetty_metric_$1
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jetty
spec:
  selector:
    matchLabels:
+      app.kubernetes.io/name: jetty
  replicas: 1
  template:
    metadata:
      labels:
+        app.kubernetes.io/name: jetty
    spec:
      containers:
      - name: jetty
        image: jetty:11-jdk17-alpine
+        ports:
+        - containerPort: 1099
+          name: jmx
+        env:
+        - name: JAVA_OPTIONS
+          value: "-Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.rmi.port=1099"
+        args:
+        - "--module=jmx"
+        - "jetty.threadPool.maxThreads=500"
+      - name: exporter
+        image: bitnami/jmx-exporter:0.17.0
+        ports:
+        - containerPort: 9000
+          name: prometheus
+        command:
+          - java
+          - -jar
+          - jmx_prometheus_httpserver.jar
+        args:
+          - "9000"
+          - /opt/jmx_exporter/config.yaml
+        volumeMounts:
+        - mountPath: /opt/jmx_exporter/config.yaml
+          subPath: config.yaml
+          name: jetty-exporter
+      volumes:
+      - name: jetty-exporter
+        configMap:
+          name: jetty-exporter
+          items:
+          - key: config.yaml
+            path: config.yaml
---

您必须将开头为 + 符号的所有行添加到您的配置中。

如需从本地文件应用配置更改,请运行以下命令:

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

您还可以使用 Terraform 管理您的配置。

定义 PodMonitoring 资源

对于目标发现,Managed Service for Prometheus Operator 需要与同一命名空间中的 Jetty 导出器对应的 PodMonitoring 资源。

您可以使用以下 PodMonitoring 配置:

# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: monitoring.googleapis.com/v1
kind: PodMonitoring
metadata:
  name: jetty
  labels:
    app.kubernetes.io/name: jetty
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  endpoints:
  - port: prometheus
    scheme: http
    interval: 30s
    path: /metrics
  selector:
    matchLabels:
      app.kubernetes.io/name: jetty

确保标签选择器和端口与安装 Jetty 导出器中使用的选择器和端口匹配。

如需从本地文件应用配置更改,请运行以下命令:

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

您还可以使用 Terraform 管理您的配置。

定义规则和提醒

您可以使用以下 Rules 配置来定义 Jetty 指标提醒:

# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: monitoring.googleapis.com/v1
kind: Rules
metadata:
  name: jetty-rules
  labels:
    app.kubernetes.io/component: rules
    app.kubernetes.io/name: jetty-rules
    app.kubernetes.io/part-of: google-cloud-managed-prometheus
spec:
  groups:
  - name: jetty
    interval: 30s
    rules:
    - alert: JettyLongThreadQueue
      annotations:
        description: |-
          Jetty long thread queue
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Jetty long thread queue (instance {{ $labels.instance }})
      expr: jetty_util_thread_queueSize >= 10
      for: 5m
      labels:
        severity: critical
    - alert: JettyNoThreadsAvailable
      annotations:
        description: |-
          Jetty no threads available
            VALUE = {{ $value }}
            LABELS: {{ $labels }}
        summary: Jetty no threads available (instance {{ $labels.instance }})
      expr: jetty_util_thread_idleThreads == 0
      for: 5m
      labels:
        severity: critical

如需从本地文件应用配置更改,请运行以下命令:

kubectl apply -n NAMESPACE_NAME -f FILE_NAME

您还可以使用 Terraform 管理您的配置。

如需详细了解如何将规则应用于您的集群,请参阅代管式规则评估和提醒

您可以根据您的应用调整提醒阈值。

验证配置

您可以使用 Metrics Explorer 验证您是否正确配置了 Jetty 导出器。Cloud Monitoring 可能需要一两分钟时间来注入您的指标。

要验证指标是否已注入,请执行以下操作:

  1. 在 Google Cloud 控制台的导航面板中,选择 Monitoring,然后选择  Metrics Explorer

    进入 Metrics Explorer

  2. 在查询构建器窗格的工具栏中,选择名为  MQL PromQL 的按钮。
  3. 验证已在语言切换开关中选择 PromQL。语言切换开关位于同一工具栏中,用于设置查询的格式。
  4. 输入并运行以下查询:
    up{job="jetty", cluster="CLUSTER_NAME", namespace="NAMESPACE_NAME"}

查看信息中心

Cloud Monitoring 集成包括 Jetty Prometheus 概览信息中心。当您配置集成时,系统会自动安装信息中心。您还可以在不安装集成的情况下查看信息中心的静态预览。

如需查看已安装的信息中心,请执行以下操作:

  1. 在 Google Cloud 控制台的导航面板中,选择 Monitoring,然后选择  信息中心

    前往信息中心

  2. 选择信息中心列表标签页。
  3. 选择集成类别。
  4. 点击信息中心的名称,例如 Jetty Prometheus 概览

如需查看信息中心的静态预览,请执行以下操作:

  1. 在 Google Cloud 控制台的导航面板中,选择 Monitoring,然后选择  集成

    前往集成

  2. 点击 Kubernetes Engine 部署平台过滤条件。
  3. 找到 Jetty 集成,然后点击查看详情
  4. 选择信息中心标签页。

问题排查

如需了解如何排查指标注入问题,请参阅排查注入端问题中的从导出器收集的问题