Class: OpenTelemetry::SDK::Trace::TracerProvider

Inherits:
Trace::TracerProvider
  • Object
show all
Defined in:
lib/opentelemetry/sdk/trace/tracer_provider.rb

Overview

TracerProvider is the SDK implementation of Trace::TracerProvider.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sampler: sampler_from_environment(Samplers.parent_based(root: Samplers::ALWAYS_ON)), resource: OpenTelemetry::SDK::Resources::Resource.create, id_generator: OpenTelemetry::Trace, span_limits: SpanLimits::DEFAULT) ⇒ TracerProvider

Parameters:

  • sampler (optional Sampler) (defaults to: sampler_from_environment(Samplers.parent_based(root: Samplers::ALWAYS_ON)))

    The sampling policy for new spans

  • resource (optional Resource) (defaults to: OpenTelemetry::SDK::Resources::Resource.create)

    The resource to associate with spans created by Tracers created by this TracerProvider

  • id_generator (optional IDGenerator) (defaults to: OpenTelemetry::Trace)

    The trace and span ID generation policy

  • span_limits (optional SpanLimits) (defaults to: SpanLimits::DEFAULT)

    The limits to apply to attribute, event and link counts for Spans created by Tracers created by this TracerProvider



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/opentelemetry/sdk/trace/tracer_provider.rb', line 30

def initialize(sampler: sampler_from_environment(Samplers.parent_based(root: Samplers::ALWAYS_ON)),
               resource: OpenTelemetry::SDK::Resources::Resource.create,
               id_generator: OpenTelemetry::Trace,
               span_limits: SpanLimits::DEFAULT)
  @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_generatorObject

Returns the value of attribute id_generator.



15
16
17
# File 'lib/opentelemetry/sdk/trace/tracer_provider.rb', line 15

def id_generator
  @id_generator
end

#resourceObject (readonly)

Returns the value of attribute resource.



16
17
18
# File 'lib/opentelemetry/sdk/trace/tracer_provider.rb', line 16

def resource
  @resource
end

#samplerObject

Returns the value of attribute sampler.



15
16
17
# File 'lib/opentelemetry/sdk/trace/tracer_provider.rb', line 15

def sampler
  @sampler
end

#span_limitsObject

Returns the value of attribute span_limits.



15
16
17
# File 'lib/opentelemetry/sdk/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 OpenTelemetry::SDK::Trace::Tracer.

Parameters:

  • span_processor

    the new SpanProcessor to be added.



118
119
120
121
122
123
124
125
126
# File 'lib/opentelemetry/sdk/trace/tracer_provider.rb', line 118

def add_span_processor(span_processor)
  @mutex.synchronize do
    if @stopped
      OpenTelemetry.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.

Parameters:

  • timeout (optional Numeric) (defaults to: nil)

    An optional timeout in seconds.

Returns:

  • (Integer)

    Export::SUCCESS if no error occurred, Export::FAILURE if a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/opentelemetry/sdk/trace/tracer_provider.rb', line 100

def force_flush(timeout: nil)
  @mutex.synchronize do
    return Export::SUCCESS if @stopped

    start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
    results = @span_processors.map do |processor|
      remaining_timeout = OpenTelemetry::Common::Utilities.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_create_span(name, kind, trace_id, parent_span_id, attributes, links, start_timestamp, parent_context, parent_span, instrumentation_library) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



129
130
131
132
133
134
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
# File 'lib/opentelemetry/sdk/trace/tracer_provider.rb', line 129

def internal_create_span(name, kind, trace_id, parent_span_id, attributes, links, start_timestamp, parent_context, parent_span, instrumentation_library) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  parent_span_context = parent_span.context
  if parent_span_context.valid?
    parent_span_id = parent_span_context.span_id
    trace_id = parent_span_context.trace_id
  end
  name ||= 'empty'
  trace_id ||= @id_generator.generate_trace_id
  kind ||= :internal
  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 result.recording? && !@stopped
    trace_flags = result.sampled? ? OpenTelemetry::Trace::TraceFlags::SAMPLED : OpenTelemetry::Trace::TraceFlags::DEFAULT
    context = OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id, trace_flags: trace_flags, tracestate: result.tracestate)
    attributes = attributes&.merge(result.attributes) || result.attributes
    Span.new(
      context,
      parent_context,
      parent_span,
      name,
      kind,
      parent_span_id,
      @span_limits,
      @span_processors,
      attributes,
      links,
      start_timestamp,
      @resource,
      instrumentation_library
    )
  else
    OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id, tracestate: result.tracestate))
  end
end

#shutdown(timeout: nil) ⇒ Integer

Attempts to stop all the activity for this OpenTelemetry::SDK::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.

Parameters:

  • timeout (optional Numeric) (defaults to: nil)

    An optional timeout in seconds.

Returns:

  • (Integer)

    Export::SUCCESS if no error occurred, Export::FAILURE if a non-specific failure occurred, Export::TIMEOUT if a timeout occurred.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/opentelemetry/sdk/trace/tracer_provider.rb', line 70

def shutdown(timeout: nil)
  @mutex.synchronize do
    if @stopped
      OpenTelemetry.logger.warn('calling Tracer#shutdown multiple times.')
      return Export::FAILURE
    end

    start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
    results = @span_processors.map do |processor|
      remaining_timeout = OpenTelemetry::Common::Utilities.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 OpenTelemetry::SDK::Trace::Tracer instance.

Parameters:

  • name (optional String) (defaults to: nil)

    Instrumentation package name

  • version (optional String) (defaults to: nil)

    Instrumentation package version

Returns:



51
52
53
54
55
56
# File 'lib/opentelemetry/sdk/trace/tracer_provider.rb', line 51

def tracer(name = nil, version = nil)
  name ||= ''
  version ||= ''
  OpenTelemetry.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