刘沙河 刘沙河
首页
  • Go语言基础

    • 数据类型
    • 反射
    • Go指针
  • Go语言进阶

    • go泛型
    • go条件编译
    • cgo教程
    • Go协程调度原理及GPM模型
    • Go内存管理
    • Go垃圾回收机制
    • Go语言内存对齐
  • Go语言实现原理

    • channel 实现原理
    • slice 实现原理
    • map 实现原理
    • sync.Mutex 实现原理
    • 乐观锁CAS 实现原理
    • singlefight 实现原理
  • gin框架

    • gin中间件原理
    • gin路由原理
  • gorm

    • GORM介绍和使用
    • GORM_CURD操作指南
  • go测试

    • benchmark基准测试
    • pprof 性能分析
  • python进阶

    • Numpy&Pandas
    • celery分布式任务队列
  • Django

    • Django 常见命令
    • middleware中间件
    • Django缓存系统
    • Django信号系统
    • Django REST Framework
  • Flask

    • Flask基础知识总结
    • Flask-SQLAlchemy
  • 爬虫

    • aiohttp
    • scrapy框架
  • Mysql

    • Mysql存储引擎和索引
    • MySQL主从复制
    • Mysql读写分离
    • 数据库分库分表
    • Mysql锁
    • Mysql事务和MVCC原理
    • 分库分表带来的读扩散问题
  • Redis

    • redis基础和数据类型
    • redis主从架构
    • redis哨兵架构
    • redis集群模式
    • 如何保证缓存和数据库双写一致
    • redis底层数据结构
    • redis分布式锁
  • Elasticsearch

    • es基本概念
    • es基础语法
    • es倒排索引
  • etcd

    • Go操作etcd
    • Raft原理
    • etcd分布式锁
  • kafka

    • 消息队列MQ总结
    • kafka 概述及原理
    • kafka 消费问题记录
    • 零拷贝技术
    • kafka分区规范
  • RabbitMQ

    • rabbitMQ基础
    • Go操作rabbitmq
  • RocketMQ

    • 可靠消息队列 rocketMQ
  • Http&Https

    • http&https
    • TCP和UDP
    • Ping 原理
  • RPC

    • RPC初识
    • grpc初识和实现
  • gRPC

    • grpc 初识
    • grpc 上下文 metadata
    • grpc 健康检查
    • grpc keepalive
    • grpc 命名解析
    • grpc 中间件&拦截器
    • grpc 负载均衡
    • grpc 身份认证
    • grpc 超时重试
    • grpc 链路追踪
    • grpc-gw将gRPC转RESTfu api
    • grpc-gw自定义选项
  • protobuf

    • protobuf 进阶
    • protobuf 编码原理
  • Docker

    • Docker基础
    • Docker常用命令
    • Dockerfile
    • Docker-Compose
    • Docker多阶段构建
    • Docker Config 教程
    • Docker Swarm 教程
    • Docker Stack 教程
    • Docker Buildx 教程
  • k8s

    • k8s 基础概念
    • k8s 集群架构
    • k8s 工作负载
    • Pod 网络
    • Service 网络
    • 外部接入网络
    • 一张图搞懂k8s各种pod
    • k8s 存储抽象
    • mac快速启动k8s
    • 自制申威架构k8s-reloader
  • go-kit

    • go-kit初识
    • go-kit启动http服务
    • go-kit集成gin启动服务
    • go-kit集成grpc和protobuf
    • go-kit中间件
    • go-kit服务注册发现与负载均衡
    • go-kit限流和熔断
    • go-kit链路追踪
    • go-kit集成Prometheus
  • 设计模式

    • 初识设计模式
    • 创建型模式
    • 结构型模式
    • 行为模式
  • 数据结构

    • 时间轮
    • 堆、双向链表、环形队列
    • 队列:优先队列
    • 队列:延迟队列
  • 算法

    • 递归算法
    • 枚举算法
    • 动态规划
    • 回溯算法
    • 分治算法
    • 贪心算法
    • LRU和LFU
    • 一致性哈希

