opentelemetry-sdk 0.4.0 → 0.5.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: bb002eea7584e4788afaee003a24012aa93582788ecd8745d6e0979d6a80970c
4
- data.tar.gz: f8b5037249031a315436a24eb28235e1cab63915a57ebaab5493784cc6116cfc
3
+ metadata.gz: 1929c4e29a936b51b4f2eca8ec7b4e2f7a597e43e72039e6e963aa72cd00f65f
4
+ data.tar.gz: cbf9a0ade97c3dab0a8005cf84729ab8d549c6f0768ea9446d8a71585d676d4f
5
5
  SHA512:
6
- metadata.gz: 03036bff46744fd6670649fea70300cff02e5364a19449bf38fe8ebfde7c5f5b831bfda12fcae685028469ecc6d7367e9b46125e247e7f5c2b0f8688ddb66b81
7
- data.tar.gz: 2f4e11052927da389fd45d7dd0d173e51eec3e8818ad4dba962f6ef42e3b9bdeda414e0f2517c6c429069607d3e4114dc4f7a6b9126c0190fd26d0a40ac4c030
6
+ metadata.gz: de2f88ace625de9e494a6800291462330c58d12e0ecac2e8a7a184a8c34382557639aea59eea3dbf16a603355c604446e00cd2b9fe8f2ad3cb009f21c68186cb
7
+ data.tar.gz: 42342b8cee795fd241a650b2abae0b8f3f4f73a202605bdcce6ec861de5fe53325fc3e273fe8c06e386df4b42a76ab3d0667990a502f31a0636de733f62b2aef
@@ -30,13 +30,13 @@ module OpenTelemetry
30
30
  # Install instrumentation individually with optional config
31
31
  #
32
32
  # OpenTelemetry::SDK.configure do |c|
33
- # c.use 'OpenTelemetry::Adapters::Faraday', tracer_middleware: SomeMiddleware
33
+ # c.use 'OpenTelemetry::Instrumentation::Faraday', tracer_middleware: SomeMiddleware
34
34
  # end
35
35
  #
36
36
  # Install all instrumentation with optional config
37
37
  #
38
38
  # OpenTelemetry::SDK.configure do |c|
39
- # c.use_all 'OpenTelemetry::Adapters::Faraday' => { tracer_middleware: SomeMiddleware }
39
+ # c.use_all 'OpenTelemetry::Instrumentation::Faraday' => { tracer_middleware: SomeMiddleware }
40
40
  # end
41
41
  #
42
42
  # Add a span processor
@@ -63,6 +63,7 @@ end
63
63
  require 'opentelemetry/sdk/configurator'
64
64
  require 'opentelemetry/sdk/correlation_context'
65
65
  require 'opentelemetry/sdk/internal'
66
+ require 'opentelemetry/sdk/instrumentation_library'
66
67
  require 'opentelemetry/sdk/resources'
67
68
  require 'opentelemetry/sdk/trace'
68
69
  require 'opentelemetry/sdk/version'
@@ -8,7 +8,7 @@ module OpenTelemetry
8
8
  module SDK
9
9
  # The configurator provides defaults and facilitates configuring the
10
10
  # SDK for use.
11
- class Configurator
11
+ class Configurator # rubocop:disable Metrics/ClassLength
12
12
  USE_MODE_UNSPECIFIED = 0
13
13
  USE_MODE_ONE = 1
14
14
  USE_MODE_ALL = 2
@@ -19,46 +19,55 @@ module OpenTelemetry
19
19
  :text_injectors
20
20
 
21
21
  def initialize
22
- @adapter_names = []
23
- @adapter_config_map = {}
22
+ @instrumentation_names = []
23
+ @instrumentation_config_map = {}
24
24
  @http_extractors = nil
25
25
  @http_injectors = nil
26
26
  @text_extractors = nil
27
27
  @text_injectors = nil
28
28
  @span_processors = []
29
29
  @use_mode = USE_MODE_UNSPECIFIED
30
- @tracer_provider = Trace::TracerProvider.new
30
+ @resource = Resources::Resource.telemetry_sdk
31
31
  end
32
32
 
33
33
  def logger
34
34
  @logger ||= Logger.new(STDOUT)
35
35
  end
36
36
 
