opentelemetry-api 0.2.0 → 0.3.0

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 (55) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +9 -0
  3. data/OVERVIEW.md +66 -0
  4. data/lib/{opentelemetry/distributed_context/manager.rb → opentelemetry-api.rb} +1 -6
  5. data/lib/opentelemetry.rb +34 -16
  6. data/lib/opentelemetry/context.rb +138 -15
  7. data/lib/opentelemetry/context/key.rb +29 -0
  8. data/lib/opentelemetry/context/propagation.rb +22 -0
  9. data/lib/opentelemetry/context/propagation/composite_propagator.rb +77 -0
  10. data/lib/opentelemetry/context/propagation/default_getter.rb +26 -0
  11. data/lib/opentelemetry/context/propagation/default_setter.rb +26 -0
  12. data/lib/opentelemetry/context/propagation/noop_extractor.rb +26 -0
  13. data/lib/opentelemetry/context/propagation/noop_injector.rb +26 -0
  14. data/lib/opentelemetry/context/propagation/propagation.rb +27 -0
  15. data/lib/opentelemetry/context/propagation/propagator.rb +64 -0
  16. data/lib/opentelemetry/correlation_context.rb +16 -0
  17. data/lib/opentelemetry/correlation_context/builder.rb +18 -0
  18. data/lib/opentelemetry/correlation_context/manager.rb +36 -0
  19. data/lib/opentelemetry/correlation_context/propagation.rb +57 -0
  20. data/lib/opentelemetry/correlation_context/propagation/context_keys.rb +27 -0
  21. data/lib/opentelemetry/correlation_context/propagation/text_extractor.rb +60 -0
  22. data/lib/opentelemetry/correlation_context/propagation/text_injector.rb +55 -0
  23. data/lib/opentelemetry/instrumentation.rb +15 -0
  24. data/lib/opentelemetry/instrumentation/adapter.rb +244 -0
  25. data/lib/opentelemetry/instrumentation/registry.rb +87 -0
  26. data/lib/opentelemetry/metrics.rb +1 -1
  27. data/lib/opentelemetry/metrics/handles.rb +5 -15
  28. data/lib/opentelemetry/metrics/instruments.rb +18 -69
  29. data/lib/opentelemetry/metrics/meter.rb +2 -39
  30. data/lib/opentelemetry/metrics/{meter_factory.rb → meter_provider.rb} +2 -2
  31. data/lib/opentelemetry/trace.rb +2 -2
  32. data/lib/opentelemetry/trace/event.rb +4 -3
  33. data/lib/opentelemetry/trace/link.rb +4 -3
  34. data/lib/opentelemetry/trace/propagation.rb +17 -0
  35. data/lib/opentelemetry/trace/propagation/context_keys.rb +35 -0
  36. data/lib/opentelemetry/trace/propagation/trace_context.rb +59 -0
  37. data/lib/opentelemetry/trace/propagation/trace_context/text_extractor.rb +58 -0
  38. data/lib/opentelemetry/trace/propagation/trace_context/text_injector.rb +55 -0
  39. data/lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb +126 -0
  40. data/lib/opentelemetry/trace/span.rb +14 -6
  41. data/lib/opentelemetry/trace/status.rb +7 -2
  42. data/lib/opentelemetry/trace/tracer.rb +47 -13
  43. data/lib/opentelemetry/trace/tracer_provider.rb +22 -0
  44. data/lib/opentelemetry/trace/util/http_to_status.rb +47 -0
  45. data/lib/opentelemetry/version.rb +1 -1
  46. metadata +33 -13
  47. data/lib/opentelemetry/distributed_context.rb +0 -19
  48. data/lib/opentelemetry/distributed_context/distributed_context.rb +0 -24
  49. data/lib/opentelemetry/distributed_context/entry.rb +0 -66
  50. data/lib/opentelemetry/distributed_context/propagation.rb +0 -19
  51. data/lib/opentelemetry/distributed_context/propagation/binary_format.rb +0 -26
  52. data/lib/opentelemetry/distributed_context/propagation/text_format.rb +0 -76
  53. data/lib/opentelemetry/distributed_context/propagation/trace_parent.rb +0 -124
  54. data/lib/opentelemetry/trace/sampling_hint.rb +0 -22
  55. data/lib/opentelemetry/trace/tracer_factory.rb +0 -45
@@ -8,46 +8,9 @@ module OpenTelemetry
8
8
  module Metrics
9
9
  # No-op implementation of Meter.
10
10
  class Meter
