opentelemetry-api 0.2.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +9 -0
  3. data/CHANGELOG.md +13 -0
  4. data/LICENSE +1 -1
  5. data/OVERVIEW.md +66 -0
  6. data/lib/{opentelemetry/distributed_context/manager.rb → opentelemetry-api.rb} +1 -6
  7. data/lib/opentelemetry.rb +37 -19
  8. data/lib/opentelemetry/baggage.rb +16 -0
  9. data/lib/opentelemetry/baggage/builder.rb +18 -0
  10. data/lib/opentelemetry/baggage/manager.rb +41 -0
  11. data/lib/opentelemetry/baggage/propagation.rb +57 -0
  12. data/lib/opentelemetry/baggage/propagation/context_keys.rb +27 -0
  13. data/lib/opentelemetry/baggage/propagation/text_map_extractor.rb +60 -0
  14. data/lib/opentelemetry/baggage/propagation/text_map_injector.rb +55 -0
  15. data/lib/opentelemetry/context.rb +138 -15
  16. data/lib/opentelemetry/context/key.rb +29 -0
  17. data/lib/opentelemetry/context/propagation.rb +22 -0
  18. data/lib/opentelemetry/context/propagation/composite_propagator.rb +73 -0
  19. data/lib/opentelemetry/context/propagation/default_getter.rb +26 -0
  20. data/lib/opentelemetry/context/propagation/default_setter.rb +26 -0
  21. data/lib/opentelemetry/context/propagation/noop_extractor.rb +26 -0
  22. data/lib/opentelemetry/context/propagation/noop_injector.rb +26 -0
  23. data/lib/opentelemetry/context/propagation/propagation.rb +27 -0
  24. data/lib/opentelemetry/context/propagation/propagator.rb +64 -0
  25. data/lib/opentelemetry/instrumentation.rb +15 -0
  26. data/lib/opentelemetry/instrumentation/base.rb +245 -0
  27. data/lib/opentelemetry/instrumentation/registry.rb +87 -0
  28. data/lib/opentelemetry/metrics.rb +1 -1
  29. data/lib/opentelemetry/metrics/handles.rb +5 -15
  30. data/lib/opentelemetry/metrics/instruments.rb +18 -69
  31. data/lib/opentelemetry/metrics/meter.rb +2 -39
  32. data/lib/opentelemetry/metrics/{meter_factory.rb → meter_provider.rb} +2 -2
  33. data/lib/opentelemetry/trace.rb +14 -17
  34. data/lib/opentelemetry/trace/link.rb +4 -3
  35. data/lib/opentelemetry/trace/propagation.rb +17 -0
  36. data/lib/opentelemetry/trace/propagation/context_keys.rb +35 -0
  37. data/lib/opentelemetry/trace/propagation/trace_context.rb +59 -0
  38. data/lib/opentelemetry/trace/propagation/trace_context/text_map_extractor.rb +58 -0
  39. data/lib/opentelemetry/trace/propagation/trace_context/text_map_injector.rb +55 -0
  40. data/lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb +130 -0
  41. data/lib/opentelemetry/trace/span.rb +20 -20
  42. data/lib/opentelemetry/trace/span_context.rb +25 -1
  43. data/lib/opentelemetry/trace/status.rb +7 -2
  44. data/lib/opentelemetry/trace/tracer.rb +47 -13
  45. data/lib/opentelemetry/trace/tracer_provider.rb +22 -0
  46. data/lib/opentelemetry/trace/util/http_to_status.rb +47 -0
  47. data/lib/opentelemetry/version.rb +1 -1
  48. metadata +35 -17
  49. data/lib/opentelemetry/distributed_context.rb +0 -19
  50. data/lib/opentelemetry/distributed_context/distributed_context.rb +0 -24
  51. data/lib/opentelemetry/distributed_context/entry.rb +0 -66
  52. data/lib/opentelemetry/distributed_context/propagation.rb +0 -19
  53. data/lib/opentelemetry/distributed_context/propagation/binary_format.rb +0 -26
  54. data/lib/opentelemetry/distributed_context/propagation/text_format.rb +0 -76
  55. data/lib/opentelemetry/distributed_context/propagation/trace_parent.rb +0 -124
  56. data/lib/opentelemetry/internal.rb +0 -22
  57. data/lib/opentelemetry/trace/event.rb +0 -45
  58. data/lib/opentelemetry/trace/sampling_hint.rb +0 -22
  59. data/lib/opentelemetry/trace/tracer_factory.rb +0 -45
@@ -4,28 +4,151 @@
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
7
+ require 'opentelemetry/context/key'
8
+ require 'opentelemetry/context/propagation'
9
+
7
10
  module OpenTelemetry