花开半夏,半夏花开
首页
  • Go语言基础

    • 数据类型
    • 反射
    • Go指针
  • Go语言进阶

    • go泛型
    • go条件编译
    • cgo教程
    • Go协程调度原理及GPM模型
    • Go内存管理
    • Go垃圾回收机制
    • Go语言内存对齐
  • Go语言实现原理

    • channel 实现原理
    • slice 实现原理
    • map 实现原理
    • sync.Mutex 实现原理
    • 乐观锁CAS 实现原理
    • singlefight 实现原理
  • gin框架

    • gin中间件原理
    • gin路由原理
  • gorm

    • GORM介绍和使用
    • GORM_CURD操作指南
  • go测试

    • benchmark基准测试
    • pprof 性能分析
  • python进阶

    • Numpy&Pandas
    • celery分布式任务队列
  • Django

    • Django 常见命令
    • middleware中间件
    • Django缓存系统
    • Django信号系统
    • Django REST Framework
  • Flask

    • Flask基础知识总结
    • Flask-SQLAlchemy
  • 爬虫

    • aiohttp
    • scrapy框架
  • Mysql

    • Mysql存储引擎和索引
    • MySQL主从复制
    • Mysql读写分离
    • 数据库分库分表
    • Mysql锁
    • Mysql事务和MVCC原理
    • 分库分表带来的读扩散问题
  • Redis

    • redis基础和数据类型
    • redis主从架构
    • redis哨兵架构
    • redis集群模式
    • 如何保证缓存和数据库双写一致
    • redis底层数据结构
    • redis分布式锁
  • Elasticsearch

    • es基本概念
    • es基础语法
    • es倒排索引
  • etcd

    • Go操作etcd
    • Raft原理
    • etcd分布式锁
  • kafka

    • 消息队列MQ总结
    • kafka 概述及原理
    • kafka 消费问题记录
    • 零拷贝技术
    • kafka分区规范
  • RabbitMQ

    • rabbitMQ基础
    • Go操作rabbitmq
  • RocketMQ

    • 可靠消息队列 rocketMQ
  • Http&Https

    • http&https
    • TCP和UDP
    • Ping 原理
  • RPC

    • RPC初识
    • grpc初识和实现
  • gRPC

    • grpc 初识
    • grpc 上下文 metadata
    • grpc 健康检查
    • grpc keepalive
    • grpc 命名解析
    • grpc 中间件&拦截器
    • grpc 负载均衡
    • grpc 身份认证
    • grpc 超时重试
    • grpc 链路追踪
    • grpc-gw将gRPC转RESTfu api
    • grpc-gw自定义选项
  • protobuf

    • protobuf 进阶
    • protobuf 编码原理
  • Docker

    • Docker基础
    • Docker常用命令
    • Dockerfile
    • Docker-Compose
    • Docker多阶段构建
    • Docker Config 教程
    • Docker Swarm 教程
    • Docker Stack 教程
    • Docker Buildx 教程
  • k8s

    • k8s 基础概念
    • k8s 集群架构
    • k8s 工作负载
    • Pod 网络
    • Service 网络
    • 外部接入网络
    • 一张图搞懂k8s各种pod
    • k8s 存储抽象
    • mac快速启动k8s
    • 自制申威架构k8s-reloader
  • go-kit

    • go-kit初识
    • go-kit启动http服务
    • go-kit集成gin启动服务
    • go-kit集成grpc和protobuf
    • go-kit中间件
    • go-kit服务注册发现与负载均衡
    • go-kit限流和熔断
    • go-kit链路追踪
    • go-kit集成Prometheus
  • 设计模式

    • 初识设计模式
    • 创建型模式
    • 结构型模式
    • 行为模式
  • 数据结构

    • 时间轮
    • 堆、双向链表、环形队列
    • 队列:优先队列
    • 队列:延迟队列
  • 算法

    • 递归算法
    • 枚举算法
    • 动态规划
    • 回溯算法
    • 分治算法
    • 贪心算法
    • LRU和LFU
    • 一致性哈希
  • go-kit

    • 【go-kit教程】go-kit初识
    • 【go-kit教程】go-kit启动http服务
    • 【go-kit教程】go-kit集成gin启动服务
    • 【go-kit教程】go-kit集成grpc和protobuf
    • 【go-kit教程】go-kit中间件
    • 【go-kit教程】go-kit服务注册发现与负载均衡
    • 【go-kit教程】go-kit限流和熔断
    • 【go-kit教程】go-kit链路追踪
    • 【go-kit教程】go-kit集成Prometheus
  • 微服务
  • go-kit
