opentelemetry-sdk 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f952e49fadd62c0a174e8edfebff16e774a8421fd361c05a24af58402d2b267
4
- data.tar.gz: e34fd801558f119a59e6399f1f66ac9408413af12bf72a8ab9788a1d2a303e61
3
+ metadata.gz: 3a01fcf121db23108ffb47d196f9594993018bb1b945242710620bb87daa8320
4
+ data.tar.gz: ef3b3e9cd4e2853a22ffb149e7a18cbd021988412d24c40350192e222b3be58b
5
5
  SHA512:
6
- metadata.gz: 7d67df753f0f6af7626833ab968994bcbf3c6fd387638fbd0290c46ead629e25a5ccb2c421cb86a9c129876f007c9f1ce0bbac148e5830223352768624079399
7
- data.tar.gz: '07867ecc29a180417a4e2cc3243fd9cec2857390799192bc8e6b5be5641c4d64225ca5dcadf9311c23b73a4ab09729cca3006c3ee92aa39924d5887edf30ca3b'
6
+ metadata.gz: 5da08d0e902ece66efc7afa892f99f2b9fe3bcd5b89b51cd3da1a565a23ab0917eb1c39d6fb44ed6b82a71048624cb357539d9b6305b1a0c8debc594ef2467d3
7
+ data.tar.gz: 5fe47387c0d91e8d5436be4d4464839f08a9ae46964e8fe1679ab6ef7e7bf8b7a59ae086472d465e6f12c57b70c712a982064351f287a8ebbb6bbe173c37b45d
@@ -1 +1,24 @@
1
1
  # Release History: opentelemetry-sdk
2
+
3
+ ### v0.6.0 / 2020-09-10
4
+
5
+ * BREAKING CHANGE: Rename Resource labels to attributes
6
+ * BREAKING CHANGE: Export resource from Span/SpanData instead of library_resource
7
+ * BREAKING CHANGE: Rename CorrelationContext to Baggage
8
+ * BREAKING CHANGE: Rename Text* to TextMap* (propagators, injectors, extractors)
9
+ * BREAKING CHANGE: Rename span.record_error to span.record_exception
10
+ * BREAKING CHANGE: Update samplers to match spec
11
+ * BREAKING CHANGE: Remove support for lazy event creation
12
+
13
+ * ADDED: Add OTLP exporter
14
+ * ADDED: Add support for OTEL_LOG_LEVEL env var
15
+ * FIXED: Rename Resource labels to attributes
16
+ * ADDED: Environment variable resource detection
17
+ * ADDED: BatchSpanProcessor environment variable support
18
+ * FIXED: Remove semver prefix
19
+ * FIXED: Docs for array valued attributes
20
+ * ADDED: Add hex_trace_id and hex_span_id helpers to SpanData
21
+ * FIXED: Fix ProbabilitySampler
22
+ * ADDED: Implement GetCorrelations
23
+ * FIXED: Change default Sampler to ParentOrElse(AlwaysOn)
24
+ * FIXED: Fix probability sampler
data/README.md CHANGED
@@ -41,7 +41,7 @@ tracer.in_span('foo') do |span|
41
41
  # set an attribute
42
42
  span.set_attribute('platform', 'osx')
43
43
  # add an event
44
- span.add_event(name: 'event in bar')
44
+ span.add_event('event in bar')
45
45
  # create bar as child of foo
46
46
  tracer.in_span('bar') do |child_span|
47
47
  # inspect the span
@@ -61,7 +61,7 @@ module OpenTelemetry
61
61
  end
62
62
 
63
63
  require 'opentelemetry/sdk/configurator'
64
- require 'opentelemetry/sdk/correlation_context'
64
+ require 'opentelemetry/sdk/baggage'
65
65
  require 'opentelemetry/sdk/internal'
66
66
  require 'opentelemetry/sdk/instrumentation_library'
67
67
  require 'opentelemetry/sdk/resources'
@@ -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/sdk/baggage/builder'
8
+ require 'opentelemetry/sdk/baggage/manager'
9
+
10
+ module OpenTelemetry
11
+ module SDK
12
+ # Contains operational implementations of the Baggage::Manager
13
+ module Baggage
14
+ end
15
+ end
16
+ end
@@ -6,8 +6,8 @@
6
6
 
7
7
  module OpenTelemetry
8
8
  module SDK
9
- module CorrelationContext
10
- # SDK implementation of CorrelationContext::Builder
9
+ module Baggage
10
+ # SDK implementation of Baggage::Builder
11
11
  class Builder
12
12
  attr_reader :entries
13
13
 
@@ -15,7 +15,7 @@ module OpenTelemetry
15
15
  @entries = entries
16
16
  end
17
17
 
18
- # Set key-value in the to-be-created correlation context
18
+ # Set key-value in the to-be-created baggage
19
19
  #
20
20
  # @param [String] key The key to store this value under
21
21
  # @param [String] value String value to be stored under key
@@ -23,14 +23,14 @@ module OpenTelemetry
23
23
  @entries[key] = value.to_s
24
24
  end
25
25
 
26
- # Removes key from the to-be-created correlation context
26
+ # Removes key from the to-be-created baggage
27
27
  #
28
28
  # @param [String] key The key to remove
29
29
  def remove_value(key)
30
30
  @entries.delete(key)
31
31
  end
32
32
 
33
- # Clears all correlations from the to-be-created correlation context
33
+ # Clears all baggage from the to-be-created baggage
34
34
  def clear
35
35
  @entries.clear
36
36
  end
@@ -6,38 +6,38 @@
6
6
 
7
7
  module OpenTelemetry
8
8
  module SDK
9
- module CorrelationContext
10
- # Manages correlation context
9
+ module Baggage
10
+ # Manages baggage
11
11
  class Manager
12
- CORRELATION_CONTEXT_KEY = OpenTelemetry::CorrelationContext::Propagation::ContextKeys.correlation_context_key
13
- EMPTY_CORRELATION_CONTEXT = {}.freeze
14
- private_constant(:CORRELATION_CONTEXT_KEY, :EMPTY_CORRELATION_CONTEXT)
12
+ BAGGAGE_KEY = OpenTelemetry::Baggage::Propagation::ContextKeys.baggage_key
13
+ EMPTY_BAGGAGE = {}.freeze
14
+ private_constant(:BAGGAGE_KEY, :EMPTY_BAGGAGE)
15
15
 
