opentelemetry-api 0.9.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/LICENSE +1 -1
  4. data/README.md +2 -2
  5. data/lib/opentelemetry-api.rb +1 -1
  6. data/lib/opentelemetry.rb +16 -2
  7. data/lib/opentelemetry/baggage.rb +1 -1
  8. data/lib/opentelemetry/baggage/builder.rb +1 -1
  9. data/lib/opentelemetry/baggage/manager.rb +1 -1
  10. data/lib/opentelemetry/baggage/propagation.rb +1 -1
  11. data/lib/opentelemetry/baggage/propagation/context_keys.rb +1 -1
  12. data/lib/opentelemetry/baggage/propagation/text_map_extractor.rb +1 -1
  13. data/lib/opentelemetry/baggage/propagation/text_map_injector.rb +1 -1
  14. data/lib/opentelemetry/context.rb +1 -1
  15. data/lib/opentelemetry/context/key.rb +1 -1
  16. data/lib/opentelemetry/context/propagation.rb +1 -1
  17. data/lib/opentelemetry/context/propagation/composite_propagator.rb +1 -1
  18. data/lib/opentelemetry/context/propagation/default_getter.rb +1 -1
  19. data/lib/opentelemetry/context/propagation/default_setter.rb +1 -1
  20. data/lib/opentelemetry/context/propagation/noop_extractor.rb +1 -1
  21. data/lib/opentelemetry/context/propagation/noop_injector.rb +1 -1
  22. data/lib/opentelemetry/context/propagation/propagation.rb +1 -1
  23. data/lib/opentelemetry/context/propagation/propagator.rb +1 -1
  24. data/lib/opentelemetry/error.rb +1 -1
  25. data/lib/opentelemetry/instrumentation.rb +1 -1
  26. data/lib/opentelemetry/instrumentation/base.rb +1 -1
  27. data/lib/opentelemetry/instrumentation/registry.rb +2 -3
  28. data/lib/opentelemetry/metrics.rb +1 -1
  29. data/lib/opentelemetry/metrics/handles.rb +1 -1
  30. data/lib/opentelemetry/metrics/instruments.rb +1 -1
  31. data/lib/opentelemetry/metrics/meter.rb +1 -1
  32. data/lib/opentelemetry/metrics/meter_provider.rb +1 -1
  33. data/lib/opentelemetry/trace.rb +15 -4
  34. data/lib/opentelemetry/trace/link.rb +1 -1
  35. data/lib/opentelemetry/trace/propagation.rb +1 -1
  36. data/lib/opentelemetry/trace/propagation/trace_context.rb +1 -1
  37. data/lib/opentelemetry/trace/propagation/trace_context/text_map_extractor.rb +3 -5
  38. data/lib/opentelemetry/trace/propagation/trace_context/text_map_injector.rb +2 -2
  39. data/lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb +1 -1
  40. data/lib/opentelemetry/trace/span.rb +6 -2
  41. data/lib/opentelemetry/trace/span_context.rb +3 -3
  42. data/lib/opentelemetry/trace/span_kind.rb +1 -1
  43. data/lib/opentelemetry/trace/status.rb +1 -1
  44. data/lib/opentelemetry/trace/trace_flags.rb +1 -1
  45. data/lib/opentelemetry/trace/tracer.rb +1 -1
  46. data/lib/opentelemetry/trace/tracer_provider.rb +1 -1
  47. data/lib/opentelemetry/trace/tracestate.rb +158 -0
  48. data/lib/opentelemetry/trace/util/http_to_status.rb +1 -1
  49. data/lib/opentelemetry/version.rb +2 -2
  50. metadata +6 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f120b2ac09cec8b23e216b7b246880412cb4592c97ca777fa91e1fd54ead58d