37
- # Install an instrumentation adapter with specificied optional +config+.
38
- # Use can be called multiple times to install multiple instrumentation
39
- # adapters. Only +use+ or +use_all+, but not both when installing
37
+ # Accepts a resource object that is merged with the default telemetry sdk
38
+ # resource. The use of this method is optional, and is provided as means
39
+ # to add additional resource information.
40
+ #
41
+ # @param [Resource] new_resource The resource to be merged
42
+ def resource=(new_resource)
43
+ @resource = @resource.merge(new_resource)
44
+ end
45
+
46
+ # Install an instrumentation with specificied optional +config+.
47
+ # Use can be called multiple times to install multiple instrumentation.
48
+ # Only +use+ or +use_all+, but not both when installing
40
49
  # instrumentation. A call to +use_all+ after +use+ will result in an
41
50
  # exception.
42
51
  #
43
- # @param [String] adapter_name The name of the instrumentation adapter
44
- # @param [optional Hash] config The config for this adapter
45
- def use(adapter_name, config = nil)
52
+ # @param [String] instrumentation_name The name of the instrumentation
53
+ # @param [optional Hash] config The config for this instrumentation
54
+ def use(instrumentation_name, config = nil)
46
55
  check_use_mode!(USE_MODE_ONE)
47
- @adapter_names << adapter_name
48
- @adapter_config_map[adapter_name] = config if config
56
+ @instrumentation_names << instrumentation_name
57
+ @instrumentation_config_map[instrumentation_name] = config if config
49
58
  end
50
59
 
51
60
  # Install all registered instrumentation. Configuration for specific
52
- # adapters can be provided with the optional +adapter_config_map+
61
+ # instrumentation can be provided with the optional +instrumentation_config_map+
53
62
  # parameter. Only +use+ or +use_all+, but not both when installing
54
63
  # instrumentation. A call to +use+ after +use_all+ will result in an
55
64
  # exception.
56
65
  #
57
- # @param [optional Hash<String,Hash>] adapter_config_map A map with string keys
58
- # representing the adapter name and values specifying the adapter config
59
- def use_all(adapter_config_map = {})
66
+ # @param [optional Hash<String,Hash>] instrumentation_config_map A map with string keys
67
+ # representing the instrumentation name and values specifying the instrumentation config
68
+ def use_all(instrumentation_config_map = {})
60
69
  check_use_mode!(USE_MODE_ALL)
61
- @adapter_config_map = adapter_config_map
70
+ @instrumentation_config_map = instrumentation_config_map
62
71
  end
63
72
 
64
73
  # Add a span processor to the export pipeline
@@ -83,12 +92,16 @@ module OpenTelemetry
83
92
  OpenTelemetry.correlations = CorrelationContext::Manager.new
84
93
  configure_propagation
85
94
  configure_span_processors
86
- OpenTelemetry.tracer_provider = @tracer_provider
95
+ OpenTelemetry.tracer_provider = tracer_provider
87
96
  install_instrumentation
88
97
  end
89
98
 
90
99
  private
91
100
 
101
+ def tracer_provider
102
+ @tracer_provider ||= Trace::TracerProvider.new(@resource)
103
+ end
104
+
92
105
  def check_use_mode!(mode)
93
106
  @use_mode = mode if @use_mode == USE_MODE_UNSPECIFIED
94
107
  raise 'Use either `use_all` or `use`, but not both' unless @use_mode == mode
@@ -97,15 +110,15 @@ module OpenTelemetry
97
110
  def install_instrumentation
98
111
  case @use_mode
99
112
  when USE_MODE_ONE
100
- OpenTelemetry.instrumentation_registry.install(@adapter_names, @adapter_config_map)
113
+ OpenTelemetry.instrumentation_registry.install(@instrumentation_names, @instrumentation_config_map)
101
114
  when USE_MODE_ALL
102
- OpenTelemetry.instrumentation_registry.install_all(@adapter_config_map)
115
+ OpenTelemetry.instrumentation_registry.install_all(@instrumentation_config_map)
103
116
  end
104
117
  end
105
118
 
106
119
  def configure_span_processors
107
120
  processors = @span_processors.empty? ? [default_span_processor] : @span_processors
108
- processors.each { |p| @tracer_provider.add_span_processor(p) }
121
+ processors.each { |p| tracer_provider.add_span_processor(p) }
109
122
  end
110
123
 
