Class: Instana::Trace::TracerProvider
- Inherits:
-
OpenTelemetry::Trace::TracerProvider
- Object
- OpenTelemetry::Trace::TracerProvider
- Instana::Trace::TracerProvider
- Defined in:
- lib/instana/trace/tracer_provider.rb
Overview
TracerProvider is the Instana implementation of OpenTelemetry::Trace::TracerProvider.
Instance Attribute Summary collapse
-
#id_generator ⇒ Object
Returns the value of attribute id_generator.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#sampler ⇒ Object
Returns the value of attribute sampler.
-
#span_limits ⇒ Object
Returns the value of attribute span_limits.
Instance Method Summary collapse
-
#add_span_processor(span_processor) ⇒ Object
Adds a new SpanProcessor to this Instana::Tracer.
-
#force_flush(timeout: nil) ⇒ Integer
Immediately export all spans that have not yet been exported for all the registered SpanProcessors.
-
#initialize(sampler: sampler_from_environment(Samplers.parent_based(root: Samplers::ALWAYS_ON)), resource: nil, id_generator: ::Instana::Trace, span_limits: SpanLimits::DEFAULT) ⇒ TracerProvider
constructor
Returns a new TracerProvider instance.
-
#internal_start_span(name, kind, attributes, links, start_timestamp, parent_context, instrumentation_scope) ⇒ Object
This method serves as the primary entry point for span creation.
-
#shutdown(timeout: nil) ⇒ Integer
Attempts to stop all the activity for this TracerProvider.
-
#tracer(name = nil, version = nil) ⇒ Tracer
Returns a Instana::Tracer instance.
Constructor Details
#initialize(sampler: sampler_from_environment(Samplers.parent_based(root: Samplers::ALWAYS_ON)), resource: nil, id_generator: ::Instana::Trace, span_limits: SpanLimits::DEFAULT) ⇒ TracerProvider
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/instana/trace/tracer_provider.rb', line 34 def initialize(sampler: sampler_from_environment(Samplers.parent_based(root: Samplers::ALWAYS_ON)), resource: nil, # Instana::Resources::Resource.create id_generator: ::Instana::Trace, span_limits: SpanLimits::DEFAULT) super() @mutex = Mutex.new @registry = {} @registry_mutex = Mutex.new @span_processors = [] @span_limits = span_limits @sampler = sampler @id_generator = id_generator @stopped = false @resource = resource end |
Instance Attribute Details
#id_generator ⇒ Object
Returns the value of attribute id_generator.
15 16 17 |
# File 'lib/instana/trace/tracer_provider.rb', line 15 def id_generator @id_generator end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
16 17 18 |
# File 'lib/instana/trace/tracer_provider.rb', line 16 def resource @resource end |
#sampler ⇒ Object
Returns the value of attribute sampler.
15 16 17 |
# File 'lib/instana/trace/tracer_provider.rb', line 15 def sampler @sampler end |
#span_limits ⇒ Object
Returns the value of attribute span_limits.
15 16 17 |
# File 'lib/instana/trace/tracer_provider.rb', line 15 def span_limits @span_limits end |
Instance Method Details
#add_span_processor(span_processor) ⇒ Object
Adds a new SpanProcessor to this Instana::Tracer.
123 124 125 126 127 128 129 130 131 |
# File 'lib/instana/trace/tracer_provider.rb', line 123 def add_span_processor(span_processor) @mutex.synchronize do if @stopped ::Instana.logger.warn('calling Tracer#add_span_processor after shutdown.') return end @span_processors = @span_processors.dup.push(span_processor) end end |
#force_flush(timeout: nil) ⇒ Integer
Immediately export all spans that have not yet been exported for all the registered SpanProcessors.
This method should only be called in cases where it is absolutely necessary, such as when using some FaaS providers that may suspend the process after an invocation, but before the ‘Processor` exports the completed spans.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/instana/trace/tracer_provider.rb', line 105 def force_flush(timeout: nil) @mutex.synchronize do return Export::SUCCESS if @stopped start_time = Instana::Util. results = @span_processors.map do |processor| remaining_timeout = Instana::Util.maybe_timeout(timeout, start_time) return Export::TIMEOUT if remaining_timeout&.zero? processor.force_flush(timeout: remaining_timeout) end results.max || Export::SUCCESS end end |
#internal_start_span(name, kind, attributes, links, start_timestamp, parent_context, instrumentation_scope) ⇒ Object
This method serves as the primary entry point for span creation. It initializes an Instana span, handles context, and manages sampling before returning the created span.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/instana/trace/tracer_provider.rb', line 135 def internal_start_span(name, kind, attributes, links, , parent_context, instrumentation_scope) # rubocop:disable Metrics/ParameterLists parent_span = OpenTelemetry::Trace.current_span(parent_context) parent_span_context = parent_span.context if parent_span if parent_span_context&.valid? parent_span_id = parent_span_context.span_id trace_id = parent_span_context.trace_id span_id = @id_generator.generate_span_id end trace_id ||= @id_generator.generate_trace_id if OpenTelemetry::Common::Utilities.untraced?(parent_context) span_id = parent_span_id || @id_generator.generate_span_id return OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id)) end result = @sampler.should_sample?(trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes) span_id ||= @id_generator.generate_span_id if !@stopped && result.recording? && !@stopped trace_flags = result.sampled? ? OpenTelemetry::Trace::TraceFlags::SAMPLED : OpenTelemetry::Trace::TraceFlags::DEFAULT context = Instana::SpanContext.new(trace_id: trace_id, span_id: span_id, trace_flags: trace_flags, tracestate: result.tracestate) attributes = attributes&.merge(result.attributes) || result.attributes.dup Instana::Span.new( name, parent_span_context, context, parent_span, kind, parent_span_id, @span_limits, @span_processors, attributes, links, , @resource, instrumentation_scope ) else Instana::Trace.non_recording_span(Instana::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id, tracestate: result.tracestate)) # Todo add tracestate so that the trcing doesnot happen for this span # rubocop:disable Layout/LineLength end end |
#shutdown(timeout: nil) ⇒ Integer
Attempts to stop all the activity for this Instana::Trace::TracerProvider. Calls SpanProcessor#shutdown for all registered SpanProcessors.
This operation may block until all the Spans are processed. Must be called before turning off the main application to ensure all data are processed and exported.
After this is called all the newly created Spans will be no-op.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/instana/trace/tracer_provider.rb', line 75 def shutdown(timeout: nil) @mutex.synchronize do if @stopped ::Instana.logger.warn('calling Tracer#shutdown multiple times.') return Export::FAILURE end start_time = Instana::Util. results = @span_processors.map do |processor| remaining_timeout = Instana::Util.maybe_timeout(timeout, start_time) break [Export::TIMEOUT] if remaining_timeout&.zero? processor.shutdown(timeout: remaining_timeout) end @stopped = true results.max || Export::SUCCESS end end |
#tracer(name = nil, version = nil) ⇒ Tracer
Returns a Instana::Tracer instance.
56 57 58 59 60 61 |
# File 'lib/instana/trace/tracer_provider.rb', line 56 def tracer(name = nil, version = nil) name ||= '' version ||= '' ::Instana.logger.warn 'calling TracerProvider#tracer without providing a tracer name.' if name.empty? @registry_mutex.synchronize { @registry[Key.new(name, version)] ||= Tracer.new(name, version, self) } end |