8
- # The Context module provides per-thread storage.
9
- module Context
10
- extend self
11
+ # Manages context on a per-fiber basis
12
+ class Context
13
+ KEY = :__opentelemetry_context__
14
+ EMPTY_ENTRIES = {}.freeze
15
+
16
+ class << self
17
+ # Returns a key used to index a value in a Context
18
+ #
19
+ # @param [String] name The key name
20
+ # @return [Context::Key]
21
+ def create_key(name)
22
+ Key.new(name)
23
+ end
24
+
25
+ # Returns current context, which is never nil
26
+ #
27
+ # @return [Context]
28
+ def current
29
+ Thread.current[KEY] ||= ROOT
30
+ end
31
+
32
+ # Sets the current context
33
+ #
34
+ # @param [Context] ctx The context to be made active
35
+ def current=(ctx)
36
+ Thread.current[KEY] = ctx
37
+ end
38
+
39
+ # Executes a block with ctx as the current context. It restores
40
+ # the previous context upon exiting.
41
+ #
42
+ # @param [Context] ctx The context to be made active
43
+ # @yield [context] Yields context to the block
44
+ def with_current(ctx)
45
+ prev = ctx.attach
46
+ yield ctx
47
+ ensure
48
+ ctx.detach(prev)
49
+ end
50
+
51
+ # Execute a block in a new context with key set to value. Restores the
52
+ # previous context after the block executes.
53
+
54
+ # @param [String] key The lookup key
55
+ # @param [Object] value The object stored under key
56
+ # @param [Callable] Block to execute in a new context
57
+ # @yield [context, value] Yields the newly created context and value to
58
+ # the block
59
+ def with_value(key, value)
60
+ ctx = current.set_value(key, value)
61
+ prev = ctx.attach
62
+ yield ctx, value
63
+ ensure
64
+ ctx.detach(prev)
65
+ end
66
+
67
+ # Execute a block in a new context where its values are merged with the
68
+ # incoming values. Restores the previous context after the block executes.
69
+
70
+ # @param [String] key The lookup key
71
+ # @param [Hash] values Will be merged with values of the current context
72
+ # and returned in a new context
73
+ # @param [Callable] Block to execute in a new context
74
+ # @yield [context, values] Yields the newly created context and values
75
+ # to the block
76
+ def with_values(values)
77
+ ctx = current.set_values(values)
78
+ prev = ctx.attach
79
+ yield ctx, values
80
+ ensure
81
+ ctx.detach(prev)
82
+ end
83
+
84
+ # Returns the value associated with key in the current context
85
+ #
86
+ # @param [String] key The lookup key
87
+ def value(key)
88
+ current.value(key)
89
+ end
90
+
91
+ def clear
92
+ self.current = ROOT
93
+ end
11
94
 
12
- def get(key)
13
- storage[key]
95
+ def empty
96
+ new(nil, EMPTY_ENTRIES)
97
+ end
14
98
  end
15
99
 
16
- def with(key, value)
17
- store = storage
18
- previous = store[key]
19
- store[key] = value
20
- yield value
21
- ensure
22
- store[key] = previous
100
+ def initialize(parent, entries)
101
+ @parent = parent
102
+ @entries = entries.freeze
23
103
  end
24
104
 
25
- private
105
+ # Returns the corresponding value (or nil) for key
106
+ #
107
+ # @param [Key] key The lookup key
108
+ # @return [Object]
109
+ def value(key)
110
+ @entries[key]
111
+ end
112
+
113
+ alias [] value
114
+
115
+ # Returns a new Context where entries contains the newly added key and value
116
+ #
117
+ # @param [Key] key The key to store this value under
118
+ # @param [Object] value Object to be stored under key
119
+ # @return [Context]
120
+ def set_value(key, value)
121
+ new_entries = @entries.dup
122
+ new_entries[key] = value
123
+ Context.new(self, new_entries)
124
+ end
125
+
126
+ # Returns a new Context with the current context's entries merged with the
127
+ # new entries
128
+ #
129
+ # @param [Hash] values The values to be merged with the current context's
130
+ # entries.
131
+ # @param [Object] value Object to be stored under key
132
+ # @return [Context]
133
+ def set_values(values) # rubocop:disable Naming/AccessorMethodName:
134
+ Context.new(self, @entries.merge(values))
135
+ end
26
136
 
27
- def storage
28
- Thread.current[:__opentelemetry__] ||= {}
137
+ # @api private
138
+ def attach
139
+ prev = self.class.current
140
+ self.class.current = self
141
+ prev
29
142
  end
