Skip to content

Commit dbb9249

Browse files
motiz88facebook-github-bot
authored andcommitted
Support recording request initiator call stacks in NetworkHandler (facebook#54050)
Summary: Changelog: [Internal] A naive approach to keeping track of request initiators inside the `NetworkHandler` singleton for CDP reporting purposes: 1. Expose a new `recordRequestInitiatorStack` method. 2. Keep the CDP-formatted stack trace (as a `folly::dynamic`) in a map keyed by request ID. 3. Destructively consume the stack trace during `onRequestWillBeSent`. Differential Revision: D83754143
1 parent 7cce17f commit dbb9249

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

packages/react-native/ReactCommon/jsinspector-modern/network/NetworkHandler.cpp

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ void NetworkHandler::onRequestWillBeSent(
6969
}
7070

7171
double timestamp = getCurrentUnixTimestampSeconds();
72+
std::optional<folly::dynamic> initiator;
73+
initiator = consumeStoredRequestInitiator(requestId);
7274
auto params = cdp::network::RequestWillBeSentParams{
7375
.requestId = requestId,
7476
.loaderId = "",
@@ -79,7 +81,9 @@ void NetworkHandler::onRequestWillBeSent(
7981
// Unix epoch for both.
8082
.timestamp = timestamp,
8183
.wallTime = timestamp,
82-
.initiator = folly::dynamic::object("type", "script"),
84+
.initiator = initiator.has_value()
85+
? std::move(initiator.value())
86+
: folly::dynamic::object("type", "script"),
8387
.redirectHasExtraInfo = redirectResponse.has_value(),
8488
.redirectResponse = redirectResponse,
8589
};
@@ -114,7 +118,7 @@ void NetworkHandler::onResponseReceived(
114118

115119
auto resourceType = cdp::network::resourceTypeFromMimeType(response.mimeType);
116120
{
117-
std::lock_guard<std::mutex> lock(resourceTypeMapMutex_);
121+
std::lock_guard<std::mutex> lock(requestMetadataMutex_);
118122
resourceTypeMap_.emplace(requestId, resourceType);
119123
}
120124

@@ -175,7 +179,7 @@ void NetworkHandler::onLoadingFailed(
175179
}
176180

177181
{
178-
std::lock_guard<std::mutex> lock(resourceTypeMapMutex_);
182+
std::lock_guard<std::mutex> lock(requestMetadataMutex_);
179183
auto params = cdp::network::LoadingFailedParams{
180184
.requestId = requestId,
181185
.timestamp = getCurrentUnixTimestampSeconds(),
@@ -212,4 +216,30 @@ std::optional<std::tuple<std::string, bool>> NetworkHandler::getResponseBody(
212216
responseBody->data, responseBody->base64Encoded);
213217
}
214218

219+
void NetworkHandler::recordRequestInitiatorStack(
220+
const std::string& requestId,
221+
folly::dynamic stackTrace) {
222+
if (!isEnabledNoSync()) {
223+
return;
224+
}
225+
226+
std::lock_guard<std::mutex> lock(requestMetadataMutex_);
227+
requestInitiatorById_.emplace(
228+
requestId,
229+
folly::dynamic::object("type", "script")("stack", std::move(stackTrace)));
230+
}
231+
232+
std::optional<folly::dynamic> NetworkHandler::consumeStoredRequestInitiator(
233+
const std::string& requestId) {
234+
std::lock_guard<std::mutex> lock(requestMetadataMutex_);
235+
auto it = requestInitiatorById_.find(requestId);
236+
if (it == requestInitiatorById_.end()) {
237+
return std::nullopt;
238+
}
239+
// Remove and return
240+
auto result = std::move(it->second);
241+
requestInitiatorById_.erase(it);
242+
return result;
243+
}
244+
215245
} // namespace facebook::react::jsinspector_modern

packages/react-native/ReactCommon/jsinspector-modern/network/NetworkHandler.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ class NetworkHandler {
124124
std::optional<std::tuple<std::string, bool>> getResponseBody(
125125
const std::string& requestId);
126126

127+
/**
128+
* Associate the given stack trace with the given request ID.
129+
*/
130+
void recordRequestInitiatorStack(
131+
const std::string& requestId,
132+
folly::dynamic stackTrace);
133+
127134
private:
128135
NetworkHandler() = default;
129136
NetworkHandler(const NetworkHandler&) = delete;
@@ -136,10 +143,14 @@ class NetworkHandler {
136143
return enabled_.load(std::memory_order_relaxed);
137144
}
138145

146+
std::optional<folly::dynamic> consumeStoredRequestInitiator(
147+
const std::string& requestId);
148+
139149
FrontendChannel frontendChannel_;
140150

141151
std::map<std::string, std::string> resourceTypeMap_{};
142-
std::mutex resourceTypeMapMutex_{};
152+
std::map<std::string, folly::dynamic> requestInitiatorById_{};
153+
std::mutex requestMetadataMutex_{};
143154

144155
BoundedRequestBuffer responseBodyBuffer_{};
145156
std::mutex requestBodyMutex_;

0 commit comments

Comments
 (0)