如何获取有关 *gin.Context.Request.Context() 的详细信息?

huangapple go评论74阅读模式
英文:

How to get details information about *gin.Context.Request.Context()

问题

我正在尝试使用otelgin来跟踪我的项目,它说where required. It is available from gin.Context.Request.Context(),那么我如何从gin.Context中获取详细信息以查看它是否起作用呢?

官方文档中,在初始化跟踪器提供程序时,他们将stdout作为导出器添加进去:

func initTracer() (*sdktrace.TracerProvider, error) {
	// stdout as exporter
    exporter, err := stdout.New(stdout.WithPrettyPrint())
	if err != nil {
		return nil, err
	}
	tp := sdktrace.NewTracerProvider(
		sdktrace.WithSampler(sdktrace.AlwaysSample()),
		sdktrace.WithBatcher(exporter),
	)
	otel.SetTracerProvider(tp)
 
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
	return tp, nil
}

这是部分命令行输出:

"Name": "/users/:id",
	"SpanContext": {
		"TraceID": "e7a43d30c0e507b4c59fd65dc3bc6d77",
		"SpanID": "636c22201c903573",
		"TraceFlags": "01",
		"TraceState": "",
		"Remote": false
	},
	"Parent": {
		"TraceID": "00000000000000000000000000000000",
		"SpanID": "0000000000000000",
		"TraceFlags": "00",
		"TraceState": "",
		"Remote": false
	},
	"SpanKind": 2,
	"StartTime": "2022-11-12T16:02:07.871843+08:00",
	"EndTime": "2022-11-12T16:02:07.871843+08:00",

但是有没有办法从gin.Context中获取这些信息?或者我可以从gin.Context中获取导出器的详细信息吗?我尝试添加一个中间件来捕获它们,如下所示:

func TracerGetMiddleware(c *gin.Context) {
	//var tracer oteltrace.Tracer
	//tracerInterface, ok := c.Get("otel-go-contrib-tracer")
	//if ok {
	///	tracer, ok = tracerInterface.(oteltrace.Tracer)
	//}
	//tracer.Start(c, "test")
	fmt.Println(c.Request.Context())
}

但是这是c.Request.Context()的输出:

(type *http.contextKey, val <not Stringer>).WithValue(type *http.contextKey, val [::1]:8088).WithCancel.WithCancel.WithValue(type trace.traceContextKeyType, val <not Stringer>)
英文:

I'm trying to use otelgin to trace my project, and it's says that where required. It is available from gin.Context.Request.Context(), so how can i get the detail information from gin.context to see if it works.

Officially, they add a stdout as the exporter when init tracer provider:

func initTracer() (*sdktrace.TracerProvider, error) {
	// stdout as exporter
    exporter, err := stdout.New(stdout.WithPrettyPrint())
	if err != nil {
		return nil, err
	}
	tp := sdktrace.NewTracerProvider(
		sdktrace.WithSampler(sdktrace.AlwaysSample()),
		sdktrace.WithBatcher(exporter),
	)
	otel.SetTracerProvider(tp)
 
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
	return tp, nil
}

here is part of the command line output:

&quot;Name&quot;: &quot;/users/:id&quot;,
	&quot;SpanContext&quot;: {
		&quot;TraceID&quot;: &quot;e7a43d30c0e507b4c59fd65dc3bc6d77&quot;,
		&quot;SpanID&quot;: &quot;636c22201c903573&quot;,
		&quot;TraceFlags&quot;: &quot;01&quot;,
		&quot;TraceState&quot;: &quot;&quot;,
		&quot;Remote&quot;: false
	},
	&quot;Parent&quot;: {
		&quot;TraceID&quot;: &quot;00000000000000000000000000000000&quot;,
		&quot;SpanID&quot;: &quot;0000000000000000&quot;,
		&quot;TraceFlags&quot;: &quot;00&quot;,
		&quot;TraceState&quot;: &quot;&quot;,
		&quot;Remote&quot;: false
	},
	&quot;SpanKind&quot;: 2,
	&quot;StartTime&quot;: &quot;2022-11-12T16:02:07.871843+08:00&quot;,
	&quot;EndTime&quot;: &quot;2022-11-12T16:02:07.871843+08:00&quot;,

But is there any way to get this form gin.Context? Or can I get the exporter's detail information from gin.Context? I have tried to add a middleware to capture them below:

func TracerGetMiddleware(c *gin.Context) {
	//var tracer oteltrace.Tracer
	//tracerInterface, ok := c.Get(&quot;otel-go-contrib-tracer&quot;)
	//if ok {
	///	tracer, ok = tracerInterface.(oteltrace.Tracer)
	//}
	//tracer.Start(c, &quot;test&quot;)
	fmt.Println(c.Request.Context())
}

But this is output of c.Request.Context()

(type *http.contextKey, val &lt;not Stringer&gt;).WithValue(type *http.contextKey, val [::1]:8088).WithCancel.WithCancel.WithValue(type trace.traceContextKeyType, val &lt;not Stringer&gt;)

答案1

得分: 0

目前,我理解你最初想要得到的答案是如何在gin.context中获取traceID、SpanID和其他数据。

你的初始想法是通过otelgin在上下文中传递跟踪信息,然后通过gin的中间件捕获traceID等信息以进行zap日志记录。但当时你不知道如何获取TraceID等信息,后来在gint-contrib/zap库中找到了正确的方法。

英文:

At present, I understand the answer I originally wanted to get, which is to get traceID, SpanID and other data in gin.context

import (
	&quot;fmt&quot;
	&quot;github.com/gin-gonic/gin&quot;
	oteltrace &quot;go.opentelemetry.io/otel/trace&quot;
)

func GetIDMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		if oteltrace.SpanFromContext(c.Request.Context()).SpanContext().IsValid() {
			TraceID := oteltrace.SpanFromContext(c.Request.Context()).SpanContext().TraceID().String()
			SpanID := oteltrace.SpanFromContext(c.Request.Context()).SpanContext().SpanID().String()
		    fmt.Println(TraceID)
			fmt.Println(SpanID)
        }
	}
}

My initial idea was to pass trace information in the context through otelgin, and then capture traceID and so on through context in gin's middleware for zap logging, but at that time I didn't know how to get TraceID and other information, and when I saw gin-contrib/zap library find the right way.

huangapple
  • 本文由 发表于 2022年11月12日 16:55:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/74411580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定