traces-backend-open_telemetry 0.3.0 → 0.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e827995b4fdb7c14068483b4a7796eb958378f4ad093c59f7d86be165d8b91f2
4
- data.tar.gz: 1fa19c544b849a9a761ae757ec9198767432517106187d801d6f0e0f836522a1
3
+ metadata.gz: b87e401f00f7dadeb4c3030d9abfd80be9a7343f7a61b8088886ff6ece86b9a2
4
+ data.tar.gz: 0ef5ed27b0a48492f672b7f4fc3b867387bffef46cf665fccf624252da8fbb04
5
5
  SHA512:
6
- metadata.gz: 3d08dd5e72d329c9f114e7f10bad100144ca856f52d2e966cc52161a6cbf59c3c4bfa4ed4b5653e33c6c036f96ca6b4b71fa534ef944d5b68176a08623a0444c
7
- data.tar.gz: d7115c5989a4ecb957a285382744b61e565cf9951668998cc0edd4596bd35507cde8f913b5fd34c95a2f2788d9891182d3f7e8e02462a3f68e0eeff33eff7091
6
+ metadata.gz: '0023923b237b02779db6c6bb35a9013b9751e04a9e3ea8098eb08de28db4b5797068abc2ec4ff2ea83dba2b5cf2fda6938710d99c07c39ed91eaac56f6f2e42d'
7
+ data.tar.gz: 5846bf33abdb432cd5c3d26334eb81891e98a43430fe005b41b01ee38b7ec9373e903b41fd945c0c8e3f35dba1550c32e32233ab1c3b951269a9cda29266f9ac
checksums.yaml.gz.sig CHANGED
Binary file
@@ -15,11 +15,19 @@ module Traces
15
15
  # See <https://github.com/open-telemetry/opentelemetry-ruby> for more details.
16
16
  TRACER = ::OpenTelemetry.tracer_provider.tracer(Traces::Backend::OpenTelemetry.name, Traces::Backend::OpenTelemetry::VERSION)
17
17
 
18
+ # Provides the interface implementation for OpenTelemetry tracing backend.
18
19
  module Interface
20
+ # Creates a trace span with the given name and attributes.
21
+ # @parameter name [String] The name of the trace span.
22
+ # @parameter attributes [Hash | Nil] Optional attributes to attach to the span.
23
+ # @yields {|span| ...} The block to execute within the trace span.
24
+ # @returns [Object] The result of the block execution.
19
25
  def trace(name, attributes: nil, &block)
20
26
  TRACER.in_span(name, attributes: attributes&.transform_keys(&:to_s), &block)
21
27
  end
22
28
 
29
+ # Sets the current trace context.
30
+ # @parameter context [Traces::Context | Nil] The trace context to set as current.
23
31
  def trace_context=(context)
24
32
  if context
25
33
  span_context = ::OpenTelemetry::Trace::SpanContext.new(
@@ -36,7 +44,20 @@ module Traces
36
44
  end
37
45
  end
38
46
 
47
+ # Checks if there is currently an active trace span.
48
+ # @returns [Boolean] `true` if there is an active trace span, `false` otherwise.
49
+ def active?
50
+ # Check if there's a real active trace using OpenTelemetry's INVALID span:
51
+ ::OpenTelemetry::Trace.current_span != ::OpenTelemetry::Trace::Span::INVALID
52
+ end
53
+
54
+ # Gets the current trace context from the active span.
55
+ # @parameter span [OpenTelemetry::Trace::Span] The span to extract context from, defaults to current span.
56
+ # @returns [Traces::Context | Nil] The trace context, or `nil` if no active trace.
39
57
  def trace_context(span = ::OpenTelemetry::Trace.current_span)
58
+ # Return nil if no active trace (using INVALID span check):
59
+ return nil if span == ::OpenTelemetry::Trace::Span::INVALID
60
+
40
61
  if span_context = span.context
41
62
  flags = 0
42
63
 
@@ -54,10 +75,16 @@ module Traces
54
75
  end
55
76
  end
56
77
 
78
+ # Gets the current OpenTelemetry context.
79
+ # @returns [OpenTelemetry::Context] The current OpenTelemetry context.
57
80
  def current_context
58
81
  ::OpenTelemetry::Context.current
59
82
  end
60
83
 
84
+ # Executes code within the given context or attaches the context.
85
+ # @parameter context [OpenTelemetry::Context] The context to use.
86
+ # @yields {|| ...} Optional block to execute within the context.
87
+ # @returns [Object] The result of the block if given, otherwise the detach token.
61
88
  def with_context(context)
62
89
  if block_given?
63
90
  ::OpenTelemetry::Context.with_current(context) do
@@ -68,6 +95,10 @@ module Traces
68
95
  end
69
96
  end
70
97
 
98
+ # Injects trace context into headers for propagation.
99
+ # @parameter headers [Hash | Nil] Optional headers hash to inject into, defaults to new hash.
100
+ # @parameter context [OpenTelemetry::Context | Nil] Optional context to inject, defaults to current context.
101
+ # @returns [Hash | Nil] The headers with injected trace context, or `nil` if no injection occurred.
71
102
  def inject(headers = nil, context = nil)
72
103
  context ||= ::OpenTelemetry::Context.current
73
104
  headers ||= Hash.new
@@ -84,6 +115,9 @@ module Traces
84
115
  return headers
85
116
  end
86
117
 
118
+ # Extracts trace context from headers.
119
+ # @parameter headers [Hash] The headers to extract trace context from.
120
+ # @returns [OpenTelemetry::Context] The extracted context.
87
121
  def extract(headers)
88
122
  ::OpenTelemetry.propagation.extract(headers)
89
123
  end
@@ -3,10 +3,13 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2021-2025, by Samuel Williams.
5
5
 
6
+ # @namespace
6
7
  module Traces
8
+ # @namespace
7
9
  module Backend
10
+ # @namespace
8
11
  module OpenTelemetry
9
- VERSION = "0.3.0"
12
+ VERSION = "0.4.0"
10
13
  end
11
14
  end
12
15
  end
data/readme.md CHANGED
@@ -20,6 +20,11 @@ Please see the [project documentation](https://github.com/socketry/traces-backen
20
20
 
21
21
  Please see the [project releases](https://github.com/socketry/traces-backend-open_telemetryreleases/index) for all releases.
22
22
 
23
+ ### v0.4.0
24
+
25
+ - Fixed `Traces.active?` to correctly return `false` when there is no active trace, instead of always returning `true`.
26
+ - Fixed `Traces.trace_context` to return `nil` when there is no active trace, instead of returning invalid Context objects.
27
+
23
28
  ### v0.3.0
24
29
 
25
30
  - [New Context Propagation Interface](https://github.com/socketry/traces-backend-open_telemetryreleases/index#new-context-propagation-interface)
data/releases.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Releases
2
2
 
3
+ ## v0.4.0
4
+
5
+ - Fixed `Traces.active?` to correctly return `false` when there is no active trace, instead of always returning `true`.
6
+ - Fixed `Traces.trace_context` to return `nil` when there is no active trace, instead of returning invalid Context objects.
7
+
3
8
  ## v0.3.0
4
9
 
5
10
  ### New Context Propagation Interface
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traces-backend-open_telemetry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
metadata.gz.sig CHANGED
Binary file