ddtrace.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Unless explicitly stated otherwise all files in this repository are licensed
  2. // under the Apache License Version 2.0.
  3. // This product includes software developed at Datadog (https://www.datadoghq.com/).
  4. // Copyright 2016 Datadog, Inc.
  5. // Package ddtrace contains the interfaces that specify the implementations of Datadog's
  6. // tracing library, as well as a set of sub-packages containing various implementations:
  7. // our native implementation ("tracer"), a wrapper that can be used with Opentracing
  8. // ("opentracer") and a mock tracer to be used for testing ("mocktracer"). Additionally,
  9. // package "ext" provides a set of tag names and values specific to Datadog's APM product.
  10. //
  11. // To get started, visit the documentation for any of the packages you'd like to begin
  12. // with by accessing the subdirectories of this package: https://godoc.org/gopkg.in/DataDog/dd-trace-go.v1/ddtrace#pkg-subdirectories.
  13. package ddtrace // import "gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
  14. import (
  15. "context"
  16. "time"
  17. "gopkg.in/DataDog/dd-trace-go.v1/internal/log"
  18. )
  19. // Tracer specifies an implementation of the Datadog tracer which allows starting
  20. // and propagating spans. The official implementation if exposed as functions
  21. // within the "tracer" package.
  22. type Tracer interface {
  23. // StartSpan starts a span with the given operation name and options.
  24. StartSpan(operationName string, opts ...StartSpanOption) Span
  25. // Extract extracts a span context from a given carrier. Note that baggage item
  26. // keys will always be lower-cased to maintain consistency. It is impossible to
  27. // maintain the original casing due to MIME header canonicalization standards.
  28. Extract(carrier interface{}) (SpanContext, error)
  29. // Inject injects a span context into the given carrier.
  30. Inject(context SpanContext, carrier interface{}) error
  31. // Stop stops the tracer. Calls to Stop should be idempotent.
  32. Stop()
  33. }
  34. // Span represents a chunk of computation time. Spans have names, durations,
  35. // timestamps and other metadata. A Tracer is used to create hierarchies of
  36. // spans in a request, buffer and submit them to the server.
  37. type Span interface {
  38. // SetTag sets a key/value pair as metadata on the span.
  39. SetTag(key string, value interface{})
  40. // SetOperationName sets the operation name for this span. An operation name should be
  41. // a representative name for a group of spans (e.g. "grpc.server" or "http.request").
  42. SetOperationName(operationName string)
  43. // BaggageItem returns the baggage item held by the given key.
  44. BaggageItem(key string) string
  45. // SetBaggageItem sets a new baggage item at the given key. The baggage
  46. // item should propagate to all descendant spans, both in- and cross-process.
  47. SetBaggageItem(key, val string)
  48. // Finish finishes the current span with the given options. Finish calls should be idempotent.
  49. Finish(opts ...FinishOption)
  50. // Context returns the SpanContext of this Span.
  51. Context() SpanContext
  52. }
  53. // SpanContext represents a span state that can propagate to descendant spans
  54. // and across process boundaries. It contains all the information needed to
  55. // spawn a direct descendant of the span that it belongs to. It can be used
  56. // to create distributed tracing by propagating it using the provided interfaces.
  57. type SpanContext interface {
  58. // SpanID returns the span ID that this context is carrying.
  59. SpanID() uint64
  60. // TraceID returns the trace ID that this context is carrying.
  61. TraceID() uint64
  62. // ForeachBaggageItem provides an iterator over the key/value pairs set as
  63. // baggage within this context. Iteration stops when the handler returns
  64. // false.
  65. ForeachBaggageItem(handler func(k, v string) bool)
  66. }
  67. // StartSpanOption is a configuration option that can be used with a Tracer's StartSpan method.
  68. type StartSpanOption func(cfg *StartSpanConfig)
  69. // FinishOption is a configuration option that can be used with a Span's Finish method.
  70. type FinishOption func(cfg *FinishConfig)
  71. // FinishConfig holds the configuration for finishing a span. It is usually passed around by
  72. // reference to one or more FinishOption functions which shape it into its final form.
  73. type FinishConfig struct {
  74. // FinishTime represents the time that should be set as finishing time for the
  75. // span. Implementations should use the current time when FinishTime.IsZero().
  76. FinishTime time.Time
  77. // Error holds an optional error that should be set on the span before
  78. // finishing.
  79. Error error
  80. // NoDebugStack will prevent any set errors from generating an attached stack trace tag.
  81. NoDebugStack bool
  82. // StackFrames specifies the number of stack frames to be attached in spans that finish with errors.
  83. StackFrames uint
  84. // SkipStackFrames specifies the offset at which to start reporting stack frames from the stack.
  85. SkipStackFrames uint
  86. }
  87. // StartSpanConfig holds the configuration for starting a new span. It is usually passed
  88. // around by reference to one or more StartSpanOption functions which shape it into its
  89. // final form.
  90. type StartSpanConfig struct {
  91. // Parent holds the SpanContext that should be used as a parent for the
  92. // new span. If nil, implementations should return a root span.
  93. Parent SpanContext
  94. // StartTime holds the time that should be used as the start time of the span.
  95. // Implementations should use the current time when StartTime.IsZero().
  96. StartTime time.Time
  97. // Tags holds a set of key/value pairs that should be set as metadata on the
  98. // new span.
  99. Tags map[string]interface{}
  100. // Force-set the SpanID, rather than use a random number. If no Parent SpanContext is present,
  101. // then this will also set the TraceID to the same value.
  102. SpanID uint64
  103. // Context is the parent context where the span should be stored.
  104. Context context.Context
  105. }
  106. // Logger implementations are able to log given messages that the tracer or profiler might output.
  107. type Logger interface {
  108. // Log prints the given message.
  109. Log(msg string)
  110. }
  111. // UseLogger sets l as the logger for all tracer and profiler logs.
  112. func UseLogger(l Logger) {
  113. log.UseLogger(l)
  114. }