16
- # Used to chain modifications to correlation context. The result is a
17
- # context with an updated correlation context. If only a single
18
- # modification is being made to correlation context, use the other
16
+ # Used to chain modifications to baggage. The result is a
17
+ # context with an updated baggage. If only a single
18
+ # modification is being made to baggage, use the other
19
19
  # methods on +Manager+, if multiple modifications are being made, use
20
20
  # this one.
21
21
  #
22
22
  # @param [optional Context] context The context to update with with new
23
- # modified correlation context. Defaults to +Context.current+
23
+ # modified baggage. Defaults to +Context.current+
24
24
  # @return [Context]
25
25
  def build_context(context: Context.current)
26
- builder = Builder.new(correlations_for(context).dup)
26
+ builder = Builder.new(baggage_for(context).dup)
27
27
  yield builder
28
- context.set_value(CORRELATION_CONTEXT_KEY, builder.entries)
28
+ context.set_value(BAGGAGE_KEY, builder.entries)
29
29
  end
30
30
 
31
- # Returns a new context with empty correlations
31
+ # Returns a new context with empty baggage
32
32
  #
33
- # @param [optional Context] context Context to clear correlations from. Defaults
33
+ # @param [optional Context] context Context to clear baggage from. Defaults
34
34
  # to +Context.current+
35
35
  # @return [Context]
36
36
  def clear(context: Context.current)
37
- context.set_value(CORRELATION_CONTEXT_KEY, EMPTY_CORRELATION_CONTEXT)
37
+ context.set_value(BAGGAGE_KEY, EMPTY_BAGGAGE)
38
38
  end
39
39
 
40
- # Returns the corresponding correlation value (or nil) for key
40
+ # Returns the corresponding baggage value (or nil) for key
41
41
  #
42
42
  # @param [String] key The lookup key
43
43
  # @param [optional Context] context The context from which to retrieve
@@ -45,7 +45,17 @@ module OpenTelemetry
45
45
  # Defaults to +Context.current+
46
46
  # @return [String]
47
47
  def value(key, context: Context.current)
48
- correlations_for(context)[key]
48
+ baggage_for(context)[key]
49
+ end
50
+
51
+ # Returns the baggage
52
+ #
53
+ # @param [optional Context] context The context from which to retrieve
54
+ # the baggage.
55
+ # Defaults to +Context.current+
56
+ # @return [Hash]
57
+ def values(context: Context.current)
58
+ baggage_for(context).dup.freeze
49
59
  end
50
60
 
51
61
  # Returns a new context with new key-value pair
@@ -56,30 +66,30 @@ module OpenTelemetry
56
66
  # value. Defaults to +Context.current+
57
67
  # @return [Context]
58
68
  def set_value(key, value, context: Context.current)
59
- new_correlations = correlations_for(context).dup
60
- new_correlations[key] = value
61
- context.set_value(CORRELATION_CONTEXT_KEY, new_correlations)
69
+ new_baggage = baggage_for(context).dup
70
+ new_baggage[key] = value
71
+ context.set_value(BAGGAGE_KEY, new_baggage)
62
72
  end
63
73
 
64
74
  # Returns a new context with value at key removed
65
75
  #
66
76
  # @param [String] key The key to remove
67
- # @param [optional Context] context The context to remove correlation
77
+ # @param [optional Context] context The context to remove baggage
68
78
  # from. Defaults to +Context.current+
69
79
  # @return [Context]
70
80
  def remove_value(key, context: Context.current)
71
- correlations = correlations_for(context)
72
- return context unless correlations.key?(key)
81
+ baggage = baggage_for(context)
82
+ return context unless baggage.key?(key)
73
83
 
74
- new_correlations = correlations.dup
75
- new_correlations.delete(key)
76
- context.set_value(CORRELATION_CONTEXT_KEY, new_correlations)
84
+ new_baggage = baggage.dup
85
+ new_baggage.delete(key)
86
+ context.set_value(BAGGAGE_KEY, new_baggage)
77
87
  end
78
88
 
79
89
  private
80
90
 
81
- def correlations_for(context)
82
- context.value(CORRELATION_CONTEXT_KEY) || EMPTY_CORRELATION_CONTEXT
91
+ def baggage_for(context)
92
+ context.value(BAGGAGE_KEY) || EMPTY_BAGGAGE
83
93
  end
84
94
  end
85
95
  end
@@ -15,32 +15,33 @@ module OpenTelemetry
15
15
 
16
16
  private_constant :USE_MODE_UNSPECIFIED, :USE_MODE_ONE, :USE_MODE_ALL
17
17
 
18
- attr_writer :logger, :http_extractors, :http_injectors, :text_extractors,
19
- :text_injectors
18
+ attr_writer :logger, :http_extractors, :http_injectors, :text_map_extractors,
19
+ :text_map_injectors
20
20
 
21
21
  def initialize
22
22
  @instrumentation_names = []
23
23
  @instrumentation_config_map = {}
24
24
  @http_extractors = nil
25
25
  @http_injectors = nil
26
- @text_extractors = nil
27
- @text_injectors = nil
26
+ @text_map_extractors = nil
27
+ @text_map_injectors = nil
28
28
  @span_processors = []
29
29
  @use_mode = USE_MODE_UNSPECIFIED
30
30
  @resource = Resources::Resource.telemetry_sdk
31
31
  end
32
32
 
33
33
  def logger
34
- @logger ||= Logger.new(STDOUT)
34
+ @logger ||= OpenTelemetry.logger
35
35
  end
36
36
 
37
37
  # Accepts a resource object that is merged with the default telemetry sdk
38
38
  # resource. The use of this method is optional, and is provided as means
39
- # to add additional resource information.
39
+ # to include additional resource information.
40
+ # If a resource key collision occurs the passed in resource takes priority.
40
41
  #
41
42
  # @param [Resource] new_resource The resource to be merged
42
43
  def resource=(new_resource)
43
- @resource = @resource.merge(new_resource)
44
+ @resource = new_resource.merge(@resource)
44
45
  end