11
- NOOP_LABEL_SET = Object.new
12
- private_constant(:NOOP_LABEL_SET)
11
+ def record_batch(*measurements, labels: nil); end
13
12
 
14
- def record_batch(*measurements, label_set: nil); end
15
-
16
- # Canonicalizes labels, returning an opaque {LabelSet} object.
17
- #
18
- # @param [Hash<String, String>] labels
19
- # @return [LabelSet]
20
- def labels(labels)
21
- NOOP_LABEL_SET
22
- end
23
-
24
- # Create and return a floating point gauge.
25
- #
26
- # @param [String] name Name of the metric. See {Meter} for required metric name syntax.
27
- # @param [optional String] description Descriptive text documenting the instrument.
28
- # @param [optional String] unit Unit specified according to http://unitsofmeasure.org/ucum.html.
29
- # @param [optional Enumerable<String>] recommended_label_keys Recommended grouping keys for this instrument.
30
- # @param [optional Boolean] monotonic Whether the gauge accepts only monotonic updates. Defaults to false.
31
- # @return [FloatGauge]
32
- def create_float_gauge(name, description: nil, unit: nil, recommended_label_keys: nil, monotonic: false)
33
- raise ArgumentError if name.nil?
34
-
35
- Instruments::FloatGauge.new
36
- end
37
-
38
- # Create and return an integer gauge.
39
- #
40
- # @param [String] name Name of the metric. See {Meter} for required metric name syntax.
41
- # @param [optional String] description Descriptive text documenting the instrument.
42
- # @param [optional String] unit Unit specified according to http://unitsofmeasure.org/ucum.html.
43
- # @param [optional Enumerable<String>] recommended_label_keys Recommended grouping keys for this instrument.
44
- # @param [optional Boolean] monotonic Whether the gauge accepts only monotonic updates. Defaults to false.
45
- # @return [IntegerGauge]
46
- def create_integer_gauge(name, description: nil, unit: nil, recommended_label_keys: nil, monotonic: false)
47
- raise ArgumentError if name.nil?
48
-
49
- Instruments::IntegerGauge.new
50
- end
13
+ # TODO: Observers.
51
14
 
52
15
  # Create and return a floating point counter.
53
16
  #
@@ -6,8 +6,8 @@
6
6
 
7
7
  module OpenTelemetry
8
8
  module Metrics
9
- # No-op implementation of a meter factory.
10
- class MeterFactory
9
+ # No-op implementation of a meter provider.
10
+ class MeterProvider
11
11
  # Returns a {Meter} instance.
12
12
  #
13
13
  # @param [optional String] name Instrumentation package name
@@ -43,11 +43,11 @@ end
43
43
 
44
44
  require 'opentelemetry/trace/event'
45
45
  require 'opentelemetry/trace/link'
46
+ require 'opentelemetry/trace/propagation'
46
47
  require 'opentelemetry/trace/trace_flags'
47
48
  require 'opentelemetry/trace/span_context'
48
49
  require 'opentelemetry/trace/span_kind'
49
50
  require 'opentelemetry/trace/span'
50
- require 'opentelemetry/trace/sampling_hint'
51
51
  require 'opentelemetry/trace/status'
52
52
  require 'opentelemetry/trace/tracer'
53
- require 'opentelemetry/trace/tracer_factory'
53
+ require 'opentelemetry/trace/tracer_provider'
@@ -19,7 +19,7 @@ module OpenTelemetry
19
19
 
20
20
  # Returns the frozen attributes for this event
21
21
  #
22
- # @return [Hash<String, Object>]
22
+ # @return [Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}]
23
23
  attr_reader :attributes
24
24
 
25
25
  # Returns the timestamp for this event
@@ -30,8 +30,9 @@ module OpenTelemetry
30
30
  # Returns a new immutable {Event}.
31
31
  #
32
32
  # @param [String] name The name of this event
33
- # @param [optional Hash<String, Object>] attributes A hash of attributes for this
34
- # event. Attributes will be frozen during Event initialization.
33
+ # @param [optional Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}]
34
+ # attributes A hash of attributes for this event. Attributes will be
35
+ # frozen during Event initialization.
35
36
  # @param [optional Time] timestamp The timestamp for this event.
36
37
  # Defaults to Time.now.
37
38
  # @return [Event]
@@ -22,14 +22,15 @@ module OpenTelemetry
22
22
 
23
23
  # Returns the frozen attributes for this link.
24
24
  #
25
- # @return [Hash<String, Object>]
25
+ # @return [Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}]
26
26
  attr_reader :attributes
