Skip to content

Commit 321f513

Browse files
authored
Merge pull request #4031 from DataDog/ivoanjo/fix-crashtracking-pid-runtimeid-on-fork
[NO-TICKET] Fix crashtracking sending parent pid/runtime-id when child crashes
2 parents 503d1e9 + 06dfc27 commit 321f513

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

lib/datadog/core/crashtracking/component.rb

+8-5
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ def initialize(tags:, agent_base_url:, ld_library_path:, path_to_crashtracking_r
6666
def start
6767
Utils::AtForkMonkeyPatch.apply!
6868

69-
start_or_update_on_fork(action: :start)
69+
start_or_update_on_fork(action: :start, tags: tags)
70+
7071
ONLY_ONCE.run do
7172
Utils::AtForkMonkeyPatch.at_fork(:child) do
7273
# Must NOT reference `self` here, as only the first instance will
@@ -77,8 +78,10 @@ def start
7778
end
7879
end
7980

80-
def update_on_fork
81-
start_or_update_on_fork(action: :update_on_fork)
81+
def update_on_fork(settings: Datadog.configuration)
82+
# Here we pick up the latest settings, so that we pick up any tags that change after forking
83+
# such as the pid or runtime-id
84+
start_or_update_on_fork(action: :update_on_fork, tags: TagBuilder.call(settings))
8285
end
8386

8487
def stop
@@ -92,7 +95,7 @@ def stop
9295

9396
attr_reader :tags, :agent_base_url, :ld_library_path, :path_to_crashtracking_receiver_binary, :logger
9497

95-
def start_or_update_on_fork(action:)
98+
def start_or_update_on_fork(action:, tags:)
9699
self.class._native_start_or_update_on_fork(
97100
action: action,
98101
agent_base_url: agent_base_url,
@@ -101,7 +104,7 @@ def start_or_update_on_fork(action:)
101104
tags_as_array: tags.to_a,
102105
upload_timeout_seconds: 1
103106
)
104-
logger.debug("Crash tracking #{action} successfully")
107+
logger.debug("Crash tracking action: #{action} successful")
105108
rescue => e
106109
logger.error("Failed to #{action} crash tracking: #{e.message}")
107110
end

sig/datadog/core/crashtracking/component.rbs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module Datadog
2121

2222
def start: -> void
2323

24-
def update_on_fork: -> void
24+
def update_on_fork: (settings: Datadog::Core::Configuration::Settings) -> void
2525

2626
def stop: -> void
2727

@@ -33,7 +33,7 @@ module Datadog
3333
attr_reader path_to_crashtracking_receiver_binary: ::String
3434
attr_reader logger: untyped
3535

36-
def start_or_update_on_fork: (action: :start | :update_on_fork) -> void
36+
def start_or_update_on_fork: (action: :start | :update_on_fork, tags: ::Hash[::String, ::String]) -> void
3737

3838
def self._native_start_or_update_on_fork: (
3939
action: :start | :update_on_fork,

spec/datadog/core/crashtracking/component_spec.rb

+19-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
require 'fiddle'
66

77
RSpec.describe Datadog::Core::Crashtracking::Component, skip: !CrashtrackingHelpers.supported? do
8+
let(:logger) { Logger.new($stdout) }
9+
810
describe '.build' do
911
let(:settings) { Datadog::Core::Configuration::Settings.new }
1012
let(:agent_settings) { double('agent_settings') }
11-
let(:logger) { Logger.new($stdout) }
1213
let(:tags) { { 'tag1' => 'value1' } }
1314
let(:agent_base_url) { 'agent_base_url' }
1415
let(:ld_library_path) { 'ld_library_path' }
@@ -100,7 +101,6 @@
100101
describe '#start' do
101102
context 'when _native_start_or_update_on_fork raises an exception' do
102103
it 'logs the exception' do
103-
logger = Logger.new($stdout)
104104
crashtracker = build_crashtracker(logger: logger)
105105

106106
expect(described_class).to receive(:_native_start_or_update_on_fork) { raise 'Test failure' }
@@ -114,7 +114,6 @@
114114
describe '#stop' do
115115
context 'when _native_stop_crashtracker raises an exception' do
116116
it 'logs the exception' do
117-
logger = Logger.new($stdout)
118117
crashtracker = build_crashtracker(logger: logger)
119118

120119
expect(described_class).to receive(:_native_stop) { raise 'Test failure' }
@@ -126,9 +125,10 @@
126125
end
127126

128127
describe '#update_on_fork' do
128+
before { allow(logger).to receive(:debug) }
129+
129130
context 'when _native_stop_crashtracker raises an exception' do
130131
it 'logs the exception' do
131-
logger = Logger.new($stdout)
132132
crashtracker = build_crashtracker(logger: logger)
133133

134134
expect(described_class).to receive(:_native_start_or_update_on_fork) { raise 'Test failure' }
@@ -138,12 +138,25 @@
138138
end
139139
end
140140

141-
it 'update_on_fork the crash tracker' do
141+
it 'updates the crash tracker' do
142142
expect(described_class).to receive(:_native_start_or_update_on_fork).with(
143143
hash_including(action: :update_on_fork)
144144
)
145145

146-
crashtracker = build_crashtracker
146+
crashtracker = build_crashtracker(logger: logger)
147+
148+
crashtracker.update_on_fork
149+
end
150+
151+
it 'refreshes the latest settings' do
152+
allow(Datadog).to receive(:configuration).and_return(:latest_settings)
153+
allow(Datadog::Core::Crashtracking::TagBuilder).to receive(:call).with(:latest_settings).and_return([:latest_tags])
154+
155+
expect(described_class).to receive(:_native_start_or_update_on_fork).with(
156+
hash_including(tags_as_array: [:latest_tags])
157+
)
158+
159+
crashtracker = build_crashtracker(logger: logger)
147160

148161
crashtracker.update_on_fork
149162
end

0 commit comments

Comments
 (0)