https://github.com/asppj/lolita
基于gin 微服务opentrace集成
https://github.com/asppj/lolita
gin-web grpc jaeger
Last synced: 4 months ago
JSON representation
基于gin 微服务opentrace集成
- Host: GitHub
- URL: https://github.com/asppj/lolita
- Owner: asppj
- License: mit
- Created: 2019-11-30T04:16:22.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-03-16T08:17:05.000Z (over 2 years ago)
- Last Synced: 2024-11-23T12:56:57.414Z (7 months ago)
- Topics: gin-web, grpc, jaeger
- Language: Go
- Size: 442 KB
- Stars: 14
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.com/asppj/lolita)
1. Logging - 用于记录离散的事件。例如,应用程序的调试信息或错误信息。它是我们诊断问题的依据。
2. Metrics - 用于记录可聚合的数据。例如,队列的当前深度可被定义为一个度量值,在元素入队或出队时被更新;HTTP 请求个数可被定义为一个计数器,新请求到来时进行累加。
3. Tracing - 用于记录请求范围内的信息。例如,一次远程方法调用的执行过程和耗时。它是我们排查系统性能问题的利器。## Opentrace
1. 埋点:gin-web,http 请求,grpc 请求封装并埋点;
2. 收集:zipkin,AppDash,jaeger 选择其一(阿里云日志服务优先)
3. 展示:### 埋点
1. Inject 请求方注入
- http header
- grpc with 接口
2. Extract 服务方提取
- http header
- grpc with 接口### 1. gin-web 服务方 埋点(完成,待测)
> 添加中间件的方式
### 2. requests 请求 埋点(完成,待测)
>封装 GET、POST、DELET、PUT 等 http 请求方法
### 3. grpc 埋点(完成,待测)
>grpc 使用 grpc 提供的钩子函数
### 4. DB 驱动埋点
>1. mongodb 埋点 (完成)
>2. redis 埋点 (未开始)
>3. es 埋点 (完成)## opentrance-go 介绍
### 1. Trace 调用链
### 2. Span 调用链节点
#### 1. Span-Tag 调用链节点标签
```text
ext.PeerHostname.Set(sp, c.Request.Host)
ext.PeerAddress.Set(sp, c.Request.RemoteAddr)
ext.PeerService.Set(sp, c.ClientIP())
ext.HTTPStatusCode.Set(sp, uint16(statusCode))
ext.HTTPMethod.Set(sp, c.Request.Method)
ext.HTTPUrl.Set(sp, c.Request.URL.Path)
```#### 2. Span-Log 调用链节点日志
```golang
span.LogKV("Request:", ctx.Request)
span.LogFields("Request:", ctx.Request)
```#### 3. Baggage item 全局范围不属于 span
```golang
// set
span.SetBaggageItem("greeting", greeting)
// get
greeting := span.BaggageItem("greeting")
```#### 4. Sampling,采样[0,1]
>sampling.priority - integer
```text
1. const,全量采集,采样率设置0,1 分别对应打开和关闭
2. probabilistic ,概率采集,默认万份之一,0~1之间取值,
3. rateLimiting ,限速采集,每秒只能采集一定量的数据
4. remote ,一种动态采集策略,根据当前系统的访问量调节采集策略
```#### inject/extract
>childOf、followOf
```text
1. opentracing.GlobalTracer().Extract 方法提取HTTP头中的spanContexts
2. opentracing.ChildOf 方法基于提取出来的spanContexts生成新的child spanContexts
3. opentracing.GlobalTracer().StartSpan 方法生成一个新的span
4. github.com/opentracing/opentracing-go/ext 通过ext可以为追踪添加一些tag来展示更多信息,比如URL,请求类型(GET,POST...), 返回码
5. sp.Finish() 结束这一个span
```#### context 传递 span
```golang
// 存储到 context 中
ctx := context.Background()
ctx = opentracing.ContextWithSpan(ctx, span)
//....// 其他过程获取并开始子 span
span, ctx := opentracing.StartSpanFromContext(ctx, "newspan")
defer span.Finish()
// StartSpanFromContext 会将新span保存到ctx中更新```
### context 传递
#### 网络方式传递
Extrect 从 Header 中获取
Extrect 从 grpc Ctx 中提取
inject 注入 Header
Inject 注入 grpc Ctx```golang
# 进程中
opentracing.SpanFromContext(ctx)
# 或
span, ctx := opentracing.StartSpanFromContext(ctx, "newspan")
gin.Context:ctx.Request.Context()
gin.Context!=context.Context!=opentracing.SpanContext
mongodb/redis/es:```
>
> - context 不再使用 DefaultContext()
> - 统一使用 gin.request.Context()[因为携带了 span ]
> - 另外添加中间件自定义每个接口的超时时间## jaeger 搭建(本地)
> all_in_one 镜像 tools->jaeger->docker-compose.yml
## prometheus metrics 统计
### prometheus 收集数据
### Grafana 展示数据
### prometheus 指标类型
1. Counter 单调增
2. Guage 可增减
3. Historygram 直方图
4. Summary 聚合图(1-10 占比,10-20 占比。。。)#### gin-ext web 中间件
1. 启动时长-uptime
2. 请求总数-http_request_count_total
3. 请求延时-http_request_duration_seconds
4. 请求字节-http_request_size_bytes
5. 响应字节-http_response_size_bytes#### [grpc 中间件](https://github.com/grpc-ecosystem/go-grpc-middleware)
#### kafka golang 驱动封装
##### 消费者
1. 消费延时-kafka_consumer_duration_seconds
2. 消费成功数-kafka_consumer_success_total
3. 消费失败数-kafka_consumer_failed_total
4. Payload 字节数-kafka_consumer_payload_byte_size##### 生产者
1. 生产延时-kafka_product_duration_seconds
2. 生产成功数-kafka_product_success_total
3. 生产失败数-kafka_product_failed_total
4. Payload 字节数-kafka_product_payload_byte_size#### kafka exporter 监控
> 与版本有关[参考](https://blog.csdn.net/sweatOtt/article/details/79090175)
### 官方 sdk
- github.com/prometheus/client_golang
### 工具
[dem 开发辅助工具 https://github.com/asppj/dem](https://github.com/asppj/dem)### opentrace 参考资料
1. [几种分布式调用链监控组件的比较](https://juejin.im/post/5a0579e6f265da4326524f0f#heading-0)
2. [opentracing 翻译版](https://wu-sheng.gitbooks.io/opentracing-io/content/pages/spec.html)
3. [grpc-opentracing 中间件](https://godoc.org/github.com/grpc-ecosystem/grpc-opentracing/go/otgrpc)
4. [openTracing 讲解](https://github.com/yurishkuro/opentracing-tutorial)
5. [阿里云日志服务配置 aliyun-log-jaeger](https://github.com/aliyun/aliyun-log-jaeger/blob/master/README_CN.md?)### prometheus 参考资料
1. [prometheus 介绍](https://www.imhanjm.com/2019/10/06/%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3prometheus(go%20sdk)/)
2. [【监控】Kafka - 详细指标](https://www.jianshu.com/p/92ae7e5992e2)
3. [Grafana 与数据可视化](https://yunlzheng.gitbook.io/prometheus-book/part-ii-prometheus-jin-jie/grafana/grafana-panels)
3. [其他参考](https://github.com/apache/skywalking-kubernetes)### Thanks
[](https://www.jetbrains.com/?from=lolita)