111
124
  def default_span_processor
@@ -9,7 +9,7 @@ require 'opentelemetry/sdk/correlation_context/manager'
9
9
 
10
10
  module OpenTelemetry
11
11
  module SDK
12
- # Contains operational implementataions of the CorrelationContext::Manager
12
+ # Contains operational implementations of the CorrelationContext::Manager
13
13
  module CorrelationContext
14
14
  end
15
15
  end
@@ -0,0 +1,13 @@
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
+ # InstrumentationLibrary is a struct containing library information for export.
10
+ InstrumentationLibrary = Struct.new(:name,
11
+ :version)
12
+ end
13
+ end
@@ -13,3 +13,4 @@ module OpenTelemetry
13
13
  end
14
14
 
15
15
  require 'opentelemetry/sdk/resources/resource'
16
+ require 'opentelemetry/sdk/resources/constants'
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 OpenTelemetry Authors
4
+ #
5
+ # SPDX-License-Identifier: Apache-2.0
6
+
7
+ module OpenTelemetry
8
+ module SDK
9
+ module Resources
10
+ module Constants
11
+ # Attributes describing a service instance.
12
+ SERVICE_RESOURCE = {
13
+ # Logical name of the service.
14
+ name: 'service.name',
15
+
16
+ # A namespace for `service.name`.
17
+ namespace: 'service.namespace',
18
+
19
+ # The string ID of the service instance.
20
+ instance_id: 'service.instance.id',
21
+
22
+ # The version string of the service API or implementation.
23
+ version: 'service.version'
24
+ }.freeze
25
+
26
+ # Attributes describing the telemetry library.
27
+ TELEMETRY_SDK_RESOURCE = {
28
+ # The name of the telemetry library.
29
+ name: 'telemetry.sdk.name',
30
+
31
+ # The language of the telemetry library and of the code instrumented with it.
32
+ language: 'telemetry.sdk.language',
33
+
34
+ # The version string of the telemetry library
35
+ version: 'telemetry.sdk.version'
36
+ }.freeze
37
+
38
+ # Attributes defining a compute unit (e.g. Container, Process, Lambda
39
+ # Function).
40
+ CONTAINER_RESOURCE = {
41
+ # The container name.
42
+ name: 'container.name',
43
+
44
+ # The name of the image the container was built on.
45
+ image_name: 'container.image.name',
46
+
47
+ # The container image tag.
48
+ image_tag: 'container.image.tag'
49
+ }.freeze
50
+
51
+ FAAS_RESOURCE = {
52
+ # The name of the function being executed.
53
+ name: 'faas.name',
54
+
55
+ # The unique name of the function being executed.
56
+ id: 'faas.id',
57
+
58
+ # The version string of the function being executed.
59
+ version: 'faas.version',
60
+
61
+ # The execution environment ID as a string.
62
+ instance: 'faas.instance'
63
+ }.freeze
64
+
65
+ # Attributes defining a deployment service (e.g. Kubernetes).
66
+ K8S_RESOURCE = {
67
+ # The name of the cluster that the pod is running in.
68
+ cluster_name: 'k8s.cluster.name',
69
+
70
+ # The name of the namespace that the pod is running in.
71
+ namespace_name: 'k8s.namespace.name',
72
+
73
+ # The name of the pod.
74
+ pod_name: 'k8s.pod.name',
75
+
76
+ # The name of the deployment.
77
+ deployment_name: 'k8s.deployment.name'
78
+ }.freeze
79
+
80
+ # Attributes defining a computing instance (e.g. host).
81
+ HOST_RESOURCE = {
82
+ # Hostname of the host. It contains what the hostname command returns on the
83
+ # host machine.
84
+ hostname: 'host.hostname',
85
+
86
+ # Unique host id. For Cloud this must be the instance_id assigned by the
87
+ # cloud provider
88
+ id: 'host.id',
89
+
90
+ # Name of the host. It may contain what hostname returns on Unix systems,
91
+ # the fully qualified, or a name specified by the user.
92
+ name: 'host.name',
93
+
94
+ # Type of host. For Cloud this must be the machine type.
95
+ type: 'host.type',
96
+
97
+ # Name of the VM image or OS install the host was instantiated from.
98
+ image_name: 'host.image.name',
99
+
100
+ # VM image id. For Cloud, this value is from the provider.
101
+ image_id: 'host.image.id',
102
+
103
+ # The version string of the VM image.
104
+ image_version: 'host.image.version'
105
+ }.freeze
106
+
107
+ # Attributes defining a running environment (e.g. Cloud, Data Center).
108
+ CLOUD_RESOURCE = {
109
+ # Name of the cloud provider. Example values are aws, azure, gcp.
110
+ provider: 'cloud.provider',
111
+
112
+ # The cloud account id used to identify different entities.
113
+ account_id: 'cloud.account.id',
114
+
115
+ # A specific geographical location where different entities can run.
116
+ region: 'cloud.region',
117
+
118
+ # Zones are a sub set of the region connected through low-latency links.
119
+ zone: 'cloud.zone'
120
+ }.freeze
121
+ end
122
+ end
123
+ end
124
+ end
@@ -29,6 +29,14 @@ module OpenTelemetry
29
29
 
