Skip to content
File

Blob: src/node/trace_events.ts

typescript90 lines
1// Copyright (c) 2017-2022 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4// Copyright Joyent, Inc. and other Node contributors.
5//
6// Permission is hereby granted, free of charge, to any person obtaining a
7// copy of this software and associated documentation files (the
8// "Software"), to deal in the Software without restriction, including
9// without limitation the rights to use, copy, modify, merge, publish,
10// distribute, sublicense, and/or sell copies of the Software, and to permit
11// persons to whom the Software is furnished to do so, subject to the
12// following conditions:
13//
14// The above copyright notice and this permission notice shall be included
15// in all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
20// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23// USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25import type * as TraceEvents from 'node:trace_events';
26import { ERR_TRACE_EVENTS_CATEGORY_REQUIRED } from 'node-internal:internal_errors';
27import { validateObject, validateStringArray } from 'node-internal:validators';
28import { format, customInspectSymbol } from 'node-internal:internal_inspect';
29 
30// TODO(soon): It is conceivable that we might implement this as part of
31// the workers observability features in the future. For now it's a non
32// functional stub.
33 
34class Tracing implements TraceEvents.Tracing {
35 #categories: string[];
36 #enabled = false;
37 
38 constructor(categories: string[]) {
39 this.#categories = categories;
40 }
41 
42 enable(): void {
43 this.#enabled = true;
44 // non-op
45 }
46 
47 disable(): void {
48 this.#enabled = false;
49 // non-op
50 }
51 
52 get enabled(): boolean {
53 return this.#enabled;
54 }
55 
56 get categories(): string {
57 return this.#categories.join(',');
58 }
59 
60 [customInspectSymbol](depth?: number, _: object = {}): string | object {
61 if (typeof depth === 'number' && depth < 0) return this;
62 
63 const obj = {
64 enabled: this.enabled,
65 categories: this.categories,
66 };
67 return `Tracing ${format(obj)}`;
68 }
69}
70 
71export function createTracing(
72 options: TraceEvents.CreateTracingOptions
73): Tracing {
74 validateObject(options, 'options');
75 validateStringArray(options.categories, 'options.categories');
76 if (options.categories.length <= 0) {
77 throw new ERR_TRACE_EVENTS_CATEGORY_REQUIRED();
78 }
79 return new Tracing(options.categories);
80}
81 
82export function getEnabledCategories(): string | undefined {
83 return undefined;
84}
85 
86export default {
87 createTracing,
88 getEnabledCategories,
89} satisfies typeof TraceEvents;