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:

class MyClassTest def setup @tracer_provider = TracerProvider.new @exporter = InMemorySpanExporter.new @tracer_provider.add_span_processor(SimpleSampledSpansProcessor.new(@exporter)) end

def test_finished_spans @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")

end

Instance Method Summary collapse

Constructor Details

#initializeObject



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

def initialize
  @finished_spans = []
  @stopped = false
  @mutex = Mutex.new
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



66
67
68
69
70
71
72
73
# File 'lib/opentelemetry/sdk/trace/export/in_memory_span_exporter.rb', line 66

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

    @finished_spans.concat(span_datas.to_a)
  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:



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

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

#resetObject

Clears the internal collection of finished Spans.

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



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

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.



81
82
83
84
85
86
87
# File 'lib/opentelemetry/sdk/trace/export/in_memory_span_exporter.rb', line 81

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