45
46
 
46
47
  # Install an instrumentation with specificied optional +config+.
@@ -89,7 +90,7 @@ module OpenTelemetry
89
90
  # - install instrumentation
90
91
  def configure
91
92
  OpenTelemetry.logger = logger
92
- OpenTelemetry.correlations = CorrelationContext::Manager.new
93
+ OpenTelemetry.baggage = Baggage::Manager.new
93
94
  configure_propagation
94
95
  configure_span_processors
95
96
  OpenTelemetry.tracer_provider = tracer_provider
@@ -130,8 +131,8 @@ module OpenTelemetry
130
131
  def configure_propagation
131
132
  OpenTelemetry.propagation.http = create_propagator(@http_injectors || default_http_injectors,
132
133
  @http_extractors || default_http_extractors)
133
- OpenTelemetry.propagation.text = create_propagator(@text_injectors || default_text_injectors,
134
- @text_extractors || default_text_extractors)
134
+ OpenTelemetry.propagation.text = create_propagator(@text_map_injectors || default_text_map_injectors,
135
+ @text_map_extractors || default_text_map_extractors)
135
136
  end
136
137
 
137
138
  def create_propagator(injectors, extractors)
@@ -143,27 +144,27 @@ module OpenTelemetry
143
144
  end
144
145
 
145
146
  def default_http_injectors
146
- default_text_injectors
147
+ default_text_map_injectors
147
148
  end
148
149
 
149
150
  def default_http_extractors
150
151
  [
151
152
  OpenTelemetry::Trace::Propagation::TraceContext.rack_extractor,
152
- OpenTelemetry::CorrelationContext::Propagation.rack_extractor
153
+ OpenTelemetry::Baggage::Propagation.rack_extractor
153
154
  ]
154
155
  end
155
156
 
156
- def default_text_injectors
157
+ def default_text_map_injectors
157
158
  [
158
- OpenTelemetry::Trace::Propagation::TraceContext.text_injector,
159
- OpenTelemetry::CorrelationContext::Propagation.text_injector
159
+ OpenTelemetry::Trace::Propagation::TraceContext.text_map_injector,
160
+ OpenTelemetry::Baggage::Propagation.text_map_injector
160
161
  ]
161
162
  end
162
163
 
163
- def default_text_extractors
164
+ def default_text_map_extractors
164
165
  [
165
- OpenTelemetry::Trace::Propagation::TraceContext.text_extractor,
166
- OpenTelemetry::CorrelationContext::Propagation.text_extractor
166
+ OpenTelemetry::Trace::Propagation::TraceContext.text_map_extractor,
167
+ OpenTelemetry::Baggage::Propagation.text_map_extractor
167
168
  ]
168
169
  end
169
170
  end
@@ -13,29 +13,40 @@ module OpenTelemetry
13
13
  class << self
14
14
  private :new # rubocop:disable Style/AccessModifierDeclarations
15
15
 
16
- # Returns a newly created {Resource} with the specified labels
16
+ # Returns a newly created {Resource} with the specified attributes
17
17
  #
18
- # @param [Hash{String => String, Numeric, Boolean} labels Hash of key-value pairs to be used
19
- # as labels for this resource
20
- # @raise [ArgumentError] If label keys and values are not strings
18
+ # @param [Hash{String => String, Numeric, Boolean} attributes Hash of key-value pairs to be used
19
+ # as attributes for this resource
20
+ # @raise [ArgumentError] If attribute keys and values are not strings
21
21
  # @return [Resource]
22
- def create(labels = {})
23
- frozen_labels = labels.each_with_object({}) do |(k, v), memo|
24
- raise ArgumentError, 'label keys must be strings' unless k.is_a?(String)
25
- raise ArgumentError, 'label values must be strings, integers, floats, or booleans' unless Internal.valid_value?(v)
22
+ def create(attributes = {})
23
+ frozen_attributes = attributes.each_with_object({}) do |(k, v), memo|
24
+ raise ArgumentError, 'attribute keys must be strings' unless k.is_a?(String)
25
+ raise ArgumentError, 'attribute values must be strings, integers, floats, or booleans' unless Internal.valid_value?(v)
26
26
 
27
27
  memo[-k] = v.freeze
28
28
  end.freeze
29
29
 
30
- new(frozen_labels)
30
+ new(frozen_attributes)
31
31
  end
32
32
 
33
33
  def telemetry_sdk
34
- create(
34
+ resource_attributes = {
35
35
  Constants::TELEMETRY_SDK_RESOURCE[:name] => 'opentelemetry',
36
36
  Constants::TELEMETRY_SDK_RESOURCE[:language] => 'ruby',
37
- Constants::TELEMETRY_SDK_RESOURCE[:version] => "semver:#{OpenTelemetry::SDK::VERSION}"
38
- )
37
+ Constants::TELEMETRY_SDK_RESOURCE[:version] => OpenTelemetry::SDK::VERSION
38
+ }
39
+
40
+ resource_pairs = ENV['OTEL_RESOURCE_ATTRIBUTES']
41
+ return create(resource_attributes) unless resource_pairs.is_a?(String)
42
+
43
+ resource_pairs.split(',').each do |pair|
44
+ key, value = pair.split('=')
45
+ resource_attributes[key] = value
46
+ end
47
+
48
+ resource_attributes.delete_if { |_key, value| value.nil? || value.empty? }
49
+ create(resource_attributes)
39
50
  end
40
51
  end
41
52
 
@@ -44,18 +55,18 @@ module OpenTelemetry
44
55
  # Users should use the {create} factory method to obtain a {Resource}
45
56
  # instance.
46
57
  #
47
- # @param [Hash<String, String>] frozen_labels Frozen-hash of frozen-string
48
- # key-value pairs to be used as labels for this resource
58
+ # @param [Hash<String, String>] frozen_attributes Frozen-hash of frozen-string
59
+ # key-value pairs to be used as attributes for this resource
49
60
  # @return [Resource]
50
- def initialize(frozen_labels)
51
- @labels = frozen_labels
61
+ def initialize(frozen_attributes)
62
+ @attributes = frozen_attributes
52
63
  end
53
64
 