4
- data.tar.gz: 8becd1720c0a8794d90318f921ac4b437169e8645ce2e63821ce116d7f5c34a3
3
+ metadata.gz: 42c8e52feabfa3bfc793e0699cbd50b72a1df552be7cffd96d2d80237b34e3e9
4
+ data.tar.gz: 3e18f329dff954d7b5edeb79a5b7f18f1c24f57af5df9c51cf5d3da2c5d45a63
5
5
  SHA512:
6
- metadata.gz: 5512d2c73e2153a5c9a955d5f913e1d117f99e1f4c8dcdc07f79c2b66d4cd8b2e015fcb8e684fac589cd8e647ff3f9c334e96873e600da7bb7797b38b3b92b94
7
- data.tar.gz: 83a4eca288285808f4862518b5594c3539dc1e9b6c4ef70477b95d73d4ca4ff11e9b35f71b4a8d3e2a58f8f776254b5b688097221b7c0bdb51f0edaef0938b0e
6
+ metadata.gz: 0c7f5a046e769ea40092101de2638a418aec4904ea6fec4af72f59e4d4f6f15e23899d31cfbc5c74eb0ca3e63e36b765930e377756f131bccaad5fb20c644fa5
7
+ data.tar.gz: 8fc31b5c431446a9534b0cf9cee191b0737ff95a2e824c0d1b68947aa1f53f7f4fa58ab145fc7624cbf2d5cf0840ed3f60885e7fcb10b825b332ad8fc73f2724
@@ -1,5 +1,30 @@
1
1
  # Release History: opentelemetry-api
2
2
 
3
+ ### v0.13.0 / 2021-01-29
4
+
5
+ * ADDED: Add optional attributes to record_exception
6
+ * FIXED: Small test fixes.
7
+
8
+ ### v0.12.1 / 2021-01-13
9
+
10
+ * FIXED: Eliminate warning about Random::DEFAULT on Ruby 3.0
11
+
12
+ ### v0.12.0 / 2020-12-24
13
+
14
+ * ADDED: Structured error handling
15
+
16
+ ### v0.11.0 / 2020-12-11
17
+
18
+ * BREAKING CHANGE: Implement tracestate
19
+
20
+ * ADDED: Implement tracestate
21
+ * FIXED: Missing white space from install messages
22
+ * FIXED: Copyright comments to not reference year
23
+
24
+ ### v0.10.0 / 2020-12-03
25
+
26
+ * (No significant changes)
27
+
3
28
  ### v0.9.0 / 2020-11-27
4
29
 
5
30
  * (No significant changes)
data/LICENSE CHANGED
@@ -186,7 +186,7 @@
186
186
  same "printed page" as the copyright notice for easier
187
187
  identification within third-party archives.
188
188
 
189
- Copyright 2020 OpenTelemetry Authors
189
+ Copyright The OpenTelemetry Authors
190
190
 
191
191
  Licensed under the Apache License, Version 2.0 (the "License");
192
192
  you may not use this file except in compliance with the License.
data/README.md CHANGED
@@ -59,8 +59,8 @@ The `opentelemetry-api` gem is distributed under the Apache 2.0 license. See [LI
59
59
  [opentelemetry-home]: https://opentelemetry.io
60
60
  [bundler-home]: https://bundler.io
61
61
  [repo-github]: https://github.com/open-telemetry/opentelemetry-ruby
62
- [license-github]: https://github.com/open-telemetry/opentelemetry-ruby/blob/master/LICENSE
63
- [examples-github]: https://github.com/open-telemetry/opentelemetry-ruby/tree/master/examples
62
+ [license-github]: https://github.com/open-telemetry/opentelemetry-ruby/blob/main/LICENSE
63
+ [examples-github]: https://github.com/open-telemetry/opentelemetry-ruby/tree/main/examples
64
64
  [ruby-sig]: https://github.com/open-telemetry/community#ruby-sig
65
65
  [community-meetings]: https://github.com/open-telemetry/community#community-meetings
66
66
  [ruby-gitter]: https://gitter.im/open-telemetry/opentelemetry-ruby
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -22,13 +22,27 @@ require 'opentelemetry/version'
22
22
  module OpenTelemetry
23
23
  extend self
24
24
 
25
- attr_writer :tracer_provider, :meter_provider, :baggage, :logger
25
+ attr_writer :tracer_provider, :meter_provider, :baggage, :logger, :error_handler
26
26
 
27
27
  # @return [Object, Logger] configured Logger or a default STDOUT Logger.
28
28
  def logger
29
29
  @logger ||= Logger.new(STDOUT, level: ENV['OTEL_LOG_LEVEL'] || Logger::INFO)
30
30
  end
31
31
 
32
+ # @return [Callable] configured error handler or a default that logs the
33
+ # exception and message at ERROR level.
34
+ def error_handler
35
+ @error_handler ||= ->(exception: nil, message: nil) { logger.error("OpenTelemetry error: #{[message, exception&.message].compact.join(' - ')}") }
36
+ end
37
+
38
+ # Handles an error by calling the configured error_handler.
39
+ #
40
+ # @param [optional Exception] exception The exception to be handled
41
+ # @param [optional String] message An error message.
42
+ def handle_error(exception: nil, message: nil)
43
+ error_handler.call(exception: exception, message: message)
44
+ end
45
+
32
46
  # @return [Object, Trace::TracerProvider] registered tracer provider or a
33
47
  # default no-op implementation of the tracer provider.
34
48
  def tracer_provider
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
  module OpenTelemetry
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -79,8 +79,7 @@ module OpenTelemetry
79
79
  OpenTelemetry.logger.warn "Instrumentation: #{instrumentation.name} failed to install"
80
80
  end
81
81
  rescue => e # rubocop:disable Style/RescueStandardError
82
- OpenTelemetry.logger.warn "Instrumentation: #{instrumentation.name} unhandled exception" \
83
- "during install #{e}: #{e.backtrace}"
82
+ OpenTelemetry.handle_error(exception: e, message: "Instrumentation: #{instrumentation.name} unhandled exception during install: #{e.backtrace}")
84
83
  end
85
84
  end
86
85
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -13,7 +13,17 @@ module OpenTelemetry
13
13
 
14
14
  CURRENT_SPAN_KEY = Context.create_key('current-span')
15
15
 
16
- private_constant :CURRENT_SPAN_KEY
16
+ # Random number generator for generating IDs. This is an object that can
17
+ # respond to `#bytes` and uses the system PRNG. The current logic is
18
+ # compatible with Ruby 2.5 (which does not implement the `Random.bytes`
19
+ # class method) and with Ruby 3.0+ (which deprecates `Random::DEFAULT`).
20
+ # When we drop support for Ruby 2.5, this can simply be replaced with
21
+ # the class `Random`.
22
+ #
23
+ # @return [#bytes]
24
+ RANDOM = Random.respond_to?(:bytes) ? Random : Random::DEFAULT
25
+
26
+ private_constant :CURRENT_SPAN_KEY, :RANDOM
17
27
 
18
28
  # An invalid trace identifier, a 16-byte string with all zero bytes.
19
29
  INVALID_TRACE_ID = ("\0" * 16).b
@@ -27,7 +37,7 @@ module OpenTelemetry
27
37
  # @return [String] a valid trace ID.
28
38
  def generate_trace_id
29
39
  loop do
30
- id = Random::DEFAULT.bytes(16)
40
+ id = RANDOM.bytes(16)
31
41
  return id unless id == INVALID_TRACE_ID
32
42
  end
33
43
  end
@@ -38,7 +48,7 @@ module OpenTelemetry
38
48
  # @return [String] a valid span ID.
39
49
  def generate_span_id
40
50
  loop do
41
- id = Random::DEFAULT.bytes(8)
51
+ id = RANDOM.bytes(8)
42
52
  return id unless id == INVALID_SPAN_ID
43
53
  end
44
54
  end
@@ -76,6 +86,7 @@ end
76
86
 
77
87
  require 'opentelemetry/trace/link'
78
88
  require 'opentelemetry/trace/trace_flags'
89
+ require 'opentelemetry/trace/tracestate'
79
90
  require 'opentelemetry/trace/span_context'
80
91
  require 'opentelemetry/trace/span_kind'
81
92
  require 'opentelemetry/trace/span'
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
  module OpenTelemetry
@@ -37,10 +37,8 @@ module OpenTelemetry
37
37
  # context if parsing fails.
38
38
  def extract(carrier, context, &getter)
39
39
  getter ||= default_getter
40
- header = getter.call(carrier, @traceparent_key)
41
- tp = TraceParent.from_string(header)
42
-
43
- tracestate = getter.call(carrier, @tracestate_key)
40
+ tp = TraceParent.from_string(getter.call(carrier, @traceparent_key))
41
+ tracestate = Tracestate.from_string(getter.call(carrier, @tracestate_key))
44
42
 
45
43
  span_context = Trace::SpanContext.new(trace_id: tp.trace_id,
46
44
  span_id: tp.span_id,
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
  module OpenTelemetry
@@ -37,7 +37,7 @@ module OpenTelemetry
37
37
 
38
38
  setter ||= DEFAULT_SETTER
39
39
  setter.call(carrier, @traceparent_key, TraceParent.from_span_context(span_context).to_s)
40
- setter.call(carrier, @tracestate_key, span_context.tracestate) unless span_context.tracestate.nil?
40
+ setter.call(carrier, @tracestate_key, span_context.tracestate.to_s) unless span_context.tracestate.empty?
41
41
 
42
42
  carrier
43
43
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2020 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
  module OpenTelemetry
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -85,9 +85,13 @@ module OpenTelemetry
85
85
  # can be recorded on a span.
86
86
  #
87
87
  # @param [Exception] exception The exception to recorded
88
+ # @param [optional Hash{String => String, Numeric, Boolean, Array<String, Numeric, Boolean>}]
89
+ # attributes One or more key:value pairs, where the keys must be
90
+ # strings and the values may be (array of) string, boolean or numeric
91
+ # type.
88
92
  #
89
93
  # @return [void]
90
- def record_exception(exception); end
94
+ def record_exception(exception, attributes: nil); end
91
95
 
92
96
  # Sets the Status to the Span
93
97
  #
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -18,14 +18,14 @@ module OpenTelemetry
18
18
  # @param [optional String] trace_id The trace ID associated with a {Span}.
19
19
  # @param [optional String] span_id The span ID associated with a {Span}.
20
20
  # @param [optional TraceFlags] trace_flags The trace flags associated with a {Span}.
21
- # @param [optional String] tracestate The tracestate associated with a {Span}. May be nil.
21
+ # @param [optional Tracestate] tracestate The tracestate associated with a {Span}. May be nil.
22
22
  # @param [optional Boolean] remote Whether the {SpanContext} was extracted from the wire.
23
23
  # @return [SpanContext]
24
24
  def initialize(
25
25
  trace_id: Trace.generate_trace_id,
26
26
  span_id: Trace.generate_span_id,
27
27
  trace_flags: TraceFlags::DEFAULT,
28
- tracestate: nil,
28
+ tracestate: Tracestate::DEFAULT,
29
29
  remote: false
30
30
  )
31
31
  @trace_id = trace_id
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -0,0 +1,158 @@
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 Trace
9
+ # Tracestate is a part of SpanContext, represented by an immutable list of
10
+ # string key/value pairs and formally defined by the W3C Trace Context
11
+ # specification https://www.w3.org/TR/trace-context/
12
+ class Tracestate
13
+ class << self
14
+ private :new # rubocop:disable Style/AccessModifierDeclarations
15
+
16
+ # Returns a newly created Tracestate parsed from the header provided.
17
+ #
18
+ # @param [String] header Encoding of the tracestate header defined by
19
+ # the W3C Trace Context specification https://www.w3.org/TR/trace-context/
20
+ # @return [Tracestate] A new Tracestate instance or DEFAULT
21
+ def from_string(header) # rubocop:disable Metrics/CyclomaticComplexity:
22
+ return DEFAULT if header.nil? || header.empty?
23
+
24
+ hash = header.split(',').each_with_object({}) do |member, memo|
25
+ member.strip!
26
+ kv = member.split('=')
27
+ k, v = *kv
28
+ next unless kv.length == 2 && VALID_KEY.match?(k) && VALID_VALUE.match?(v)
29
+
30
+ memo[k] = v
31
+ end
32
+ return DEFAULT if hash.empty?
33
+
34
+ new(hash)
35
+ end
36
+
37
+ # Returns a Tracestate created from a Hash.
38
+ #
39
+ # @param [Hash<String, String>] hash Key-value pairs to store in the
40
+ # Tracestate. Keys and values are validated against the W3C Trace
41
+ # Context specification, and any invalid members are logged at
42
+ # DEBUG level and dropped.
43
+ # @return [Tracestate] A new Tracestate instance or DEFAULT
44
+ def from_hash(hash)
45
+ hash = hash.select do |k, v|
46
+ valid = VALID_KEY.match?(k) && VALID_VALUE.match?(v)
47
+ OpenTelemetry.logger.debug("Invalid Tracestate member - #{k} : #{v}") unless valid
48
+ valid
49
+ end
50
+ new(hash)
51
+ end
52
+
53
+ # @api private
54
+ # Returns a new Tracestate created from the Hash provided. This
55
+ # skips validation of the keys and values, assuming they are already
56
+ # valid.
57
+ # This method is intended only for the use of instance methods in
58
+ # this class.
59
+ def create(hash)
60
+ new(hash)
61
+ end
62
+ end
63
+
64
+ MAX_MEMBER_COUNT = 32 # Defined by https://www.w3.org/TR/trace-context/
65
+ VALID_KEY = Regexp.union(%r(^[a-z][a-z0-9_\-*/]{,255}$), %r(^[a-z0-9][a-z0-9_\-*/]{,240}@[a-z][a-z0-9_\-*/]{,13}$)).freeze
66
+ VALID_VALUE = /^[ -~&&[^,=]]{,255}[!-~&&[^,=]]$/.freeze
67
+ private_constant(:MAX_MEMBER_COUNT, :VALID_KEY, :VALID_VALUE)
68
+
69
+ # @api private
70
+ # The constructor is private and only for use internally by the class.
71
+ # Users should use the {from_hash} or {from_string} factory methods to
72
+ # obtain a {Tracestate} instance.
73
+ #
74
+ # @param [Hash<String, String>] hash Key-value pairs
75
+ # @return [Tracestate]
76
+ def initialize(hash)
77
+ excess = hash.size - MAX_MEMBER_COUNT
78
+ hash = Hash[hash.drop(excess)] if excess.positive?
79
+ @hash = hash.freeze
80
+ end
81
+
82
+ # Returns the value associated with the given key, or nil if the key
83
+ # is not present.
84
+ #
85
+ # @param [String] key The key to lookup.
86
+ # @return [String] The value associated with the key, or nil.
87
+ def value(key)
88
+ @hash[key]
89
+ end
90
+
91
+ alias [] value
92
+
93
+ # Adds a new key/value pair or updates an existing value for a given key.
94
+ # Keys and values are validated against the W3C Trace Context
95
+ # specification, and any invalid members are logged at DEBUG level and
96
+ # ignored.
97
+ #
98
+ # @param [String] key The key to add or update.
99
+ # @param [String] value The value to add or update.
100
+ # @return [Tracestate] self, if unchanged, or a new Tracestate containing
101
+ # the new or updated key/value pair.
102
+ def set_value(key, value)
103
+ return self unless VALID_KEY.match?(key) && VALID_VALUE.match?(value)
104
+
105
+ h = Hash[@hash]
106
+ h[key] = value
107
+ self.class.create(h)
108
+ end
109
+
110
+ # Deletes the key/value pair associated with the given key.
111
+ #
112
+ # @param [String] key The key to remove.
113
+ # @return [Tracestate] self, if unchanged, or a new Tracestate without
114
+ # the specified key.
115
+ def delete(key)
116
+ return self unless @hash.key?(key)
117
+
118
+ h = Hash[@hash]
119
+ h.delete(key)
120
+ self.class.create(h)
121
+ end
122
+
123
+ # Returns this Tracestate encoded according to the W3C Trace Context
124
+ # specification https://www.w3.org/TR/trace-context/
125
+ #
126
+ # @return [String] this Tracestate encoded as a string.
127
+ def to_s
128
+ @hash.inject(+'') do |memo, (k, v)|
129
+ memo << k << '=' << v << ','
130
+ end.chop! || ''
131
+ end
132
+
133
+ # Returns this Tracestate as a Hash.
134
+ #
135
+ # @return [Hash] the members of this Tracestate
136
+ def to_h
137
+ @hash.dup
138
+ end
139
+
140
+ # Returns true if this Tracestate is empty.
141
+ #
142
+ # @return [Boolean] true if this Tracestate is empty, else false.
143
+ def empty?
144
+ @hash.empty?
145
+ end
146
+
147
+ # Returns true if this Tracestate equals other.
148
+ #
149
+ # @param [Tracestate] other The Tracestate for comparison.
150
+ # @return [Boolean] true if this Tracestate == other, else false.
151
+ def ==(other)
152
+ @hash == other.to_h
153
+ end
154
+
155
+ DEFAULT = new({})
156
+ end
157
+ end
158
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright 2019 OpenTelemetry Authors
3
+ # Copyright The OpenTelemetry Authors
4
4
  #
5
5
  # SPDX-License-Identifier: Apache-2.0
6
6
 
7
7
  module OpenTelemetry
8
8
  ## Current OpenTelemetry version
9
- VERSION = '0.9.0'
9
+ VERSION = '0.13.0'
10
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opentelemetry-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.13.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-11-27 00:00:00.000000000 Z
11
+ date: 2021-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: benchmark-ipsa
@@ -189,16 +189,17 @@ files:
189
189
  - lib/opentelemetry/trace/trace_flags.rb
190
190
  - lib/opentelemetry/trace/tracer.rb
191
191
  - lib/opentelemetry/trace/tracer_provider.rb
192
+ - lib/opentelemetry/trace/tracestate.rb
192
193
  - lib/opentelemetry/trace/util/http_to_status.rb
193
194
  - lib/opentelemetry/version.rb
194
195
  homepage: https://github.com/open-telemetry/opentelemetry-ruby
195
196
  licenses:
196
197
  - Apache-2.0
197
198
  metadata:
198
- changelog_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-api/v0.9.0/file.CHANGELOG.html
199
- source_code_uri: https://github.com/open-telemetry/opentelemetry-ruby/tree/master/api
199
+ changelog_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-api/v0.13.0/file.CHANGELOG.html
200
+ source_code_uri: https://github.com/open-telemetry/opentelemetry-ruby/tree/main/api
200
201
  bug_tracker_uri: https://github.com/open-telemetry/opentelemetry-ruby/issues
201
- documentation_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-api/v0.9.0
202
+ documentation_uri: https://open-telemetry.github.io/opentelemetry-ruby/opentelemetry-api/v0.13.0
202
203
  post_install_message:
203
204
  rdoc_options: []
204
205
  require_paths: