opentelemetry-instrumentation-commander 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 k65miyazakiy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # OpenTelemetry Instrumentation for Commander.js
2
+
3
+ Automatic instrumentation for [Commander.js](https://github.com/tj/commander.js) CLI framework using OpenTelemetry.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install opentelemetry-instrumentation-commander
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
15
+ import { registerInstrumentations } from '@opentelemetry/instrumentation';
16
+ import { CommanderInstrumentation } from 'opentelemetry-instrumentation-commander';
17
+
18
+ const provider = new NodeTracerProvider();
19
+ provider.register();
20
+
21
+ registerInstrumentations({
22
+ instrumentations: [
23
+ new CommanderInstrumentation(),
24
+ ],
25
+ });
26
+ ```
27
+
28
+ ## What gets instrumented?
29
+
30
+ This instrumentation creates a span for each command execution with the following attributes:
31
+
32
+ - `command.name`: The name of the executed command
33
+ - `command.args`: JSON-serialized command arguments
34
+ - `command.duration_ms`: Command execution duration in milliseconds
35
+
36
+ ## Supported Versions
37
+
38
+ - Commander.js: `>=2.0.0 <15`
39
+ - Node.js: `>=18.0.0`
40
+
41
+ ## Configuration
42
+
43
+ ```typescript
44
+ interface CommanderInstrumentationConfig {
45
+ // Currently no configuration options available
46
+ // Future options may be added here
47
+ }
48
+ ```
49
+
50
+ The instrumentation is enabled by default when registered. You can disable it using the standard `enabled` option inherited from `InstrumentationBase`:
51
+
52
+ ```typescript
53
+ new CommanderInstrumentation({ enabled: false })
54
+ ```
55
+
56
+ ## Features
57
+
58
+ - ✅ Automatic span creation for command execution
59
+ - ✅ Support for both synchronous and asynchronous commands
60
+ - ✅ Exception recording with stack traces
61
+ - ✅ Context propagation to command handlers
62
+ - ✅ Support for nested commands and subcommands
63
+
64
+ ## License
65
+
66
+ MIT
@@ -0,0 +1,3 @@
1
+ export { CommanderInstrumentation } from './instrumentation-commander';
2
+ export type { CommanderInstrumentationConfig } from './instrumentation-commander';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,6BAA6B,CAAC;AACvE,YAAY,EAAE,8BAA8B,EAAE,MAAM,6BAA6B,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CommanderInstrumentation = void 0;
4
+ var instrumentation_commander_1 = require("./instrumentation-commander");
5
+ Object.defineProperty(exports, "CommanderInstrumentation", { enumerable: true, get: function () { return instrumentation_commander_1.CommanderInstrumentation; } });
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,yEAAuE;AAA9D,qIAAA,wBAAwB,OAAA"}
@@ -0,0 +1,11 @@
1
+ import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';
2
+ export interface CommanderInstrumentationConfig {
3
+ }
4
+ export declare class CommanderInstrumentation extends InstrumentationBase<CommanderInstrumentationConfig> {
5
+ constructor(config?: CommanderInstrumentationConfig);
6
+ protected init(): InstrumentationNodeModuleDefinition;
7
+ private _patchCommander;
8
+ private _unpatchCommander;
9
+ private _patchAction;
10
+ }
11
+ //# sourceMappingURL=instrumentation-commander.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instrumentation-commander.d.ts","sourceRoot":"","sources":["../src/instrumentation-commander.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,mCAAmC,EAEpC,MAAM,gCAAgC,CAAC;AASxC,MAAM,WAAW,8BAA8B;CAE9C;AAED,qBAAa,wBAAyB,SAAQ,mBAAmB,CAAC,8BAA8B,CAAC;gBACnF,MAAM,GAAE,8BAAmC;IAIvD,SAAS,CAAC,IAAI;IASd,OAAO,CAAC,eAAe;IA2BvB,OAAO,CAAC,iBAAiB;IAczB,OAAO,CAAC,YAAY;CAkErB"}
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.CommanderInstrumentation = void 0;
7
+ const instrumentation_1 = require("@opentelemetry/instrumentation");
8
+ const api_1 = require("@opentelemetry/api");
9
+ const package_json_1 = __importDefault(require("../package.json"));
10
+ const PACKAGE_NAME = package_json_1.default.name;
11
+ const PACKAGE_VERSION = package_json_1.default.version;
12
+ class CommanderInstrumentation extends instrumentation_1.InstrumentationBase {
13
+ constructor(config = {}) {
14
+ super(PACKAGE_NAME, PACKAGE_VERSION, config);
15
+ }
16
+ init() {
17
+ return new instrumentation_1.InstrumentationNodeModuleDefinition('commander', ['>=2.0.0 <15'], this._patchCommander.bind(this), this._unpatchCommander.bind(this));
18
+ }
19
+ _patchCommander(moduleExports, moduleVersion) {
20
+ this._diag.debug(`Patching commander@${moduleVersion}`);
21
+ const Command = moduleExports.Command;
22
+ if (!Command || !Command.prototype) {
23
+ this._diag.warn('Could not find Command class in commander module');
24
+ return moduleExports;
25
+ }
26
+ if ((0, instrumentation_1.isWrapped)(Command.prototype.action)) {
27
+ this._unwrap(Command.prototype, 'action');
28
+ }
29
+ this._wrap(Command.prototype, 'action', this._patchAction.bind(this));
30
+ this._diag.debug('commander module patched successfully');
31
+ return moduleExports;
32
+ }
33
+ _unpatchCommander(moduleExports, moduleVersion) {
34
+ this._diag.debug(`Unpatching commander@${moduleVersion}`);
35
+ const Command = moduleExports.Command;
36
+ if (Command && Command.prototype) {
37
+ if ((0, instrumentation_1.isWrapped)(Command.prototype.action)) {
38
+ this._unwrap(Command.prototype, 'action');
39
+ }
40
+ }
41
+ }
42
+ _patchAction(original) {
43
+ const instrumentation = this;
44
+ return function patchedAction(fn) {
45
+ const wrappedFn = function (...args) {
46
+ const commandName = getCommandName(this);
47
+ const spanName = `command ${commandName}`;
48
+ return instrumentation.tracer.startActiveSpan(spanName, {
49
+ kind: api_1.SpanKind.INTERNAL,
50
+ attributes: {
51
+ 'command.name': commandName,
52
+ 'command.args': JSON.stringify(args.slice(0, -1)),
53
+ },
54
+ }, (span) => {
55
+ const startTime = Date.now();
56
+ try {
57
+ const result = fn.apply(this, args);
58
+ if (result && typeof result.then === 'function') {
59
+ return result
60
+ .then((res) => {
61
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
62
+ span.setAttribute('command.duration_ms', Date.now() - startTime);
63
+ span.end();
64
+ return res;
65
+ })
66
+ .catch((error) => {
67
+ span.setStatus({
68
+ code: api_1.SpanStatusCode.ERROR,
69
+ message: error.message,
70
+ });
71
+ span.recordException(error);
72
+ span.setAttribute('command.duration_ms', Date.now() - startTime);
73
+ span.end();
74
+ throw error;
75
+ });
76
+ }
77
+ span.setStatus({ code: api_1.SpanStatusCode.OK });
78
+ span.setAttribute('command.duration_ms', Date.now() - startTime);
79
+ span.end();
80
+ return result;
81
+ }
82
+ catch (error) {
83
+ span.setStatus({
84
+ code: api_1.SpanStatusCode.ERROR,
85
+ message: error instanceof Error ? error.message : String(error),
86
+ });
87
+ if (error instanceof Error) {
88
+ span.recordException(error);
89
+ }
90
+ span.setAttribute('command.duration_ms', Date.now() - startTime);
91
+ span.end();
92
+ throw error;
93
+ }
94
+ });
95
+ };
96
+ return original.call(this, wrappedFn);
97
+ };
98
+ }
99
+ }
100
+ exports.CommanderInstrumentation = CommanderInstrumentation;
101
+ /**
102
+ * Extract command name from Commander instance, handling version differences.
103
+ * Commander v9+ uses _name property, earlier versions use name() method.
104
+ */
105
+ function getCommandName(command) {
106
+ const name = command._name || command.name() || 'unknown';
107
+ return name;
108
+ }
109
+ //# sourceMappingURL=instrumentation-commander.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"instrumentation-commander.js","sourceRoot":"","sources":["../src/instrumentation-commander.ts"],"names":[],"mappings":";;;;;;AAAA,oEAIwC;AACxC,4CAA8D;AAE9D,mEAA0C;AAE1C,MAAM,YAAY,GAAG,sBAAW,CAAC,IAAI,CAAC;AACtC,MAAM,eAAe,GAAG,sBAAW,CAAC,OAAO,CAAC;AAO5C,MAAa,wBAAyB,SAAQ,qCAAmD;IAC/F,YAAY,SAAyC,EAAE;QACrD,KAAK,CAAC,YAAY,EAAE,eAAe,EAAE,MAAM,CAAC,CAAC;IAC/C,CAAC;IAES,IAAI;QACZ,OAAO,IAAI,qDAAmC,CAC5C,WAAW,EACX,CAAC,aAAa,CAAC,EACf,IAAI,CA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S,cAAc,CAAC,OAA+B;IACrD,MAAM,IAAI,GAAI,OAAe,CAAC,KAAK,IAAK,OAAe,CAAC,IAAI,EAAE,IAAI,SAAS,CAAC;IAC5E,OAAO,IAAI,CAAC;AACd,CAAC"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "opentelemetry-instrumentation-commander",
3
+ "version": "0.1.0",
4
+ "description": "OpenTelemetry automatic instrumentation for Commander.js",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "test": "mocha",
10
+ "test:watch": "mocha --watch",
11
+ "clean": "rm -rf dist",
12
+ "prepack": "npm run clean && npm run build"
13
+ },
14
+ "keywords": [
15
+ "opentelemetry",
16
+ "instrumentation",
17
+ "commander",
18
+ "cli",
19
+ "tracing"
20
+ ],
21
+ "author": "k65miyazakiy",
22
+ "license": "MIT",
23
+ "files": [
24
+ "dist",
25
+ "LICENSE",
26
+ "README.md"
27
+ ],
28
+ "devDependencies": {
29
+ "@opentelemetry/context-async-hooks": "^2.2.0",
30
+ "@opentelemetry/sdk-trace-base": "^1.19.0",
31
+ "@opentelemetry/sdk-trace-node": "^1.19.0",
32
+ "@types/mocha": "^10.0.10",
33
+ "@types/node": "^20.10.0",
34
+ "commander": "^14.0.2",
35
+ "mocha": "^11.7.5",
36
+ "ts-node": "^10.9.2",
37
+ "typescript": "^5.3.3"
38
+ },
39
+ "dependencies": {
40
+ "@opentelemetry/api": "^1.7.0",
41
+ "@opentelemetry/instrumentation": "^0.208.0",
42
+ "@opentelemetry/semantic-conventions": "^1.19.0"
43
+ },
44
+ "peerDependencies": {
45
+ "commander": ">=2.0.0 <15"
46
+ },
47
+ "engines": {
48
+ "node": ">=18.0.0"
49
+ }
50
+ }