Skip to content

Commit

Permalink
[LOG]Support logging with context(#596)
Browse files Browse the repository at this point in the history
1. add composite logger to manage main/sql/tx logger
2. support to parse and propagate the trace context to logging
3. make the logger achieve context level isolation
  • Loading branch information
Mulavar committed Jul 17, 2023
1 parent 39096e4 commit dd583a2
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 110 deletions.
2 changes: 1 addition & 1 deletion cmd/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func Run(bootstrapPath string, addr string) error {
registryConf := discovery.GetServiceRegistry(context.Background())
serviceDiscovery, err := registry.InitDiscovery(registryConf.Name, registryConf.Options)
if err != nil {
log.Fatal("init service discovert failed: %v", err)
log.Fatal("init service discovery failed: %v", err)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion conf/bootstrap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ supervisor:
password: root

logging_config:
log_name: arana_log
log_name: arana.log
log_path: log
log_level: 7 #KERN_DEBUG
log_max_size: 10
Expand Down
14 changes: 13 additions & 1 deletion pkg/executor/redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import (

"github.com/pkg/errors"

rtrace "go.opentelemetry.io/otel/trace"

"go.uber.org/zap"

"golang.org/x/exp/slices"
)

Expand Down Expand Up @@ -174,7 +178,15 @@ func (executor *RedirectExecutor) doExecutorComQuery(ctx *proto.Context, act ast
hints = append(hints, h)
}

trace.Extract(ctx, hints)
// extract trace context
if trace.Extract(ctx, hints) {
traceBytes, err := rtrace.SpanFromContext(ctx).SpanContext().MarshalJSON()
if err != nil {
return nil, 0, err
}
ctx.Context = log.NewContext(ctx.Context, ctx.C.ID(), zap.String("trace-context", strings.ReplaceAll(string(traceBytes), "\"", "")))
}

metrics.ParserDuration.Observe(time.Since(start).Seconds())

if len(ctx.C.Schema()) < 1 {
Expand Down
16 changes: 10 additions & 6 deletions pkg/trace/jaeger/jaeger.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (j *Jaeger) Initialize(_ context.Context, traceCfg *config.Trace) error {
// Register our TracerProvider as the global so any imported
// instrumentation in the future will default to using it.
otel.SetTracerProvider(tp)
otel.SetTextMapPropagator(&propagation.TraceContext{})
return nil
}

Expand All @@ -76,17 +77,20 @@ func (j *Jaeger) tracerProvider(traceCfg *config.Trace) (*tracesdk.TracerProvide
return tp, nil
}

func (j *Jaeger) Extract(ctx *proto.Context, hints []*hint.Hint) {
var traceId string
func (j *Jaeger) Extract(ctx *proto.Context, hints []*hint.Hint) bool {
var (
traceContext string
)
for _, h := range hints {
if h.Type != hint.TypeTrace {
continue
}
traceId = h.Inputs[0].V
traceContext = h.Inputs[0].V
break
}
if len(traceId) == 0 {
return
if len(traceContext) == 0 {
return false
}
ctx.Context = otel.GetTextMapPropagator().Extract(ctx.Context, propagation.MapCarrier{parentKey: traceId})
ctx.Context = otel.GetTextMapPropagator().Extract(ctx.Context, propagation.MapCarrier{parentKey: traceContext})
return true
}
6 changes: 3 additions & 3 deletions pkg/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ func Initialize(ctx context.Context, traceCfg *config.Trace) error {
return err
}

func Extract(ctx *proto.Context, hints []*hint.Hint) {
currentProvider.Extract(ctx, hints)
func Extract(ctx *proto.Context, hints []*hint.Hint) bool {
return currentProvider.Extract(ctx, hints)
}

type Provider interface {
Initialize(ctx context.Context, traceCfg *config.Trace) error
Extract(ctx *proto.Context, hints []*hint.Hint)
Extract(ctx *proto.Context, hints []*hint.Hint) bool
}
Loading

0 comments on commit dd583a2

Please sign in to comment.