config.go 9.2 KB

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