Golang 微服务监控浅谈

本文涉及的产品
可观测链路 OpenTelemetry 版,每月50GB免费额度
可观测可视化 Grafana 版,10个用户账号 1个月
可观测监控 Prometheus 版,每月50GB免费额度
简介: 监控作为度量可视化工具在服务治理中一直是承担比较重要的角色。一个良好的架构设计,监控一定是完善且使用便捷的。这篇文章我将会介绍Go微服务的度量与监控方案,参照我的使用经验,在Consul、Kubernetes不同部署环境下的使用。

监控作为度量可视化工具在服务治理中一直是承担比较重要的角色。一个良好的架构设计,监控一定是完善且使用便捷的。这篇文章我将会介绍Go微服务的度量与监控方案,参照我的使用经验,在Consul、Kubernetes不同部署环境下的使用。

方案选择上我们采用流行的Prometheus+Grafana方案,相对Graphite、InfluxDB Prometheus采用的主动拉取的方案的,大大降低了接入难度。

一、监控样例

监控样例

二、什么是度量

度量(Metrics), 简单来说就是个数字,它可以是当前服务内存使用率,可以是基于时间的访问次数,度量是我们用来评价系统状态的一个确定值, 是评价是否达到某个指标的计算来源。

1. 度量类型

根据使用场景的不同,我们常用的有Gauge、Counter、Histogram、Summary等几种数据类型。

其中Gauge是要用来存储一些诸如CPU 内存之类的瞬时值;Counter用来存储诸如访问次数之类的累积值;Histogram要比较复杂些,它常用来计算服务响应时间。

2. 我们该怎么设计度量

度量方案设计与度量值确定需要我们对服务架构有一定了解,比如GRPC微服务 我们关心的就是QPS、延迟、成功率(错误率)等数据; API Gateway 我们除了关心上述几个度量值,我们还关心并发请求数量,当前带宽等数据;无论哪种类型的服务与业务我们都关心运行环境的CPU/内存使用率等数据,以确定当前资源水位是否安全,是否有扩缩容的必要。

余下内容我们重点介绍业务服务度量,服务器与中间件监控请参照 Prometheus文档选择开源exporters

三、度量应该怎么记录

1.数据记录

上文提到服务数据度量大部分采用侵入式方案,不同于服务网格Sidecar的采集方式,侵入式打点能获取到更多的状态数据。下面的样例就是 prometheus 官方提供的SDK使用样例,非常简单。同时Prometheus还提供Java、Python、Rudy等官方SDK。

package main

import (
    "log"
    "net/http"

    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
    cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
   
   
        Name: "cpu_temperature_celsius",
        Help: "Current temperature of the CPU.",
    })
    hdFailures = prometheus.NewCounterVec(
        prometheus.CounterOpts{
   
   
            Name: "hd_errors_total",
            Help: "Number of hard-disk errors.",
        },
        []string{
   
   "device"},
    )
)

func init() {
   
   
    // Metrics have to be registered to be exposed:
    prometheus.MustRegister(cpuTemp)
    prometheus.MustRegister(hdFailures)
}

func main() {
   
   
    cpuTemp.Set(65.3)
    hdFailures.With(prometheus.Labels{
   
   "device":"/dev/sda"}).Inc()

    // The Handler function provides a default handler to expose metrics
    // via an HTTP server. "/metrics" is the usual endpoint for that.
    http.Handle("/metrics", promhttp.Handler())
    log.Fatal(http.ListenAndServe(":8080", nil))
}

2.GRPC下的使用方案

对于GRPC服务,我推荐使用三方包来简化接入流程,尽可能少的对业务代码产生影响。

  • go-grpc-prometheus grpc ecosystem提供的一个封装方案,采用拦截器方案。优点: 支持grpc v1.26.x 以下版本,代码基本无侵入;缺点:纯面向 GRPC的度量,扩展不便。
  • QGRPC 优点:代码侵入较小,提供更多grpc封装; 缺点:使用不灵活。
  • Qkits 优点:能方便扩充度量;缺点:侵入项目略多,文档支持不够(当然代码也没几行),GRPC需要1.26以上。

四、度量采集

对于一个配置好度量的服务,应该能通过 http://localhost/metrics 访问到类似如下数据


# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 7.7299e-05
go_gc_duration_seconds{quantile="0.25"} 9.7869e-05
go_gc_duration_seconds{quantile="0.5"} 0.000115081
go_gc_duration_seconds{quantile="0.75"} 0.000141982
go_gc_duration_seconds{quantile="1"} 0.088763487
go_gc_duration_seconds_sum 54.35006894
go_gc_duration_seconds_count 93984
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge

....

qietv_grpc_server_request_total{method="WinnerList",service="rpc.gRPCLottery"} 11

