item-atlas/docs/deployment/observability.md

56 lines
6.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 预发布监控部署
应用、采集器和查询端都使用 Kubernetes 内部服务。预发布由五个独立的上游 Helm release 组成。Prometheus 拉指标,Alloy 从 Kubernetes API 读应用日志并接收 OTLP 链路,Grafana 统一查询。告警规则由 Prometheus 计算,当前在 Prometheus 页面查看。
| 组件 | Chart 版本 | 内网入口 | 存储 |
| --- | --- | --- | --- |
| Prometheus | `prometheus-community/prometheus@30.0.0` | `atlas-prometheus-server:80` | 2 GiB,保留 3 天,容量目标 1200 MB |
| Loki | `grafana-community/loki@21.6.0` | `atlas-loki:3100` | 2 GiB,单进程 TSDB 文件存储,保留 72 小时 |
| Tempo | `grafana-community/tempo@2.4.0` | 查询 `atlas-tempo:3200`,OTLP HTTP `atlas-tempo:4318` | 2 GiB,单进程本地块存储,保留 72 小时 |
| Alloy | `grafana/alloy@1.16.0` | OTLP HTTP `atlas-alloy:4318` | 收发缓冲 |
| Grafana | `grafana-community/grafana@14.5.1` | `atlas-grafana:80` | 1 GiB,数据源由 Helm 提供 |
Tempo 固定在 2.x 的单进程部署。升级到 3.x 前要单独核对消息队列、存储和迁移要求。现有预发布的开发型 StorageClass 由 SSD VM 提供,文件和 TSDB 的保留占用以实际磁盘监控为准。 [Prometheus Chart](https://github.com/prometheus-community/helm-charts/tree/main/charts/prometheus) · [Grafana Helm](https://grafana.com/docs/grafana/latest/setup-grafana/installation/helm/) · [Loki 单进程](https://grafana.com/docs/loki/latest/setup/install/helm/install-monolithic/) · [Tempo 部署](https://grafana.com/docs/tempo/latest/set-up-for-tracing/setup-tempo/deploy/) · [Alloy Helm](https://grafana.com/docs/alloy/latest/set-up/install/kubernetes/)
## 安装和更新
先核对工作集群、`item-atlas-staging` 命名空间、默认 StorageClass 和 SSD 余量。由集群维护账号创建监控命名空间,并在其下创建 `atlas-grafana-admin` Secret。将管理员密码以受控文件输入,执行后安全移除临时文件。
```sh
kubectl --context kind-atlas-dev create namespace item-atlas-observability
kubectl --context kind-atlas-dev -n item-atlas-observability create secret generic atlas-grafana-admin \
--from-literal=admin-user=admin --from-file=admin-password=/path/to/managed-password-file
sh deploy/observability/install.sh kind-atlas-dev
kubectl --context kind-atlas-dev -n item-atlas-observability get deployment,statefulset,pvc,service
```
安装脚本核对现有环境,用服务端 dry run 校验 Role、RoleBinding 和 ServiceAccount,再执行 Helm `--atomic --wait`。若环境只能从内网下载,先按脚本的版本将官方 Chart 下载为 `组件-版本.tgz`,通过 `ATLAS_CHART_DIR` 指定其目录。Prometheus 只需在预发布读 Pod,Alloy 另需读同一命名空间的 `pods/log`。应用遥测端点、数据查询端和 Collector 均使用 ClusterIP。上线环境应当在入口只转业务路径,并为采集和查询配置对应的网络权限。 [Kubernetes RBAC](https://kubernetes.io/docs/reference/access-authn-authz/rbac/)
## Java 接入的位置
1. 三个服务的 `pom.xml` 都引入 `spring-boot-starter-actuator`、`micrometer-registry-prometheus`、`spring-boot-starter-opentelemetry`,版本由 Spring Boot BOM 管。各自 `application.yml` 打开 Prometheus endpoint,启用 HTTP 延迟直方图并设置结构化日志。
2. HTTP 服务和从 Boot 注入的 `RestClient.Builder` 继承 Spring 的观测配置。用代码自行 new 出的 HTTP 客户端要单独检查透传和采集。配置 OTLP HTTP exporter 时,应用的 URL 带 `/v1/traces`。Alloy → Tempo 的 exporter 使用基础地址,由 exporter 拼接对应路径。
3. `DraftService.confirm` 在同一事务保存操作和 W3C `traceparent`。`OutboxWorker.deliverTask` 恢复父上下文,给每次尝试开一个 span,用结构化字段写操作号、尝试序号和结果。操作号和 trace ID 只用于日志与链路,指标标签使用有界的状态和结果。
4. `deploy/helm/item-atlas/values.yaml` 的 `observability.otlpTracesEndpoint` 为空时关闭 OTLP 导出。Drone 的预发布 Promotion 已传入 Alloy 地址,采样默认 10%。专项演练可通过 `observability.traceSampleRate` 设置采样,完成后恢复原值。 [Spring Boot 指标](https://docs.spring.io/spring-boot/reference/actuator/metrics.html) · [Spring Boot 链路](https://docs.spring.io/spring-boot/reference/actuator/tracing.html) · [结构化日志](https://docs.spring.io/spring-boot/reference/features/logging.html#features.logging.structured)
## 验收和排错
从有集群权限的终端做回环端口转发。下面各条命令分别运行,Grafana 使用安装时保存在 Secret 管理渠道的账号。
```sh
kubectl -n item-atlas-observability port-forward svc/atlas-grafana 3000:80
kubectl -n item-atlas-observability port-forward svc/atlas-prometheus-server 9090:80
kubectl -n item-atlas-observability port-forward svc/atlas-loki 3100:3100
kubectl -n item-atlas-observability port-forward svc/atlas-tempo 3200:3200
```
| 目的 | 查询和预期 |
| --- | --- |
| 确认采集 | Prometheus 查看 `up{job="item-atlas"}`,三个稳定工作负载正常时均为 1。若全无结果,依次查 Pod 标签、RBAC、目标发现与 endpoint。 |
| 查看 HTTP P95 | `histogram_quantile(0.95, sum by (le, workload) (rate(http_server_requests_seconds_bucket{job="item-atlas",uri!~"/actuator/.*"}[5m])))`,演练前先造一定真实业务流量。 |
| 查录入积压 | `max by (state) (atlas_outbox_operations{job="item-atlas"})`,同一数据库可能被多副本采集,取最大值。用 `increase(atlas_outbox_delivery_attempts_total[5m])` 看尝试结果。 |
| 查日志 | Grafana Explore 中使用 `{namespace="item-atlas-staging", app="item-atlas-intake-service"} | json | operationId = "操作号"`。 |
| 查链路 | 从上述日志的 TraceID 进入 Tempo。被采样的请求可沿网关到录入,再看到 `atlas.outbox.delivery` 到物品服务。还可用 `GET /api/traces/<traceId>` 验证 Tempo 已接收。 |
遇到日志、指标或链路缺一项,先检查该信号的写入端和中间组件。指标与日志时间窗以查询端为准。合成探针和短时间提采样适合演练。升级各 Chart 前记录 `helm history`,故障用 `helm rollback RELEASE REVISION -n item-atlas-observability --wait`,随后再查 Pod、PVC、数据源与代表请求。监控中断时,应用回退和数据库恢复按照 [后端交付和回退](backend-release.md) 单独处理。