Skip to content

Commit 804f238

Browse files
authored
chore: Optimizing hertz observability (cloudwego#845)
Signed-off-by: rogerogers <[email protected]>
1 parent 27cbcf7 commit 804f238

File tree

9 files changed

+134
-44
lines changed

9 files changed

+134
-44
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: "Instrumentation"
3+
date: 2023-10-28
4+
weight: 3
5+
keywords: ["Hertz", "Stats", "Instrumentation"]
6+
description: Hertz supports flexible enabling of basic and fine-grained Instrumentation
7+
---
8+
9+
## Stats Level
10+
11+
| Option | Description | Enable Strategy |
12+
| ------------- | --------------------------------------- | ------------------------------------------------ |
13+
| LevelDisabled | disable all events | When tracer is not available, enable by default. |
14+
| LevelBase | enable basic events | |
15+
| LevelDetailed | enable basic events and detailed events | When tracer is available, enable by default. |
16+
17+
## Stats Level Control
18+
19+
```go
20+
package main
21+
22+
import (
23+
"github.com/cloudwego/hertz/pkg/app/server"
24+
"github.com/cloudwego/hertz/pkg/common/tracer/stats"
25+
)
26+
27+
func main() {
28+
h := server.Default(server.WithTraceLevel(stats.LevelBase))
29+
h.Spin()
30+
}
31+
```
32+
33+
## Stats introduction
34+
35+
### Basic Stats Event
36+
37+
1. `HTTPStart` : Http start
38+
2. `HTTPFinish` : Http finish
39+
40+
### Detailed Stats Event
41+
42+
1. `ReadHeaderStart`:read header start
43+
2. `ReadHeaderFinish`:read header finish
44+
3. `ReadBodyStart`:read body start
45+
4. `ReadBodyFinish`:read body finish
46+
5. `ServerHandleStart`:server handler start
47+
6. `ServerHandleFinish`:server handler finish
48+
7. `WriteStart`:write response start
49+
8. `WriteFinish`:write response finish
50+
51+
> If you do not want to record this information, you can either not register any tracer or set the tracking strategy to LevelDisabled, and the framework will not record this information.
52+
>
53+
> - **Setting the stats level of a certain node in the tracing to `LevelDisabled` will result in the loss of spans/metrics for that node, but it will not cause the tracing to be interrupted.**
54+
> - **Not registering any tracer will also result in the loss of spans/metrics for that node, and it will also cause the tracing to be interrupted.**
55+
56+
### Timeline
57+
58+
![timeline](/img/docs/hertz_tracing_timeline.png)

content/en/docs/hertz/tutorials/observability/monitoring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: "Monitoring"
33
linkTitle: "Monitoring"
44
date: 2022-06-21
5-
weight: 3
5+
weight: 4
66
keywords: ["Monitoring"]
77
description: "Hertz provides monitoring capabilities."
88

content/en/docs/hertz/tutorials/observability/open-telemetry.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "OpenTelemetry"
33
date: 2022-09-13
4-
weight: 4
4+
weight: 5
55
keywords: ["OpenTelemetry"]
66
description: "Hertz provides openTelemetry capabilities."
77
---

content/en/docs/hertz/tutorials/observability/tracing.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Tracing"
33
linkTitle: "Tracing"
44
weight: 2
55
keywords: ["Tracing"]
6-
description: "Hertz provides tracing capabilities."
6+
description: "Hertz provides tracing capabilities"
77
---
88

99
In microservices, link tracing is a very important capability, which plays an important role in quickly locating problems, analyzing business bottlenecks, and restoring the link status of a request. Hertz provides the capability of link tracking and also supports user-defined link tracking.
@@ -41,26 +41,13 @@ type HTTPStats interface {
4141
}
4242
```
4343

44-
Events include:
44+
Hertz supports flexible enabling of basic and fine-grained instrumentation. For more details, please refer to [Instrumentation](../instrumentation).
4545

46-
```go
47-
HTTPStart = newEvent(httpStart, LevelBase) // Request start
48-
HTTPFinish = newEvent(httpFinish, LevelBase) // Request end
49-
50-
ServerHandleStart = newEvent(serverHandleStart, LevelDetailed) // Business handler start
51-
ServerHandleFinish = newEvent(serverHandleFinish, LevelDetailed) // Business handler end
52-
ReadHeaderStart = newEvent(readHeaderStart, LevelDetailed) // Read header start
53-
ReadHeaderFinish = newEvent(readHeaderFinish, LevelDetailed) // Read header end
54-
ReadBodyStart = newEvent(readBodyStart, LevelDetailed) // Read body start
55-
ReadBodyFinish = newEvent(readBodyFinish, LevelDetailed) // Read body end
56-
WriteStart = newEvent(writeStart, LevelDetailed) // Write response start
57-
WriteFinish = newEvent(writeFinish, LevelDetailed) // Write response end
58-
```
59-
60-
The above information is available at Finish
46+
The hertz-contrib provides the extension methods for [opentracing](https://opentracing.io/) and [opentelemetry](https://opentelemetry.io), and the hertz-examples also provide the [opentracing example](https://github.com/cloudwego/hertz-examples/tree/main/tracer) and [opentelemetry example](https://github.com/cloudwego/hertz-examples/tree/main/opentelemetry).
6147

62-
At the same time, if you don't want to log this information, you don't have to register any tracer, and the framework stops logging this information.
48+
Related repositories:
6349

64-
An extension for opentracing is provided in hertz-contrib, and a demo for calling from http to rpc is also available in [hertz-examples](https://github.com/cloudwego/hertz-examples/tree/main/tracer).
50+
- [hertz opentelemetry](https://github.com/hertz-contrib/obs-opentelemetry/)
51+
- [hertz opentracing](https://github.com/hertz-contrib/tracer)
6552

66-
Related Repository: https://github.com/hertz-contrib/tracer
53+
> OpenTracing has been deprecated. For specific reasons, please refer to [Deprecating OpenTracing](https://github.com/opentracing/specification/issues/163). Unless there are specific reasons, it is recommended to use [Opentelemetry](../open-telemetry).
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: "埋点"
3+
date: 2023-10-28
4+
weight: 3
5+
keywords: ["Hertz", "Stats", "Instrumentation"]
6+
description: Hertz 支持灵活启用基本埋点和细粒度埋点
7+
---
8+
9+
## 埋点粒度
10+
11+
| 参数 | 介绍 | 启用策略 |
12+
| ------------- | ------------------------ | ---------------------- |
13+
| LevelDisabled | 禁用埋点 | 无 tracer 时,默认启用 |
14+
| LevelBase | 仅启用基本埋点 | |
15+
| LevelDetailed | 启用基本埋点和细粒度埋点 | 有 tracer 时,默认启用 |
16+
17+
## 埋点粒度控制
18+
19+
```go
20+
package main
21+
22+
import (
23+
"github.com/cloudwego/hertz/pkg/app/server"
24+
"github.com/cloudwego/hertz/pkg/common/tracer/stats"
25+
)
26+
27+
func main() {
28+
h := server.Default(server.WithTraceLevel(stats.LevelBase))
29+
h.Spin()
30+
}
31+
```
32+
33+
## 埋点说明
34+
35+
### 基本埋点
36+
37+
1. `HTTPStart` : 请求开始
38+
2. `HTTPFinish` : 请求结束
39+
40+
### 细粒度埋点
41+
42+
1. `ReadHeaderStart`:读取 header 开始
43+
2. `ReadHeaderFinish`:读取 header 结束
44+
3. `ReadBodyStart`:读取 body 开始
45+
4. `ReadBodyFinish`:读取 body 结束
46+
5. `ServerHandleStart`:业务 handler 开始
47+
6. `ServerHandleFinish`:业务 handler 结束
48+
7. `WriteStart`:写 response 开始
49+
8. `WriteFinish`:写 response 结束
50+
51+
> 如果不希望记录这些信息,可以不注册任何 tracer 或者将埋点策略设置为 `LevelDisabled`,则框架不会记录这些信息。
52+
>
53+
> - **将链路中某节点埋点策略设置为 `LevelDisabled`,会导致本节点 span/matrics 丢失,不会导致链路中断**
54+
> - **不注册任何 tracer,也会导致本节点 span/metrics 丢失,同时也会导致链路中断**
55+
56+
### 时序图
57+
58+
![timeline](/img/docs/hertz_tracing_timeline.png)

content/zh/docs/hertz/tutorials/observability/monitoring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "监控"
33
linkTitle: "监控"
4-
weight: 3
4+
weight: 4
55
keywords: ["监控"]
66
description: "Hertz 提供的监控能力。"
77

content/zh/docs/hertz/tutorials/observability/open-telemetry.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "OpenTelemetry"
33
date: 2022-09-01
4-
weight: 4
4+
weight: 5
55
keywords: ["OpenTelemetry"]
66
description: "Hertz 提供的 OpenTelemetry 能力。"
77
---

content/zh/docs/hertz/tutorials/observability/tracing.md

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ title: "链路追踪"
33
linkTitle: "链路追踪"
44
weight: 2
55
keywords: ["链路追踪"]
6-
description: "Hertz 提供的链路追踪能力。"
7-
6+
description: "Hertz 提供的链路追踪能力"
87
---
98

109
在微服务中,链路追踪是一项很重要的能力,在快速定位问题,分析业务瓶颈,还原一次请求的链路情况等方面发挥重要作用。Hertz 提供了链路追踪的能力,也支持用户自定义链路跟踪。
@@ -42,25 +41,13 @@ type HTTPStats interface {
4241
}
4342
```
4443

45-
事件包括:
46-
47-
```go
48-
HTTPStart = newEvent(httpStart, LevelBase) // 请求开始
49-
HTTPFinish = newEvent(httpFinish, LevelBase) // 请求结束
44+
Hertz 支持灵活启用基本埋点和细粒度埋点,具体请参考 [埋点](../instrumentation)
5045

51-
ServerHandleStart = newEvent(serverHandleStart, LevelDetailed) // 业务 handler 开始
52-
ServerHandleFinish = newEvent(serverHandleFinish, LevelDetailed) // 业务 handler 结束
53-
ReadHeaderStart = newEvent(readHeaderStart, LevelDetailed) // 读取 header 开始
54-
ReadHeaderFinish = newEvent(readHeaderFinish, LevelDetailed) // 读取 header 结束
55-
ReadBodyStart = newEvent(readBodyStart, LevelDetailed) // 读取 body 开始
56-
ReadBodyFinish = newEvent(readBodyFinish, LevelDetailed) // 读取 body 结束
57-
WriteStart = newEvent(writeStart, LevelDetailed) // 写 response 开始
58-
WriteFinish = newEvent(writeFinish, LevelDetailed) // 写 response 结束
59-
```
46+
hertz-contrib 中提供了 [opentracing](https://opentracing.io/)[opentelemetry](https://opentelemetry.io/) 的扩展方式,也在 hertz-examples 中提供了 [opentracing example](https://github.com/cloudwego/hertz-examples/tree/main/tracer) 以及 [opentelemetry example](https://github.com/cloudwego/hertz-examples/tree/main/opentelemetry)
6047

61-
在 Finish 时可以获取到上述信息。
48+
相关仓库:
6249

63-
同时,如果不希望记录这些信息,可以不注册任何 tracer,则框架停止对这些信息的记录。
50+
- [hertz opentelemetry](https://github.com/hertz-contrib/obs-opentelemetry/)
51+
- [hertz opentracing](https://github.com/hertz-contrib/tracer)
6452

65-
hertz-contrib 中提供了 opentracing 的扩展方式,也在 [hertz-examples](https://github.com/cloudwego/hertz-examples/tree/main/tracer) 提供了可以从 http 到 rpc 调用的 demo。
66-
仓库:https://github.com/hertz-contrib/tracer
53+
> OpenTracing 已经被弃用,具体原因可以查看 [Deprecating OpenTracing](https://github.com/opentracing/specification/issues/163),如果没有特殊的理由,推荐使用 [Opentelemetry](../open-telemetry)
29.6 KB
Loading

0 commit comments

Comments
 (0)