30
30
  new(frozen_labels)
31
31
  end
32
+
33
+ def telemetry_sdk
34
+ create(
35
+ Constants::TELEMETRY_SDK_RESOURCE[:name] => 'opentelemetry',
36
+ Constants::TELEMETRY_SDK_RESOURCE[:language] => 'ruby',
37
+ Constants::TELEMETRY_SDK_RESOURCE[:version] => "semver:#{OpenTelemetry::SDK::VERSION}"
38
+ )
39
+ end
32
40
  end
33
41
 
34
42
  # @api private
@@ -15,13 +15,8 @@ module OpenTelemetry
15
15
  # The export operation finished successfully.
16
16
  SUCCESS = 0
17
17
 
18
- # The export operation finished with an error, but retrying may
19
- # succeed.
20
- FAILED_RETRYABLE = 1
21
-
22
- # The export operation finished with an error, the caller should not
23
- # try to export the same data again.
24
- FAILED_NOT_RETRYABLE = 2
18
+ # The export operation finished with an error.
19
+ FAILURE = 1
25
20
  end
26
21
  end
27
22
  end
@@ -22,24 +22,18 @@ module OpenTelemetry
22
22
  # If the queue gets half full a preemptive notification is sent to the
23
23
  # worker thread that exports the spans to wake up and start a new
24
24
  # export cycle.
25
- #
26
- # max_export_attempts attempts are made to export each batch, while
27
- # export fails with {FAILED_RETRYABLE}, backing off linearly in 100ms
28
- # increments.
29
25
  class BatchSpanProcessor
30
26
  EXPORTER_TIMEOUT_MILLIS = 30_000
31
27
  SCHEDULE_DELAY_MILLIS = 5_000
32
28
  MAX_QUEUE_SIZE = 2048
33
29
  MAX_EXPORT_BATCH_SIZE = 512
34
- MAX_EXPORT_ATTEMPTS = 5
35
- private_constant(:SCHEDULE_DELAY_MILLIS, :MAX_QUEUE_SIZE, :MAX_EXPORT_BATCH_SIZE, :MAX_EXPORT_ATTEMPTS)
30
+ private_constant(:SCHEDULE_DELAY_MILLIS, :MAX_QUEUE_SIZE, :MAX_EXPORT_BATCH_SIZE)
36
31
 
37
32
  def initialize(exporter:,
38
33
  exporter_timeout_millis: EXPORTER_TIMEOUT_MILLIS,
39
34
  schedule_delay_millis: SCHEDULE_DELAY_MILLIS,
40
35
  max_queue_size: MAX_QUEUE_SIZE,
41
- max_export_batch_size: MAX_EXPORT_BATCH_SIZE,
42
- max_export_attempts: MAX_EXPORT_ATTEMPTS)
36
+ max_export_batch_size: MAX_EXPORT_BATCH_SIZE)
43
37
  raise ArgumentError if max_export_batch_size > max_queue_size
44
38
 
45
39
  @exporter = exporter
@@ -50,7 +44,6 @@ module OpenTelemetry
50
44
  @delay_seconds = schedule_delay_millis / 1000.0
51
45
  @max_queue_size = max_queue_size
52
46
  @batch_size = max_export_batch_size
53
- @export_attempts = max_export_attempts
54
47
  @spans = []
55
48
  @thread = Thread.new { work }
56
49
  end
@@ -121,20 +114,14 @@ module OpenTelemetry
121
114
  end
