Golang 微服务监控浅谈

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
云拨测,每月3000次拨测额度
简介: 监控作为度量可视化工具在服务治理中一直是承担比较重要的角色。一个良好的架构设计,监控一定是完善且使用便捷的。这篇文章我将会介绍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

目录
相关文章
|
8天前
|
监控 网络协议 Go
应用监控 eBPF 版:实现 Golang 微服务的无侵入应用监控
应用监控 eBPF 版:实现 Golang 微服务的无侵入应用监控
109671 118
|
6月前
|
Cloud Native Go 微服务
golang 微服务中的断路器 hystrix
golang 微服务中的断路器 hystrix
|
8天前
|
消息中间件 Go API
Golang深入浅出之-Go语言中的微服务架构设计与实践
【5月更文挑战第4天】本文探讨了Go语言在微服务架构中的应用,强调了单一职责、标准化API、服务自治和容错设计等原则。同时,指出了过度拆分、服务通信复杂性、数据一致性和部署复杂性等常见问题,并提出了DDD拆分、使用成熟框架、事件驱动和配置管理与CI/CD的解决方案。文中还提供了使用Gin构建HTTP服务和gRPC进行服务间通信的示例。
33 0
|
8天前
|
Prometheus 监控 Cloud Native
Golang深入浅出之-Go语言中的分布式追踪与监控系统集成
【5月更文挑战第4天】本文探讨了Go语言中分布式追踪与监控的重要性,包括追踪的三个核心组件和监控系统集成。常见问题有追踪数据丢失、性能开销和监控指标不当。解决策略涉及使用OpenTracing或OpenTelemetry协议、采样策略以及聚焦关键指标。文中提供了OpenTelemetry和Prometheus的Go代码示例,强调全面可观测性对微服务架构的意义,并提示选择合适工具和策略以确保系统稳定高效。
148 5
|
8天前
|
数据采集 运维 监控
微服务监控:守护系统稳定的终极防线
微服务监控在数字化时代日益重要,它帮助运维和开发人员实时监测服务性能、状态和安全,确保微服务架构的稳定性和可用性。构建微服务监控体系需关注合理监控策略、数据采集处理、可视化及告警。数据采集的三大支柱是指标、日志和链路追踪。监控涵盖基础设施、系统、应用和业务层面。通过优化监控体系、融合业务场景和建立跨团队协作,可提升监控效果。未来,AI和云计算将推动微服务监控向更精准、高效和安全的方向发展。
77 0
|
6月前
|
负载均衡 算法 网络协议
golang 微服务的负载均衡
golang 微服务的负载均衡
|
8天前
|
XML JSON Go
Golang微服务基础技术
Golang微服务基础技术
36 2
|
8天前
|
数据采集 存储 监控
如何监控微服务调用
【2月更文挑战第2天】搭建一个服务监控系统,涉及数据采集、数据传输、数据处理、数据展示等多个环节。
|
8天前
|
Prometheus 监控 Cloud Native
微服务框架(二十二)Prometheus + Grafana 可视化监控
此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。 本文为Prometheus + Grafana 可视化监控的介绍,下篇为Prometheus + Grafana...
|
8天前
|
Prometheus 监控 Cloud Native
微服务框架(十九)Spring Boot 可视化监控 Prometheus + Grafana
  此系列文章将会描述Java框架Spring Boot、服务治理框架Dubbo、应用容器引擎Docker,及使用Spring Boot集成Dubbo、Mybatis等开源框架,其中穿插着Spring Boot中日志切面等技术的实现,然后通过gitlab-CI以持续集成为Docker镜像。   本文为Spring Boot 通过 micrometer 的监控门面,实现Prometheus + G...