github.com/blend/go-sdk@v1.20220411.3/tracing/vaulttrace/tracer.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package vaulttrace 9 10 import ( 11 "context" 12 "strconv" 13 "time" 14 15 "github.com/opentracing/opentracing-go" 16 17 "github.com/blend/go-sdk/tracing" 18 "github.com/blend/go-sdk/vault" 19 ) 20 21 // Tracer returns a request tracer that also injects span context into outgoing headers. 22 func Tracer(tracer opentracing.Tracer) vault.Tracer { 23 return &vaultTracer{tracer: tracer} 24 } 25 26 type vaultTracer struct { 27 tracer opentracing.Tracer 28 } 29 30 func (vt vaultTracer) Start(ctx context.Context, options ...vault.TraceOption) (vault.TraceFinisher, error) { 31 var config vault.SecretTraceConfig 32 for _, opt := range options { 33 err := opt(&config) 34 if err != nil { 35 return vaultTraceFinisher{}, nil 36 } 37 } 38 startOptions := []opentracing.StartSpanOption{ 39 opentracing.Tag{Key: tracing.TagKeySpanType, Value: tracing.SpanTypeVault}, 40 tracing.TagMeasured(), 41 opentracing.StartTime(time.Now().UTC()), 42 } 43 if config.VaultOperation != "" { 44 startOptions = append(startOptions, opentracing.Tag{Key: tracing.TagSecretsOperation, Value: config.VaultOperation}) 45 } 46 if config.KeyName != "" { 47 startOptions = append(startOptions, opentracing.Tag{Key: tracing.TagSecretKey, Value: config.KeyName}) 48 } 49 span, _ := tracing.StartSpanFromContext(ctx, vt.tracer, tracing.OperationVaultAPI, startOptions...) 50 return vaultTraceFinisher{span: span}, nil 51 } 52 53 type vaultTraceFinisher struct { 54 span opentracing.Span 55 } 56 57 func (vtf vaultTraceFinisher) Finish(_ context.Context, vaultStatusCode int, vaultError error) { 58 if vtf.span == nil { 59 return 60 } 61 if vaultError != nil { 62 tracing.SpanError(vtf.span, vaultError) 63 } 64 vtf.span.SetTag(tracing.TagKeyHTTPCode, strconv.Itoa(vaultStatusCode)) 65 vtf.span.Finish() 66 }