Class: OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/opentelemetry/sdk/trace/export/in_memory_span_exporter.rb

Overview

A SpanExporter implementation that can be used to test OpenTelemetry integration.

Example usage in a test suite:

class MyClassTest def setup @tracer_provider = TracerProvider.new # The default is recording: true, which is appropriate in non-test environments. @exporter = InMemorySpanExporter.new(recording: false) @tracer_provider.add_span_processor(SimpleSampledSpansProcessor.new(@exporter)) end

def test_finished_spans @exporter.recording = true @tracer_provider.tracer.in_span(“span”) {}

spans = @exporter.finished_spans
spans.wont_be_nil
spans.size.must_equal(1)
spans[0].name.must_equal("span")

@exporter.recording = false

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recording: true) ⇒ Object



42
43
44
45
46
47
# File 'lib/opentelemetry/sdk/trace/export/in_memory_span_exporter.rb', line 42

def initialize(recording: true)
  @finished_spans = []
  @stopped = false
  @recording = recording
  @mutex = Mutex.new
end

Instance Attribute Details

#recordingBoolean

Controls whether or not the exporter will record spans, or discard them.

Returns:

  • (Boolean)

    when true, the exporter is recording. By default, this is true.



37
38
39
# File 'lib/opentelemetry/sdk/trace/export/in_memory_span_exporter.rb', line 37

def recording
  @recording
end

Instance Method Details

#export(span_datas, timeout: nil) ⇒ Integer

Called to export sampled SpanDatas.

Parameters:

  • span_datas (Enumerable<SpanData>)

    the list of sampled SpanDatas to be exported.

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

    An optional timeout in seconds.

Returns:

  • (Integer)

    the result of the export, SUCCESS or FAILURE



75
76
77
78
79
80
81
82
# File 'lib/opentelemetry/sdk/trace/export/in_memory_span_exporter.rb', line 75

def export(span_datas, timeout: nil)
  @mutex.synchronize do
    return FAILURE if @stopped

    @finished_spans.concat(span_datas.to_a) if @recording
  end
  SUCCESS
end

#finished_spansArray<SpanData>

Returns a frozen array of the finished SpanDatas, represented by OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.ioio.opentelemetryio.opentelemetry.protoio.opentelemetry.proto.traceio.opentelemetry.proto.trace.v1io.opentelemetry.proto.trace.v1.Span.

Returns:



53
54
55
56
57
# File 'lib/opentelemetry/sdk/trace/export/in_memory_span_exporter.rb', line 53

def finished_spans
  @mutex.synchronize do
    @finished_spans.clone.freeze
  end
end

#force_flush(timeout: nil) ⇒ Integer

Called when TracerProvider#force_flush is called, if this exporter is registered to a TracerProvider object.

Parameters:

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

    An optional timeout in seconds.

Returns:

  • (Integer)

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



90
91
92
# File 'lib/opentelemetry/sdk/trace/export/in_memory_span_exporter.rb', line 90

def force_flush(timeout: nil)
  SUCCESS
end

#resetObject

Clears the internal collection of finished Spans.

Does not reset the state of this exporter if already shutdown.



62
63
64
65
66
# File 'lib/opentelemetry/sdk/trace/export/in_memory_span_exporter.rb', line 62

def reset
  @mutex.synchronize do
    @finished_spans.clear
  end
end

#shutdown(timeout: nil) ⇒ Integer

Called when TracerProvider#shutdown is called, if this exporter is registered to a TracerProvider object.

Parameters:

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

    An optional timeout in seconds.

Returns:

  • (Integer)

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



100
101
102
103
104
105
106
# File 'lib/opentelemetry/sdk/trace/export/in_memory_span_exporter.rb', line 100

def shutdown(timeout: nil)
  @mutex.synchronize do
    @finished_spans.clear
    @stopped = true
  end
  SUCCESS
end