config.go 9.8 KB

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