143
+
144
+ # @api private
145
+ def detach(ctx_to_attach = nil)
146
+ OpenTelemetry.logger.warn 'Calls to detach should match corresponding calls to attach' if self.class.current != self
147
+
148
+ ctx_to_attach ||= @parent || ROOT
149
+ ctx_to_attach.attach
150
+ end
151
+
152
+ ROOT = empty.freeze
30
153
  end
31
154
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ class Context
9
+ # The Key class provides mechanisms to index and access values from a
10
+ # Context
11
+ class Key
12
+ attr_reader :name
13
+
14
+ # @api private
15
+ # Use Context.create_key to obtain a Key instance.
16
+ def initialize(name)
17
+ @name = name
18
+ end
19
+
20
+ # Returns the value indexed by this Key in the specified context
21
+ #
22
+ # @param [optional Context] context The Context to lookup the key from.
23
+ # Defaults to +Context.current+.
24
+ def get(context = Context.current)
25
+ context[self]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require 'opentelemetry/context/propagation/composite_propagator'
8
+ require 'opentelemetry/context/propagation/default_getter'
9
+ require 'opentelemetry/context/propagation/default_setter'
10
+ require 'opentelemetry/context/propagation/noop_extractor'
11
+ require 'opentelemetry/context/propagation/noop_injector'
12
+ require 'opentelemetry/context/propagation/propagation'
13
+ require 'opentelemetry/context/propagation/propagator'
14
+
15
+ module OpenTelemetry
16
+ class Context
17
+ # The propagation module contains APIs and utilities to interact with context
18
+ # and propagate across process boundaries.
19
+ module Propagation
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ class Context
9
+ module Propagation
10
+ # A composite propagator composes a list of injectors and extractors into
11
+ # single interface exposing inject and extract methods. Injection and
12
+ # extraction will preserve the order of the injectors and extractors
13
+ # passed in during initialization.
14
+ class CompositePropagator
15
+ # Returns a Propagator that extracts using the provided extractors
16
+ # and injectors.
17
+ #
18
+ # @param [Array<#inject>] injectors
19
+ # @param [Array<#extract>] extractors
20
+ def initialize(injectors, extractors)
21
+ @injectors = injectors
22
+ @extractors = extractors
23
+ end
24
+
25
+ # Runs injectors in order and returns a carrier. If an injection fails
26
+ # a warning will be logged and remaining injectors will be executed.
27
+ # Always returns a valid carrier.
28
+ #
29
+ # @param [Object] carrier A carrier to inject context into
30
+ # context into
31
+ # @param [optional Context] context Context to be injected into carrier.
32
+ # Defaults to +Context.current+
33
+ # @param [optional Callable] setter An optional callable that takes a
34
+ # carrier, a key and a value and assigns the key-value pair in the
35
+ # carrier. If omitted the default setter will be used which expects
36
+ # the carrier to respond to [] and []=.
37
+ #
38
+ # @return [Object] carrier
39
+ def inject(carrier, context = Context.current, &setter)
40
+ @injectors.inject(carrier) do |memo, injector|
41
+ injector.inject(memo, context, &setter)
42
+ rescue => e # rubocop:disable Style/RescueStandardError
43
+ OpenTelemetry.logger.warn "Error in CompositePropagator#inject #{e.message}"
44
+ carrier
45
+ end
46
+ end
47
+
48
+ # Runs extractors in order and returns a Context updated with the
49
+ # results of each extraction. If an extraction fails, a warning will be
50
+ # logged and remaining extractors will continue to be executed. Always
51
+ # returns a valid context.
52
+ #
53
+ # @param [Object] carrier The carrier to extract context from
54
+ # @param [optional Context] context Context to be updated with the state
55
+ # extracted from the carrier. Defaults to +Context.current+
56
+ # @param [optional Callable] getter An optional callable that takes a carrier and a key and
57
+ # returns the value associated with the key. If omitted the default getter will be used
58
+ # which expects the carrier to respond to [] and []=.
59
+ #
60
+ # @return [Context] a new context updated with state extracted from the
61
+ # carrier
62
+ def extract(carrier, context = Context.current, &getter)
63
+ @extractors.inject(context) do |ctx, extractor|
64
+ extractor.extract(carrier, ctx, &getter)
65
+ rescue => e # rubocop:disable Style/RescueStandardError
66
+ OpenTelemetry.logger.warn "Error in CompositePropagator#extract #{e.message}"
67
+ ctx
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ class Context
9
+ module Propagation
10
+ # The default getter module provides a common method for reading
11
+ # a key from a carrier that implements +[]+
12
+ module DefaultGetter
13
+ DEFAULT_GETTER = ->(carrier, key) { carrier[key] }
14
+ private_constant :DEFAULT_GETTER
15
+
16
+ # Returns a callable that can read a key from a carrier that implements
17
+ # +[]+. Useful for extract operations.
18
+ #
19
+ # @return [Callable]
20
+ def default_getter
21
+ DEFAULT_GETTER
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ class Context
9
+ module Propagation
10
+ # The default setter module provides a common method for writing
11
+ # a key into a carrier that implements +[]=+
12
+ module DefaultSetter
13
+ DEFAULT_SETTER = ->(carrier, key, value) { carrier[key] = value }
14
+ private_constant :DEFAULT_SETTER
15
+
16
+ # Returns a callable that can write a key into a carrier that implements
17
+ # +[]=+. Useful for inject operations.
18
+ #
19
+ # @return [Callable]
20
+ def default_setter
21
+ DEFAULT_SETTER
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ class Context
9
+ module Propagation
10
+ # A no-op extractor implementation
11
+ class NoopExtractor
12
+ # Extract a context from the given carrier
13
+ #
14
+ # @param [Object] carrier The carrier to extract the context from
15
+ # @param [Context] context The context to be upated with the extracted
16
+ # context
17
+ # @param [optional Callable] getter An optional callable that takes a carrier and a key and
18
+ # and returns the value associated with the key
19
+ # @return [Context]
20
+ def extract(carrier, context, &getter)
21
+ context
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ class Context
9
+ module Propagation
10
+ # A no-op injector implementation
11
+ class NoopInjector
12
+ # Inject the given context into the specified carrier
13
+ #
14
+ # @param [Object] carrier The carrier to inject the provided context
15
+ # into
16
+ # @param [Context] context The context to be injected
17
+ # @param [optional Callable] setter An optional callable that takes a carrier and a key and
18
+ # a value and assigns the key-value pair in the carrier
19
+ # @return [Object] carrier
20
+ def inject(carrier, context, &setter)
21
+ carrier
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ class Context
9
+ module Propagation
10
+ # The Propagation class provides methods to inject and extract context
11
+ # to pass across process boundaries
12
+ class Propagation
13
+ # Get or set the global http propagator. Use a CompositePropagator
14
+ # to propagate multiple formats.
15
+ attr_accessor :http
16
+
17
+ # Get or set the global text propagator. Use a CompositePropagator
18
+ # to propagate multiple formats.
19
+ attr_accessor :text
20
+
21
+ def initialize
22
+ @http = @text = Propagator.new(NoopInjector.new, NoopExtractor.new)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ class Context
9
+ module Propagation
10
+ # A propagator composes an extractor and injector into a single interface
11
+ # exposing inject and extract methods
12
+ class Propagator
13
+ # Returns a Propagator that delegates inject and extract to the provided
14
+ # injector and extractor
15
+ #
16
+ # @param [#inject] injector
17
+ # @param [#extract] extractor
18
+ def initialize(injector, extractor)
19
+ @injector = injector
20
+ @extractor = extractor
21
+ end
22
+
23
+ # Returns a carrier with the provided context injected according the
24
+ # underlying injector. Returns the carrier and logs a warning if
25
+ # injection fails.
26
+ #
27
+ # @param [Object] carrier A carrier to inject context into
28
+ # context into
29
+ # @param [optional Context] context Context to be injected into carrier. Defaults
30
+ # to +Context.current+
31
+ # @param [optional Callable] setter An optional callable that takes a carrier, a key and
32
+ # a value and assigns the key-value pair in the carrier. If omitted the default setter
33
+ # will be used which expects the carrier to respond to [] and []=.
34
+ #
35
+ # @return [Object] carrier
36
+ def inject(carrier, context = Context.current, &setter)
37
+ @injector.inject(carrier, context, &setter)
38
+ rescue => e # rubocop:disable Style/RescueStandardError
39
+ OpenTelemetry.logger.warn "Error in Propagator#inject #{e.message}"
40
+ carrier
41
+ end
42
+
43
+ # Extracts and returns context from a carrier. Returns the provided
44
+ # context and logs a warning if an error if extraction fails.
45
+ #
46
+ # @param [Object] carrier The carrier to extract context from
47
+ # @param [optional Context] context Context to be updated with the state
48
+ # extracted from the carrier. Defaults to +Context.current+
49
+ # @param [optional Callable] getter An optional callable that takes a carrier and a key and
50
+ # returns the value associated with the key. If omitted the default getter will be used
51
+ # which expects the carrier to respond to [] and []=.
52
+ #
53
+ # @return [Context] a new context updated with state extracted from the
54
+ # carrier
55
+ def extract(carrier, context = Context.current, &getter)
56
+ @extractor.extract(carrier, context, &getter)
57
+ rescue => e # rubocop:disable Style/RescueStandardError
58
+ OpenTelemetry.logger.warn "Error in Propagator#extract #{e.message}"
59
+ context
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end