gocontext.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package opentracing
  2. import "context"
  3. type contextKey struct{}
  4. var activeSpanKey = contextKey{}
  5. // ContextWithSpan returns a new `context.Context` that holds a reference to
  6. // the span. If span is nil, a new context without an active span is returned.
  7. func ContextWithSpan(ctx context.Context, span Span) context.Context {
  8. if span != nil {
  9. if tracerWithHook, ok := span.Tracer().(TracerContextWithSpanExtension); ok {
  10. ctx = tracerWithHook.ContextWithSpanHook(ctx, span)
  11. }
  12. }
  13. return context.WithValue(ctx, activeSpanKey, span)
  14. }
  15. // SpanFromContext returns the `Span` previously associated with `ctx`, or
  16. // `nil` if no such `Span` could be found.
  17. //
  18. // NOTE: context.Context != SpanContext: the former is Go's intra-process
  19. // context propagation mechanism, and the latter houses OpenTracing's per-Span
  20. // identity and baggage information.
  21. func SpanFromContext(ctx context.Context) Span {
  22. val := ctx.Value(activeSpanKey)
  23. if sp, ok := val.(Span); ok {
  24. return sp
  25. }
  26. return nil
  27. }
  28. // StartSpanFromContext starts and returns a Span with `operationName`, using
  29. // any Span found within `ctx` as a ChildOfRef. If no such parent could be
  30. // found, StartSpanFromContext creates a root (parentless) Span.
  31. //
  32. // The second return value is a context.Context object built around the
  33. // returned Span.
  34. //
  35. // Example usage:
  36. //
  37. // SomeFunction(ctx context.Context, ...) {
  38. // sp, ctx := opentracing.StartSpanFromContext(ctx, "SomeFunction")
  39. // defer sp.Finish()
  40. // ...
  41. // }
  42. func StartSpanFromContext(ctx context.Context, operationName string, opts ...StartSpanOption) (Span, context.Context) {
  43. return StartSpanFromContextWithTracer(ctx, GlobalTracer(), operationName, opts...)
  44. }
  45. // StartSpanFromContextWithTracer starts and returns a span with `operationName`
  46. // using a span found within the context as a ChildOfRef. If that doesn't exist
  47. // it creates a root span. It also returns a context.Context object built
  48. // around the returned span.
  49. //
  50. // It's behavior is identical to StartSpanFromContext except that it takes an explicit
  51. // tracer as opposed to using the global tracer.
  52. func StartSpanFromContextWithTracer(ctx context.Context, tracer Tracer, operationName string, opts ...StartSpanOption) (Span, context.Context) {
  53. if parentSpan := SpanFromContext(ctx); parentSpan != nil {
  54. opts = append(opts, ChildOf(parentSpan.Context()))
  55. }
  56. span := tracer.StartSpan(operationName, opts...)
  57. return span, ContextWithSpan(ctx, span)
  58. }