@@ -15,11 +15,19 @@ module OpenTelemetry
1515 # See <https://github.com/open-telemetry/opentelemetry-ruby> for more details.
1616 TRACER = ::OpenTelemetry . tracer_provider . tracer ( Traces ::Backend ::OpenTelemetry . name , Traces ::Backend ::OpenTelemetry ::VERSION )
1717
18+ # Provides the interface implementation for OpenTelemetry tracing backend.
1819 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.
1925 def trace ( name , attributes : nil , &block )
2026 TRACER . in_span ( name , attributes : attributes &.transform_keys ( &:to_s ) , &block )
2127 end
2228
29+ # Sets the current trace context.
30+ # @parameter context [Traces::Context | Nil] The trace context to set as current.
2331 def trace_context = ( context )
2432 if context
2533 span_context = ::OpenTelemetry ::Trace ::SpanContext . new (
@@ -36,11 +44,16 @@ def trace_context=(context)
3644 end
3745 end
3846
47+ # Checks if there is currently an active trace span.
48+ # @returns [Boolean] `true` if there is an active trace span, `false` otherwise.
3949 def active?
4050 # Check if there's a real active trace using OpenTelemetry's INVALID span:
4151 ::OpenTelemetry ::Trace . current_span != ::OpenTelemetry ::Trace ::Span ::INVALID
4252 end
4353
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.
4457 def trace_context ( span = ::OpenTelemetry ::Trace . current_span )
4558 # Return nil if no active trace (using INVALID span check):
4659 return nil if span == ::OpenTelemetry ::Trace ::Span ::INVALID
@@ -62,10 +75,16 @@ def trace_context(span = ::OpenTelemetry::Trace.current_span)
6275 end
6376 end
6477
78+ # Gets the current OpenTelemetry context.
79+ # @returns [OpenTelemetry::Context] The current OpenTelemetry context.
6580 def current_context
6681 ::OpenTelemetry ::Context . current
6782 end
6883
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.
6988 def with_context ( context )
7089 if block_given?
7190 ::OpenTelemetry ::Context . with_current ( context ) do
@@ -76,6 +95,10 @@ def with_context(context)
7695 end
7796 end
7897
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.
79102 def inject ( headers = nil , context = nil )
80103 context ||= ::OpenTelemetry ::Context . current
81104 headers ||= Hash . new
@@ -92,6 +115,9 @@ def inject(headers = nil, context = nil)
92115 return headers
93116 end
94117
118+ # Extracts trace context from headers.
119+ # @parameter headers [Hash] The headers to extract trace context from.
120+ # @returns [OpenTelemetry::Context] The extracted context.
95121 def extract ( headers )
96122 ::OpenTelemetry . propagation . extract ( headers )
97123 end
0 commit comments