数据采集

1. VM 与Docker 部署方案。

参考配置

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: qietv_go
    consul_sd_configs:
      - server: 172.17.3.79:8500
        datacenter: tx
        tags:
          - metrics
    relabel_configs:
      - source_labels:  ["__meta_consul_service_id"]
        target_label: "service_id"
      - source_labels:  ["__meta_consul_service"]
        target_label: "service"
  - job_name: 'mongo'
    static_configs:
    - targets: ['172.17.1.130:9216']
  - job_name: etcd
    scheme: https
    tls_config:
      cert_file: /opt/etcd/cert/79/client.crt
      key_file: /opt/etcd/cert/79/client.key
      insecure_skip_verify: true
    static_configs:
    - targets: ['172.17.3.79:2379','172.17.3.80:2379','172.17.3.81:2379']

2. 基于Consul服务发现的微服务集群

参考

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: qietv_go
    consul_sd_configs:
      - server: 172.17.3.79:8500
        datacenter: tx
        tags:
          - metrics
    relabel_configs:
      - source_labels:  ["__meta_consul_service_id"]
        target_label: "service_id"
      - source_labels:  ["__meta_consul_service"]
        target_label: "service"

3. Kubernetes 环境

  • 集群安装prometheus-operator
  • 新增ServiceMonitor资源

    kubectl apply -n monitoring -f <<
    apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    spec:
    endpoints:
    - interval: 10s
      port: http
    namespaceSelector:
      matchNames:
      - appnamespace
    selector:
      matchLabels:
        metrics: prometheus
    targetLabels:
    - app
    - service
    - project
    - metrics
    
    EOF
    
  • Kubernetes Workload 添加Selecter
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    labels:
      app: nginx
      metrics: promethues
    

原文发布于:https://notes.icool.io/metrics

相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
目录
相关文章
|
21天前
|
运维 监控 数据可视化
ARMS的微服务监控
【8月更文挑战第23天】
39 6
|
4月前
|
监控 网络协议 Go
应用监控 eBPF 版:实现 Golang 微服务的无侵入应用监控
应用监控 eBPF 版:实现 Golang 微服务的无侵入应用监控
109758 125
|
17天前
|
存储 设计模式 前端开发
|
1月前
|
负载均衡 监控 Java
SpringCloud常见面试题(一):SpringCloud 5大组件,服务注册和发现,nacos与eureka区别,服务雪崩、服务熔断、服务降级,微服务监控
SpringCloud常见面试题(一):SpringCloud 5大组件,服务注册和发现,nacos与eureka区别,服务雪崩、服务熔断、服务降级,微服务监控
SpringCloud常见面试题(一):SpringCloud 5大组件,服务注册和发现,nacos与eureka区别,服务雪崩、服务熔断、服务降级,微服务监控
|
22天前
|
Prometheus 监控 Cloud Native
使用Prometheus搞定微服务监控
使用Prometheus搞定微服务监控
使用Prometheus搞定微服务监控
|
1月前
|
存储 Prometheus 监控
Golang 搭建 WebSocket 应用(六) - 监控
Golang 搭建 WebSocket 应用(六) - 监控
31 3
|
19天前
|
Prometheus 监控 Cloud Native
微服务的监控与可观测性
【8月更文第29天】在微服务架构中,确保每个服务的健康状态和性能表现是非常重要的。为了达到这一目标,我们需要实施一套完整的监控和可观测性方案。本篇文章将介绍如何通过日志、指标和追踪来监测微服务的状态和性能,并提供相应的代码示例。
34 0
|
22天前
|
Prometheus 监控 Cloud Native
基于prometheus的微服务指标监控
基于prometheus的微服务指标监控
|
1月前
|
存储 监控 负载均衡
微服务架构中的服务治理与监控技术
【8月更文挑战第3天】微服务架构中的服务治理与监控是确保系统稳定、高效运行的重要手段。通过构建注册中心实现服务的自动注册和发现,通过部署监控工具实现对服务的全面监控,可以有效地提高系统的可靠性和可用性。未来,随着技术的不断发展,服务治理与监控技术也将不断完善和优化,为微服务架构的广泛应用提供更加坚实的支撑。
|
2月前
|
监控 Java 微服务
Spring Boot微服务部署与监控的实战指南
【7月更文挑战第19天】Spring Boot微服务的部署与监控是保障应用稳定运行和高效维护的重要环节。通过容器化部署和云平台支持,可以实现微服务的快速部署和弹性伸缩。而利用Actuator、Prometheus、Grafana等监控工具,可以实时获取应用的运行状态和性能指标,及时发现并解决问题。在实际操作中,还需根据应用的具体需求和场景,选择合适的部署和监控方案,以达到最佳效果。