|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe ApplicationCable::Channel do |
| 6 | + include AuthenticationHelpers |
| 7 | + include ActionCable::Channel::TestCase::Behavior |
| 8 | + |
| 9 | + include_context 'with graphql subscription support' |
| 10 | + |
| 11 | + tests GraphqlChannel |
| 12 | + |
| 13 | + let(:exporter) { OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new } |
| 14 | + let(:span_processor) { OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(exporter) } |
| 15 | + let(:tracer_provider) do |
| 16 | + OpenTelemetry::SDK::Trace::TracerProvider.new.tap do |tp| |
| 17 | + tp.add_span_processor(span_processor) |
| 18 | + end |
| 19 | + end |
| 20 | + let(:tracer) { tracer_provider.tracer('sagittarius-cable') } |
| 21 | + |
| 22 | + let(:user) { create(:user) } |
| 23 | + let(:token) { "Session #{authorization_token(user)}" } |
| 24 | + |
| 25 | + before do |
| 26 | + propagator = OpenTelemetry::Trace::Propagation::TraceContext::TextMapPropagator.new |
| 27 | + allow(OpenTelemetry).to receive(:propagation).and_return(propagator) |
| 28 | + allow(described_class).to receive(:tracer).and_return(tracer) |
| 29 | + allow(GraphqlChannel).to receive(:tracer).and_return(tracer) |
| 30 | + end |
| 31 | + |
| 32 | + describe '#subscribe_to_channel' do |
| 33 | + it 'creates a span with correct name' do |
| 34 | + stub_connection(otel_context: OpenTelemetry::Context.current) |
| 35 | + subscribe(token: token) |
| 36 | + |
| 37 | + span = exporter.finished_spans.find { |s| s.name.include?('subscribe') } |
| 38 | + expect(span.name).to eq('GraphqlChannel subscribe') |
| 39 | + end |
| 40 | + |
| 41 | + it 'sets messaging attributes' do |
| 42 | + stub_connection(otel_context: OpenTelemetry::Context.current) |
| 43 | + subscribe(token: token) |
| 44 | + |
| 45 | + span = exporter.finished_spans.find { |s| s.name.include?('subscribe') } |
| 46 | + expect(span.attributes['messaging.system']).to eq('action_cable') |
| 47 | + expect(span.attributes['messaging.operation']).to eq('subscribe') |
| 48 | + expect(span.attributes['code.namespace']).to eq('GraphqlChannel') |
| 49 | + end |
| 50 | + |
| 51 | + context 'when connection has a trace context' do |
| 52 | + let(:parent_span_context) do |
| 53 | + OpenTelemetry::Trace::SpanContext.new( |
| 54 | + trace_id: OpenTelemetry::Trace.generate_trace_id, |
| 55 | + span_id: OpenTelemetry::Trace.generate_span_id, |
| 56 | + trace_flags: OpenTelemetry::Trace::TraceFlags::SAMPLED |
| 57 | + ) |
| 58 | + end |
| 59 | + let(:parent_context) do |
| 60 | + OpenTelemetry::Trace.context_with_span( |
| 61 | + OpenTelemetry::Trace.non_recording_span(parent_span_context) |
| 62 | + ) |
| 63 | + end |
| 64 | + |
| 65 | + it 'creates spans as children of the connection context' do |
| 66 | + stub_connection(otel_context: parent_context) |
| 67 | + subscribe(token: token) |
| 68 | + |
| 69 | + span = exporter.finished_spans.find { |s| s.name.include?('subscribe') } |
| 70 | + expect(span.hex_trace_id).to eq(parent_span_context.hex_trace_id) |
| 71 | + expect(span.hex_parent_span_id).to eq(parent_span_context.hex_span_id) |
| 72 | + end |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + describe '#perform_action' do |
| 77 | + before do |
| 78 | + stub_connection(otel_context: OpenTelemetry::Context.current) |
| 79 | + subscribe(token: token) |
| 80 | + end |
| 81 | + |
| 82 | + it 'creates a span with the action name' do |
| 83 | + perform :execute, |
| 84 | + query: 'subscription($message: String) { echo(message: $message) { message } }', |
| 85 | + variables: { message: 'hello' } |
| 86 | + |
| 87 | + span = exporter.finished_spans.find { |s| s.name == 'GraphqlChannel execute' } |
| 88 | + expect(span).not_to be_nil |
| 89 | + end |
| 90 | + |
| 91 | + it 'sets messaging attributes' do |
| 92 | + perform :execute, |
| 93 | + query: 'subscription($message: String) { echo(message: $message) { message } }', |
| 94 | + variables: { message: 'hello' } |
| 95 | + |
| 96 | + span = exporter.finished_spans.find { |s| s.name == 'GraphqlChannel execute' } |
| 97 | + expect(span).not_to be_nil |
| 98 | + expect(span.attributes['messaging.system']).to eq('action_cable') |
| 99 | + expect(span.attributes['code.namespace']).to eq('GraphqlChannel') |
| 100 | + end |
| 101 | + end |
| 102 | +end |
0 commit comments