|
| 1 | +# This file is distributed under New Relic's license terms. |
| 2 | +# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. |
| 3 | +# frozen_string_literal: true |
| 4 | + |
| 5 | +module NewRelic |
| 6 | + module Agent |
| 7 | + module OpenTelemetry |
| 8 | + class AbstractSegmentPatchTest < Minitest::Test |
| 9 | + def setup |
| 10 | + harvest_transaction_events! |
| 11 | + harvest_span_events! |
| 12 | + end |
| 13 | + |
| 14 | + def test_force_finish_with_otel_span_that_cannot_finish_segment |
| 15 | + in_transaction do |txn| |
| 16 | + txn.stubs(:sampled?).returns(true) |
| 17 | + segment = Tracer.start_segment(name: 'test_segment') |
| 18 | + otel_span = segment.instance_variable_get(:@otel_span) |
| 19 | + |
| 20 | + otel_span.stubs(:instance_variable_get).with(:@finished).returns(false) |
| 21 | + otel_span.stubs(:finish).returns(nil) |
| 22 | + |
| 23 | + segment.force_finish |
| 24 | + |
| 25 | + assert_predicate segment, :finished? |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def test_force_finish_with_successful_otel_span_finish |
| 30 | + in_transaction do |txn| |
| 31 | + txn.stubs(:sampled?).returns(true) |
| 32 | + segment = Tracer.start_segment(name: 'test_segment') |
| 33 | + otel_span = segment.instance_variable_get(:@otel_span) |
| 34 | + |
| 35 | + otel_span.stubs(:instance_variable_get).with(:@finished).returns(false) |
| 36 | + otel_span.stubs(:finish) do |
| 37 | + segment.finish |
| 38 | + end |
| 39 | + |
| 40 | + refute_predicate segment, :finished?, 'Segment should start unfinished' |
| 41 | + |
| 42 | + segment.force_finish |
| 43 | + |
| 44 | + assert_predicate segment, :finished?, 'Segment should be finished by span.finish' |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + def test_force_finish_handles_otel_span_exceptions_gracefully |
| 49 | + in_transaction do |txn| |
| 50 | + txn.stubs(:sampled?).returns(true) |
| 51 | + segment = Tracer.start_segment(name: 'test_segment') |
| 52 | + otel_span = segment.instance_variable_get(:@otel_span) |
| 53 | + |
| 54 | + otel_span.stubs(:instance_variable_get).with(:@finished).returns(false) |
| 55 | + otel_span.stubs(:finish).raises(StandardError.new('Test exception')) |
| 56 | + |
| 57 | + logger_mock = mock() |
| 58 | + logger_mock.expects(:debug).with(regexp_matches(/Error finishing OpenTelemetry span during force_finish.*Test exception/)) |
| 59 | + NewRelic::Agent.stubs(:logger).returns(logger_mock) |
| 60 | + |
| 61 | + segment.force_finish |
| 62 | + |
| 63 | + assert_predicate segment, :finished?, 'Segment should still be finished via fallback after exception' |
| 64 | + end |
| 65 | + end |
| 66 | + end |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments