opentelemetry-sdk 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) 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/README.md +73 -0
  6. data/lib/opentelemetry-sdk.rb +7 -0
  7. data/lib/opentelemetry/sdk.rb +69 -0
  8. data/lib/opentelemetry/sdk/configurator.rb +171 -0
  9. data/lib/opentelemetry/sdk/correlation_context.rb +16 -0
  10. data/lib/opentelemetry/sdk/correlation_context/builder.rb +40 -0
  11. data/lib/opentelemetry/sdk/correlation_context/manager.rb +87 -0
  12. data/lib/opentelemetry/sdk/instrumentation_library.rb +13 -0
  13. data/lib/opentelemetry/sdk/internal.rb +52 -0
  14. data/lib/opentelemetry/sdk/resources.rb +16 -0
  15. data/lib/opentelemetry/sdk/resources/constants.rb +124 -0
  16. data/lib/opentelemetry/sdk/resources/resource.rb +84 -0
  17. data/lib/opentelemetry/sdk/trace.rb +24 -0
  18. data/lib/opentelemetry/sdk/trace/config.rb +18 -0
  19. data/lib/opentelemetry/sdk/trace/config/trace_config.rb +77 -0
  20. data/lib/opentelemetry/sdk/trace/export.rb +30 -0
  21. data/lib/opentelemetry/sdk/trace/export/batch_span_processor.rb +144 -0
  22. data/lib/opentelemetry/sdk/trace/export/console_span_exporter.rb +40 -0
  23. data/lib/opentelemetry/sdk/trace/export/in_memory_span_exporter.rb +86 -0
  24. data/lib/opentelemetry/sdk/trace/export/multi_span_exporter.rb +58 -0
  25. data/lib/opentelemetry/sdk/trace/export/noop_span_exporter.rb +42 -0
  26. data/lib/opentelemetry/sdk/trace/export/simple_span_processor.rb +72 -0
  27. data/lib/opentelemetry/sdk/trace/multi_span_processor.rb +62 -0
  28. data/lib/opentelemetry/sdk/trace/noop_span_processor.rb +50 -0
  29. data/lib/opentelemetry/sdk/trace/samplers.rb +90 -0
  30. data/lib/opentelemetry/sdk/trace/samplers/constant_sampler.rb +33 -0
  31. data/lib/opentelemetry/sdk/trace/samplers/decision.rb +26 -0
  32. data/lib/opentelemetry/sdk/trace/samplers/parent_or_else.rb +43 -0
  33. data/lib/opentelemetry/sdk/trace/samplers/probability_sampler.rb +64 -0
  34. data/lib/opentelemetry/sdk/trace/samplers/result.rb +55 -0
  35. data/lib/opentelemetry/sdk/trace/span.rb +336 -0
  36. data/lib/opentelemetry/sdk/trace/span_data.rb +34 -0
  37. data/lib/opentelemetry/sdk/trace/tracer.rb +78 -0
  38. data/lib/opentelemetry/sdk/trace/tracer_provider.rb +84 -0
  39. data/lib/opentelemetry/sdk/version.rb +12 -0
  40. metadata +207 -0
