github.com/erda-project/erda-infra@v1.0.9/pkg/trace/inject/redis/process.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package redis 16 17 import ( 18 "github.com/go-redis/redis" 19 "go.opentelemetry.io/otel/codes" 20 "go.opentelemetry.io/otel/trace" 21 22 injectcontext "github.com/erda-project/erda-infra/pkg/trace/inject/context" 23 ) 24 25 func newProcessWrapper(cfg *config) func(oldProcess func(cmd redis.Cmder) error) func(cmd redis.Cmder) error { 26 return func(process func(cmd redis.Cmder) error) func(cmd redis.Cmder) error { 27 return func(cmd redis.Cmder) (err error) { 28 ctx := injectcontext.GetContext() 29 if ctx != nil { 30 record := cfg.SpanOptions.AllowRoot || trace.SpanContextFromContext(ctx).IsValid() 31 if record && !cfg.SpanOptions.Ping && cmd.Name() == "ping" { 32 record = false 33 } 34 if record { 35 var span trace.Span 36 ctx, span = cfg.Tracer.Start(ctx, cfg.SpanNameFormatter.Format(ctx, cmd), 37 trace.WithSpanKind(trace.SpanKindClient), 38 trace.WithAttributes(withDBStatement(cfg, cmd)...), 39 ) 40 defer func() { 41 if err != nil { 42 recordSpanError(span, cfg.SpanOptions, err) 43 } else { 44 span.SetStatus(codes.Ok, "") 45 } 46 span.End() 47 }() 48 } 49 } 50 err = process(cmd) 51 return err 52 } 53 } 54 } 55 56 func newProcessPipeline(cfg *config) func(process func([]redis.Cmder) error) func([]redis.Cmder) error { 57 return func(process func(cmds []redis.Cmder) error) func([]redis.Cmder) error { 58 return func(cmds []redis.Cmder) (err error) { 59 ctx := injectcontext.GetContext() 60 if ctx != nil { 61 if cfg.SpanOptions.AllowRoot || trace.SpanContextFromContext(ctx).IsValid() { 62 var span trace.Span 63 ctx, span = cfg.Tracer.Start(ctx, cfg.SpanNameFormatter.FormatBatch(ctx, cmds), 64 trace.WithSpanKind(trace.SpanKindClient), 65 trace.WithAttributes(withBatchDBStatement(cfg, cmds)...), 66 ) 67 defer func() { 68 if err != nil { 69 recordSpanError(span, cfg.SpanOptions, err) 70 } else { 71 span.SetStatus(codes.Ok, "") 72 } 73 span.End() 74 }() 75 } 76 } 77 err = process(cmds) 78 return err 79 } 80 } 81 } 82 83 func recordSpanError(span trace.Span, opts SpanOptions, err error) { 84 if span == nil { 85 return 86 } 87 if opts.RecordError != nil && !opts.RecordError(err) { 88 return 89 } 90 switch err { 91 case nil: 92 return 93 default: 94 span.RecordError(err) 95 span.SetStatus(codes.Error, "") 96 } 97 }