122
115
 
123
116
  def export_batch(batch)
124
- result_code = nil
125
- @export_attempts.times do |attempts|
126
- result_code = export_with_timeout(batch)
127
- break unless result_code == FAILED_RETRYABLE
128
-
129
- sleep(0.1 * attempts)
130
- end
117
+ result_code = export_with_timeout(batch)
131
118
  report_result(result_code, batch)
132
119
  end
133
120
 
134
121
  def export_with_timeout(batch)
135
122
  Timeout.timeout(@exporter_timeout_seconds) { @exporter.export(batch) }
136
123
  rescue Timeout::Error
137
- FAILED_NOT_RETRYABLE
124
+ FAILURE
138
125
  end
139
126
 
140
127
  def report_result(result_code, batch)
@@ -23,7 +23,7 @@ module OpenTelemetry
23
23
  end
24
24
 
25
25
  def export(spans)
26
- return ResultCodes::FAILED_NOT_RETRYABLE if @stopped
26
+ return ResultCodes::FAILURE if @stopped
27
27
 
28
28
  Array(spans).each { |s| pp s }
29
29
 
@@ -61,10 +61,10 @@ module OpenTelemetry
61
61
  # @param [Enumerable<SpanData>] span_datas the list of sampled {SpanData}s to be
62
62
  # exported.
63
63
  # @return [Integer] the result of the export, SUCCESS or
64
- # FAILED_NOT_RETRYABLE
64
+ # FAILURE
65
65
  def export(span_datas)
66
66
  @mutex.synchronize do
67
- return FAILED_NOT_RETRYABLE if @stopped
67
+ return FAILURE if @stopped
68
68
 
69
69
  @finished_spans.concat(span_datas.to_a)
70
70
  end
@@ -29,7 +29,7 @@ module OpenTelemetry
29
29
  merge_result_code(result_code, span_exporter.export(spans))
30
30
  rescue => e # rubocop:disable Style/RescueStandardError
31
31
  OpenTelemetry.logger.warn("exception raised by export - #{e}")
32
- FAILED_NOT_RETRYABLE
32
+ FAILURE
33
33
  end
34
34
  end
35
35
 
@@ -46,13 +46,9 @@ module OpenTelemetry
46
46
  if result_code == SUCCESS && new_result_code == SUCCESS
47
47
  # If both errors are success then return success.
48
48
  SUCCESS
49
- elsif result_code == FAILED_NOT_RETRYABLE || new_result_code == FAILED_NOT_RETRYABLE
50
- # If any of the codes is not retryable then return not_retryable.
51
- FAILED_NOT_RETRYABLE
52
49
  else
53
- # At this point at least one of the code is FAILED_RETRYABLE and
54
- # none are FAILED_NOT_RETRYABLE, so return FAILED_RETRYABLE.
55
- FAILED_RETRYABLE
50
+ # At this point at least one of the code is FAILURE, so return FAILURE.
51
+ FAILURE
56
52
  end
57
53
  end
58
54
  end
@@ -27,7 +27,7 @@ module OpenTelemetry
27
27
  def export(spans)
28
28
  return SUCCESS unless @stopped
29
29
 
30
- FAILED_NOT_RETRYABLE
30
+ FAILURE
31
31
  end
32
32
 
33
33
  # Called when {TracerProvider#shutdown} is called, if this exporter is
@@ -6,6 +6,8 @@
6
6
 
7
7
  require 'opentelemetry/sdk/trace/samplers/decision'
8
8
  require 'opentelemetry/sdk/trace/samplers/result'
9
+ require 'opentelemetry/sdk/trace/samplers/constant_sampler'
10
+ require 'opentelemetry/sdk/trace/samplers/parent_or_else'
9
11
  require 'opentelemetry/sdk/trace/samplers/probability_sampler'
10
12
 
11
13
  module OpenTelemetry
@@ -13,17 +15,16 @@ module OpenTelemetry
13
15
  module Trace
14
16
  # The Samplers module contains the sampling logic for OpenTelemetry. The
15
17
  # reference implementation provides a {ProbabilitySampler}, {ALWAYS_ON},
16
- # {ALWAYS_OFF}, and {ALWAYS_PARENT}.
18
+ # {ALWAYS_OFF}, and {ParentOrElse}.
17
19
  #
