From acee2ee29345973ee3f5f717f9a413d59ffc420a Mon Sep 17 00:00:00 2001 From: zhang-wei Date: Wed, 2 Mar 2022 14:45:53 +0800 Subject: [PATCH] Add WithSpan (#153) --- README.md | 8 ++++++++ trace_context.go | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/README.md b/README.md index 2780238..f807c1e 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,14 @@ Get the `activeSpan` in the `Context`. go2sky.ActiveSpan(ctx) ``` +## With Span + +Save the `activeSpan` to `Context` + +```go +go2sky.WithSpan(ctx, activeSpan) +``` + ## Get Global Service Name Get the `ServiceName` of the `activeSpan` in the `Context`. diff --git a/trace_context.go b/trace_context.go index 0f0045b..4eeaa19 100644 --- a/trace_context.go +++ b/trace_context.go @@ -81,6 +81,10 @@ func ActiveSpan(ctx context.Context) Span { return nil } +func WithSpan(ctx context.Context, span Span) context.Context { + return context.WithValue(ctx, ctxKeyInstance, span) +} + func extractSpanString(ctx context.Context, noopResult string) (*segmentSpan, string, bool) { activeSpan := ctx.Value(ctxKeyInstance) if activeSpan != nil {