54
- # Returns an enumerator for labels of this {Resource}
65
+ # Returns an enumerator for attributes of this {Resource}
55
66
  #
56
67
  # @return [Enumerator]
57
- def label_enumerator
58
- @label_enumerator ||= labels.to_enum
68
+ def attribute_enumerator
69
+ @attribute_enumerator ||= attributes.to_enum
59
70
  end
60
71
 
61
72
  # Returns a new, merged {Resource} by merging the current {Resource} with
@@ -68,16 +79,16 @@ module OpenTelemetry
68
79
  def merge(other)
69
80
  return self unless other.is_a?(Resource)
70
81
 
71
- merged_labels = labels.merge(other.labels) do |_, old_v, new_v|
82
+ merged_attributes = attributes.merge(other.attributes) do |_, old_v, new_v|
72
83
  old_v.empty? ? new_v : old_v
73
84
  end
74
85
 
75
- self.class.send(:new, merged_labels.freeze)
86
+ self.class.send(:new, merged_attributes.freeze)
76
87
  end
77
88
 
78
89
  protected
79
90
 
80
- attr_reader :labels
91
+ attr_reader :attributes
81
92
  end
82
93
  end
83
94
  end
@@ -15,6 +15,7 @@ end
15
15
 
16
16
  require 'opentelemetry/sdk/trace/samplers'
17
17
  require 'opentelemetry/sdk/trace/config'
18
+ require 'opentelemetry/sdk/trace/event'
18
19
  require 'opentelemetry/sdk/trace/export'
19
20
  require 'opentelemetry/sdk/trace/multi_span_processor'
20
21
  require 'opentelemetry/sdk/trace/noop_span_processor'
@@ -10,7 +10,7 @@ module OpenTelemetry
10
10
  module Config
11
11
  # Class that holds global trace parameters.
12
12
  class TraceConfig
13
- DEFAULT_SAMPLER = Samplers::ALWAYS_ON
13
+ DEFAULT_SAMPLER = Samplers.parent_based(root: Samplers::ALWAYS_ON)
14
14
  DEFAULT_MAX_ATTRIBUTES_COUNT = 32
15
15
  DEFAULT_MAX_EVENTS_COUNT = 128
16
16
  DEFAULT_MAX_LINKS_COUNT = 32
@@ -30,13 +30,13 @@ module OpenTelemetry
30
30
  # The global default max number of attributes per {Span}.
31
31
  attr_reader :max_attributes_count
32
32
 
33
- # The global default max number of {OpenTelemetry::Trace::Event}s per {Span}.
33
+ # The global default max number of {OpenTelemetry::SDK::Trace::Event}s per {Span}.
34
34
  attr_reader :max_events_count
35
35
 
36
36
  # The global default max number of {OpenTelemetry::Trace::Link} entries per {Span}.
37
37
  attr_reader :max_links_count
38
38
 
39
- # The global default max number of attributes per {OpenTelemetry::Trace::Event}.
39
+ # The global default max number of attributes per {OpenTelemetry::SDK::Trace::Event}.
40
40
  attr_reader :max_attributes_per_event
41
41
 
42
42
  # The global default max number of attributes per {OpenTelemetry::Trace::Link}.
@@ -0,0 +1,48 @@
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 SDK
9
+ module Trace
10
+ # A text annotation with a set of attributes and a timestamp.
11
+ class Event
12
+ EMPTY_ATTRIBUTES = {}.freeze
13
+
14
+ private_constant :EMPTY_ATTRIBUTES
15
+
16
+ # Returns the name of this event
17
+ #
18
+ # @return [String]
19
+ attr_reader :name
20
+
21
+ # Returns the frozen attributes for this event
22
+ #
23
+ # @return [Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}]
24
+ attr_reader :attributes
25
+
26
+ # Returns the timestamp for this event
27
+ #
28
+ # @return [Time]
29
+ attr_reader :timestamp
30
+
31
+ # Returns a new immutable {Event}.
32
+ #
33
+ # @param [String] name The name of this event
34
+ # @param [optional Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}]
35
+ # attributes A hash of attributes for this event. Attributes will be
36
+ # frozen during Event initialization.
37
+ # @param [optional Time] timestamp The timestamp for this event.
38
+ # Defaults to Time.now.
39
+ # @return [Event]
40
+ def initialize(name:, attributes: nil, timestamp: nil)
41
+ @name = name
42
+ @attributes = attributes.freeze || EMPTY_ATTRIBUTES
43
+ @timestamp = timestamp || Time.now
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -23,17 +23,28 @@ module OpenTelemetry
23
23
  # worker thread that exports the spans to wake up and start a new
24
24
  # export cycle.
25
25
  class BatchSpanProcessor
26
- EXPORTER_TIMEOUT_MILLIS = 30_000
27
- SCHEDULE_DELAY_MILLIS = 5_000
28
- MAX_QUEUE_SIZE = 2048
29
- MAX_EXPORT_BATCH_SIZE = 512
30
- private_constant(:SCHEDULE_DELAY_MILLIS, :MAX_QUEUE_SIZE, :MAX_EXPORT_BATCH_SIZE)
31
-
26
+ # Returns a new instance of the {BatchSpanProcessor}.
27
+ #
28
+ # @param [SpanExporter] exporter
29
+ # @param [Numeric] exporter_timeout_millis the delay interval between two
30
+ # consecutive exports. Defaults to the value of the OTEL_BSP_EXPORT_TIMEOUT_MILLIS
31
+ # environment variable, if set, or 30,000 (30 seconds).
32
+ # @param [Numeric] schedule_delay_millis the maximum allowed time to export data.
33
+ # Defaults to the value of the OTEL_BSP_SCHEDULE_DELAY_MILLIS environment
34
+ # variable, if set, or 5,000 (5 seconds).
35
+ # @param [Integer] max_queue_size the maximum queue size in spans.
36
+ # Defaults to the value of the OTEL_BSP_MAX_QUEUE_SIZE environment
37
+ # variable, if set, or 2048.
38
+ # @param [Integer] max_export_batch_size the maximum batch size in spans.
39
+ # Defaults to the value of the OTEL_BSP_MAX_EXPORT_BATCH_SIZE environment
40
+ # variable, if set, or 512.
41
+ #
42
+ # @return a new instance of the {BatchSpanProcessor}.
32
43
  def initialize(exporter:,
33
- exporter_timeout_millis: EXPORTER_TIMEOUT_MILLIS,
34
- schedule_delay_millis: SCHEDULE_DELAY_MILLIS,
35
- max_queue_size: MAX_QUEUE_SIZE,
36
- max_export_batch_size: MAX_EXPORT_BATCH_SIZE)
44
+ exporter_timeout_millis: Float(ENV.fetch('OTEL_BSP_EXPORT_TIMEOUT_MILLIS', 30_000)),
45
+ schedule_delay_millis: Float(ENV.fetch('OTEL_BSP_SCHEDULE_DELAY_MILLIS', 5_000)),
46
+ max_queue_size: Integer(ENV.fetch('OTEL_BSP_MAX_QUEUE_SIZE', 2048)),
47
+ max_export_batch_size: Integer(ENV.fetch('OTEL_BSP_MAX_EXPORT_BATCH_SIZE', 512)))
37
48
  raise ArgumentError if max_export_batch_size > max_queue_size
