opentelemetry-api 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +9 -0
  3. data/CHANGELOG.md +1 -0
  4. data/LICENSE +201 -0
  5. data/OVERVIEW.md +66 -0
  6. data/lib/opentelemetry-api.rb +7 -0
  7. data/lib/opentelemetry.rb +61 -0
  8. data/lib/opentelemetry/context.rb +154 -0
  9. data/lib/opentelemetry/context/key.rb +29 -0
  10. data/lib/opentelemetry/context/propagation.rb +22 -0
  11. data/lib/opentelemetry/context/propagation/composite_propagator.rb +73 -0
  12. data/lib/opentelemetry/context/propagation/default_getter.rb +26 -0
  13. data/lib/opentelemetry/context/propagation/default_setter.rb +26 -0
  14. data/lib/opentelemetry/context/propagation/noop_extractor.rb +26 -0
  15. data/lib/opentelemetry/context/propagation/noop_injector.rb +26 -0
  16. data/lib/opentelemetry/context/propagation/propagation.rb +27 -0
  17. data/lib/opentelemetry/context/propagation/propagator.rb +64 -0
  18. data/lib/opentelemetry/correlation_context.rb +16 -0
  19. data/lib/opentelemetry/correlation_context/builder.rb +18 -0
  20. data/lib/opentelemetry/correlation_context/manager.rb +36 -0
  21. data/lib/opentelemetry/correlation_context/propagation.rb +57 -0
  22. data/lib/opentelemetry/correlation_context/propagation/context_keys.rb +27 -0
  23. data/lib/opentelemetry/correlation_context/propagation/text_extractor.rb +60 -0
  24. data/lib/opentelemetry/correlation_context/propagation/text_injector.rb +55 -0
  25. data/lib/opentelemetry/error.rb +9 -0
  26. data/lib/opentelemetry/instrumentation.rb +15 -0
  27. data/lib/opentelemetry/instrumentation/base.rb +245 -0
  28. data/lib/opentelemetry/instrumentation/registry.rb +87 -0
  29. data/lib/opentelemetry/internal.rb +22 -0
  30. data/lib/opentelemetry/metrics.rb +16 -0
  31. data/lib/opentelemetry/metrics/handles.rb +44 -0
  32. data/lib/opentelemetry/metrics/instruments.rb +105 -0
  33. data/lib/opentelemetry/metrics/meter.rb +72 -0
  34. data/lib/opentelemetry/metrics/meter_provider.rb +22 -0
  35. data/lib/opentelemetry/trace.rb +51 -0
  36. data/lib/opentelemetry/trace/event.rb +46 -0
  37. data/lib/opentelemetry/trace/link.rb +46 -0
  38. data/lib/opentelemetry/trace/propagation.rb +17 -0
  39. data/lib/opentelemetry/trace/propagation/context_keys.rb +35 -0
  40. data/lib/opentelemetry/trace/propagation/trace_context.rb +59 -0
  41. data/lib/opentelemetry/trace/propagation/trace_context/text_extractor.rb +58 -0
  42. data/lib/opentelemetry/trace/propagation/trace_context/text_injector.rb +55 -0
  43. data/lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb +130 -0
  44. data/lib/opentelemetry/trace/span.rb +145 -0
  45. data/lib/opentelemetry/trace/span_context.rb +56 -0
  46. data/lib/opentelemetry/trace/span_kind.rb +35 -0
  47. data/lib/opentelemetry/trace/status.rb +114 -0
  48. data/lib/opentelemetry/trace/trace_flags.rb +50 -0
  49. data/lib/opentelemetry/trace/tracer.rb +103 -0
  50. data/lib/opentelemetry/trace/tracer_provider.rb +22 -0
  51. data/lib/opentelemetry/trace/util/http_to_status.rb +47 -0
  52. data/lib/opentelemetry/version.rb +10 -0
  53. metadata +220 -0
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module Trace
9
+ module Propagation
10
+ # Contains the keys used to index the current span, or extracted span
11
+ # context in a {Context} instance
12
+ module ContextKeys
13
+ extend self
14
+
15
+ EXTRACTED_SPAN_CONTEXT_KEY = Context.create_key('extracted-span-context')
16
+ CURRENT_SPAN_KEY = Context.create_key('current-span')
17
+ private_constant :EXTRACTED_SPAN_CONTEXT_KEY, :CURRENT_SPAN_KEY
18
+
19
+ # Returns the context key that an extracted span context is indexed by
20
+ #
21
+ # @return [Context::Key]
22
+ def extracted_span_context_key
23
+ EXTRACTED_SPAN_CONTEXT_KEY
24
+ end
25
+
26
+ # Returns the context key that the current span is indexed by
27
+ #
28
+ # @return [Context::Key]
29
+ def current_span_key
30
+ CURRENT_SPAN_KEY
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require 'opentelemetry/trace/propagation/trace_context/trace_parent'
8
+ require 'opentelemetry/trace/propagation/trace_context/text_extractor'
9
+ require 'opentelemetry/trace/propagation/trace_context/text_injector'
10
+
11
+ module OpenTelemetry
12
+ module Trace
13
+ module Propagation
14
+ # The TraceContext module contains injectors, extractors, and utilties
15
+ # for context propagation in the W3C Trace Context format.
16
+ module TraceContext
17
+ extend self
18
+
19
+ TEXT_EXTRACTOR = TextExtractor.new
20
+ TEXT_INJECTOR = TextInjector.new
21
+ RACK_EXTRACTOR = TextExtractor.new(
22
+ traceparent_key: 'HTTP_TRACEPARENT',
23
+ tracestate_key: 'HTTP_TRACESTATE'
24
+ )
25
+ RACK_INJECTOR = TextInjector.new(
26
+ traceparent_key: 'HTTP_TRACEPARENT',
27
+ tracestate_key: 'HTTP_TRACESTATE'
28
+ )
29
+
30
+ private_constant :TEXT_INJECTOR, :TEXT_EXTRACTOR,
31
+ :RACK_INJECTOR, :RACK_EXTRACTOR
32
+
33
+ # Returns an extractor that extracts context using the W3C Trace Context
34
+ # format
35
+ def text_extractor
36
+ TEXT_EXTRACTOR
37
+ end
38
+
39
+ # Returns an injector that injects context using the W3C Trace Context
40
+ # format
41
+ def text_injector
42
+ TEXT_INJECTOR
43
+ end
44
+
45
+ # Returns an extractor that extracts context using the W3C Trace Context
46
+ # with Rack normalized keys (upcased and prefixed with HTTP_)
47
+ def rack_extractor
48
+ RACK_EXTRACTOR
49
+ end
50
+
51
+ # Returns an injector that injects context using the W3C Trace Context
52
+ # format with Rack normalized keys (upcased and prefixed with HTTP_)
53
+ def rack_injector
54
+ RACK_INJECTOR
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+ module OpenTelemetry
7
+ module Trace
8
+ module Propagation
9
+ module TraceContext
10
+ # Extracts context from carriers in the W3C Trace Context format
11
+ class TextExtractor
12
+ include Context::Propagation::DefaultGetter
13
+
14
+ # Returns a new TextExtractor that extracts context using the
15
+ # specified header keys
16
+ #
17
+ # @param [String] traceparent_key The traceparent header key used in the carrier
18
+ # @param [String] tracestate_key The tracestate header key used in the carrier
19
+ # @return [TextExtractor]
20
+ def initialize(traceparent_key: 'traceparent',
21
+ tracestate_key: 'tracestate')
22
+ @traceparent_key = traceparent_key
23
+ @tracestate_key = tracestate_key
24
+ end
25
+
26
+ # Extract a remote {Trace::SpanContext} from the supplied carrier.
27
+ # Invalid headers will result in a new, valid, non-remote {Trace::SpanContext}.
28
+ #
29
+ # @param [Carrier] carrier The carrier to get the header from.
30
+ # @param [Context] context The context to be updated with extracted context
31
+ # @param [optional Callable] getter An optional callable that takes a carrier and a key and
32
+ # returns the value associated with the key. If omitted the default getter will be used
33
+ # which expects the carrier to respond to [] and []=.
34
+ # @yield [Carrier, String] if an optional getter is provided, extract will yield the carrier
35
+ # and the header key to the getter.
36
+ # @return [Context] Updated context with span context from the header, or the original
37
+ # context if parsing fails.
38
+ def extract(carrier, context, &getter)
39
+ getter ||= default_getter
40
+ header = getter.call(carrier, @traceparent_key)
41
+ tp = TraceParent.from_string(header)
42
+
43
+ tracestate = getter.call(carrier, @tracestate_key)
44
+
45
+ span_context = Trace::SpanContext.new(trace_id: tp.trace_id,
46
+ span_id: tp.span_id,
47
+ trace_flags: tp.flags,
48
+ tracestate: tracestate,
49
+ remote: true)
50
+ context.set_value(ContextKeys.extracted_span_context_key, span_context)
51
+ rescue OpenTelemetry::Error
52
+ context
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+ module OpenTelemetry
7
+ module Trace
8
+ module Propagation
9
+ module TraceContext
10
+ # Injects context into carriers using the W3C Trace Context format
11
+ class TextInjector
12
+ include Context::Propagation::DefaultSetter
13
+
14
+ # Returns a new TextInjector that injects context using the
15
+ # specified header keys
16
+ #
17
+ # @param [String] traceparent_key The traceparent header key used in the carrier
18
+ # @param [String] tracestate_key The tracestate header key used in the carrier
19
+ # @return [TextInjector]
20
+ def initialize(traceparent_key: 'traceparent',
21
+ tracestate_key: 'tracestate')
22
+ @traceparent_key = traceparent_key
23
+ @tracestate_key = tracestate_key
24
+ end
25
+
26
+ # Set the span context on the supplied carrier.
27
+ #
28
+ # @param [Context] context The active {Context}.
29
+ # @param [optional Callable] setter An optional callable that takes a carrier and a key and
30
+ # a value and assigns the key-value pair in the carrier. If omitted the default setter
31
+ # will be used which expects the carrier to respond to [] and []=.
32
+ # @yield [Carrier, String, String] if an optional setter is provided, inject will yield
33
+ # carrier, header key, header value to the setter.
34
+ # @return [Object] the carrier with context injected
35
+ def inject(carrier, context, &setter)
36
+ return carrier unless (span_context = span_context_from(context))
37
+
38
+ setter ||= DEFAULT_SETTER
39
+ setter.call(carrier, @traceparent_key, TraceParent.from_context(span_context).to_s)
40
+ setter.call(carrier, @tracestate_key, span_context.tracestate) unless span_context.tracestate.nil?
41
+
42
+ carrier
43
+ end
44
+
45
+ private
46
+
47
+ def span_context_from(context)
48
+ context[ContextKeys.current_span_key]&.context ||
49
+ context[ContextKeys.extracted_span_context_key]
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+ module OpenTelemetry
7
+ module Trace
8
+ module Propagation
9
+ module TraceContext
10
+ # A TraceParent is an implementation of the W3C trace context specification
11
+ # https://www.w3.org/TR/trace-context/
12
+ # {Trace::SpanContext}
13
+ class TraceParent
14
+ InvalidFormatError = Class.new(Error)
15
+ InvalidVersionError = Class.new(Error)
16
+ InvalidTraceIDError = Class.new(Error)
17
+ InvalidSpanIDError = Class.new(Error)
18
+
19
+ TRACE_PARENT_HEADER = 'traceparent'
20
+ SUPPORTED_VERSION = 0
21
+ private_constant :SUPPORTED_VERSION
22
+ MAX_VERSION = 254
23
+ private_constant :MAX_VERSION
24
+
25
+ REGEXP = /^(?<version>[A-Fa-f0-9]{2})-(?<trace_id>[A-Fa-f0-9]{32})-(?<span_id>[A-Fa-f0-9]{16})-(?<flags>[A-Fa-f0-9]{2})(?<ignored>-.*)?$/.freeze
26
+ private_constant :REGEXP
27
+
28
+ INVALID_TRACE_ID = OpenTelemetry::Trace::INVALID_TRACE_ID.unpack1('H*')
29
+ INVALID_SPAN_ID = OpenTelemetry::Trace::INVALID_SPAN_ID.unpack1('H*')
30
+ private_constant :INVALID_TRACE_ID, :INVALID_SPAN_ID
31
+
32
+ class << self
33
+ # Creates a new {TraceParent} from a supplied {Trace::SpanContext}
34
+ # @param [SpanContext] ctx The context
35
+ # @return [TraceParent] a trace parent
36
+ def from_context(ctx)
37
+ new(trace_id: ctx.trace_id, span_id: ctx.span_id, flags: ctx.trace_flags)
38
+ end
39
+
40
+ # Deserializes the {TraceParent} from the string representation
41
+ # @param [String] string The serialized trace parent
42
+ # @return [TraceParent] a trace_parent
43
+ # @raise [InvalidFormatError] on an invalid format
44
+ # @raise [InvalidVerionError] on an invalid version
45
+ # @raise [InvalidTraceIDError] on an invalid trace_id
46
+ # @raise [InvalidSpanIDError] on an invalid span_id
47
+ def from_string(string)
48
+ matches = match_input(string)
49
+
50
+ version = parse_version(matches[:version])
51
+ raise InvalidFormatError if version > SUPPORTED_VERSION && string.length < 55
52
+
53
+ trace_id = parse_trace_id(matches[:trace_id])
54
+ span_id = parse_span_id(matches[:span_id])
55
+ flags = parse_flags(matches[:flags])
56
+
57
+ new(trace_id: trace_id, span_id: span_id, flags: flags)
58
+ end
59
+
60
+ private
61
+
62
+ def match_input(string)
63
+ matches = REGEXP.match(string)
64
+ raise InvalidFormatError, 'regexp match failed' if !matches || matches.length < 6
65
+
66
+ matches
67
+ end
68
+
69
+ def parse_version(string)
70
+ v = string.to_i(16)
71
+ raise InvalidFormatError, string unless v
72
+ raise InvalidVersionError, v if v > MAX_VERSION
73
+
74
+ v
75
+ end
76
+
77
+ def parse_trace_id(string)
78
+ raise InvalidTraceIDError, string if string == INVALID_TRACE_ID
79
+
80
+ string.downcase!
81
+ Array(string).pack('H*')
82
+ end
83
+
84
+ def parse_span_id(string)
85
+ raise InvalidSpanIDError, string if string == INVALID_SPAN_ID
86
+
87
+ string.downcase!
88
+ Array(string).pack('H*')
89
+ end
90
+
91
+ def parse_flags(string)
92
+ OpenTelemetry::Trace::TraceFlags.from_byte(string.to_i(16))
93
+ end
94
+ end
95
+
96
+ attr_reader :version, :trace_id, :span_id, :flags
97
+
98
+ private_class_method :new
99
+
100
+ # Returns the sampling choice from the trace_flags
101
+ # @return [Boolean] the sampling choice
102
+ def sampled?
103
+ flags.sampled?
104
+ end
105
+
106
+ # converts this object into a string according to the w3c spec
107
+ # @return [String] the serialized trace_parent
108
+ def to_s
109
+ "00-#{trace_id.unpack1('H*')}-#{span_id.unpack1('H*')}-#{flag_string}"
110
+ end
111
+
112
+ private
113
+
114
+ def flag_string
115
+ # the w3c standard only dictates the one flag for this version
116
+ # therefore we can only output the one flag.
117
+ flags.sampled? ? '01' : '00'
118
+ end
119
+
120
+ def initialize(trace_id: nil, span_id: nil, version: SUPPORTED_VERSION, flags: Trace::TraceFlags::DEFAULT)
121
+ @trace_id = trace_id
122
+ @span_id = span_id
123
+ @version = version
124
+ @flags = flags
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,145 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module Trace
9
+ # Span represents a single operation within a trace. Spans can be nested to
10
+ # form a trace tree. Often, a trace contains a root span that describes the
11
+ # end-to-end latency and, optionally, one or more sub-spans for its
12
+ # sub-operations.
13
+ #
14
+ # Once Span {Tracer#start_span is created} - Span operations can be used to
15
+ # add additional properties to it like attributes, links, events, name and
16
+ # resulting status. Span cannot be used to retrieve these properties. This
17
+ # prevents the mis-use of spans as an in-process information propagation
18
+ # mechanism.
19
+ #
20
+ # {Span} must be ended by calling {#finish}.
21
+ class Span
22
+ # Retrieve the spans SpanContext
23
+ #
24
+ # The returned value may be used even after the Span is finished.
25
+ #
26
+ # @return [SpanContext]
27
+ attr_reader :context
28
+
29
+ # Spans must be created using {Tracer}. This is for internal use only.
30
+ #
31
+ # @api private
32
+ def initialize(span_context: nil)
33
+ @context = span_context || SpanContext.new
34
+ end
35
+
36
+ # Return whether this span is recording.
37
+ #
38
+ # @return [Boolean] true if this Span is active and recording information
39
+ # like events with the #add_event operation and attributes using
40
+ # #set_attribute.
41
+ def recording?
42
+ false
43
+ end
44
+
45
+ # Set attribute
46
+ #
47
+ # Note that the OpenTelemetry project
48
+ # {https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md
49
+ # documents} certain "standard attributes" that have prescribed semantic
50
+ # meanings.
51
+ #
52
+ # @param [String] key
53
+ # @param [String, Boolean, Numeric] value
54
+ #
55
+ # @return [self] returns itself
56
+ def set_attribute(key, value)
57
+ self
58
+ end
59
+ alias []= set_attribute
60
+
61
+ # Add an Event to a {Span}. This can be accomplished eagerly or lazily.
62
+ # Lazy evaluation is useful when the event attributes are expensive to
63
+ # build and where the cost can be avoided for an unsampled {Span}.
64
+ #
65
+ # Eager example:
66
+ #
67
+ # span.add_event(name: 'event', attributes: {'eager' => true})
68
+ #
69
+ # Lazy example:
70
+ #
71
+ # span.add_event { tracer.create_event(name: 'event', attributes: {'eager' => false}) }
72
+ #
73
+ # Note that the OpenTelemetry project
74
+ # {https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md
75
+ # documents} certain "standard event names and keys" which have
76
+ # prescribed semantic meanings.
77
+ #
78
+ # @param [optional String] name Optional name of the event. This is
79
+ # required if a block is not given.
80
+ # @param [optional Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}]
81
+ # attributes One or more key:value pairs, where the keys must be
82
+ # strings and the values may be (array of) string, boolean or numeric
83
+ # type. This argument should only be used when passing in a name.
84
+ # @param [optional Time] timestamp Optional timestamp for the event.
85
+ # This argument should only be used when passing in a name.
86
+ #
87
+ # @return [self] returns itself
88
+ def add_event(name: nil, attributes: nil, timestamp: nil)
89
+ self
90
+ end
91
+
92
+ # Record an error during the execution of this span. Multiple errors
93
+ # can be recorded on a span.
94
+ #
95
+ # @param [Exception] error The error to recorded
96
+ #
97
+ # @return [void]
98
+ def record_error(error); end
99
+
100
+ # Sets the Status to the Span
101
+ #
102
+ # If used, this will override the default Span status. Default is OK.
103
+ #
104
+ # Only the value of the last call will be recorded, and implementations
105
+ # are free to ignore previous calls.
106
+ #
107
+ # @param [Status] status The new status, which overrides the default Span
108
+ # status, which is OK.
109
+ #
110
+ # @return [void]
111
+ def status=(status); end
112
+
113
+ # Updates the Span name
114
+ #
115
+ # Upon this update, any sampling behavior based on Span name will depend
116
+ # on the implementation.
117
+ #
118
+ # @param [String] new_name The new operation name, which supersedes
119
+ # whatever was passed in when the Span was started
120
+ #
121
+ # @return [void]
122
+ def name=(new_name); end
123
+
124
+ # Finishes the Span
125
+ #
126
+ # Implementations MUST ignore all subsequent calls to {#finish} (there
127
+ # might be exceptions when Tracer is streaming event and has no mutable
128
+ # state associated with the Span).
129
+ #
130
+ # Call to {#finish} MUST not have any effects on child spans. Those may
131
+ # still be running and can be ended later.
132
+ #
133
+ # This API MUST be non-blocking.
134
+ #
135
+ # @param [Time] end_timestamp optional end timestamp for the span.
136
+ #
137
+ # @return [self] returns itself
138
+ def finish(end_timestamp: nil)
139
+ self
140
+ end
141
+
142
+ INVALID = new(span_context: SpanContext::INVALID)
143
+ end
144
+ end
145
+ end