18
- # Custom samplers can be provided by SDK users. The required interface is
19
- # a callable with the signature:
20
+ # Custom samplers can be provided by SDK users. The required interface is:
20
21
  #
21
- # (trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:) -> Result
22
+ # should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:) -> Result
23
+ # description -> String
22
24
  #
23
25
  # Where:
24
26
  #
25
27
  # @param [String] trace_id The trace_id of the {Span} to be created.
26
- # @param [String] span_id The span_id of the {Span} to be created.
27
28
  # @param [OpenTelemetry::Trace::SpanContext] parent_context The
28
29
  # {OpenTelemetry::Trace::SpanContext} of a parent span, typically
29
30
  # extracted from the wire. Can be nil for a root span.
@@ -44,27 +45,20 @@ module OpenTelemetry
44
45
 
45
46
  private_constant(:RECORD_AND_SAMPLED, :NOT_RECORD, :RECORD, :SAMPLING_HINTS, :APPLY_PROBABILITY_TO_SYMBOLS)
46
47
 
47
- # rubocop:disable Lint/UnusedBlockArgument
48
-
49
48
  # Returns a {Result} with {Decision::RECORD_AND_SAMPLED}.
50
- ALWAYS_ON = ->(trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:) { RECORD_AND_SAMPLED }
49
+ ALWAYS_ON = ConstantSampler.new(result: RECORD_AND_SAMPLED, description: 'AlwaysOnSampler')
51
50
 
52
51
  # Returns a {Result} with {Decision::NOT_RECORD}.
53
- ALWAYS_OFF = ->(trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:) { NOT_RECORD }
52
+ ALWAYS_OFF = ConstantSampler.new(result: NOT_RECORD, description: 'AlwaysOffSampler')
54
53
 
55
- # Returns a {Result} with {Decision::RECORD_AND_SAMPLED} if the parent
56
- # context is sampled or {Decision::NOT_RECORD} otherwise, or if there
57
- # is no parent context.
58
- # rubocop:disable Style/Lambda
59
- ALWAYS_PARENT = ->(trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:) do
60
- if parent_context&.trace_flags&.sampled?
61
- RECORD_AND_SAMPLED
62
- else
63
- NOT_RECORD
64
- end
54
+ # Returns a new sampler. It either respects the parent span's sampling
55
+ # decision or delegates to delegate_sampler for root spans.
56
+ #
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)
65
61
  end
66
- # rubocop:enable Style/Lambda
67
- # rubocop:enable Lint/UnusedBlockArgument
68
62
 
69
63
  # Returns a new sampler. The probability of sampling a trace is equal
70
64
  # to that of the specified probability.
@@ -0,0 +1,33 @@
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
+ # Implements a sampler returning a constant result.
14
+ class ConstantSampler
15
+ attr_reader :description
16
+
17
+ def initialize(result:, description:)
18
+ @result = result
19
+ @description = description
20
+ end
21
+
22
+ # @api private
23
+ #
24
+ # See {Samplers}.
25
+ def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:)
26
+ # All arguments ignored for sampling decision.
27
+ @result
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,43 @@
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
@@ -12,18 +12,21 @@ module OpenTelemetry
12
12
  #
13
13
  # Implements sampling based on a probability.
14
14
  class ProbabilitySampler
15
+ attr_reader :description
16
+
15
17
  def initialize(probability, ignore_parent:, apply_to_remote_parent:, apply_to_all_spans:)
16
18
  @probability = probability
17
19
  @id_upper_bound = format('%016x', (probability * (2**64 - 1)).ceil)
18
20
  @use_parent_sampled_flag = !ignore_parent
19
21
  @apply_to_remote_parent = apply_to_remote_parent
20
22
  @apply_to_all_spans = apply_to_all_spans
23
+ @description = format('ProbabilitySampler{%.6f}', probability)
21
24
  end
22
25
 
23
26
  # @api private
24
27
  #
25
- # Callable interface for probability sampler. See {Samplers}.
26
- def call(trace_id:, span_id:, parent_context:, links:, name:, kind:, attributes:)
28
+ # See {Samplers}.
29
+ def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:)
27
30
  # Ignored for sampling decision: links, name, kind, attributes.
28
31
 
29
32
  if sample?(trace_id, parent_context)
