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 {