add switch variable for internal type tracing components

This commit is contained in:
John Guo 2021-02-14 22:00:56 +08:00
parent a3fa10d820
commit 5adde275fc
3 changed files with 36 additions and 11 deletions

View File

@ -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(

View File

@ -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,
})
}
return
}

View File

@ -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()