@monocle.sh/adonisjs-agent 1.0.1 → 1.0.3

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,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2024-present, Julien Ripouteau
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package/dist/init.mjs CHANGED
@@ -74,6 +74,19 @@ async function init(dirname) {
74
74
  await instrumentMail(mailConfig ?? { enabled: true }, dirname);
75
75
  }
76
76
  /**
77
+ * Queue Instrumentation
78
+ *
79
+ * Automatically instruments @boringnode/queue if installed.
80
+ * Creates PRODUCER spans for dispatch and CONSUMER spans for execution.
81
+ *
82
+ * @see ./instrumentations/queue/instrumentation.ts for implementation details
83
+ */
84
+ const queueConfig = config.queue;
85
+ if (queueConfig !== false) {
86
+ const { instrumentQueue } = await import("./src/instrumentations/queue/instrumentation.mjs");
87
+ await instrumentQueue(queueConfig ?? { enabled: true }, dirname);
88
+ }
89
+ /**
77
90
  * Cache Instrumentation
78
91
  *
79
92
  * Automatically instruments @adonisjs/cache (bentocache) if installed.
@@ -20,13 +20,30 @@ function getHeader(response, name) {
20
20
  }
21
21
  }
22
22
  /**
23
+ * Returns the raw HTTP header string from a ServerResponse.
24
+ * After `writeHead()` is called with inline headers, `getHeader()` returns
25
+ * undefined. Node.js stores the serialized headers in `_header` which is
26
+ * the only reliable way to read them after they've been flushed.
27
+ */
28
+ function getRawHeader(response) {
29
+ const raw = response._header;
30
+ if (typeof raw === "string") return raw;
31
+ }
32
+ /**
23
33
  * Detects the connection type based on response headers.
24
34
  * Returns 'sse' for Server-Sent Events, 'websocket' for WebSocket upgrades,
25
35
  * or undefined for standard HTTP connections.
36
+ *
37
+ * Checks both `getHeader()` and the raw `_header` string to handle cases
38
+ * where headers are passed inline to `writeHead()` (e.g. @hono/node-server).
26
39
  */
27
40
  function detectConnectionType(response) {
28
41
  if (getHeader(response, "content-type")?.includes("text/event-stream")) return "sse";
29
42
  if (getHeader(response, "upgrade")?.toLowerCase() === "websocket") return "websocket";
43
+ const raw = getRawHeader(response);
44
+ if (!raw) return;
45
+ if (raw.includes("text/event-stream")) return "sse";
46
+ if (raw.toLowerCase().includes("upgrade: websocket")) return "websocket";
30
47
  }
31
48
  /**
32
49
  * Creates a response hook that detects long-running HTTP connections
@@ -112,23 +112,34 @@ var MailInstrumentation = class extends InstrumentationBase {
112
112
  }
113
113
  /**
114
114
  * Patches Mailer.prototype methods with tracing wrappers.
115
+ *
116
+ * Note: InstrumentationBase constructor calls enable() synchronously
117
+ * without awaiting (it ignores the returned Promise). Then instrumentMail()
118
+ * also calls `await enable()`. Setting `this.patched = true` before any
119
+ * `await` prevents the second call from double-patching the prototype,
120
+ * which would create duplicate spans for every email sent.
115
121
  */
116
122
  async enable() {
117
123
  if (this.patched) return;
124
+ this.patched = true;
118
125
  const appRoot = this.getConfig().appRoot;
119
126
  const require = createRequire(appRoot ? pathToFileURL(`${appRoot}/package.json`).href : import.meta.url);
120
127
  let mailerPath;
121
128
  try {
122
129
  mailerPath = require.resolve("@adonisjs/mail");
123
130
  } catch {
131
+ this.patched = false;
124
132
  return;
125
133
  }
126
- this.mailerClass = (await import(pathToFileURL(mailerPath).href)).Mailer;
127
- this.originalSendCompiled = this.mailerClass.prototype.sendCompiled;
128
- this.originalSendLaterCompiled = this.mailerClass.prototype.sendLaterCompiled;
129
- this.mailerClass.prototype.sendCompiled = this.#createSendCompiledWrapper(this.originalSendCompiled);
130
- this.mailerClass.prototype.sendLaterCompiled = this.#createSendLaterCompiledWrapper(this.originalSendLaterCompiled);
131
- this.patched = true;
134
+ try {
135
+ this.mailerClass = (await import(pathToFileURL(mailerPath).href)).Mailer;
136
+ this.originalSendCompiled = this.mailerClass.prototype.sendCompiled;
137
+ this.originalSendLaterCompiled = this.mailerClass.prototype.sendLaterCompiled;
138
+ this.mailerClass.prototype.sendCompiled = this.#createSendCompiledWrapper(this.originalSendCompiled);
139
+ this.mailerClass.prototype.sendLaterCompiled = this.#createSendLaterCompiledWrapper(this.originalSendLaterCompiled);
140
+ } catch {
141
+ this.patched = false;
142
+ }
132
143
  }
