From 5adde275fc2834e3264c194ab0d6f14f34cf2019 Mon Sep 17 00:00:00 2001 From: John Guo Date: Sun, 14 Feb 2021 22:00:56 +0800 Subject: [PATCH] add switch variable for internal type tracing components --- database/gdb/gdb_core_tracing.go | 13 ++++++++++++- database/gredis/gredis_conn.go | 15 ++++++--------- database/gredis/gredis_conn_tracing.go | 19 ++++++++++++++++++- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/database/gdb/gdb_core_tracing.go b/database/gdb/gdb_core_tracing.go index 08209931c..678ad8558 100644 --- a/database/gdb/gdb_core_tracing.go +++ b/database/gdb/gdb_core_tracing.go @@ -12,6 +12,7 @@ import ( "fmt" "github.com/gogf/gf" "github.com/gogf/gf/net/gtrace" + "github.com/gogf/gf/os/gcmd" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/label" @@ -33,9 +34,19 @@ const ( tracingEventDbExecutionType = "db.execution.type" ) +var ( + // tracingInternal enables tracing for internal type spans. + // It's true in default. + tracingInternal = true +) + +func init() { + tracingInternal = gcmd.GetOptWithEnv("gf.tracing.internal", true).Bool() +} + // addSqlToTracing adds sql information to tracer if it's enabled. func (c *Core) addSqlToTracing(ctx context.Context, sql *Sql) { - if !gtrace.IsActivated(ctx) { + if !tracingInternal || !gtrace.IsActivated(ctx) { return } tr := otel.GetTracerProvider().Tracer( diff --git a/database/gredis/gredis_conn.go b/database/gredis/gredis_conn.go index 26cc1ad6a..f33d4528e 100644 --- a/database/gredis/gredis_conn.go +++ b/database/gredis/gredis_conn.go @@ -11,7 +11,6 @@ import ( "errors" "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" @@ -60,14 +59,12 @@ func (c *Conn) do(timeout time.Duration, commandName string, args ...interface{} timestampMilli2 := gtime.TimestampMilli() // Tracing. - if gtrace.IsActivated(c.ctx) { - c.addTracingItem(&tracingItem{ - err: err, - commandName: commandName, - arguments: args, - costMilli: timestampMilli2 - timestampMilli1, - }) - } + c.addTracingItem(&tracingItem{ + err: err, + commandName: commandName, + arguments: args, + costMilli: timestampMilli2 - timestampMilli1, + }) return } diff --git a/database/gredis/gredis_conn_tracing.go b/database/gredis/gredis_conn_tracing.go index 1e6990839..2e4c794fa 100644 --- a/database/gredis/gredis_conn_tracing.go +++ b/database/gredis/gredis_conn_tracing.go @@ -12,6 +12,7 @@ import ( "github.com/gogf/gf" "github.com/gogf/gf/internal/json" "github.com/gogf/gf/net/gtrace" + "github.com/gogf/gf/os/gcmd" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/label" @@ -37,9 +38,25 @@ const ( tracingEventRedisExecutionArguments = "redis.execution.arguments" ) +var ( + // tracingInternal enables tracing for internal type spans. + // It's true in default. + tracingInternal = true +) + +func init() { + tracingInternal = gcmd.GetOptWithEnv("gf.tracing.internal", true).Bool() +} + // addTracingItem checks and adds redis tracing information to OpenTelemetry. func (c *Conn) addTracingItem(item *tracingItem) { - tr := otel.GetTracerProvider().Tracer(tracingInstrumentName, trace.WithInstrumentationVersion(gf.VERSION)) + if !tracingInternal || !gtrace.IsActivated(c.ctx) { + return + } + tr := otel.GetTracerProvider().Tracer( + tracingInstrumentName, + trace.WithInstrumentationVersion(gf.VERSION), + ) ctx := c.ctx if ctx == nil { ctx = context.Background()