38
49
 
39
50
  @exporter = exporter
@@ -7,15 +7,15 @@
7
7
  require 'opentelemetry/sdk/trace/samplers/decision'
8
8
  require 'opentelemetry/sdk/trace/samplers/result'
9
9
  require 'opentelemetry/sdk/trace/samplers/constant_sampler'
10
- require 'opentelemetry/sdk/trace/samplers/parent_or_else'
11
- require 'opentelemetry/sdk/trace/samplers/probability_sampler'
10
+ require 'opentelemetry/sdk/trace/samplers/parent_based'
11
+ require 'opentelemetry/sdk/trace/samplers/trace_id_ratio_based'
12
12
 
13
13
  module OpenTelemetry
14
14
  module SDK
15
15
  module Trace
16
16
  # The Samplers module contains the sampling logic for OpenTelemetry. The
17
- # reference implementation provides a {ProbabilitySampler}, {ALWAYS_ON},
18
- # {ALWAYS_OFF}, and {ParentOrElse}.
17
+ # reference implementation provides a {TraceIdRatioBased}, {ALWAYS_ON},
18
+ # {ALWAYS_OFF}, and {ParentBased}.
19
19
  #
20
20
  # Custom samplers can be provided by SDK users. The required interface is:
21
21
  #
@@ -41,9 +41,8 @@ module OpenTelemetry
41
41
  NOT_RECORD = Result.new(decision: Decision::NOT_RECORD)
42
42
  RECORD = Result.new(decision: Decision::RECORD)
43
43
  SAMPLING_HINTS = [Decision::NOT_RECORD, Decision::RECORD, Decision::RECORD_AND_SAMPLED].freeze
44
- APPLY_PROBABILITY_TO_SYMBOLS = %i[root_spans root_spans_and_remote_parent all_spans].freeze
45
44
 
46
- private_constant(:RECORD_AND_SAMPLED, :NOT_RECORD, :RECORD, :SAMPLING_HINTS, :APPLY_PROBABILITY_TO_SYMBOLS)
45
+ private_constant(:RECORD_AND_SAMPLED, :NOT_RECORD, :RECORD, :SAMPLING_HINTS)
47
46
 
48
47
  # Returns a {Result} with {Decision::RECORD_AND_SAMPLED}.
49
48
  ALWAYS_ON = ConstantSampler.new(result: RECORD_AND_SAMPLED, description: 'AlwaysOnSampler')
@@ -51,38 +50,46 @@ module OpenTelemetry
51
50
  # Returns a {Result} with {Decision::NOT_RECORD}.
52
51
  ALWAYS_OFF = ConstantSampler.new(result: NOT_RECORD, description: 'AlwaysOffSampler')
53
52
 
54
- # Returns a new sampler. It either respects the parent span's sampling
55
- # decision or delegates to delegate_sampler for root spans.
53
+ # Returns a new sampler. It delegates to samplers according to the following rules:
56
54
  #
57
- # @param [Sampler] delegate_sampler The sampler to which the sampling
58
- # decision is delegated for root spans.
59
- def self.parent_or_else(delegate_sampler)
60
- ParentOrElse.new(delegate_sampler)
55
+ # | Parent | parent.remote? | parent.trace_flags.sampled? | Invoke sampler |
56
+ # |--|--|--|--|
57
+ # | absent | n/a | n/a | root |
58
+ # | present | true | true | remote_parent_sampled |
59
+ # | present | true | false | remote_parent_not_sampled |
60
+ # | present | false | true | local_parent_sampled |
61
+ # | present | false | false | local_parent_not_sampled |
62
+ #
63
+ # @param [Sampler] root The sampler to which the sampling
64
+ # decision is delegated for spans with no parent (root spans).
65
+ # @param [optional Sampler] remote_parent_sampled The sampler to which the sampling
66
+ # decision is delegated for remote parent sampled spans. Defaults to ALWAYS_ON.
67
+ # @param [optional Sampler] remote_parent_not_sampled The sampler to which the sampling
68
+ # decision is delegated for remote parent not sampled spans. Defaults to ALWAYS_OFF.
69
+ # @param [optional Sampler] local_parent_sampled The sampler to which the sampling
70
+ # decision is delegated for local parent sampled spans. Defaults to ALWAYS_ON.
71
+ # @param [optional Sampler] local_parent_not_sampled The sampler to which the sampling
72
+ # decision is delegated for local parent not sampld spans. Defaults to ALWAYS_OFF.
73
+ def self.parent_based(
74
+ root:,
75
+ remote_parent_sampled: ALWAYS_ON,
76
+ remote_parent_not_sampled: ALWAYS_OFF,
77
+ local_parent_sampled: ALWAYS_ON,
78
+ local_parent_not_sampled: ALWAYS_OFF
79
+ )
80
+ ParentBased.new(root, remote_parent_sampled, remote_parent_not_sampled, local_parent_sampled, local_parent_not_sampled)
61
81
  end
62
82
 
63
- # Returns a new sampler. The probability of sampling a trace is equal
64
- # to that of the specified probability.
83
+ # Returns a new sampler. The ratio describes the proportion of the trace ID
84
+ # space that is sampled.
65
85
  #