133
144
  /**
134
145
  * Restores original Mailer.prototype methods.
@@ -0,0 +1,42 @@
1
+ import { createRequire } from "node:module";
2
+ import { pathToFileURL } from "node:url";
3
+ //#region src/instrumentations/queue/instrumentation.ts
4
+ /**
5
+ * Auto-instruments @boringnode/queue with OpenTelemetry spans
6
+ * for job dispatch (PRODUCER) and execution (CONSUMER).
7
+ *
8
+ * Patches `QueueManager.init()` to auto-inject the OTel plugin,
9
+ * so users don't need to configure plugins in their queue config.
10
+ */
11
+ async function instrumentQueue(config, appRoot) {
12
+ if (config.enabled === false) return void 0;
13
+ const appRequire = createRequire(appRoot ? pathToFileURL(`${appRoot}/package.json`).href : import.meta.url);
14
+ let queueOtelPath;
15
+ try {
16
+ queueOtelPath = appRequire.resolve("@boringnode/queue/otel");
17
+ } catch {
18
+ try {
19
+ queueOtelPath = appRequire.resolve("@adonisjs/queue/otel");
20
+ } catch {
21
+ return;
22
+ }
23
+ }
24
+ const { QueueInstrumentation } = await import(pathToFileURL(queueOtelPath).href);
25
+ const instrumentation = new QueueInstrumentation({
26
+ executionSpanLinkMode: config.executionSpanLinkMode,
27
+ messagingSystem: config.messagingSystem
28
+ });
29
+ instrumentation.enable();
30
+ try {
31
+ const queueModule = await import(pathToFileURL(appRequire.resolve("@boringnode/queue")).href);
32
+ instrumentation.manuallyRegister(queueModule);
33
+ } catch {
34
+ try {
35
+ const queueModule = await import(pathToFileURL(appRequire.resolve("@adonisjs/queue")).href);
36
+ instrumentation.manuallyRegister(queueModule);
37
+ } catch {}
38
+ }
39
+ return instrumentation;
40
+ }
41
+ //#endregion
42
+ export { instrumentQueue };
@@ -6,6 +6,14 @@ import { DestinationMap, OtelConfig } from "@adonisjs/otel/types";
6
6
  * Configuration for cache instrumentation
7
7
  */
8
8
  interface CacheInstrumentationConfig extends BentoCacheInstrumentationConfig {}
9
+ /**
10
+ * Configuration for queue instrumentation
11
+ */
12
+ interface QueueInstrumentationConfig {
13
+ enabled?: boolean;
14
+ executionSpanLinkMode?: 'link' | 'parent';
15
+ messagingSystem?: string;
16
+ }
9
17
  /**
10
18
  * Configuration for mail instrumentation
11
19
  */
@@ -143,6 +151,13 @@ interface MonocleConfig extends Omit<OtelConfig, 'traceExporter' | 'metricExport
143
151
  * @default { enabled: true }
144
152
  */
145
153
  cache?: false | CacheInstrumentationConfig;
154
+ /**
155
+ * Queue instrumentation configuration.
156
+ * Automatically instruments @boringnode/queue if installed.
157
+ * Set to `false` to disable.
158
+ * @default { enabled: true }
159
+ */
160
+ queue?: false | QueueInstrumentationConfig;
146
161
  }
147
162
  //#endregion
148
163
  export { BatchConfig, CliTracingConfig, HostMetricsConfig, MonocleConfig };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monocle.sh/adonisjs-agent",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Monocle agent for AdonisJS - sends telemetry to Monocle cloud",
