add common tracing labels

This commit is contained in:
John Guo 2021-01-27 19:50:32 +08:00
parent 152c472bc2
commit 2c15aad0e7
4 changed files with 15 additions and 9 deletions

View File

@ -20,14 +20,14 @@ import (
// addSqlToTracing adds sql information to tracer if it's enabled.
func (c *Core) addSqlToTracing(ctx context.Context, sql *Sql) {
if gtrace.IsActivated(ctx) {
if !gtrace.IsActivated(ctx) {
return
}
tr := otel.GetTracerProvider().Tracer(
"github.com/gogf/gf/database/gdb",
trace.WithInstrumentationVersion(fmt.Sprintf(`%s`, gf.VERSION)),
)
ctx, span := tr.Start(ctx, sql.Type)
ctx, span := tr.Start(ctx, sql.Type, trace.WithSpanKind(trace.SpanKindInternal))
defer span.End()
if sql.Error != nil {

View File

@ -66,7 +66,7 @@ func (c *Conn) do(timeout time.Duration, commandName string, args ...interface{}
timestampMilli2 := gtime.TimestampMilli()
// Tracing.
if gtrace.IsActivated(c.ctx) {
if !gtrace.IsActivated(c.ctx) {
return
}
tr := otel.GetTracerProvider().Tracer(
@ -77,7 +77,7 @@ func (c *Conn) do(timeout time.Duration, commandName string, args ...interface{}
if ctx == nil {
ctx = context.Background()
}
_, span := tr.Start(ctx, "Redis."+commandName)
_, span := tr.Start(ctx, "Redis."+commandName, trace.WithSpanKind(trace.SpanKindInternal))
defer span.End()
if err != nil {
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err))

View File

@ -43,7 +43,7 @@ func MiddlewareServerTracing(r *Request) {
propagation.Baggage{},
)
ctx := propagator.Extract(r.Context(), r.Header)
ctx, span := tr.Start(ctx, r.URL.String())
ctx, span := tr.Start(ctx, r.URL.String(), trace.WithSpanKind(trace.SpanKindServer))
defer span.End()
span.SetAttributes(gtrace.CommonLabels()...)
@ -59,7 +59,7 @@ func MiddlewareServerTracing(r *Request) {
reqBodyContent = string(reqBodyContentBytes)
} else {
reqBodyContent = fmt.Sprintf(
"[Request Body Too Large For Logging, Max: %d bytes]",
"[Request Body Too Large For Tracing, Max: %d bytes]",
tracingMaxContentLogSize,
)
}
@ -80,7 +80,10 @@ func MiddlewareServerTracing(r *Request) {
if r.Response.BufferLength() <= tracingMaxContentLogSize {
resBodyContent = r.Response.BufferString()
} else {
resBodyContent = fmt.Sprintf("[Response Body Too Large For Logging, Max: %d bytes]", tracingMaxContentLogSize)
resBodyContent = fmt.Sprintf(
"[Response Body Too Large For Tracing, Max: %d bytes]",
tracingMaxContentLogSize,
)
}
span.AddEvent("http.response", trace.WithAttributes(
label.Any(`http.response.headers`, httputil.HeaderToMap(r.Response.Header())),

View File

@ -32,7 +32,7 @@ func MiddlewareTracing(c *Client, r *http.Request) (response *Response, err erro
"github.com/gogf/gf/net/ghttp.Client",
trace.WithInstrumentationVersion(fmt.Sprintf(`%s`, gf.VERSION)),
)
ctx, span := tr.Start(r.Context(), r.URL.String())
ctx, span := tr.Start(r.Context(), r.URL.String(), trace.WithSpanKind(trace.SpanKindClient))
defer span.End()
span.SetAttributes(gtrace.CommonLabels()...)
@ -64,7 +64,10 @@ func MiddlewareTracing(c *Client, r *http.Request) (response *Response, err erro
resBodyContent = string(reqBodyContentBytes)
response.Body = utils.NewReadCloser(reqBodyContentBytes, false)
} else {
resBodyContent = fmt.Sprintf("[Response Body Too Large For Logging, Max: %d bytes]", tracingMaxContentLogSize)
resBodyContent = fmt.Sprintf(
"[Response Body Too Large For Tracing, Max: %d bytes]",
tracingMaxContentLogSize,
)
}
span.AddEvent("http.response", trace.WithAttributes(