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
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright 2019 OpenTelemetry Authors
4
- #
5
- # SPDX-License-Identifier: Apache-2.0
6
-
7
- require 'opentelemetry/distributed_context/propagation/binary_format'
8
- require 'opentelemetry/distributed_context/propagation/trace_parent'
9
- require 'opentelemetry/distributed_context/propagation/text_format'
10
-
11
- module OpenTelemetry
12
- module DistributedContext
13
- # Propagation API consists of two main formats:
14
- # - @see BinaryFormat is used to serialize and deserialize a value into a binary representation.
15
- # - @see TextFormat is used to inject and extract a value as text into carriers that travel in-band across process boundaries.
16
- module Propagation
17
- end
18
- end
19
- end
@@ -1,26 +0,0 @@
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 DistributedContext
9
- module Propagation
10
- # Formatter for serializing and deserializing a SpanContext into a binary format.
11
- class BinaryFormat
12
- EMPTY_BYTE_ARRAY = [].freeze
13
-
14
- private_constant(:EMPTY_BYTE_ARRAY)
15
-
16
- def to_bytes(span_context)
17
- EMPTY_BYTE_ARRAY
18
- end
19
-
20
- def from_bytes(bytes)
21
- Trace::SpanContext.invalid
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,76 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright 2019 OpenTelemetry Authors
4
- #
5
- # SPDX-License-Identifier: Apache-2.0
6
- module OpenTelemetry
7
- module DistributedContext
8
- module Propagation
9
- # TextFormat is a formatter that injects and extracts a value as text into carriers that travel in-band across
10
- # process boundaries.
11
- # Encoding is expected to conform to the HTTP Header Field semantics. Values are often encoded as RPC/HTTP request
12
- # headers.
13
- #
14
- # The carrier of propagated data on both the client (injector) and server (extractor) side is usually an http request.
15
- # Propagation is usually implemented via library-specific request interceptors, where the client-side injects values
16
- # and the server-side extracts them.
17
- class TextFormat
18
- DEFAULT_GETTER = ->(carrier, key) { carrier[key] }
19
- DEFAULT_SETTER = ->(carrier, key, value) { carrier[key] = value }
20
- private_constant(:DEFAULT_GETTER, :DEFAULT_SETTER)
21
-
22
- # Returns an array with the trace context header keys used by this formatter
23
- attr_reader :fields
24
-
25
- # Returns a new TextFormat that injects and extracts using the specified trace context
26
- # header keys
27
- #
28
- # @param [String] traceparent_header_key The traceparent header key used in the carrier
29
- # @param [String] tracestate_header_key The tracestate header key used in the carrier
30
- # @return [TextFormatter]
31
- def initialize(traceparent_header_key:, tracestate_header_key:)
32
- @traceparent_header_key = traceparent_header_key
33
- @tracestate_header_key = tracestate_header_key
34
- @fields = [traceparent_header_key, tracestate_header_key].freeze
35
- end
36
-
37
- # Return a remote {Trace::SpanContext} extracted from the supplied carrier. Expects the
38
- # the supplied carrier to have keys in rack normalized format (HTTP_#{UPPERCASE_KEY}).
39
- # Invalid headers will result in a new, valid, non-remote {Trace::SpanContext}.
40
- #
41
- # @param [Carrier] carrier The carrier to get the header from.
42
- # @param [optional Callable] getter An optional callable that takes a carrier and a key and
43
- # returns the value associated with the key. If omitted the default getter will be used
44
- # which expects the carrier to respond to [] and []=.
45
- # @yield [Carrier, String] if an optional getter is provided, extract will yield the carrier
46
- # and the header key to the getter.
47
- # @return [SpanContext] the span context from the header, or a new one if parsing fails.
48
- def extract(carrier, &getter)
49
- getter ||= DEFAULT_GETTER
50
- header = getter.call(carrier, @traceparent_header_key)
51
- tp = TraceParent.from_string(header)
52
-
53
- tracestate = getter.call(carrier, @tracestate_header_key)
54
-
55
- Trace::SpanContext.new(trace_id: tp.trace_id, span_id: tp.span_id, trace_flags: tp.flags, tracestate: tracestate, remote: true)
56
- rescue OpenTelemetry::Error
57
- Trace::SpanContext.new
58
- end
59
-
60
- # Set the span context on the supplied carrier.
61
- #
62
- # @param [SpanContext] context The active {Trace::SpanContext}.
63
- # @param [optional Callable] setter An optional callable that takes a carrier and a key and
64
- # a value and assigns the key-value pair in the carrier. If omitted the default setter
65
- # will be used which expects the carrier to respond to [] and []=.
66
- # @yield [Carrier, String, String] if an optional setter is provided, inject will yield
67
- # carrier, header key, header value to the setter.
68
- def inject(context, carrier, &setter)
69
- setter ||= DEFAULT_SETTER
70
- setter.call(carrier, @traceparent_header_key, TraceParent.from_context(context).to_s)
71
- setter.call(carrier, @tracestate_header_key, context.tracestate) unless context.tracestate.nil?
72
- end
73
- end
74
- end
75
- end
76
- end
@@ -1,124 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright 2019 OpenTelemetry Authors
4
- #
5
- # SPDX-License-Identifier: Apache-2.0
6
- module OpenTelemetry
7
- module DistributedContext
8
- module Propagation
9
- # A TraceParent is an implementation of the W3C trace context specification
10
- # https://www.w3.org/TR/trace-context/
11
- # {Trace::SpanContext}
12
- class TraceParent
13
- InvalidFormatError = Class.new(Error)
14
- InvalidVersionError = Class.new(Error)
15
- InvalidTraceIDError = Class.new(Error)
16
- InvalidSpanIDError = Class.new(Error)
17
-
18
- TRACE_PARENT_HEADER = 'traceparent'
19
- SUPPORTED_VERSION = 0
20
- private_constant :SUPPORTED_VERSION
21
- MAX_VERSION = 254
22
- private_constant :MAX_VERSION
23
-
24
- 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
25
- private_constant :REGEXP
26
-
27
- class << self
28
- # Creates a new {TraceParent} from a supplied {Trace::SpanContext}
29
- # @param [SpanContext] ctx The context
30
- # @return [TraceParent] a trace parent
31
- def from_context(ctx)
32
- new(trace_id: ctx.trace_id, span_id: ctx.span_id, flags: ctx.trace_flags)
33
- end
34
-
35
- # Deserializes the {TraceParent} from the string representation
36
- # @param [String] string The serialized trace parent
37
- # @return [TraceParent] a trace_parent
38
- # @raise [InvalidFormatError] on an invalid format
39
- # @raise [InvalidVerionError] on an invalid version
40
- # @raise [InvalidTraceIDError] on an invalid trace_id
41
- # @raise [InvalidSpanIDError] on an invalid span_id
42
- def from_string(string)
43
- matches = match_input(string)
44
-
45
- version = parse_version(matches[:version])
46
- raise InvalidFormatError if version > SUPPORTED_VERSION && string.length < 55
47
-
48
- trace_id = parse_trace_id(matches[:trace_id])
49
- span_id = parse_span_id(matches[:span_id])
50
- flags = parse_flags(matches[:flags])
51
-
52
- new(trace_id: trace_id, span_id: span_id, flags: flags)
53
- end
54
-
55
- private
56
-
57
- def match_input(string)
58
- matches = REGEXP.match(string)
59
- raise InvalidFormatError, 'regexp match failed' if !matches || matches.length < 6
60
-
61
- matches
62
- end
63
-
64
- def parse_version(string)
65
- v = string.to_i(16)
66
- raise InvalidFormatError, string unless v
67
- raise InvalidVersionError, v if v > MAX_VERSION
68
-
69
- v
70
- end
71
-
72
- def parse_trace_id(string)
73
- raise InvalidTraceIDError, string if string == OpenTelemetry::Trace::INVALID_TRACE_ID
74
-
75
- string.downcase!
76
- string
77
- end
78
-
79
- def parse_span_id(string)
80
- raise InvalidSpanIDError, string if string == OpenTelemetry::Trace::INVALID_SPAN_ID
81
-
82
- string.downcase!
83
- string
84
- end
85
-
86
- def parse_flags(string)
87
- OpenTelemetry::Trace::TraceFlags.from_byte(string.to_i(16))
88
- end
89
- end
90
-
91
- attr_reader :version, :trace_id, :span_id, :flags
92
-
93
- private_class_method :new
94
-
95
- # Returns the sampling choice from the trace_flags
96
- # @return [Boolean] the sampling choice
97
- def sampled?
98
- flags.sampled?
99
- end
100
-
101
- # converts this object into a string according to the w3c spec
102
- # @return [String] the serialized trace_parent
103
- def to_s
104
- "00-#{trace_id}-#{span_id}-#{flag_string}"
105
- end
106
-
107
- private
108
-
109
- def flag_string
110
- # the w3c standard only dictates the one flag for this version
111
- # therefore we can only output the one flag.
112
- flags.sampled? ? '01' : '00'
113
- end
114
-
115
- def initialize(trace_id: nil, span_id: nil, version: SUPPORTED_VERSION, flags: Trace::TraceFlags::DEFAULT)
116
- @trace_id = trace_id
117
- @span_id = span_id
118
- @version = version
119
- @flags = flags
120
- end
121
- end
122
- end
123
- end
124
- end
@@ -1,22 +0,0 @@
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
- # Hints to influence sampling decisions. The default option for span
10
- # creation is to not provide any suggestion.
11
- module SamplingHint
12
- # Suggest to not record events and not sample.
13
- NOT_RECORD = :__not_record__
14
-
15
- # Suggest to record events and not sample.
16
- RECORD = :__record__
17
-
18
- # Suggest to record events and sample.
19
- RECORD_AND_SAMPLED = :__record_and_sampled__
20
- end
21
- end
22
- end
@@ -1,45 +0,0 @@
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
- # No-op implementation of a tracer factory.
10
- class TracerFactory
11
- HTTP_TEXT_FORMAT = DistributedContext::Propagation::TextFormat.new(
12
- traceparent_header_key: 'traceparent',
13
- tracestate_header_key: 'tracestate'
14
- )
15
- RACK_HTTP_TEXT_FORMAT = DistributedContext::Propagation::TextFormat.new(
16
- traceparent_header_key: 'HTTP_TRACEPARENT',
17
- tracestate_header_key: 'HTTP_TRACESTATE'
18
- )
19
- BINARY_FORMAT = DistributedContext::Propagation::BinaryFormat.new
20
- private_constant(:HTTP_TEXT_FORMAT, :RACK_HTTP_TEXT_FORMAT, :BINARY_FORMAT)
21
-
22
- # Returns a {Tracer} instance.
23
- #
24
- # @param [optional String] name Instrumentation package name
25
- # @param [optional String] version Instrumentation package version
26
- #
27
- # @return [Tracer]
28
- def tracer(name = nil, version = nil)
29
- @tracer ||= Tracer.new
30
- end
31
-
32
- def binary_format
33
- BINARY_FORMAT
34
- end
35
-
36
- def http_text_format
37
- HTTP_TEXT_FORMAT
38
- end
39
-
40
- def rack_http_text_format
41
- RACK_HTTP_TEXT_FORMAT
42
- end
43
- end
44
- end
45
- end