@@ -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/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
@@ -0,0 +1,40 @@
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 CorrelationContext
10
+ # SDK implementation of CorrelationContext::Builder
11
+ class Builder
12
+ attr_reader :entries
13
+
14
+ def initialize(entries)
15
+ @entries = entries
16
+ end
17
+
18
+ # Set key-value in the to-be-created correlation context
19
+ #
20
+ # @param [String] key The key to store this value under
21
+ # @param [String] value String value to be stored under key
22
+ def set_value(key, value)
23
+ @entries[key] = value.to_s
24
+ end
25
+
26
+ # Removes key from the to-be-created correlation context
27
+ #
28
+ # @param [String] key The key to remove
29
+ def remove_value(key)
30
+ @entries.delete(key)
31
+ end
32
+
33
+ # Clears all correlations from the to-be-created correlation context
34
+ def clear
35
+ @entries.clear
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,87 @@
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 CorrelationContext
10
+ # Manages correlation context
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)
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
19
+ # methods on +Manager+, if multiple modifications are being made, use
20
+ # this one.
21
+ #
22
+ # @param [optional Context] context The context to update with with new
23
+ # modified correlation context. Defaults to +Context.current+
24
+ # @return [Context]
25
+ def build_context(context: Context.current)
26
+ builder = Builder.new(correlations_for(context).dup)
27
+ yield builder
28
+ context.set_value(CORRELATION_CONTEXT_KEY, builder.entries)
29
+ end
30
+
31
+ # Returns a new context with empty correlations
32
+ #
33
+ # @param [optional Context] context Context to clear correlations from. Defaults
34
+ # to +Context.current+
35
+ # @return [Context]
36
+ def clear(context: Context.current)
37
+ context.set_value(CORRELATION_CONTEXT_KEY, EMPTY_CORRELATION_CONTEXT)
38
+ end
39
+
40
+ # Returns the corresponding correlation value (or nil) for key
41
+ #
42
+ # @param [String] key The lookup key
43
+ # @param [optional Context] context The context from which to retrieve
44
+ # the key.
45
+ # Defaults to +Context.current+
46
+ # @return [String]
47
+ def value(key, context: Context.current)
48
+ correlations_for(context)[key]
49
+ end
50
+
51
+ # Returns a new context with new key-value pair
52
+ #
53
+ # @param [String] key The key to store this value under
54
+ # @param [String] value String value to be stored under key
55
+ # @param [optional Context] context The context to update with new
56
+ # value. Defaults to +Context.current+
57
+ # @return [Context]
58
+ 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)
62
+ end
63
+
64
+ # Returns a new context with value at key removed
65
+ #
66
+ # @param [String] key The key to remove
67
+ # @param [optional Context] context The context to remove correlation
68
+ # from. Defaults to +Context.current+
69
+ # @return [Context]
70
+ def remove_value(key, context: Context.current)
71
+ correlations = correlations_for(context)
72
+ return context unless correlations.key?(key)
73
+
74
+ new_correlations = correlations.dup
75
+ new_correlations.delete(key)
76
+ context.set_value(CORRELATION_CONTEXT_KEY, new_correlations)
77
+ end
78
+
79
+ private
80
+
81
+ def correlations_for(context)
82
+ context.value(CORRELATION_CONTEXT_KEY) || EMPTY_CORRELATION_CONTEXT
83
+ end
84
+ end
85
+ end
86
+ end
87
+ 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
@@ -0,0 +1,52 @@
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
+ # @api private
10
+ #
11
+ # Internal contains helpers used by the reference implementation.
12
+ module Internal
13
+ extend self
14
+
15
+ def boolean?(value)
16
+ value.is_a?(TrueClass) || value.is_a?(FalseClass)
17
+ end
18
+
19
+ def valid_key?(key)
20
+ key.instance_of?(String)
21
+ end
22
+
23
+ def valid_simple_value?(value)
24
+ value.instance_of?(String) || value == false || value == true || value.is_a?(Numeric)
25
+ end
26
+
27
+ def valid_array_value?(value)
28
+ return false unless value.is_a?(Array)
29
+ return true if value.empty?
30
+
31
+ case value.first
32
+ when String
33
+ value.all? { |v| v.instance_of?(String) }
34
+ when TrueClass, FalseClass
35
+ value.all? { |v| boolean?(v) }
36
+ when Numeric
37
+ value.all? { |v| v.is_a?(Numeric) }
38
+ else
39
+ false
40
+ end
41
+ end
42
+
43
+ def valid_value?(value)
44
+ valid_simple_value?(value) || valid_array_value?(value)
45
+ end
46
+
47
+ def valid_attributes?(attrs)
48
+ attrs.nil? || attrs.all? { |k, v| valid_key?(k) && valid_value?(v) }
49
+ end
50
+ end
51
+ end
52
+ 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
+ module OpenTelemetry
8
+ module SDK
9
+ # Resources contains the {Resource} class.
10
+ module Resources
11
+ end
12
+ end
13
+ end
14
+
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
@@ -0,0 +1,84 @@
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 Resources
10
+ # Resource represents a resource, which captures identifying information about the entities
11
+ # for which telemetry (metrics or traces) is reported.
12
+ class Resource
13
+ class << self
14
+ private :new # rubocop:disable Style/AccessModifierDeclarations
15
+
16
+ # Returns a newly created {Resource} with the specified labels
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
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)
26
+
27
+ memo[-k] = v.freeze
28
+ end.freeze
29
+
30
+ new(frozen_labels)
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
40
+ end
41
+
42
+ # @api private
43
+ # The constructor is private and only for use internally by the class.
44
+ # Users should use the {create} factory method to obtain a {Resource}
45
+ # instance.
46
+ #
47
+ # @param [Hash<String, String>] frozen_labels Frozen-hash of frozen-string
48
+ # key-value pairs to be used as labels for this resource
49
+ # @return [Resource]
50
+ def initialize(frozen_labels)
51
+ @labels = frozen_labels
52
+ end
53
+
54
+ # Returns an enumerator for labels of this {Resource}
55
+ #
56
+ # @return [Enumerator]
57
+ def label_enumerator
58
+ @label_enumerator ||= labels.to_enum
59
+ end
60
+
61
+ # Returns a new, merged {Resource} by merging the current {Resource} with
62
+ # the other {Resource}. In case of a collision, the current {Resource}
63
+ # takes precedence
64
+ #
65
+ # @param [Resource] other The other resource to merge
66
+ # @return [Resource] A new resource formed by merging the current resource
67
+ # with other
68
+ def merge(other)
69
+ return self unless other.is_a?(Resource)
70
+
71
+ merged_labels = labels.merge(other.labels) do |_, old_v, new_v|
72
+ old_v.empty? ? new_v : old_v
73
+ end
74
+
75
+ self.class.send(:new, merged_labels.freeze)
76
+ end
77
+
78
+ protected
79
+
80
+ attr_reader :labels
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,24 @@
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
+ # The Trace module contains the OpenTelemetry tracing reference
10
+ # implementation.
11
+ module Trace
12
+ end
13
+ end
14
+ end
15
+
16
+ require 'opentelemetry/sdk/trace/samplers'
17
+ require 'opentelemetry/sdk/trace/config'
18
+ require 'opentelemetry/sdk/trace/export'
19
+ require 'opentelemetry/sdk/trace/multi_span_processor'
20
+ require 'opentelemetry/sdk/trace/noop_span_processor'
21
+ require 'opentelemetry/sdk/trace/span_data'
22
+ require 'opentelemetry/sdk/trace/span'
23
+ require 'opentelemetry/sdk/trace/tracer'
24
+ require 'opentelemetry/sdk/trace/tracer_provider'