Skip to content
Open
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
3 changes: 2 additions & 1 deletion doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -3381,7 +3381,8 @@ category.
### `ERR_TRACE_EVENTS_UNAVAILABLE`

The `node:trace_events` module could not be loaded because Node.js was compiled
with the `--without-v8-platform` flag.
with the `--without-v8-platform` flag, or because the process was initialized by
an embedder that provides its own V8 platform.

<a id="ERR_TRAILING_JUNK_AFTER_STREAM_END"></a>

Expand Down
8 changes: 6 additions & 2 deletions lib/trace_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ const {
} = require('internal/errors').codes;

const { ownsProcessState } = require('internal/worker');
if (!hasTracing || !ownsProcessState)
const {
CategorySet,
getEnabledCategories,
hasAgent,
} = internalBinding('trace_events');
if (!hasTracing || !ownsProcessState || !hasAgent())
throw new ERR_TRACE_EVENTS_UNAVAILABLE();

const { CategorySet, getEnabledCategories } = internalBinding('trace_events');
const { customInspectSymbol } = require('internal/util');
const { format } = require('internal/util/inspect');
const {
Expand Down
6 changes: 6 additions & 0 deletions src/node_trace_events.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ void NodeCategorySet::Disable(const FunctionCallbackInfo<Value>& args) {
}
}

static void HasAgent(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(tracing::Agent::GetInstance() != nullptr);
}

void GetEnabledCategories(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
std::string categories =
Expand Down Expand Up @@ -162,6 +166,7 @@ void NodeCategorySet::Initialize(Local<Object> target,
Environment* env = Environment::GetCurrent(context);
Isolate* isolate = env->isolate();

SetMethod(context, target, "hasAgent", HasAgent);
SetMethod(context, target, "getEnabledCategories", GetEnabledCategories);
SetMethod(context,
target,
Expand Down Expand Up @@ -203,6 +208,7 @@ void NodeCategorySet::Initialize(Local<Object> target,

void NodeCategorySet::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(HasAgent);
registry->Register(GetEnabledCategories);
registry->Register(SetTraceCategoryStateUpdateHandler);
registry->Register(GetCategoryEnabledBuffer);
Expand Down
18 changes: 18 additions & 0 deletions test/embedding/test-embedding-trace-events-unavailable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

// The embedtest binary runs on its own MultiIsolatePlatform without Node's
// tracing agent, so node:trace_events must report itself as unavailable.

const common = require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');

spawnSyncAndAssert(
common.resolveBuiltBinary('embedtest'),
[
'try { require("node:trace_events").createTracing({ categories: ["v8"] }); }' +
'catch (e) { console.log(e.code); }',
],
{
trim: true,
stdout: 'ERR_TRACE_EVENTS_UNAVAILABLE',
});
Loading