@@ -18,7 +18,7 @@ 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
21
+ attr_reader :name, :status, :kind, :parent_span_id, :start_timestamp, :end_timestamp, :links, :library_resource, :instrumentation_library
22
22
 
23
23
  # Return a frozen copy of the current attributes. This is intended for
24
24
  # use of SpanProcesses and should not be considered part of the public
@@ -237,14 +237,16 @@ module OpenTelemetry
237
237
  @links,
238
238
  @events,
239
239
  @library_resource,
240
+ @instrumentation_library,
240
241
  context.span_id,
241
242
  context.trace_id,
242
- context.trace_flags
243
+ context.trace_flags,
244
+ context.tracestate
243
245
  )
244
246
  end
245
247
 
246
248
  # @api private
247
- def initialize(context, name, kind, parent_span_id, trace_config, span_processor, attributes, links, start_timestamp, library_resource) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
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
248
250
  super(span_context: context)
249
251
  @mutex = Mutex.new
250
252
  @name = name
@@ -253,6 +255,7 @@ module OpenTelemetry
253
255
  @trace_config = trace_config
254
256
  @span_processor = span_processor
255
257
  @library_resource = library_resource
258
+ @instrumentation_library = instrumentation_library
256
259
  @ended = false
257
260
  @status = nil
258
261
  @child_count = 0
@@ -24,9 +24,11 @@ module OpenTelemetry
24
24
  :links,
25
25
  :events,
26
26
  :library_resource,
27
+ :instrumentation_library,
27
28
  :span_id,
28
29
  :trace_id,
29
- :trace_flags)
30
+ :trace_flags,
31
+ :tracestate)
30
32
  end
31
33
  end
32
34
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright 2020 OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -11,6 +11,7 @@ module OpenTelemetry
11
11
  class Tracer < OpenTelemetry::Trace::Tracer
12
12
  attr_reader :name
13
13
  attr_reader :version
14
+ attr_reader :tracer_provider
14
15
 
15
16
  # @api private
16
17
  #
@@ -18,12 +19,14 @@ module OpenTelemetry
18
19
  #
19
20
  # @param [String] name Instrumentation package name
20
21
  # @param [String] version Instrumentation package version
22
+ # @param [TracerProvider] tracer_provider TracerProvider that initialized the tracer
21
23
  #
22
24
  # @return [Tracer]
23
- def initialize(name, version)
25
+ def initialize(name, version, tracer_provider)
24
26
  @name = name
25
27
  @version = version
26
- @resource = Resources::Resource.create('name' => name, 'version' => version)
28
+ @instrumentation_library = InstrumentationLibrary.new(name, version)
29
+ @tracer_provider = tracer_provider
27
30
  end
28
31
 
29
32
  def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
@@ -40,22 +43,31 @@ module OpenTelemetry
40
43
  trace_id = parent_span_context&.trace_id
41
44
  trace_id ||= OpenTelemetry::Trace.generate_trace_id
42
45
  span_id = OpenTelemetry::Trace.generate_span_id
43
- sampler = OpenTelemetry.tracer_provider.active_trace_config.sampler
44
- result = sampler.call(trace_id: trace_id, span_id: span_id, parent_context: parent_span_context, links: links, name: name, kind: kind, attributes: attributes)
45
-
46
+ sampler = tracer_provider.active_trace_config.sampler
47
+ result = sampler.should_sample?(trace_id: trace_id, parent_context: parent_span_context, links: links, name: name, kind: kind, attributes: attributes)
46
48
  internal_create_span(result, name, kind, trace_id, span_id, parent_span_id, attributes, links, start_timestamp, tracestate)
47
49
  end
48
50
 
49
51
  private
50
52
 
51
53
  def internal_create_span(result, name, kind, trace_id, span_id, parent_span_id, attributes, links, start_timestamp, tracestate) # rubocop:disable Metrics/AbcSize
52
- if result.recording? && !OpenTelemetry.tracer_provider.stopped?
54
+ if result.recording? && !tracer_provider.stopped?
53
55
  trace_flags = result.sampled? ? OpenTelemetry::Trace::TraceFlags::SAMPLED : OpenTelemetry::Trace::TraceFlags::DEFAULT
54
56
  context = OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, trace_flags: trace_flags, tracestate: tracestate)
55
57
  attributes = attributes&.merge(result.attributes) || result.attributes