66
- # @param [Numeric] probability The desired probability of sampling.
86
+ # @param [Numeric] ratio The desired sampling ratio.
67
87
  # Must be within [0.0, 1.0].
68
- # @param [optional Boolean] ignore_parent Whether to ignore parent
69
- # sampling. Defaults to not ignore parent sampling.
70
- # @param [optional Symbol] apply_probability_to Whether to apply
71
- # probability sampling to root spans, root spans and remote parents,
72
- # or all spans. Allowed values include :root_spans, :root_spans_and_remote_parent,
73
- # and :all_spans. Defaults to :root_spans_and_remote_parent.
74
- # @raise [ArgumentError] if probability is out of range
75
- # @raise [ArgumentError] if apply_probability_to is not one of the allowed symbols
76
- def self.probability(probability,
77
- ignore_parent: false,
78
- apply_probability_to: :root_spans_and_remote_parent)
79
- raise ArgumentError, 'probability must be in range [0.0, 1.0]' unless (0.0..1.0).include?(probability)
80
- raise ArgumentError, 'apply_probability_to' unless APPLY_PROBABILITY_TO_SYMBOLS.include?(apply_probability_to)
88
+ # @raise [ArgumentError] if ratio is out of range
89
+ def self.trace_id_ratio_based(ratio)
90
+ raise ArgumentError, 'ratio must be in range [0.0, 1.0]' unless (0.0..1.0).include?(ratio)
81
91
 
82
- ProbabilitySampler.new(probability,
83
- ignore_parent: ignore_parent,
84
- apply_to_remote_parent: apply_probability_to != :root_spans,
85
- apply_to_all_spans: apply_probability_to == :all_spans)
92
+ TraceIdRatioBased.new(ratio)
86
93
  end
87
94
  end
88
95
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright The OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Trace
10
+ module Samplers
11
+ # @api private
12
+ #
13
+ # This is a composite sampler. ParentBased helps distinguished between the
14
+ # following cases:
15
+ # * No parent (root span).
16
+ # * Remote parent (SpanContext.remote? with trace_flags.sampled?)
17
+ # * Remote parent (SpanContext.remote? with !trace_flags.sampled?)
18
+ # * Local parent (!SpanContext.remote? with trace_flags.sampled?)
19
+ # * Local parent (!SpanContext.remote? with !trace_flags.sampled?)
20
+ class ParentBased
21
+ def initialize(root, remote_parent_sampled, remote_parent_not_sampled, local_parent_sampled, local_parent_not_sampled)
22
+ @root = root
23
+ @remote_parent_sampled = remote_parent_sampled
24
+ @remote_parent_not_sampled = remote_parent_not_sampled
25
+ @local_parent_sampled = local_parent_sampled
26
+ @local_parent_not_sampled = local_parent_not_sampled
27
+ end
28
+
29
+ # @api private
30
+ #
31
+ # See {Samplers}.
32
+ def description
33
+ "ParentBased{root=#{@root.description}, remote_parent_sampled=#{@remote_parent_sampled.description}, remote_parent_not_sampled=#{@remote_parent_not_sampled.description}, local_parent_sampled=#{@local_parent_sampled.description}, local_parent_not_sampled=#{@local_parent_not_sampled.description}}"
34
+ end
35
+
36
+ # @api private
37
+ #
38
+ # See {Samplers}.
39
+ def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:)
40
+ delegate = if parent_context.nil?
41
+ @root
42
+ elsif parent_context.remote?
43
+ parent_context.trace_flags.sampled? ? @remote_parent_sampled : @remote_parent_not_sampled
44
+ else
45
+ parent_context.trace_flags.sampled? ? @local_parent_sampled : @local_parent_not_sampled
46
+ end
47
+ delegate.should_sample?(trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,45 @@
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 SDK
9
+ module Trace
10
+ module Samplers
11
+ # @api private
12
+ #
13
+ # Implements sampling based on a probability.
14
+ class TraceIdRatioBased
15
+ attr_reader :description
16
+
17
+ def initialize(probability)
18
+ @probability = probability
19
+ @id_upper_bound = (probability * (2**64 - 1)).ceil
20
+ @description = format('TraceIdRatioBased{%.6f}', probability)
21
+ end
22
+
23
+ # @api private
24
+ #
25
+ # See {Samplers}.
26
+ def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:)
27
+ # Ignored for sampling decision: parent_context:, links, name, kind, attributes.
28
+
29
+ if sample?(trace_id)
30
+ RECORD_AND_SAMPLED
31
+ else
32
+ NOT_RECORD
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def sample?(trace_id)
39
+ @probability == 1.0 || trace_id[8, 8].unpack1('Q>') < @id_upper_bound
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -18,10 +18,10 @@ module OpenTelemetry
18
18
  class Span < OpenTelemetry::Trace::Span
19
19
  # The following readers are intended for the use of SpanProcessors and
20
20
  # should not be considered part of the public interface for instrumentation.
21
- attr_reader :name, :status, :kind, :parent_span_id, :start_timestamp, :end_timestamp, :links, :library_resource, :instrumentation_library
21
+ attr_reader :name, :status, :kind, :parent_span_id, :start_timestamp, :end_timestamp, :links, :resource, :instrumentation_library
22
22
 
23
23
  # Return a frozen copy of the current attributes. This is intended for
24
- # use of SpanProcesses and should not be considered part of the public
24
+ # use of SpanProcessors and should not be considered part of the public
25
25
  # interface for instrumentation.
26
26
  #
27
27
  # @return [Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}] may be nil.
@@ -59,7 +59,7 @@ module OpenTelemetry
59
59
  # meanings.
60
60
  #
61
61
  # @param [String] key
62
- # @param [String, Boolean, Numeric] value
62
+ # @param [String, Boolean, Numeric, Array<String, Numeric, Boolean>] value
63
63
  #
64
64
  # @return [self] returns itself
65
65
  def set_attribute(key, value)
@@ -76,37 +76,29 @@ module OpenTelemetry
76
76
  end
77
77
  self
78
78
  end
79
+ alias []= set_attribute
79
80
 
