improve tracing feature

This commit is contained in:
John Guo 2021-02-01 17:10:50 +08:00
parent 3e33d66ab4
commit e6b4662ec2
5 changed files with 22 additions and 24 deletions

View File

@ -19,6 +19,7 @@ import (
)
const (
tracingInstrumentName = "github.com/gogf/gf/database/gdb"
tracingAttrDbType = "db.type"
tracingAttrDbHost = "db.host"
tracingAttrDbPort = "db.port"
@ -37,10 +38,7 @@ func (c *Core) addSqlToTracing(ctx context.Context, sql *Sql) {
if !gtrace.IsActivated(ctx) {
return
}
tr := otel.GetTracerProvider().Tracer(
"github.com/gogf/gf/database/gdb",
trace.WithInstrumentationVersion(fmt.Sprintf(`%s`, gf.VERSION)),
)
tr := otel.GetTracerProvider().Tracer(tracingInstrumentName, trace.WithInstrumentationVersion(gf.VERSION))
ctx, span := tr.Start(ctx, sql.Type, trace.WithSpanKind(trace.SpanKindInternal))
defer span.End()

View File

@ -27,6 +27,7 @@ type tracingItem struct {
}
const (
tracingInstrumentName = "github.com/gogf/gf/database/gredis"
tracingAttrRedisHost = "redis.host"
tracingAttrRedisPort = "redis.port"
tracingAttrRedisDb = "redis.db"
@ -38,10 +39,7 @@ const (
// 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)),
)
tr := otel.GetTracerProvider().Tracer(tracingInstrumentName, trace.WithInstrumentationVersion(gf.VERSION))
ctx := c.ctx
if ctx == nil {
ctx = context.Background()

View File

@ -24,6 +24,7 @@ import (
const (
tracingMaxContentLogSize = 512 * 1024 // Max log size for request and response body.
tracingInstrumentName = "github.com/gogf/gf/net/ghttp.Server"
tracingEventHttpRequest = "http.request"
tracingEventHttpRequestHeaders = "http.request.headers"
tracingEventHttpRequestBody = "http.request.body"
@ -39,10 +40,7 @@ func MiddlewareClientTracing(c *Client, r *http.Request) (*ClientResponse, error
// MiddlewareServerTracing is a serer middleware that enables tracing feature using standards of OpenTelemetry.
func MiddlewareServerTracing(r *Request) {
tr := otel.GetTracerProvider().Tracer(
"github.com/gogf/gf/net/ghttp.Server",
trace.WithInstrumentationVersion(fmt.Sprintf(`%s`, gf.VERSION)),
)
tr := otel.GetTracerProvider().Tracer(tracingInstrumentName, trace.WithInstrumentationVersion(gf.VERSION))
// Tracing content parsing, start root span.
propagator := propagation.NewCompositeTextMapPropagator(
propagation.TraceContext{},

View File

@ -24,6 +24,7 @@ import (
const (
tracingMaxContentLogSize = 512 * 1024 // Max log size for request and response body.
tracingInstrumentName = "github.com/gogf/gf/net/ghttp.Client"
tracingAttrHttpAddressRemote = "http.address.remote"
tracingAttrHttpAddressLocal = "http.address.local"
tracingAttrHttpDnsStart = "http.dns.start"
@ -40,10 +41,7 @@ const (
// MiddlewareTracing is a client middleware that enables tracing feature using standards of OpenTelemetry.
func MiddlewareTracing(c *Client, r *http.Request) (response *Response, err error) {
tr := otel.GetTracerProvider().Tracer(
"github.com/gogf/gf/net/ghttp.Client",
trace.WithInstrumentationVersion(fmt.Sprintf(`%s`, gf.VERSION)),
)
tr := otel.GetTracerProvider().Tracer(tracingInstrumentName, trace.WithInstrumentationVersion(gf.VERSION))
ctx, span := tr.Start(r.Context(), r.URL.String(), trace.WithSpanKind(trace.SpanKindClient))
defer span.End()

View File

@ -11,33 +11,39 @@ import (
"context"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/net/gipv4"
"github.com/gogf/gf/text/gstr"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/otel/label"
"go.opentelemetry.io/otel/trace"
"os"
"strings"
)
const (
commonLabelHostname = "hostname"
commonLabelIpIntranet = "ip.intranet"
)
var (
intranetIps, _ = gipv4.GetIntranetIpArray()
intranetIpStr = strings.Join(intranetIps, ",")
hostname, _ = os.Hostname()
)
// IsActivated checks and returns if tracing feature is activated.
func IsActivated(ctx context.Context) bool {
return GetTraceId(ctx) != ""
}
// CommonLabels returns common used attribute labels:
// ip.intranet, hostname.
func CommonLabels() []label.KeyValue {
return []label.KeyValue{
label.String(`ip.intranet`, gstr.Join(intranetIps, ",")),
label.String(`hostname`, hostname),
label.String(commonLabelHostname, hostname),
label.String(commonLabelIpIntranet, intranetIpStr),
}
}
// IsActivated checks and returns if tracing feature is activated.
func IsActivated(ctx context.Context) bool {
return GetTraceId(ctx) != ""
}
// Tracer is a short function for retrieve Tracer.
func Tracer(name ...string) trace.Tracer {
tracerName := ""