opentelemetry-api 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
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,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
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require 'opentelemetry/correlation_context/builder'
8
+ require 'opentelemetry/correlation_context/manager'
9
+ require 'opentelemetry/correlation_context/propagation'
10
+
11
+ module OpenTelemetry
12
+ # The CorrelationContext module provides functionality to record and propagate
13
+ # correlations in a distributed trace
14
+ module CorrelationContext
15
+ end
16
+ end
@@ -0,0 +1,18 @@
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 CorrelationContext
9
+ # No op implementation of CorrelationContext::Builder
10
+ class Builder
11
+ def set_value(key, value); end
12
+
13
+ def remove_value(key); end
14
+
15
+ def clear; end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,36 @@
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 CorrelationContext
9
+ # No op implementation of CorrelationContext::Manager
10
+ class Manager
11
+ NOOP_BUILDER = Builder.new
12
+ private_constant :NOOP_BUILDER
13
+
14
+ def build(context: Context.current)
15
+ yield NOOP_BUILDER
16
+ context
17
+ end
18
+
19
+ def set_value(key, value, context: Context.current)
20
+ context
21
+ end
22
+
23
+ def value(key, context: Context.current)
24
+ nil
25
+ end
26
+
27
+ def remove_value(key, context: Context.current)
28
+ context
29
+ end
30
+
31
+ def clear(context: Context.current)
32
+ context
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2019 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ require 'opentelemetry/correlation_context/propagation/context_keys'
8
+ require 'opentelemetry/correlation_context/propagation/text_injector'
9
+ require 'opentelemetry/correlation_context/propagation/text_extractor'
10
+
11
+ module OpenTelemetry
12
+ module CorrelationContext
13
+ # The Correlation::Propagation module contains injectors and
14
+ # extractors for sending and receiving correlation context over the wire
15
+ module Propagation
16
+ extend self
17
+
18
+ TEXT_EXTRACTOR = TextExtractor.new
19
+ TEXT_INJECTOR = TextInjector.new
20
+ RACK_EXTRACTOR = TextExtractor.new(
21
+ correlation_context_key: 'HTTP_OTCORRELATIONS'
22
+ )
23
+ RACK_INJECTOR = TextInjector.new(
24
+ correlation_context_key: 'HTTP_OTCORRELATIONS'
25
+ )
26
+
27
+ private_constant :TEXT_INJECTOR, :TEXT_EXTRACTOR, :RACK_INJECTOR,
28
+ :RACK_EXTRACTOR
29
+
30
+ # Returns an extractor that extracts context using the W3C Correlation
31
+ # Context format
32
+ def text_injector
33
+ TEXT_INJECTOR
34
+ end
35
+
36
+ # Returns an injector that injects context using the W3C Correlation
37
+ # Context format
38
+ def text_extractor
39
+ TEXT_EXTRACTOR
40
+ end
41
+
42
+ # Returns an extractor that extracts context using the W3C Correlation
43
+ # Context format with Rack normalized keys (upcased and prefixed with
44
+ # HTTP_)
45
+ def rack_injector
46
+ RACK_INJECTOR
47
+ end
48
+
49
+ # Returns an injector that injects context using the W3C Correlation
50
+ # Context format with Rack normalized keys (upcased and prefixed with
51
+ # HTTP_)
52
+ def rack_extractor
53
+ RACK_EXTRACTOR
54
+ end
55
+ end
56
+ end
57
+ end