Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

基于现有的 Tracer 设计,如何监控 inflight 指标? #1094

Closed
sunhailin-Leo opened this issue Apr 18, 2024 · 10 comments
Closed

基于现有的 Tracer 设计,如何监控 inflight 指标? #1094

sunhailin-Leo opened this issue Apr 18, 2024 · 10 comments
Labels
question Further information is requested

Comments

@sunhailin-Leo
Copy link

Describe the Question

疑惑点:想问一下 hertz server(hertz/pkg/protocol/http1/server.go at develop · cloudwego/hertz) 这一段逻辑前置在 req.ReadHeader(https://github.com/cloudwego/hertz/blob/develop/pkg/protocol/http1/server.go#L195)是有什么 tracer 设计上的考虑嘛?
问题:由于上面的疑惑点会导致无法监控 inflight 指标(想监控请求中的条数)

Expected behavior

如何能够监控到 inflight request 指标?

Hertz version:

v0.8.1

Environment:

go1.21.7

@li-jin-gou
Copy link
Member

li-jin-gou commented Apr 18, 2024

  1. 为了细粒度买点,所以在很前置的位置开始了
  2. 参考 https://www.cloudwego.io/zh/docs/hertz/tutorials/observability/instrumentation/ 在 ReadBodyFinish 完成时扩展一个 tracer 读取 body & 解析数据获得条数。可以看看 https://www.cloudwego.io/zh/docs/hertz/tutorials/observability/tracing/https://www.cloudwego.io/zh/docs/hertz/tutorials/framework-exten/monitor/

@sunhailin-Leo
Copy link
Author

sunhailin-Leo commented Apr 18, 2024

  1. 为了细粒度买点,所以在很前置的位置开始了
  2. 参考 https://www.cloudwego.io/zh/docs/hertz/tutorials/observability/instrumentation/ 在 ReadBodyFinish 完成时扩展一个 tracer 读取 body & 解析数据获得条数。可以看看 https://www.cloudwego.io/zh/docs/hertz/tutorials/observability/tracing/https://www.cloudwego.io/zh/docs/hertz/tutorials/framework-exten/monitor/
  • 第一点明白了
  • 第二点目前不太明白怎么实现。我想在下面 Start 的时候就获取到 FullPath 的信息。扩展的这部分有没有一些例子可以参考下?
// genLabels make labels values.
func genLabels(c *app.RequestContext) prometheus.Labels {
	labels := make(prometheus.Labels)
	labels[labelMethod] = defaultValIfEmpty(string(c.Request.Method()), unknownLabelValue)
	labels[labelStatusCode] = defaultValIfEmpty(strconv.Itoa(c.Response.Header.StatusCode()), unknownLabelValue)
	labels[labelPath] = defaultValIfEmpty(c.FullPath(), unknownLabelValue)

	return labels
}

// Start record the start of server handling request from client.
func (s *ServerTracer) Start(ctx context.Context, c *app.RequestContext) context.Context {
	// inflight
	gaugeIncDec(s.serverHandledInFlight, true, genLabels(c))

	return ctx
}

// Finish record the ending of server handling request from client.
func (s *ServerTracer) Finish(_ context.Context, c *app.RequestContext) {
	if c.GetTraceInfo().Stats().Level() == stats.LevelDisabled {
		return
	}

	httpStart := c.GetTraceInfo().Stats().GetEvent(stats.HTTPStart)
	httpFinish := c.GetTraceInfo().Stats().GetEvent(stats.HTTPFinish)
	if httpFinish == nil || httpStart == nil {
		return
	}
	cost := httpFinish.Time().Sub(httpStart.Time())
	_ = counterAdd(s.serverHandledCounter, 1, genLabels(c))
	_ = histogramObserve(s.serverHandledHistogram, cost, genLabels(c))
	gaugeIncDec(s.serverHandledInFlight, false, genLabels(c))
}

@li-jin-gou
Copy link
Member

cc @rogerogers 帮看下哈,这里是不是要判断一下所处的阶段

@li-jin-gou
Copy link
Member

得在 Finish 里获取数据,埋点只是记录耗时...

@sunhailin-Leo
Copy link
Author

得在 Finish 里获取数据,埋点只是记录耗时...

  • 如果在 Finish 里获取数据,那已经 flight 完了 😭,inflight 的指标就意义不大了~

@sunhailin-Leo
Copy link
Author

@li-jin-gou

@rogerogers
Copy link
Contributor

目前 start 获取不到 path 信息

@sunhailin-Leo
Copy link
Author

目前 start 获取不到 path 信息

那现阶段有比较好的解决方案嘛?🤔

@rogerogers
Copy link
Contributor

@li-jin-gou

这种中间件的方式,用 hertz 中间件也可以实现类似的

@sunhailin-Leo
Copy link
Author

@li-jin-gou

这种中间件的方式,用 hertz 中间件也可以实现类似的

那暂时先按这种方式实现先迁移吧。

@li-jin-gou li-jin-gou added the question Further information is requested label Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

3 participants