27
27
 
28
28
  # Returns a new immutable {Link}.
29
29
  #
30
30
  # @param [SpanContext] span_context The context of the linked {Span}.
31
- # @param [optional Hash<String, Object>] attributes A hash of attributes for
32
- # this link. Attributes will be frozen during Link initialization.
31
+ # @param [optional Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}]
32
+ # attributes A hash of attributes for this link. Attributes will be
33
+ # frozen during Link initialization.
33
34
  # @return [Link]
34
35
  def initialize(span_context, attributes = nil)
35
36
  @context = span_context
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require 'opentelemetry/trace/propagation/context_keys'
8
+ require 'opentelemetry/trace/propagation/trace_context'
9
+
10
+ module OpenTelemetry
11
+ module Trace
12
+ # The Trace::Propagation module contains injectors and extractors for
13
+ # sending and receiving span context over the wire
14
+ module Propagation
15
+ end
16
+ end
17
+ end
@@ -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,126 @@
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
+ class << self
29
+ # Creates a new {TraceParent} from a supplied {Trace::SpanContext}
30
+ # @param [SpanContext] ctx The context
31
+ # @return [TraceParent] a trace parent
32
+ def from_context(ctx)
33
+ new(trace_id: ctx.trace_id, span_id: ctx.span_id, flags: ctx.trace_flags)
34
+ end
35
+
36
+ # Deserializes the {TraceParent} from the string representation
37
+ # @param [String] string The serialized trace parent
38
+ # @return [TraceParent] a trace_parent
39
+ # @raise [InvalidFormatError] on an invalid format
40
+ # @raise [InvalidVerionError] on an invalid version
41
+ # @raise [InvalidTraceIDError] on an invalid trace_id
42
+ # @raise [InvalidSpanIDError] on an invalid span_id
43
+ def from_string(string)
44
+ matches = match_input(string)
45
+
46
+ version = parse_version(matches[:version])
47
+ raise InvalidFormatError if version > SUPPORTED_VERSION && string.length < 55
48
+
49
+ trace_id = parse_trace_id(matches[:trace_id])
50
+ span_id = parse_span_id(matches[:span_id])
51
+ flags = parse_flags(matches[:flags])
52
+
53
+ new(trace_id: trace_id, span_id: span_id, flags: flags)
54
+ end
55
+
56
+ private
57
+
58
+ def match_input(string)
59
+ matches = REGEXP.match(string)
60
+ raise InvalidFormatError, 'regexp match failed' if !matches || matches.length < 6
61
+
62
+ matches
63
+ end
64
+
65
+ def parse_version(string)
66
+ v = string.to_i(16)
67
+ raise InvalidFormatError, string unless v
68
+ raise InvalidVersionError, v if v > MAX_VERSION
69
+
70
+ v
71
+ end
72
+
73
+ def parse_trace_id(string)
74
+ raise InvalidTraceIDError, string if string == OpenTelemetry::Trace::INVALID_TRACE_ID
75
+
76
+ string.downcase!
77
+ string
78
+ end
79
+
80
+ def parse_span_id(string)
81
+ raise InvalidSpanIDError, string if string == OpenTelemetry::Trace::INVALID_SPAN_ID
82
+
83
+ string.downcase!
84
+ string
85
+ end
86
+
87
+ def parse_flags(string)
88
+ OpenTelemetry::Trace::TraceFlags.from_byte(string.to_i(16))
89
+ end
90
+ end
91
+
92
+ attr_reader :version, :trace_id, :span_id, :flags
93
+
94
+ private_class_method :new
95
+
96
+ # Returns the sampling choice from the trace_flags
97
+ # @return [Boolean] the sampling choice
98
+ def sampled?
99
+ flags.sampled?
100
+ end
101
+
102
+ # converts this object into a string according to the w3c spec
103
+ # @return [String] the serialized trace_parent
104
+ def to_s
105
+ "00-#{trace_id}-#{span_id}-#{flag_string}"
106
+ end
107
+
108
+ private
109
+
110
+ def flag_string
111
+ # the w3c standard only dictates the one flag for this version
112
+ # therefore we can only output the one flag.
113
+ flags.sampled? ? '01' : '00'
114
+ end
115
+
116
+ def initialize(trace_id: nil, span_id: nil, version: SUPPORTED_VERSION, flags: Trace::TraceFlags::DEFAULT)
117
+ @trace_id = trace_id
118
+ @span_id = span_id
119
+ @version = version
120
+ @flags = flags
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end