80
- # Add an Event to a {Span}. This can be accomplished eagerly or lazily.
81
- # Lazy evaluation is useful when the event attributes are expensive to
82
- # build and where the cost can be avoided for an unsampled {Span}.
81
+ # Add an Event to a {Span}.
83
82
  #
84
- # Eager example:
83
+ # Example:
85
84
  #
86
- # span.add_event(name: 'event', attributes: {'eager' => true})
87
- #
88
- # Lazy example:
89
- #
90
- # span.add_event { OpenTelemetry::Trace::Event.new(name: 'event', attributes: {'eager' => false}) }
85
+ # span.add_event('event', attributes: {'eager' => true})
91
86
  #
92
87
  # Note that the OpenTelemetry project
93
88
  # {https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md
94
89
  # documents} certain "standard event names and keys" which have
95
90
  # prescribed semantic meanings.
96
91
  #
97
- # @param [optional String] name Optional name of the event. This is
98
- # required if a block is not given.
92
+ # @param [String] name Name of the event.
99
93
  # @param [optional Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}] attributes
100
94
  # One or more key:value pairs, where the keys must be strings and the
101
- # values may be string, boolean or numeric type. This argument should
102
- # only be used when passing in a name.
95
+ # values may be string, boolean or numeric type.
103
96
  # @param [optional Time] timestamp Optional timestamp for the event.
104
- # This argument should only be used when passing in a name.
105
97
  #
106
98
  # @return [self] returns itself
107
- def add_event(name: nil, attributes: nil, timestamp: nil)
99
+ def add_event(name, attributes: nil, timestamp: nil)
108
100
  super
109
- event = block_given? ? yield : OpenTelemetry::Trace::Event.new(name: name, attributes: attributes, timestamp: timestamp || Time.now)
101
+ event = Event.new(name: name, attributes: attributes, timestamp: timestamp || Time.now)
110
102
 
111
103
  @mutex.synchronize do
112
104
  if @ended
@@ -120,18 +112,18 @@ module OpenTelemetry
120
112
  self
121
113
  end
122
114
 
123
- # Record an error during the execution of this span. Multiple errors
115
+ # Record an exception during the execution of this span. Multiple exceptions
124
116
  # can be recorded on a span.
125
117
  #
126
- # @param [Exception] error The error to be recorded
118
+ # @param [Exception] exception The exception to be recorded
127
119
  #
128
120
  # @return [void]
129
- def record_error(error)
130
- add_event(name: 'error',
121
+ def record_exception(exception)
122
+ add_event('exception',
131
123
  attributes: {
132
- 'error.type' => error.class.to_s,
133
- 'error.message' => error.message,
134
- 'error.stack' => error.backtrace.join("\n")
124
+ 'exception.type' => exception.class.to_s,
125
+ 'exception.message' => exception.message,
126
+ 'exception.stacktrace' => exception.full_message(highlight: false, order: :top)
135
127
  })
136
128
  end
137
129
 
@@ -236,7 +228,7 @@ module OpenTelemetry
236
228
  @attributes,
237
229
  @links,
238
230
  @events,
239
- @library_resource,
231
+ @resource,
240
232
  @instrumentation_library,
241
233
  context.span_id,
242
234
  context.trace_id,
@@ -246,7 +238,7 @@ module OpenTelemetry
246
238
  end
247
239
 
248
240
  # @api private
249
- def initialize(context, name, kind, parent_span_id, trace_config, span_processor, attributes, links, start_timestamp, library_resource, instrumentation_library) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
241
+ def initialize(context, name, kind, parent_span_id, trace_config, span_processor, attributes, links, start_timestamp, resource, instrumentation_library) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
250
242
  super(span_context: context)
251
243
  @mutex = Mutex.new
252
244
  @name = name
@@ -254,7 +246,7 @@ module OpenTelemetry
254
246
  @parent_span_id = parent_span_id.freeze || OpenTelemetry::Trace::INVALID_SPAN_ID
255
247
  @trace_config = trace_config
256
248
  @span_processor = span_processor
257
- @library_resource = library_resource
249
+ @resource = resource
258
250
  @instrumentation_library = instrumentation_library
259
251
  @ended = false
260
252
  @status = nil
@@ -325,7 +317,7 @@ module OpenTelemetry
325
317
  attrs.keep_if { |key, value| Internal.valid_key?(key) && Internal.valid_value?(value) }
326
318
  excess = attrs.size - max_attributes_per_event
327
319
  excess.times { attrs.shift } if excess.positive?
328
- event = OpenTelemetry::Trace::Event.new(name: event.name, attributes: attrs, timestamp: event.timestamp)
320
+ event = Event.new(name: event.name, attributes: attrs, timestamp: event.timestamp)
329
321
  end
330
322
  events << event
331
323
  end
@@ -23,12 +23,26 @@ module OpenTelemetry
23
23
  :attributes,
24
24
  :links,
25
25
  :events,
26
- :library_resource,
26
+ :resource,
27
27
  :instrumentation_library,
28
28
  :span_id,
29
29
  :trace_id,
30
30
  :trace_flags,
31
- :tracestate)
31
+ :tracestate) do
32
+ # Returns the lowercase [hex encoded](https://tools.ietf.org/html/rfc4648#section-8) span ID.
33
+ #
34
+ # @return [String] A 16-hex-character lowercase string.
35
+ def hex_span_id
36
+ span_id.unpack1('H*')
37
+ end
38
+
39
+ # Returns the lowercase [hex encoded](https://tools.ietf.org/html/rfc4648#section-8) trace ID.
40
+ #
41
+ # @return [String] A 32-hex-character lowercase string.
42
+ def hex_trace_id
43
+ trace_id.unpack1('H*')
44
+ end
45
+ end
32
46
  end
33
47
  end
34
48
  end
@@ -7,6 +7,6 @@
7
7
  module OpenTelemetry
8
8
  module SDK
9
9
  ## Current OpenTelemetry version
10
- VERSION = '0.5.1'
10
+ VERSION = '0.6.0'
11
11
  end
12
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opentelemetry-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OpenTelemetry Authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-21 00:00:00.000000000 Z
11
+ date: 2020-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opentelemetry-api
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.5.0
19
+ version: 0.6.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.5.0
26
+ version: 0.6.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -149,10 +149,10 @@ files:
149
149
  - README.md
