Module: OpenTelemetry::TestHelpers

Extended by:
TestHelpers
Included in:
TestHelpers
Defined in:
lib/opentelemetry/test_helpers/version.rb,
lib/opentelemetry/test_helpers.rb

Overview

The TestHelpers module contains a collection of test helpers for the various OpenTelemetry Ruby gems.

Constant Summary collapse

VERSION =
'0.5.0'
NULL_LOGGER =
Logger.new(File::NULL)

Instance Method Summary collapse

Instance Method Details

#create_log_record_data(timestamp: OpenTelemetry::TestHelpers.exportable_timestamp, observed_timestamp: OpenTelemetry::TestHelpers.exportable_timestamp, severity_text: nil, severity_number: nil, body: nil, attributes: nil, trace_id: OpenTelemetry::Trace.generate_trace_id, span_id: OpenTelemetry::Trace.generate_span_id, trace_flags: OpenTelemetry::Trace::TraceFlags::DEFAULT, resource: nil, instrumentation_scope: OpenTelemetry::SDK::InstrumentationScope.new('', 'v0.0.1'), total_recorded_attributes: 0) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/opentelemetry/test_helpers.rb', line 76

def create_log_record_data(timestamp: OpenTelemetry::TestHelpers.exportable_timestamp,
                           observed_timestamp: OpenTelemetry::TestHelpers.exportable_timestamp,
                           severity_text: nil,
                           severity_number: nil,
                           body: nil,
                           attributes: nil,
                           trace_id: OpenTelemetry::Trace.generate_trace_id,
                           span_id: OpenTelemetry::Trace.generate_span_id,
                           trace_flags: OpenTelemetry::Trace::TraceFlags::DEFAULT,
                           resource: nil,
                           instrumentation_scope: OpenTelemetry::SDK::InstrumentationScope.new('', 'v0.0.1'),
                           total_recorded_attributes: 0)
  resource ||= OpenTelemetry::SDK::Resources::Resource.telemetry_sdk
  OpenTelemetry::SDK::Logs::LogRecordData.new(
    timestamp,
    observed_timestamp,
    severity_text,
    severity_number,
    body,
    attributes,
    trace_id,
    span_id,
    trace_flags,
    resource,
    instrumentation_scope,
    total_recorded_attributes
  )
end

#create_span_data(name: '', kind: nil, status: nil, parent_span_id: OpenTelemetry::Trace::INVALID_SPAN_ID, total_recorded_attributes: 0, total_recorded_events: 0, total_recorded_links: 0, start_timestamp: OpenTelemetry::TestHelpers.exportable_timestamp, end_timestamp: OpenTelemetry::TestHelpers.exportable_timestamp, attributes: nil, links: nil, events: nil, resource: nil, instrumentation_scope: OpenTelemetry::SDK::InstrumentationScope.new('', 'v0.0.1'), span_id: OpenTelemetry::Trace.generate_span_id, trace_id: OpenTelemetry::Trace.generate_trace_id, trace_flags: OpenTelemetry::Trace::TraceFlags::DEFAULT, tracestate: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/opentelemetry/test_helpers.rb', line 64

def create_span_data(name: '', kind: nil, status: nil, parent_span_id: OpenTelemetry::Trace::INVALID_SPAN_ID,
                     total_recorded_attributes: 0, total_recorded_events: 0, total_recorded_links: 0, start_timestamp: OpenTelemetry::TestHelpers.exportable_timestamp,
                     end_timestamp: OpenTelemetry::TestHelpers.exportable_timestamp, attributes: nil, links: nil, events: nil, resource: nil,
                     instrumentation_scope: OpenTelemetry::SDK::InstrumentationScope.new('', 'v0.0.1'),
                     span_id: OpenTelemetry::Trace.generate_span_id, trace_id: OpenTelemetry::Trace.generate_trace_id,
                     trace_flags: OpenTelemetry::Trace::TraceFlags::DEFAULT, tracestate: nil)
  resource ||= OpenTelemetry::SDK::Resources::Resource.telemetry_sdk
  OpenTelemetry::SDK::Trace::SpanData.new(name, kind, status, parent_span_id, total_recorded_attributes,
                                          total_recorded_events, total_recorded_links, start_timestamp, end_timestamp,
                                          attributes, links, events, resource, instrumentation_scope, span_id, trace_id, trace_flags, tracestate)
end

#exportable_timestamp(time = Time.now) ⇒ Object



44
45
46
# File 'lib/opentelemetry/test_helpers.rb', line 44

def exportable_timestamp(time = Time.now)
  (time.to_r * 1_000_000_000).to_i
end

#reset_opentelemetryObject

reset_opentelemetry is a test helper used to clear SDK configuration state between calls



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/opentelemetry/test_helpers.rb', line 18

def reset_opentelemetry
  OpenTelemetry.instance_variable_set(
    :@tracer_provider,
    OpenTelemetry::Internal::ProxyTracerProvider.new
  )

  # OpenTelemetry will load the defaults
  # on the next call to any of these methods
  OpenTelemetry.error_handler = nil
  OpenTelemetry.propagation = nil

  # We use a null logger to control the console
  # log output and explicitly enable it
  # when testing the log output
  OpenTelemetry.logger = NULL_LOGGER
end

#with_env(new_env) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/opentelemetry/test_helpers.rb', line 48

def with_env(new_env)
  env_to_reset = ENV.select { |k, _| new_env.key?(k) }
  keys_to_delete = new_env.keys - ENV.keys
  new_env.each_pair { |k, v| ENV[k] = v }
  yield
ensure
  env_to_reset.each_pair { |k, v| ENV[k] = v }
  keys_to_delete.each { |k| ENV.delete(k) }
end

#with_ids(trace_id, span_id, &block) ⇒ Object



58
59
60
61
62
# File 'lib/opentelemetry/test_helpers.rb', line 58

def with_ids(trace_id, span_id, &block)
  OpenTelemetry::Trace.stub(:generate_trace_id, trace_id) do
    OpenTelemetry::Trace.stub(:generate_span_id, span_id, &block)
  end
end

#with_test_loggerObject



35
36
37
38
39
40
41
42
# File 'lib/opentelemetry/test_helpers.rb', line 35

def with_test_logger
  log_stream = StringIO.new
  original_logger = OpenTelemetry.logger
  OpenTelemetry.logger = ::Logger.new(log_stream)
  yield log_stream
ensure
  OpenTelemetry.logger = original_logger
end