Skip to content

Commit d49eaa8

Browse files
authored
fix: Fix diagnostic logging for connection results in stream (#343)
1 parent 9ca4b98 commit d49eaa8

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

.github/actions/setup/action.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ runs:
1515
- uses: ruby/setup-ruby@v1
1616
with:
1717
ruby-version: ${{ inputs.version }}
18+
bundler: 2
1819

1920
- name: Install dependencies
2021
if: ${{ inputs.install-dependencies == 'true' }}

lib/ldclient-rb/events.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class EventDispatcher
231231
def initialize(inbox, sdk_key, config, diagnostic_accumulator, event_sender)
232232
@sdk_key = sdk_key
233233
@config = config
234-
@diagnostic_accumulator = config.diagnostic_opt_out? ? nil : diagnostic_accumulator
234+
@diagnostic_accumulator = diagnostic_accumulator
235235
@event_sender = event_sender
236236
@sampler = LaunchDarkly::Impl::Sampler.new(Random.new)
237237

lib/ldclient-rb/stream.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class StreamProcessor
2525
def initialize(sdk_key, config, diagnostic_accumulator = nil)
2626
@sdk_key = sdk_key
2727
@config = config
28+
@diagnostic_accumulator = diagnostic_accumulator
2829
@data_source_update_sink = config.data_source_update_sink
2930
@feature_store = config.feature_store
3031
@initialized = Concurrent::AtomicBoolean.new(false)

spec/stream_spec.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,71 @@ module LaunchDarkly
6666
expect(listener.statuses[1].last_error.kind).to eq(Interfaces::DataSource::ErrorInfo::INVALID_DATA)
6767
end
6868
end
69+
70+
describe '#log_connection_result' do
71+
it "logs successful connection when diagnostic_accumulator is provided" do
72+
diagnostic_accumulator = double("DiagnosticAccumulator")
73+
expect(diagnostic_accumulator).to receive(:record_stream_init).with(
74+
kind_of(Integer),
75+
false,
76+
kind_of(Integer)
77+
)
78+
79+
processor = subject.new("sdk_key", config, diagnostic_accumulator)
80+
processor.send(:log_connection_started)
81+
processor.send(:log_connection_result, true)
82+
end
83+
84+
it "logs failed connection when diagnostic_accumulator is provided" do
85+
diagnostic_accumulator = double("DiagnosticAccumulator")
86+
expect(diagnostic_accumulator).to receive(:record_stream_init).with(
87+
kind_of(Integer),
88+
true,
89+
kind_of(Integer)
90+
)
91+
92+
processor = subject.new("sdk_key", config, diagnostic_accumulator)
93+
processor.send(:log_connection_started)
94+
processor.send(:log_connection_result, false)
95+
end
96+
97+
it "logs connection metrics with correct timestamp and duration" do
98+
diagnostic_accumulator = double("DiagnosticAccumulator")
99+
100+
processor = subject.new("sdk_key", config, diagnostic_accumulator)
101+
102+
expect(diagnostic_accumulator).to receive(:record_stream_init) do |timestamp, failed, duration|
103+
expect(timestamp).to be_a(Integer)
104+
expect(timestamp).to be > 0
105+
expect(failed).to eq(false)
106+
expect(duration).to be_a(Integer)
107+
expect(duration).to be >= 0
108+
end
109+
110+
processor.send(:log_connection_started)
111+
sleep(0.01) # Small delay to ensure measurable duration
112+
processor.send(:log_connection_result, true)
113+
end
114+
115+
it "only logs once per connection attempt" do
116+
diagnostic_accumulator = double("DiagnosticAccumulator")
117+
expect(diagnostic_accumulator).to receive(:record_stream_init).once
118+
119+
processor = subject.new("sdk_key", config, diagnostic_accumulator)
120+
processor.send(:log_connection_started)
121+
processor.send(:log_connection_result, true)
122+
# Second call should not trigger another log
123+
processor.send(:log_connection_result, true)
124+
end
125+
126+
it "works gracefully when no diagnostic_accumulator is provided" do
127+
processor = subject.new("sdk_key", config, nil)
128+
129+
expect {
130+
processor.send(:log_connection_started)
131+
processor.send(:log_connection_result, true)
132+
}.not_to raise_error
133+
end
134+
end
69135
end
70136
end

0 commit comments

Comments
 (0)