56
- active_trace_config = OpenTelemetry.tracer_provider.active_trace_config
57
- active_span_processor = OpenTelemetry.tracer_provider.active_span_processor
58
- Span.new(context, name, kind, parent_span_id, active_trace_config, active_span_processor, attributes, links, start_timestamp || Time.now, @resource)
58
+ Span.new(
59
+ context,
60
+ name,
61
+ kind,
62
+ parent_span_id,
63
+ tracer_provider.active_trace_config,
64
+ tracer_provider.active_span_processor,
65
+ attributes,
66
+ links,
67
+ start_timestamp || Time.now,
68
+ tracer_provider.resource,
69
+ @instrumentation_library
70
+ )
59
71
  else
60
72
  OpenTelemetry::Trace::Span.new(span_context: OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id))
61
73
  end
@@ -13,20 +13,20 @@ module OpenTelemetry
13
13
  private_constant(:Key)
14
14
 
15
15
  attr_accessor :active_trace_config
16
- attr_reader :active_span_processor
17
- attr_reader :stopped
16
+ attr_reader :active_span_processor, :stopped, :resource
18
17
  alias stopped? stopped
19
18
 
20
19
  # Returns a new {TracerProvider} instance.
21
20
  #
22
21
  # @return [TracerProvider]
23
- def initialize
22
+ def initialize(resource = OpenTelemetry::SDK::Resources::Resource.create)
24
23
  @mutex = Mutex.new
25
24
  @registry = {}
26
25
  @active_span_processor = NoopSpanProcessor.instance
27
26
  @active_trace_config = Config::TraceConfig::DEFAULT
28
27
  @registered_span_processors = []
29
28
  @stopped = false
29
+ @resource = resource
30
30
  end
31
31
 
32
32
  # Returns a {Tracer} instance.
@@ -38,7 +38,7 @@ module OpenTelemetry
38
38
  def tracer(name = nil, version = nil)
39
39
  name ||= ''
40
40
  version ||= ''
41
- @mutex.synchronize { @registry[Key.new(name, version)] ||= Tracer.new(name, version) }
41
+ @mutex.synchronize { @registry[Key.new(name, version)] ||= Tracer.new(name, version, self) }
42
42
  end
43
43
 
44
44
  # Attempts to stop all the activity for this {Tracer}. Calls
@@ -7,6 +7,6 @@
7
7
  module OpenTelemetry
8
8
  module SDK
9
9
  ## Current OpenTelemetry version
10
- VERSION = '0.4.0'
10
+ VERSION = '0.5.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.4.0
4
+ version: 0.5.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-04-16 00:00:00.000000000 Z
11
+ date: 2020-07-17 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.4.0
19
+ version: 0.5.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.4.0
26
+ version: 0.5.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -153,8 +153,10 @@ files:
153
153
  - lib/opentelemetry/sdk/correlation_context.rb
154
154
  - lib/opentelemetry/sdk/correlation_context/builder.rb
155
155
  - lib/opentelemetry/sdk/correlation_context/manager.rb
156
+ - lib/opentelemetry/sdk/instrumentation_library.rb
156
157
  - lib/opentelemetry/sdk/internal.rb
157
158
  - lib/opentelemetry/sdk/resources.rb
159
+ - lib/opentelemetry/sdk/resources/constants.rb
158
160
  - lib/opentelemetry/sdk/resources/resource.rb
159
161
  - lib/opentelemetry/sdk/trace.rb
160
162
  - lib/opentelemetry/sdk/trace/config.rb
@@ -169,7 +171,9 @@ files:
169
171
  - lib/opentelemetry/sdk/trace/multi_span_processor.rb
170
172
  - lib/opentelemetry/sdk/trace/noop_span_processor.rb
171
173
  - lib/opentelemetry/sdk/trace/samplers.rb
174
+ - lib/opentelemetry/sdk/trace/samplers/constant_sampler.rb
172
175
  - lib/opentelemetry/sdk/trace/samplers/decision.rb
176
+ - lib/opentelemetry/sdk/trace/samplers/parent_or_else.rb
173
177
  - lib/opentelemetry/sdk/trace/samplers/probability_sampler.rb
174
178
  - lib/opentelemetry/sdk/trace/samplers/result.rb
175
179
  - lib/opentelemetry/sdk/trace/span.rb