5
5
  "keywords": [
6
6
  "adonisjs",
@@ -23,69 +23,23 @@
23
23
  "type": "module",
24
24
  "types": "./dist/index.d.mts",
25
25
  "exports": {
26
- ".": {
27
- "dev": "./index.ts",
28
- "default": "./dist/index.mjs"
29
- },
30
- "./decorators": {
31
- "dev": "./src/decorators.ts",
32
- "default": "./dist/decorators.mjs"
33
- },
34
- "./helpers": {
35
- "dev": "./src/helpers.ts",
36
- "default": "./dist/helpers.mjs"
37
- },
38
- "./init": {
39
- "dev": "./src/init.ts",
40
- "default": "./dist/init.mjs"
41
- },
42
- "./mcp": {
43
- "dev": "./src/mcp.ts",
44
- "default": "./dist/mcp.mjs"
45
- },
46
- "./monocle_middleware": {
47
- "dev": "./middleware/monocle_middleware.ts",
48
- "default": "./dist/monocle_middleware.mjs"
49
- },
50
- "./monocle_provider": {
51
- "dev": "./providers/monocle_provider.ts",
52
- "default": "./dist/monocle_provider.mjs"
53
- },
54
- "./types": {
55
- "dev": "./types.ts",
56
- "default": "./dist/types.mjs"
57
- },
26
+ ".": "./dist/index.mjs",
27
+ "./decorators": "./dist/decorators.mjs",
28
+ "./helpers": "./dist/helpers.mjs",
29
+ "./init": "./dist/init.mjs",
30
+ "./mcp": "./dist/mcp.mjs",
31
+ "./monocle_middleware": "./dist/monocle_middleware.mjs",
32
+ "./monocle_provider": "./dist/monocle_provider.mjs",
33
+ "./types": "./dist/types.mjs",
58
34
  "./package.json": "./package.json"
59
35
  },
60
36
  "publishConfig": {
61
37
  "access": "public",
62
- "exports": {
63
- ".": "./dist/index.mjs",
64
- "./decorators": "./dist/decorators.mjs",
65
- "./helpers": "./dist/helpers.mjs",
66
- "./init": "./dist/init.mjs",
67
- "./mcp": "./dist/mcp.mjs",
68
- "./monocle_middleware": "./dist/monocle_middleware.mjs",
69
- "./monocle_provider": "./dist/monocle_provider.mjs",
70
- "./types": "./dist/types.mjs",
71
- "./package.json": "./package.json"
72
- },
73
38
  "tag": "latest"
74
39
  },
75
- "scripts": {
76
- "build": "tsdown",
77
- "dev": "tsdown",
78
- "typecheck": "tsgo --noEmit",
79
- "test": "node --import @poppinss/ts-exec bin/test.ts",
80
- "quick:test": "node --import @poppinss/ts-exec bin/test.ts",
81
- "release": "release-it",
82
- "prepublishOnly": "pnpm run build"
83
- },
84
40
  "dependencies": {
85
41
  "@adonisjs/otel": "^1.2.3",
86
42
  "@bentocache/otel": "^0.1.2",
87
- "@monocle.sh/instrumentation-mcp": "workspace:*",
88
- "@monocle.sh/otel-utils": "workspace:*",
89
43
  "@opentelemetry/api": "^1.9.0",
90
44
  "@opentelemetry/core": "^2.6.0",
91
45
  "@opentelemetry/exporter-metrics-otlp-http": "^0.213.0",
@@ -97,17 +51,19 @@
97
51
  "@opentelemetry/semantic-conventions": "^1.40.0",
98
52
  "@sindresorhus/is": "^7.2.0",
99
53
  "error-stack-parser-es": "^1.0.5",
100
- "import-in-the-middle": "^3.0.0"
54
+ "import-in-the-middle": "^3.0.0",
55
+ "@monocle.sh/instrumentation-mcp": "^1.0.0",
56
+ "@monocle.sh/otel-utils": "^1.0.0"
101
57
  },
102
58
  "devDependencies": {
103
- "@adonisjs/core": "catalog:",
104
- "@adonisjs/tsconfig": "catalog:",
105
- "@japa/assert": "catalog:",
59
+ "@adonisjs/core": "^7.1.1",
60
+ "@adonisjs/tsconfig": "^2.0.0",
61
+ "@japa/assert": "^4.2.0",
106
62
  "@japa/file-system": "^3.0.0",
107
- "@japa/runner": "catalog:",
63
+ "@japa/runner": "^5.3.0",
108
64
  "@japa/snapshot": "^2.0.10",
109
- "@poppinss/ts-exec": "catalog:",
110
- "release-it": "catalog:"
65
+ "@poppinss/ts-exec": "^1.4.4",
66
+ "release-it": "^19.2.4"
111
67
  },
112
68
  "peerDependencies": {
113
69
  "@adonisjs/core": "^6.2.0 || ^7.0.0",
@@ -118,5 +74,12 @@
118
74
  "optional": true
119
75
  }
120
76
  },
121
- "packageManager": "pnpm@10.32.1"
122
- }
77
+ "scripts": {
78
+ "build": "tsdown",
79
+ "dev": "tsdown",
80
+ "typecheck": "tsgo --noEmit",
81
+ "test": "node --import @poppinss/ts-exec bin/test.ts",
82
+ "quick:test": "node --import @poppinss/ts-exec bin/test.ts",
83
+ "release": "release-it"
84
+ }
85
+ }