150
150
  - lib/opentelemetry-sdk.rb
151
151
  - lib/opentelemetry/sdk.rb
152
+ - lib/opentelemetry/sdk/baggage.rb
153
+ - lib/opentelemetry/sdk/baggage/builder.rb
154
+ - lib/opentelemetry/sdk/baggage/manager.rb
152
155
  - lib/opentelemetry/sdk/configurator.rb
153
- - lib/opentelemetry/sdk/correlation_context.rb
154
- - lib/opentelemetry/sdk/correlation_context/builder.rb
155
- - lib/opentelemetry/sdk/correlation_context/manager.rb
156
156
  - lib/opentelemetry/sdk/instrumentation_library.rb
157
157
  - lib/opentelemetry/sdk/internal.rb
158
158
  - lib/opentelemetry/sdk/resources.rb
@@ -161,6 +161,7 @@ files:
161
161
  - lib/opentelemetry/sdk/trace.rb
162
162
  - lib/opentelemetry/sdk/trace/config.rb
163
163
  - lib/opentelemetry/sdk/trace/config/trace_config.rb
164
+ - lib/opentelemetry/sdk/trace/event.rb
164
165
  - lib/opentelemetry/sdk/trace/export.rb
165
166
  - lib/opentelemetry/sdk/trace/export/batch_span_processor.rb
166
167
  - lib/opentelemetry/sdk/trace/export/console_span_exporter.rb
@@ -173,9 +174,9 @@ files:
173
174
  - lib/opentelemetry/sdk/trace/samplers.rb
174
175
  - lib/opentelemetry/sdk/trace/samplers/constant_sampler.rb
175
176
  - lib/opentelemetry/sdk/trace/samplers/decision.rb
176
- - lib/opentelemetry/sdk/trace/samplers/parent_or_else.rb
177
- - lib/opentelemetry/sdk/trace/samplers/probability_sampler.rb
177
+ - lib/opentelemetry/sdk/trace/samplers/parent_based.rb
178
178
  - lib/opentelemetry/sdk/trace/samplers/result.rb
179
+ - lib/opentelemetry/sdk/trace/samplers/trace_id_ratio_based.rb
179
180
  - lib/opentelemetry/sdk/trace/span.rb
180
181
  - lib/opentelemetry/sdk/trace/span_data.rb
181
182
  - lib/opentelemetry/sdk/trace/tracer.rb
@@ -200,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
200
201
  - !ruby/object:Gem::Version
201
202
  version: '0'
202
203
  requirements: []
203
- rubygems_version: 3.0.3
204
+ rubygems_version: 3.1.2
204
205
  signing_key:
205
206
  specification_version: 4
206
207
  summary: A stats collection and distributed tracing framework
@@ -1,16 +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/sdk/correlation_context/builder'
8
- require 'opentelemetry/sdk/correlation_context/manager'
9
-
10
- module OpenTelemetry
11
- module SDK
12
- # Contains operational implementations of the CorrelationContext::Manager
13
- module CorrelationContext
14
- end
15
- end
16
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Copyright The OpenTelemetry Authors
4
- #
5
- # SPDX-License-Identifier: Apache-2.0
6
-
7
- module OpenTelemetry
8
- module SDK
9
- module Trace
10
- module Samplers
11
- # @api private
12
- #
13
- # This is a composite sampler. It either respects the parent span's sampling
14
- # decision or delegates to delegate_sampler for root spans.
15
- class ParentOrElse
16
- def initialize(delegate_sampler)
17
- @delegate_sampler = delegate_sampler
18
- end
19
-
20
- # @api private
21
- #
22
- # See {Samplers}.
23
- def description
24
- "ParentOrElse{#{@delegate_sampler.description}}"
25
- end
26
-
27
- # @api private
28
- #
29
- # See {Samplers}.
30
- def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:)
31
- if parent_context.nil?
32
- @delegate_sampler.should_sample?(trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes)
33
- elsif parent_context.trace_flags.sampled?
34
- RECORD_AND_SAMPLED
35
- else
36
- NOT_RECORD
37
- end
38
- end
39
- end
40
- end
41
- end
42
- end
43
- end
@@ -1,64 +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 SDK
9
- module Trace
10
- module Samplers
11
- # @api private
12
- #
13
- # Implements sampling based on a probability.
14
- class ProbabilitySampler
15
- attr_reader :description
16
-
17
- def initialize(probability, ignore_parent:, apply_to_remote_parent:, apply_to_all_spans:)
18
- @probability = probability
19
- @id_upper_bound = (probability * (2**64 - 1)).ceil
20
- @use_parent_sampled_flag = !ignore_parent
21
- @apply_to_remote_parent = apply_to_remote_parent
22
- @apply_to_all_spans = apply_to_all_spans
23
- @description = format('ProbabilitySampler{%.6f}', probability)
24
- end
25
-
26
- # @api private
27
- #
28
- # See {Samplers}.
29
- def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:)
30
- # Ignored for sampling decision: links, name, kind, attributes.
31
-
32
- if sample?(trace_id, parent_context)
33
- RECORD_AND_SAMPLED
34
- else
35
- NOT_RECORD
36
- end
37
- end
38
-
39
- private
40
-
41
- def sample?(trace_id, parent_context)
42
- if parent_context.nil?
43
- sample_trace_id?(trace_id)
44
- else
45
- parent_sampled?(parent_context) || sample_trace_id_for_child?(parent_context, trace_id)
46
- end
47
- end
48
-
49
- def parent_sampled?(parent_context)
50
- @use_parent_sampled_flag && parent_context.trace_flags.sampled?
51
- end
52
-
53
- def sample_trace_id_for_child?(parent_context, trace_id)
54
- (@apply_to_all_spans || (@apply_to_remote_parent && parent_context.remote?)) && sample_trace_id?(trace_id)
55
- end
56
-
57
- def sample_trace_id?(trace_id)
58
- @probability == 1.0 || trace_id[8, 8].unpack1('Q>') < @id_upper_bound
59
- end
60
- end
61
- end
62
- end
63
- end
64
- end