Skip to content

Encapsulate TracingAgent #46986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 6 additions & 40 deletions packages/react-native/ReactCommon/jsinspector-modern/HostAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

#include <chrono>

#include <reactperflogger/fusebox/FuseboxTracer.h>

using namespace std::chrono;
using namespace std::literals::string_view_literals;

Expand All @@ -36,7 +34,8 @@ HostAgent::HostAgent(
targetController_(targetController),
hostMetadata_(std::move(hostMetadata)),
sessionState_(sessionState),
networkIOAgent_(NetworkIOAgent(frontendChannel, executor)) {}
networkIOAgent_(NetworkIOAgent(frontendChannel, std::move(executor))),
tracingAgent_(TracingAgent(frontendChannel)) {}

void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {
bool shouldSendOKResponse = false;
Expand Down Expand Up @@ -149,50 +148,17 @@ void HostAgent::handleRequest(const cdp::PreparsedRequest& req) {

shouldSendOKResponse = true;
isFinishedHandlingRequest = true;
} else if (req.method == "Tracing.start") {
// @cdp Tracing.start support is experimental.
if (FuseboxTracer::getFuseboxTracer().startTracing()) {
shouldSendOKResponse = true;
} else {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session already started"));
return;
}
isFinishedHandlingRequest = true;
} else if (req.method == "Tracing.end") {
// @cdp Tracing.end support is experimental.
bool firstChunk = true;
auto id = req.id;
bool wasStopped = FuseboxTracer::getFuseboxTracer().stopTracing(
[this, firstChunk, id](const folly::dynamic& eventsChunk) {
if (firstChunk) {
frontendChannel_(cdp::jsonResult(id));
}
frontendChannel_(cdp::jsonNotification(
"Tracing.dataCollected",
folly::dynamic::object("value", eventsChunk)));
});
if (!wasStopped) {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session not started"));
return;
}
frontendChannel_(cdp::jsonNotification(
"Tracing.tracingComplete",
folly::dynamic::object("dataLossOccurred", false)));
shouldSendOKResponse = true;
isFinishedHandlingRequest = true;
}

if (!isFinishedHandlingRequest &&
networkIOAgent_.handleRequest(req, targetController_.getDelegate())) {
return;
}

if (!isFinishedHandlingRequest && tracingAgent_.handleRequest(req)) {
return;
}

if (!isFinishedHandlingRequest && instanceAgent_ &&
instanceAgent_->handleRequest(req)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "HostTarget.h"
#include "NetworkIOAgent.h"
#include "SessionState.h"
#include "TracingAgent.h"

#include <jsinspector-modern/InspectorInterfaces.h>
#include <jsinspector-modern/InstanceAgent.h>
Expand Down Expand Up @@ -110,6 +111,8 @@ class HostAgent final {
SessionState& sessionState_;

NetworkIOAgent networkIOAgent_;

TracingAgent tracingAgent_;
};

} // namespace facebook::react::jsinspector_modern
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "TracingAgent.h"

#include <reactperflogger/fusebox/FuseboxTracer.h>

namespace facebook::react::jsinspector_modern {

bool TracingAgent::handleRequest(const cdp::PreparsedRequest& req) {
if (req.method == "Tracing.start") {
// @cdp Tracing.start support is experimental.
if (FuseboxTracer::getFuseboxTracer().startTracing()) {
frontendChannel_(cdp::jsonResult(req.id));
} else {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session already started"));
}

return true;
} else if (req.method == "Tracing.end") {
// @cdp Tracing.end support is experimental.
bool firstChunk = true;
auto id = req.id;
bool wasStopped = FuseboxTracer::getFuseboxTracer().stopTracing(
[this, firstChunk, id](const folly::dynamic& eventsChunk) {
if (firstChunk) {
frontendChannel_(cdp::jsonResult(id));
}
frontendChannel_(cdp::jsonNotification(
"Tracing.dataCollected",
folly::dynamic::object("value", eventsChunk)));
});

if (!wasStopped) {
frontendChannel_(cdp::jsonError(
req.id,
cdp::ErrorCode::InternalError,
"Tracing session not started"));
} else {
frontendChannel_(cdp::jsonNotification(
"Tracing.tracingComplete",
folly::dynamic::object("dataLossOccurred", false)));
}

frontendChannel_(cdp::jsonResult(req.id));
return true;
}

return false;
}

} // namespace facebook::react::jsinspector_modern
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include "CdpJson.h"
#include "InspectorInterfaces.h"

namespace facebook::react::jsinspector_modern {

/**
* Provides an agent for handling CDP's Tracing.start, Tracing.stop.
*/
class TracingAgent {
public:
/**
* \param frontendChannel A channel used to send responses to the
* frontend.
*/
explicit TracingAgent(FrontendChannel frontendChannel)
: frontendChannel_(std::move(frontendChannel)) {}

/**
* Handle a CDP request. The response will be sent over the provided
* \c FrontendChannel synchronously or asynchronously.
* \param req The parsed request.
*/
bool handleRequest(const cdp::PreparsedRequest& req);

private:
/**
* A channel used to send responses and events to the frontend.
*/
FrontendChannel frontendChannel_;
};

} // namespace facebook::react::jsinspector_modern
Loading