improve tracing by adding constants for attribute and event names

This commit is contained in:
John Guo
2021-01-27 20:25:57 +08:00
parent 2c15aad0e7
commit 6e4e1abf1e
6 changed files with 140 additions and 65 deletions

View File

@ -18,6 +18,20 @@ import (
"go.opentelemetry.io/otel/trace"
)
const (
tracingAttrDbType = "db.type"
tracingAttrDbHost = "db.host"
tracingAttrDbPort = "db.port"
tracingAttrDbName = "db.name"
tracingAttrDbUser = "db.user"
tracingAttrDbLink = "db.link"
tracingAttrDbGroup = "db.group"
tracingEventDbExecution = "db.execution"
tracingEventDbExecutionSql = "db.execution.sql"
tracingEventDbExecutionCost = "db.execution.cost"
tracingEventDbExecutionType = "db.execution.type"
)
// addSqlToTracing adds sql information to tracer if it's enabled.
func (c *Core) addSqlToTracing(ctx context.Context, sql *Sql) {
if !gtrace.IsActivated(ctx) {
@ -36,30 +50,30 @@ func (c *Core) addSqlToTracing(ctx context.Context, sql *Sql) {
labels := make([]label.KeyValue, 0)
labels = append(labels, gtrace.CommonLabels()...)
labels = append(labels,
label.String("db.type", c.DB.GetConfig().Type),
label.String(tracingAttrDbType, c.DB.GetConfig().Type),
)
if c.DB.GetConfig().Host != "" {
labels = append(labels, label.String("db.host", c.DB.GetConfig().Host))
labels = append(labels, label.String(tracingAttrDbHost, c.DB.GetConfig().Host))
}
if c.DB.GetConfig().Port != "" {
labels = append(labels, label.String("db.port", c.DB.GetConfig().Port))
labels = append(labels, label.String(tracingAttrDbPort, c.DB.GetConfig().Port))
}
if c.DB.GetConfig().Name != "" {
labels = append(labels, label.String("db.name", c.DB.GetConfig().Name))
labels = append(labels, label.String(tracingAttrDbName, c.DB.GetConfig().Name))
}
if c.DB.GetConfig().User != "" {
labels = append(labels, label.String("db.user", c.DB.GetConfig().User))
labels = append(labels, label.String(tracingAttrDbUser, c.DB.GetConfig().User))
}
if filteredLinkInfo := c.DB.FilteredLinkInfo(); filteredLinkInfo != "" {
labels = append(labels, label.String("db.link", c.DB.FilteredLinkInfo()))
labels = append(labels, label.String(tracingAttrDbLink, c.DB.FilteredLinkInfo()))
}
if group := c.DB.GetGroup(); group != "" {
labels = append(labels, label.String("db.group", group))
labels = append(labels, label.String(tracingAttrDbGroup, group))
}
span.SetAttributes(labels...)
span.AddEvent("db.execution", trace.WithAttributes(
label.String(`db.execution.sql`, sql.Format),
label.String(`db.execution.cost`, fmt.Sprintf(`%d ms`, sql.End-sql.Start)),
label.String(`db.execution.type`, sql.Type),
span.AddEvent(tracingEventDbExecution, trace.WithAttributes(
label.String(tracingEventDbExecutionSql, sql.Format),
label.String(tracingEventDbExecutionCost, fmt.Sprintf(`%d ms`, sql.End-sql.Start)),
label.String(tracingEventDbExecutionType, sql.Type),
))
}

View File

@ -9,18 +9,12 @@ package gredis
import (
"context"
"errors"
"fmt"
"github.com/gogf/gf"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/internal/json"
"github.com/gogf/gf/net/gtrace"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
"github.com/gomodule/redigo/redis"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/trace"
"reflect"
"time"
)
@ -66,34 +60,14 @@ func (c *Conn) do(timeout time.Duration, commandName string, args ...interface{}
timestampMilli2 := gtime.TimestampMilli()
// Tracing.
if !gtrace.IsActivated(c.ctx) {
return
if gtrace.IsActivated(c.ctx) {
c.addTracingItem(&tracingItem{
err: err,
commandName: commandName,
arguments: args,
costMilli: timestampMilli2 - timestampMilli1,
})
}
tr := otel.GetTracerProvider().Tracer(
"github.com/gogf/gf/database/gredis",
trace.WithInstrumentationVersion(fmt.Sprintf(`%s`, gf.VERSION)),
)
ctx := c.ctx
if ctx == nil {
ctx = context.Background()
}
_, span := tr.Start(ctx, "Redis."+commandName, trace.WithSpanKind(trace.SpanKindInternal))
defer span.End()
if err != nil {
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err))
}
span.SetAttributes(gtrace.CommonLabels()...)
span.SetAttributes(
label.String("redis.host", c.redis.config.Host),
label.Int("redis.port", c.redis.config.Port),
label.Int("redis.db", c.redis.config.Db),
)
jsonBytes, _ := json.Marshal(args)
span.AddEvent("redis.execution", trace.WithAttributes(
label.String(`redis.execution.command`, commandName),
label.String(`redis.execution.cost`, fmt.Sprintf(`%d ms`, timestampMilli2-timestampMilli1)),
label.String(`redis.execution.arguments`, string(jsonBytes)),
))
return
}

View File

@ -0,0 +1,66 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gredis
import (
"context"
"fmt"
"github.com/gogf/gf"
"github.com/gogf/gf/internal/json"
"github.com/gogf/gf/net/gtrace"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/trace"
)
// tracingItem holds the information for redis tracing.
type tracingItem struct {
err error
commandName string
arguments []interface{}
costMilli int64
}
const (
tracingAttrRedisHost = "redis.host"
tracingAttrRedisPort = "redis.port"
tracingAttrRedisDb = "redis.db"
tracingEventRedisExecution = "redis.execution"
tracingEventRedisExecutionCommand = "redis.execution.command"
tracingEventRedisExecutionCost = "redis.execution.cost"
tracingEventRedisExecutionArguments = "redis.execution.arguments"
)
// addTracingItem checks and adds redis tracing information to OpenTelemetry.
func (c *Conn) addTracingItem(item *tracingItem) {
tr := otel.GetTracerProvider().Tracer(
"github.com/gogf/gf/database/gredis",
trace.WithInstrumentationVersion(fmt.Sprintf(`%s`, gf.VERSION)),
)
ctx := c.ctx
if ctx == nil {
ctx = context.Background()
}
_, span := tr.Start(ctx, "Redis."+item.commandName, trace.WithSpanKind(trace.SpanKindInternal))
defer span.End()
if item.err != nil {
span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, item.err))
}
span.SetAttributes(gtrace.CommonLabels()...)
span.SetAttributes(
label.String(tracingAttrRedisHost, c.redis.config.Host),
label.Int(tracingAttrRedisPort, c.redis.config.Port),
label.Int(tracingAttrRedisDb, c.redis.config.Db),
)
jsonBytes, _ := json.Marshal(item.arguments)
span.AddEvent(tracingEventRedisExecution, trace.WithAttributes(
label.String(tracingEventRedisExecutionCommand, item.commandName),
label.String(tracingEventRedisExecutionCost, fmt.Sprintf(`%d ms`, item.costMilli)),
label.String(tracingEventRedisExecutionArguments, string(jsonBytes)),
))
}

View File

@ -23,7 +23,13 @@ import (
)
const (
tracingMaxContentLogSize = 512 * 1024 // Max log size for request and response body.
tracingMaxContentLogSize = 512 * 1024 // Max log size for request and response body.
tracingEventHttpRequest = "http.request"
tracingEventHttpRequestHeaders = "http.request.headers"
tracingEventHttpRequestBody = "http.request.body"
tracingEventHttpResponse = "http.response"
tracingEventHttpResponseHeaders = "http.response.headers"
tracingEventHttpResponseBody = "http.response.body"
)
// MiddlewareClientTracing is a client middleware that enables tracing feature using standards of OpenTelemetry.
@ -63,9 +69,9 @@ func MiddlewareServerTracing(r *Request) {
tracingMaxContentLogSize,
)
}
span.AddEvent("http.request", trace.WithAttributes(
label.Any(`http.request.headers`, httputil.HeaderToMap(r.Header)),
label.String(`http.request.body`, reqBodyContent),
span.AddEvent(tracingEventHttpRequest, trace.WithAttributes(
label.Any(tracingEventHttpRequestHeaders, httputil.HeaderToMap(r.Header)),
label.String(tracingEventHttpRequestBody, reqBodyContent),
))
// Continue executing.
@ -85,9 +91,9 @@ func MiddlewareServerTracing(r *Request) {
tracingMaxContentLogSize,
)
}
span.AddEvent("http.response", trace.WithAttributes(
label.Any(`http.response.headers`, httputil.HeaderToMap(r.Response.Header())),
label.String(`http.response.body`, resBodyContent),
span.AddEvent(tracingEventHttpResponse, trace.WithAttributes(
label.Any(tracingEventHttpResponseHeaders, httputil.HeaderToMap(r.Response.Header())),
label.String(tracingEventHttpResponseBody, resBodyContent),
))
return
}

View File

@ -23,7 +23,19 @@ import (
)
const (
tracingMaxContentLogSize = 512 * 1024 // Max log size for request and response body.
tracingMaxContentLogSize = 512 * 1024 // Max log size for request and response body.
tracingAttrHttpAddressRemote = "http.address.remote"
tracingAttrHttpAddressLocal = "http.address.local"
tracingAttrHttpDnsStart = "http.dns.start"
tracingAttrHttpDnsDone = "http.dns.done"
tracingAttrHttpConnectStart = "http.connect.start"
tracingAttrHttpConnectDone = "http.connect.done"
tracingEventHttpRequest = "http.request"
tracingEventHttpRequestHeaders = "http.request.headers"
tracingEventHttpRequestBody = "http.request.body"
tracingEventHttpResponse = "http.response"
tracingEventHttpResponseHeaders = "http.response.headers"
tracingEventHttpResponseBody = "http.response.body"
)
// MiddlewareTracing is a client middleware that enables tracing feature using standards of OpenTelemetry.
@ -70,9 +82,9 @@ func MiddlewareTracing(c *Client, r *http.Request) (response *Response, err erro
)
}
span.AddEvent("http.response", trace.WithAttributes(
label.Any(`http.response.headers`, httputil.HeaderToMap(response.Header)),
label.String(`http.response.body`, resBodyContent),
span.AddEvent(tracingEventHttpResponse, trace.WithAttributes(
label.Any(tracingEventHttpResponseHeaders, httputil.HeaderToMap(response.Header)),
label.String(tracingEventHttpResponseBody, resBodyContent),
))
return
}

View File

@ -69,8 +69,8 @@ func (ct *clientTracer) getConn(host string) {
func (ct *clientTracer) gotConn(info httptrace.GotConnInfo) {
ct.span.SetAttributes(
label.String("http.connection.remote", info.Conn.RemoteAddr().String()),
label.String("http.connection.local", info.Conn.LocalAddr().String()),
label.String(tracingAttrHttpAddressRemote, info.Conn.RemoteAddr().String()),
label.String(tracingAttrHttpAddressLocal, info.Conn.LocalAddr().String()),
)
}
@ -82,7 +82,7 @@ func (ct *clientTracer) putIdleConn(err error) {
func (ct *clientTracer) dnsStart(info httptrace.DNSStartInfo) {
ct.span.SetAttributes(
label.String("http.dns.start", info.Host),
label.String(tracingAttrHttpDnsStart, info.Host),
)
}
@ -98,13 +98,13 @@ func (ct *clientTracer) dnsDone(info httptrace.DNSDoneInfo) {
ct.span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, info.Err))
}
ct.span.SetAttributes(
label.String("http.dns.done", buffer.String()),
label.String(tracingAttrHttpDnsDone, buffer.String()),
)
}
func (ct *clientTracer) connectStart(network, addr string) {
ct.span.SetAttributes(
label.String("http.connect.start", network+"@"+addr),
label.String(tracingAttrHttpConnectStart, network+"@"+addr),
)
}
@ -113,7 +113,7 @@ func (ct *clientTracer) connectDone(network, addr string, err error) {
ct.span.SetStatus(codes.Error, fmt.Sprintf(`%+v`, err))
}
ct.span.SetAttributes(
label.String("http.connect.done", network+"@"+addr),
label.String(tracingAttrHttpConnectDone, network+"@"+addr),
)
}
@ -147,11 +147,14 @@ func (ct *clientTracer) wroteRequest(info httptrace.WroteRequestInfo) {
if ct.request.ContentLength <= tracingMaxContentLogSize {
bodyContent = string(ct.requestBody)
} else {
bodyContent = fmt.Sprintf("[Request Body Too Large For Logging, Max: %d bytes]", tracingMaxContentLogSize)
bodyContent = fmt.Sprintf(
"[Request Body Too Large For Logging, Max: %d bytes]",
tracingMaxContentLogSize,
)
}
ct.span.AddEvent("http.request", trace.WithAttributes(
label.Any(`http.request.headers`, ct.headers),
label.String(`http.request.body`, bodyContent),
ct.span.AddEvent(tracingEventHttpRequest, trace.WithAttributes(
label.Any(tracingEventHttpRequestHeaders, ct.headers),
label.String(tracingEventHttpRequestBody, bodyContent),
))
}