config.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. config := new(TracerConfig)
  36. for _, option := range options {
  37. option.apply(config)
  38. }
  39. return config
  40. }
  41. // TracerOption applies an option to a TracerConfig.
  42. type TracerOption interface {
  43. apply(*TracerConfig)
  44. }
  45. type tracerOptionFunc func(*TracerConfig)
  46. func (fn tracerOptionFunc) apply(cfg *TracerConfig) {
  47. 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. }
  57. // Attributes describe the associated qualities of a Span.
  58. func (cfg *SpanConfig) Attributes() []attribute.KeyValue {
  59. return cfg.attributes
  60. }
  61. // Timestamp is a time in a Span life-cycle.
  62. func (cfg *SpanConfig) Timestamp() time.Time {
  63. return cfg.timestamp
  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. c := new(SpanConfig)
  85. for _, option := range options {
  86. 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. c := new(SpanConfig)
  96. for _, option := range options {
  97. 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)
  105. }
  106. type spanOptionFunc func(*SpanConfig)
  107. func (fn spanOptionFunc) applySpanStart(cfg *SpanConfig) {
  108. 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)
  114. }
  115. // EventConfig is a group of options for an Event.
  116. type EventConfig struct {
  117. attributes []attribute.KeyValue
  118. timestamp time.Time
  119. }
  120. // Attributes describe the associated qualities of an Event.
  121. func (cfg *EventConfig) Attributes() []attribute.KeyValue {
  122. return cfg.attributes
  123. }
  124. // Timestamp is a time in an Event life-cycle.
  125. func (cfg *EventConfig) Timestamp() time.Time {
  126. return cfg.timestamp
  127. }
  128. // NewEventConfig applies all the EventOptions to a returned SpanConfig. If no
  129. // timestamp option is passed, the returned SpanConfig will have a Timestamp
  130. // set to the call time, otherwise no validation is performed on the returned
  131. // SpanConfig.
  132. func NewEventConfig(options ...EventOption) *EventConfig {
  133. c := new(EventConfig)
  134. for _, option := range options {
  135. option.applyEvent(c)
  136. }
  137. if c.timestamp.IsZero() {
  138. c.timestamp = time.Now()
  139. }
  140. return c
  141. }
  142. // EventOption applies span event options to an EventConfig.
  143. type EventOption interface {
  144. applyEvent(*EventConfig)
  145. }
  146. // SpanOption are options that can be used at both the beginning and end of a span.
  147. type SpanOption interface {
  148. SpanStartOption
  149. SpanEndOption
  150. }
  151. // SpanStartEventOption are options that can be used at the start of a span, or with an event.
  152. type SpanStartEventOption interface {
  153. SpanStartOption
  154. EventOption
  155. }
  156. type attributeOption []attribute.KeyValue
  157. func (o attributeOption) applySpan(c *SpanConfig) {
  158. c.attributes = append(c.attributes, []attribute.KeyValue(o)...)
  159. }
  160. func (o attributeOption) applySpanStart(c *SpanConfig) { o.applySpan(c) }
  161. func (o attributeOption) applyEvent(c *EventConfig) {
  162. c.attributes = append(c.attributes, []attribute.KeyValue(o)...)
  163. }
  164. var _ SpanStartEventOption = attributeOption{}
  165. // WithAttributes adds the attributes related to a span life-cycle event.
  166. // These attributes are used to describe the work a Span represents when this
  167. // option is provided to a Span's start or end events. Otherwise, these
  168. // attributes provide additional information about the event being recorded
  169. // (e.g. error, state change, processing progress, system event).
  170. //
  171. // If multiple of these options are passed the attributes of each successive
  172. // option will extend the attributes instead of overwriting. There is no
  173. // guarantee of uniqueness in the resulting attributes.
  174. func WithAttributes(attributes ...attribute.KeyValue) SpanStartEventOption {
  175. return attributeOption(attributes)
  176. }
  177. // SpanEventOption are options that can be used with an event or a span.
  178. type SpanEventOption interface {
  179. SpanOption
  180. EventOption
  181. }
  182. type timestampOption time.Time
  183. func (o timestampOption) applySpan(c *SpanConfig) { c.timestamp = time.Time(o) }
  184. func (o timestampOption) applySpanStart(c *SpanConfig) { o.applySpan(c) }
  185. func (o timestampOption) applySpanEnd(c *SpanConfig) { o.applySpan(c) }
  186. func (o timestampOption) applyEvent(c *EventConfig) { c.timestamp = time.Time(o) }
  187. var _ SpanEventOption = timestampOption{}
  188. // WithTimestamp sets the time of a Span or Event life-cycle moment (e.g.
  189. // started, stopped, errored).
  190. func WithTimestamp(t time.Time) SpanEventOption {
  191. return timestampOption(t)
  192. }
  193. // WithLinks adds links to a Span. The links are added to the existing Span
  194. // links, i.e. this does not overwrite.
  195. func WithLinks(links ...Link) SpanStartOption {
  196. return spanOptionFunc(func(cfg *SpanConfig) {
  197. cfg.links = append(cfg.links, links...)
  198. })
  199. }
  200. // WithNewRoot specifies that the Span should be treated as a root Span. Any
  201. // existing parent span context will be ignored when defining the Span's trace
  202. // identifiers.
  203. func WithNewRoot() SpanStartOption {
  204. return spanOptionFunc(func(cfg *SpanConfig) {
  205. cfg.newRoot = true
  206. })
  207. }
  208. // WithSpanKind sets the SpanKind of a Span.
  209. func WithSpanKind(kind SpanKind) SpanStartOption {
  210. return spanOptionFunc(func(cfg *SpanConfig) {
  211. cfg.spanKind = kind
  212. })
  213. }
  214. // WithInstrumentationVersion sets the instrumentation version.
  215. func WithInstrumentationVersion(version string) TracerOption {
  216. return tracerOptionFunc(func(cfg *TracerConfig) {
  217. cfg.instrumentationVersion = version
  218. })
  219. }
  220. // WithSchemaURL sets the schema URL for the Tracer.
  221. func WithSchemaURL(schemaURL string) TracerOption {
  222. return tracerOptionFunc(func(cfg *TracerConfig) {
  223. cfg.schemaURL = schemaURL
  224. })
  225. }