|
1 | 1 | # `@apm-js-collab/code-transformer` |
2 | 2 |
|
3 | | -This is a temporary fork of [`DataDog/orchestrion-js`](https://github.com/DataDog/orchestrion-js/). We intend all changes to be upstreamed to the original repository, |
| 3 | +This is a fork of |
| 4 | +[`DataDog/orchestrion-js`](https://github.com/DataDog/orchestrion-js/). |
4 | 5 |
|
5 | | -This is a library for instrumenting Node.js libraries at build or load time. |
| 6 | +This is a library to aid in instrumenting Node.js libraries at build or load |
| 7 | +time. |
6 | 8 |
|
7 | | -It provides `VisitMut` implementations for SWC's AST nodes, which can be used to insert tracing code into matching functions. |
8 | | -It can be used in SWC plugins, or anything else that mutates JavaScript ASTs using SWC. |
| 9 | +It uses SWC's Rust AST walker to inject code that calls Node.js |
| 10 | +[`TracingChannel`](https://nodejs.org/api/diagnostics_channel.html#class-tracingchannel). |
9 | 11 |
|
10 | | -`@apm-js-collab/code-transformer` is built as a JavaScript module, which can be used from Node.js. |
| 12 | +You likely don't want to use this library directly; instead, consider using: |
| 13 | + |
| 14 | +- [`@apm-js-collab/tracing-hooks/`](https://github.com/apm-js-collab/tracing-hooks/) |
| 15 | + - ESM and `require` hooks to instrument modules as they are loaded. |
| 16 | +- [`apm-js-collab/code-transformer-bundler-plugins`](https://github.com/apm-js-collab/code-transformer-bundler-plugins) |
| 17 | + - Bundler plugins for webpack, Vite, Rollup and esbuild to instrument modules |
| 18 | + at build time. |
| 19 | + |
| 20 | +## JavaScript |
| 21 | + |
| 22 | +`@apm-js-collab/code-transformer` exposes the Rust library as a WebAssembly |
| 23 | +module. |
| 24 | + |
| 25 | +### Building |
11 | 26 |
|
12 | 27 | To build the JavaScript module: |
| 28 | + |
13 | 29 | - Ensure you have [Rust installed](https://www.rust-lang.org/tools/install) |
14 | | -- Install the wasm toolchain `rustup target add wasm32-unknown-unknown --toolchain stable` |
15 | | -- Install dependencies and build the module `npm install && npm run build` |
| 30 | +- Install the wasm toolchain\ |
| 31 | + `rustup target add wasm32-unknown-unknown --toolchain stable` |
| 32 | +- Install dependencies and build the module\ |
| 33 | + `npm install && npm run build` |
| 34 | + |
| 35 | +### Usage |
| 36 | + |
| 37 | +```javascript |
| 38 | +import * as codeTransformer from "@apm-js-collab/code-transformer"; |
| 39 | + |
| 40 | +// The full instrumentation config |
| 41 | +const instrumentation = { |
| 42 | + // The name of the diagnostics channel |
| 43 | + channelName: "my-channel", |
| 44 | + // Define the module you'd like to inject tracing channels into |
| 45 | + module: { |
| 46 | + name: "my-module", |
| 47 | + versionRange: ">=1.0.0", |
| 48 | + filePath: "./dist/index.js", |
| 49 | + }, |
| 50 | + // Define the function you'd like to instrument |
| 51 | + // (e.g., match a method named 'foo' that returns a Promise) |
| 52 | + functionQuery: { |
| 53 | + methodName: "fetch", |
| 54 | + kind: "Async", |
| 55 | + }, |
| 56 | +}; |
| 57 | + |
| 58 | +// Create an InstrumentationMatcher with an array of instrumentation configs |
| 59 | +const matcher = codeTransformer.create([instrumentation]); |
| 60 | + |
| 61 | +// Get a transformer for a specific module |
| 62 | +const transformer = matcher.getTransformer( |
| 63 | + "my-module", |
| 64 | + "1.2.3", |
| 65 | + "./dist/index.js", |
| 66 | +); |
| 67 | + |
| 68 | +if (transformer === undefined) { |
| 69 | + throw new Error("No transformer found for module"); |
| 70 | +} |
| 71 | + |
| 72 | +// Transform code |
| 73 | +const inputCode = "async function fetch() { return 42; }"; |
| 74 | +const result = transformer.transform(inputCode, "unknown"); |
| 75 | +console.log(result.code); |
| 76 | + |
| 77 | +// Both the matcher and transformer should be freed after use! |
| 78 | +matcher.free(); |
| 79 | +transformer.free(); |
| 80 | +``` |
| 81 | + |
| 82 | +### API Reference |
| 83 | + |
| 84 | +```ts |
| 85 | +type ModuleType = "esm" | "cjs" | "unknown"; |
| 86 | +type FunctionKind = "Sync" | "Async"; |
| 87 | +``` |
| 88 | + |
| 89 | +#### **`FunctionQuery` Variants** |
| 90 | + |
| 91 | +```ts |
| 92 | +type FunctionQuery = |
| 93 | + | // Match class constructor |
| 94 | + { className: string; index?: number } |
| 95 | + | // Match class method |
| 96 | + { |
| 97 | + className: string; |
| 98 | + methodName: string; |
| 99 | + kind: FunctionKind; |
| 100 | + index?: number; |
| 101 | + } |
| 102 | + | // Match method on objects |
| 103 | + { methodName: string; kind: FunctionKind; index?: number } |
| 104 | + | // Match standalone function |
| 105 | + { functionName: string; kind: FunctionKind; index?: number } |
| 106 | + | // Match arrow function or function expression |
| 107 | + { expressionName: string; kind: FunctionKind; index?: number }; |
| 108 | +``` |
| 109 | + |
| 110 | +#### **`ModuleMatcher`** |
| 111 | + |
| 112 | +```ts |
| 113 | +type ModuleMatcher = { |
| 114 | + name: string; // Module name |
| 115 | + versionRange: string; // Matching semver range |
| 116 | + filePath: string; // Path to the file from the module root |
| 117 | +}; |
| 118 | +``` |
| 119 | + |
| 120 | +#### **`InstrumentationConfig`** |
| 121 | + |
| 122 | +```ts |
| 123 | +type InstrumentationConfig = { |
| 124 | + channelName: string; // Name of the diagnostics channel |
| 125 | + module: ModuleMatcher; |
| 126 | + functionQuery: FunctionQuery; |
| 127 | +}; |
| 128 | +``` |
| 129 | + |
| 130 | +### Functions |
| 131 | + |
| 132 | +```ts |
| 133 | +create(configs: InstrumentationConfig[], dc_module?: string | null): InstrumentationMatcher; |
| 134 | +``` |
| 135 | + |
| 136 | +Create a matcher for one or more instrumentation configurations. |
| 137 | + |
| 138 | +- `configs` - Array of instrumentation configurations. |
| 139 | +- `dc_module` - Optional module to import `diagnostics_channel` API from. |
| 140 | + |
| 141 | +#### **`InstrumentationMatcher`** |
| 142 | + |
| 143 | +```ts |
| 144 | +getTransformer(module_name: string, version: string, file_path: string): Transformer | undefined; |
| 145 | +``` |
| 146 | + |
| 147 | +Gets a transformer for a specific module and file. |
| 148 | + |
| 149 | +Returns a `Transformer` for the given module, or `undefined` if there were no |
| 150 | +matching instrumentation configurations. |
| 151 | + |
| 152 | +- `module_name` - Name of the module. |
| 153 | +- `version` - Version of the module. |
| 154 | +- `file_path` - Path to the file from the module root. |
| 155 | + |
| 156 | +```ts |
| 157 | +free(): void; |
| 158 | +``` |
| 159 | + |
| 160 | +Free the matcher memory when it's no longer needed. |
| 161 | + |
| 162 | +#### **`Transformer`** |
| 163 | + |
| 164 | +```ts |
| 165 | +transform(code: string, module_type: ModuleType, sourcemap?: string | undefined): TransformOutput; |
| 166 | +``` |
| 167 | + |
| 168 | +Transforms the code, injecting tracing as configured. |
| 169 | + |
| 170 | +Returns `{ code, map }`. `map` will be undefined if no sourcemap was supplied. |
| 171 | + |
| 172 | +- `code` - The JavaScript/TypeScript code to transform. |
| 173 | +- `module_type` - The type of module being transformed. |
| 174 | +- `sourcemap` - Optional existing source map for the code. |
16 | 175 |
|
17 | | -## Contributing |
| 176 | +```ts |
| 177 | +free(): void; |
| 178 | +``` |
18 | 179 |
|
19 | | -See CONTRIBUTING.md |
| 180 | +Free the transformer memory when it's no longer needed. |
20 | 181 |
|
21 | 182 | ## License |
22 | 183 |
|
|
0 commit comments