diff options
author | lash <nolash@users.noreply.github.com> | 2019-02-20 21:50:37 +0800 |
---|---|---|
committer | Viktor TrĂ³n <viktor.tron@gmail.com> | 2019-02-20 21:50:37 +0800 |
commit | d36e974ba303d12d79d769d0811dd5babcf6688f (patch) | |
tree | 22073c5ae7d6e0b243ed7f4bed4ed9aae054a116 /swarm/tracing | |
parent | 460d206f309fc0884c666bd191a1b6a4b63462fc (diff) | |
download | go-tangerine-d36e974ba303d12d79d769d0811dd5babcf6688f.tar go-tangerine-d36e974ba303d12d79d769d0811dd5babcf6688f.tar.gz go-tangerine-d36e974ba303d12d79d769d0811dd5babcf6688f.tar.bz2 go-tangerine-d36e974ba303d12d79d769d0811dd5babcf6688f.tar.lz go-tangerine-d36e974ba303d12d79d769d0811dd5babcf6688f.tar.xz go-tangerine-d36e974ba303d12d79d769d0811dd5babcf6688f.tar.zst go-tangerine-d36e974ba303d12d79d769d0811dd5babcf6688f.zip |
swarm/network: Keep span across roundtrip (#19140)
* swarm/newtork: WIP Span request span until delivery and put
* swarm/storage: Introduce new trace across single fetcher lifespan
* swarm/network: Put span ids for sendpriority in context value
* swarm: Add global span store in tracing
* swarm/tracing: Add context key constants
* swarm/tracing: Add comments
* swarm/storage: Remove redundant fix for filestore
* swarm/tracing: Elaborate constants comments
* swarm/network, swarm/storage, swarm:tracing: Minor cleanup
Diffstat (limited to 'swarm/tracing')
-rw-r--r-- | swarm/tracing/tracing.go | 77 |
1 files changed, 74 insertions, 3 deletions
diff --git a/swarm/tracing/tracing.go b/swarm/tracing/tracing.go index f95fa41b8..55875464b 100644 --- a/swarm/tracing/tracing.go +++ b/swarm/tracing/tracing.go @@ -1,21 +1,39 @@ package tracing import ( + "context" "io" "os" "strings" + "sync" "time" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/swarm/spancontext" + + opentracing "github.com/opentracing/opentracing-go" jaeger "github.com/uber/jaeger-client-go" jaegercfg "github.com/uber/jaeger-client-go/config" cli "gopkg.in/urfave/cli.v1" ) -var Enabled bool = false +var ( + // Enabled turns tracing on for the current swarm instance + Enabled bool = false + store = spanStore{} +) + +const ( + // TracingEnabledFlag is the CLI flag name to use to enable trace collections. + TracingEnabledFlag = "tracing" + + // StoreLabelId is the context value key of the name of the span to be saved + StoreLabelId = "span_save_id" -// TracingEnabledFlag is the CLI flag name to use to enable trace collections. -const TracingEnabledFlag = "tracing" + // StoreLabelMeta is the context value key that together with StoreLabelId constitutes the retrieval key for saved spans in the span store + // StartSaveSpan and ShiftSpanByKey + StoreLabelMeta = "span_save_meta" +) var ( Closer io.Closer @@ -100,3 +118,56 @@ func initTracer(endpoint, svc string) (closer io.Closer) { return closer } + +// spanStore holds saved spans +type spanStore struct { + spans sync.Map +} + +// StartSaveSpan stores the span specified in the passed context for later retrieval +// The span object but be context value on the key StoreLabelId. +// It will be stored under the the following string key context.Value(StoreLabelId)|.|context.Value(StoreLabelMeta) +func StartSaveSpan(ctx context.Context) context.Context { + if !Enabled { + return ctx + } + traceId := ctx.Value(StoreLabelId) + + if traceId != nil { + traceStr := traceId.(string) + var sp opentracing.Span + ctx, sp = spancontext.StartSpan( + ctx, + traceStr, + ) + traceMeta := ctx.Value(StoreLabelMeta) + if traceMeta != nil { + traceStr = traceStr + "." + traceMeta.(string) + } + store.spans.Store(traceStr, sp) + } + return ctx +} + +// ShiftSpanByKey retrieves the span stored under the key of the string given as argument +// The span is then deleted from the store +func ShiftSpanByKey(k string) opentracing.Span { + if !Enabled { + return nil + } + span, spanOk := store.spans.Load(k) + if !spanOk { + return nil + } + store.spans.Delete(k) + return span.(opentracing.Span) +} + +// FinishSpans calls `Finish()` on all stored spans +// It should be called on instance shutdown +func FinishSpans() { + store.spans.Range(func(_, v interface{}) bool { + v.(opentracing.Span).Finish() + return true + }) +} |