config.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package trace // import "go.opentelemetry.io/otel/trace"
  4. import (
  5. "time"
  6. "go.opentelemetry.io/otel/attribute"
  7. )
  8. // TracerConfig is a group of options for a Tracer.
  9. type TracerConfig struct {
  10. instrumentationVersion string
  11. // Schema URL of the telemetry emitted by the Tracer.
  12. schemaURL string
  13. attrs attribute.Set
  14. }
  15. // InstrumentationVersion returns the version of the library providing instrumentation.
  16. func (t *TracerConfig) InstrumentationVersion() string {
  17. return t.instrumentationVersion
  18. }
  19. // InstrumentationAttributes returns the attributes associated with the library
  20. // providing instrumentation.
  21. func (t *TracerConfig) InstrumentationAttributes() attribute.Set {
  22. return t.attrs
  23. }
  24. // SchemaURL returns the Schema URL of the telemetry emitted by the Tracer.
  25. func (t *TracerConfig) SchemaURL() string {
  26. return t.schemaURL
  27. }
  28. // NewTracerConfig applies all the options to a returned TracerConfig.
  29. func NewTracerConfig(options ...TracerOption) TracerConfig {
  30. var config TracerConfig
  31. for _, option := range options {
  32. config = option.apply(config)
  33. }
  34. return config
  35. }
  36. // TracerOption applies an option to a TracerConfig.
  37. type TracerOption interface {
  38. apply(TracerConfig) TracerConfig
  39. }
  40. type tracerOptionFunc func(TracerConfig) TracerConfig
  41. func (fn tracerOptionFunc) apply(cfg TracerConfig) TracerConfig {
  42. return fn(cfg)
  43. }
  44. // SpanConfig is a group of options for a Span.
  45. type SpanConfig struct {
  46. attributes []attribute.KeyValue
  47. timestamp time.Time
  48. links []Link
  49. newRoot bool
  50. spanKind SpanKind
  51. stackTrace bool
  52. }
  53. // Attributes describe the associated qualities of a Span.
  54. func (cfg *SpanConfig) Attributes() []attribute.KeyValue {
  55. return cfg.attributes
  56. }
  57. // Timestamp is a time in a Span life-cycle.
  58. func (cfg *SpanConfig) Timestamp() time.Time {
  59. return cfg.timestamp
  60. }
  61. // StackTrace checks whether stack trace capturing is enabled.
  62. func (cfg *SpanConfig) StackTrace() bool {
  63. return cfg.stackTrace
  64. }
  65. // Links are the associations a Span has with other Spans.
  66. func (cfg *SpanConfig) Links() []Link {
  67. return cfg.links
  68. }
  69. // NewRoot identifies a Span as the root Span for a new trace. This is
  70. // commonly used when an existing trace crosses trust boundaries and the
  71. // remote parent span context should be ignored for security.
  72. func (cfg *SpanConfig) NewRoot() bool {
  73. return cfg.newRoot
  74. }
  75. // SpanKind is the role a Span has in a trace.
  76. func (cfg *SpanConfig) SpanKind() SpanKind {
  77. return cfg.spanKind
  78. }
  79. // NewSpanStartConfig applies all the options to a returned SpanConfig.
  80. // No validation is performed on the returned SpanConfig (e.g. no uniqueness
  81. // checking or bounding of data), it is left to the SDK to perform this
  82. // action.
  83. func NewSpanStartConfig(options ...SpanStartOption) SpanConfig {
  84. var c SpanConfig
  85. for _, option := range options {
  86. c = option.applySpanStart(c)
  87. }
  88. return c
  89. }
  90. // NewSpanEndConfig applies all the options to a returned SpanConfig.
  91. // No validation is performed on the returned SpanConfig (e.g. no uniqueness
  92. // checking or bounding of data), it is left to the SDK to perform this
  93. // action.
  94. func NewSpanEndConfig(options ...SpanEndOption) SpanConfig {
  95. var c SpanConfig
  96. for _, option := range options {
  97. c = option.applySpanEnd(c)
  98. }
  99. return c
  100. }
  101. // SpanStartOption applies an option to a SpanConfig. These options are applicable
  102. // only when the span is created.
  103. type SpanStartOption interface {
  104. applySpanStart(SpanConfig) SpanConfig
  105. }
  106. type spanOptionFunc func(SpanConfig) SpanConfig
  107. func (fn spanOptionFunc) applySpanStart(cfg SpanConfig) SpanConfig {
  108. return fn(cfg)
  109. }
  110. // SpanEndOption applies an option to a SpanConfig. These options are
  111. // applicable only when the span is ended.
  112. type SpanEndOption interface {
  113. applySpanEnd(SpanConfig) SpanConfig
  114. }
  115. // EventConfig is a group of options for an Event.
  116. type EventConfig struct {
  117. attributes []attribute.KeyValue
  118. timestamp time.Time
  119. stackTrace bool
  120. }
  121. // Attributes describe the associated qualities of an Event.
  122. func (cfg *EventConfig) Attributes() []attribute.KeyValue {
  123. return cfg.attributes
  124. }
  125. // Timestamp is a time in an Event life-cycle.
  126. func (cfg *EventConfig) Timestamp() time.Time {
  127. return cfg.timestamp
  128. }
  129. // StackTrace checks whether stack trace capturing is enabled.
  130. func (cfg *EventConfig) StackTrace() bool {
  131. return cfg.stackTrace
  132. }
  133. // NewEventConfig applies all the EventOptions to a returned EventConfig. If no
  134. // timestamp option is passed, the returned EventConfig will have a Timestamp
  135. // set to the call time, otherwise no validation is performed on the returned
  136. // EventConfig.
  137. func NewEventConfig(options ...EventOption) EventConfig {
  138. var c EventConfig
  139. for _, option := range options {
  140. c = option.applyEvent(c)
  141. }
  142. if c.timestamp.IsZero() {
  143. c.timestamp = time.Now()
  144. }
  145. return c
  146. }
  147. // EventOption applies span event options to an EventConfig.
  148. type EventOption interface {
  149. applyEvent(EventConfig) EventConfig
  150. }
  151. // SpanOption are options that can be used at both the beginning and end of a span.
  152. type SpanOption interface {
  153. SpanStartOption
  154. SpanEndOption
  155. }
  156. // SpanStartEventOption are options that can be used at the start of a span, or with an event.
  157. type SpanStartEventOption interface {
  158. SpanStartOption
  159. EventOption
  160. }
  161. // SpanEndEventOption are options that can be used at the end of a span, or with an event.
  162. type SpanEndEventOption interface {
  163. SpanEndOption
  164. EventOption
  165. }
  166. type attributeOption []attribute.KeyValue
  167. func (o attributeOption) applySpan(c SpanConfig) SpanConfig {
  168. c.attributes = append(c.attributes, []attribute.KeyValue(o)...)
  169. return c
  170. }
  171. func (o attributeOption) applySpanStart(c SpanConfig) SpanConfig { return o.applySpan(c) }
  172. func (o attributeOption) applyEvent(c EventConfig) EventConfig {
  173. c.attributes = append(c.attributes, []attribute.KeyValue(o)...)
  174. return c
  175. }
  176. var _ SpanStartEventOption = attributeOption{}
  177. // WithAttributes adds the attributes related to a span life-cycle event.
  178. // These attributes are used to describe the work a Span represents when this
  179. // option is provided to a Span's start event. Otherwise, these
  180. // attributes provide additional information about the event being recorded
  181. // (e.g. error, state change, processing progress, system event).
  182. //
  183. // If multiple of these options are passed the attributes of each successive
  184. // option will extend the attributes instead of overwriting. There is no
  185. // guarantee of uniqueness in the resulting attributes.
  186. func WithAttributes(attributes ...attribute.KeyValue) SpanStartEventOption {
  187. return attributeOption(attributes)
  188. }
  189. // SpanEventOption are options that can be used with an event or a span.
  190. type SpanEventOption interface {
  191. SpanOption
  192. EventOption
  193. }
  194. type timestampOption time.Time
  195. func (o timestampOption) applySpan(c SpanConfig) SpanConfig {
  196. c.timestamp = time.Time(o)
  197. return c
  198. }
  199. func (o timestampOption) applySpanStart(c SpanConfig) SpanConfig { return o.applySpan(c) }
  200. func (o timestampOption) applySpanEnd(c SpanConfig) SpanConfig { return o.applySpan(c) }
  201. func (o timestampOption) applyEvent(c EventConfig) EventConfig {
  202. c.timestamp = time.Time(o)
  203. return c
  204. }
  205. var _ SpanEventOption = timestampOption{}
  206. // WithTimestamp sets the time of a Span or Event life-cycle moment (e.g.
  207. // started, stopped, errored).
  208. func WithTimestamp(t time.Time) SpanEventOption {
  209. return timestampOption(t)
  210. }
  211. type stackTraceOption bool
  212. func (o stackTraceOption) applyEvent(c EventConfig) EventConfig {
  213. c.stackTrace = bool(o)
  214. return c
  215. }
  216. func (o stackTraceOption) applySpan(c SpanConfig) SpanConfig {
  217. c.stackTrace = bool(o)
  218. return c
  219. }
  220. func (o stackTraceOption) applySpanEnd(c SpanConfig) SpanConfig { return o.applySpan(c) }
  221. // WithStackTrace sets the flag to capture the error with stack trace (e.g. true, false).
  222. func WithStackTrace(b bool) SpanEndEventOption {
  223. return stackTraceOption(b)
  224. }
  225. // WithLinks adds links to a Span. The links are added to the existing Span
  226. // links, i.e. this does not overwrite. Links with invalid span context are ignored.
  227. func WithLinks(links ...Link) SpanStartOption {
  228. return spanOptionFunc(func(cfg SpanConfig) SpanConfig {
  229. cfg.links = append(cfg.links, links...)
  230. return cfg
  231. })
  232. }
  233. // WithNewRoot specifies that the Span should be treated as a root Span. Any
  234. // existing parent span context will be ignored when defining the Span's trace
  235. // identifiers.
  236. func WithNewRoot() SpanStartOption {
  237. return spanOptionFunc(func(cfg SpanConfig) SpanConfig {
  238. cfg.newRoot = true
  239. return cfg
  240. })
  241. }
  242. // WithSpanKind sets the SpanKind of a Span.
  243. func WithSpanKind(kind SpanKind) SpanStartOption {
  244. return spanOptionFunc(func(cfg SpanConfig) SpanConfig {
  245. cfg.spanKind = kind
  246. return cfg
  247. })
  248. }
  249. // WithInstrumentationVersion sets the instrumentation version.
  250. func WithInstrumentationVersion(version string) TracerOption {
  251. return tracerOptionFunc(func(cfg TracerConfig) TracerConfig {
  252. cfg.instrumentationVersion = version
  253. return cfg
  254. })
  255. }
  256. // WithInstrumentationAttributes sets the instrumentation attributes.
  257. //
  258. // The passed attributes will be de-duplicated.
  259. func WithInstrumentationAttributes(attr ...attribute.KeyValue) TracerOption {
  260. return tracerOptionFunc(func(config TracerConfig) TracerConfig {
  261. config.attrs = attribute.NewSet(attr...)
  262. return config
  263. })
  264. }
  265. // WithSchemaURL sets the schema URL for the Tracer.
  266. func WithSchemaURL(schemaURL string) TracerOption {
  267. return tracerOptionFunc(func(cfg TracerConfig) TracerConfig {
  268. cfg.schemaURL = schemaURL
  269. return cfg
  270. })
  271. }