bigox
2023-02-23

【go-kit教程】go-kit集成Prometheus

  • 在 Go kit 中集成 Prometheus 进行 API 监控可以帮助开发人员更好地了解系统的性能和行为,提高系统的可观察性和可靠性。下面是一个简单的示例,演示如何在 Go kit 中集成 Prometheus 进行 API 监控:

    package main
    
    import (
    	"net/http"
    
    	"github.com/go-kit/kit/log"
    	"github.com/go-kit/kit/metrics/prometheus"
    	"github.com/go-kit/kit/metrics/provider"
    	"github.com/go-kit/kit/transport"
    	httptransport "github.com/go-kit/kit/transport/http"
    	"github.com/prometheus/client_golang/prometheus/promhttp"
    )
    
    func main() {
    	// 创建 Prometheus 监控器
    	fieldKeys := []string{"method", "error"}
    	requestCount := prometheus.NewCounterFrom(provider.NewProvider(), prometheus.CounterOpts{
    		Namespace: "my_api",
    		Name:      "request_count",
    		Help:      "Number of requests received.",
    	}, fieldKeys)
    	requestLatency := prometheus.NewSummaryFrom(provider.NewProvider(), prometheus.SummaryOpts{
    		Namespace: "my_api",
    		Name:      "request_latency",
    		Help:      "Total duration of requests in microseconds.",
    	}, fieldKeys)
    
    	// 创建 endpoint
    	helloEndpoint := func() string {
    		return "Hello, World!"
    	}
    	helloEndpoint = transport.NewServer(
    		helloEndpoint,
    		func(ctx context.Context, r *http.Request) (interface{}, error) {
    			return nil, nil
    		},
    		func(ctx context.Context, w http.ResponseWriter, response interface{}) error {
    			w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    			fmt.Fprint(w, response.(string))
    			return nil
    		},
    		transport.ServerErrorLogger(log.NewNopLogger()),
    		transport.ServerErrorEncoder(errorEncoder),
    		transport.ServerBefore(prometheus.HTTPToContext()),
    	).Endpoint()
    
    	// 创建 HTTP server
    	r := mux.NewRouter()
    	r.Handle("/hello", httptransport.NewServer(
    		helloEndpoint,
    		httpDecodeRequest,
    		httpEncodeResponse,
    		httptransport.ServerBefore(prometheus.HTTPToContext()),
    		httptransport.ServerErrorLogger(log.NewNopLogger()),
    		httptransport.ServerErrorEncoder(errorEncoder),
    		httptransport.ServerBefore(prometheus.HTTPToContext()),
    	)).Methods("GET")
    	r.Handle("/metrics", promhttp.Handler())
    
    	fmt.Println("listening on :8080...")
    	http.ListenAndServe(":8080", r)
    }
    
    func httpDecodeRequest(_ context.Context, r *http.Request) (interface{}, error) {
    	return nil, nil
    }
    
    func httpEncodeResponse(_ context.Context, w http.ResponseWriter, response interface{}) error {
    	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    	fmt.Fprint(w, response.(string))
    	return nil
    }
    
    func errorEncoder(_ context.Context, err error, w http.ResponseWriter) {
    	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
    	w.WriteHeader(http.StatusBadRequest)
    	fmt.Fprint(w, err.Error())
    }
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
#Go
上次更新: 2023/04/16, 18:35:33
【go-kit教程】go-kit链路追踪

← 【go-kit教程】go-kit链路追踪

最近更新
01
go与http代理
05-24
02
自制申威架构k8s-reloader
12-06
03
Docker Buildx 教程
12-01
更多文章>
Theme by Vdoing | Copyright © 2020-2024 小刘扎扎 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式