@monocle.sh/adonisjs-agent 1.0.0-beta.6 → 1.0.0-beta.8

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.
@@ -1,6 +1,5 @@
1
- import { recordExceptionWithContext } from "./src/context_lines.mjs";
1
+ import { ExceptionReporter, toHttpError } from "./src/exception_reporter.mjs";
2
2
  import { getCurrentSpan } from "@adonisjs/otel/helpers";
3
- import is from "@sindresorhus/is";
4
3
  import OtelMiddleware from "@adonisjs/otel/otel_middleware";
5
4
  import { OtelManager } from "@adonisjs/otel";
6
5
  import { ExceptionHandler } from "@adonisjs/core/http";
@@ -16,15 +15,6 @@ var OtelProvider = class {
16
15
  this.app = app;
17
16
  }
18
17
  /**
19
- * Convert any error to an HttpError-like object
20
- */
21
- #toHttpError(error) {
22
- const httpError = is.object(error) ? error : new Error(String(error));
23
- if (!httpError.message) httpError.message = "Internal server error";
24
- if (!httpError.status) httpError.status = 500;
25
- return httpError;
26
- }
27
- /**
28
18
  * Hook into ExceptionHandler to record exceptions in spans.
29
19
  *
30
20
  * We always record the exception on the span (for trace visibility), but we also
@@ -36,15 +26,15 @@ var OtelProvider = class {
36
26
  */
37
27
  #registerExceptionHandler() {
38
28
  const originalReport = ExceptionHandler.prototype.report;
39
- const toHttpError = this.#toHttpError;
29
+ const reporter = new ExceptionReporter();
40
30
  ExceptionHandler.macro("report", async function(error, ctx) {
41
31
  const span = getCurrentSpan();
42
32
  if (span) {
43
33
  const httpError = toHttpError(error);
44
34
  const shouldReport = this.shouldReport(httpError);
45
- await recordExceptionWithContext({
35
+ await reporter.report({
46
36
  span,
47
- error: is.error(error) ? error : new Error(String(error)),
37
+ error,
48
38
  shouldReport
49
39
  });
50
40
  }
@@ -1,4 +1,4 @@
1
- import { recordExceptionWithContext } from "./context_lines.mjs";
1
+ import { ExceptionReporter } from "./exception_reporter.mjs";
2
2
  import { createRequire } from "node:module";
3
3
  import { SpanKind, context, trace } from "@opentelemetry/api";
4
4
 
@@ -72,9 +72,10 @@ async function instrumentCliCommands(config, appRoot) {
72
72
  try {
73
73
  return await originalExec.call(this);
74
74
  } catch (error) {
75
- if (error instanceof Error) await recordExceptionWithContext({
75
+ await new ExceptionReporter().report({
76
76
  span,
77
- error
77
+ error,
78
+ shouldReport: true
78
79
  });
79
80
  throw error;
80
81
  } finally {
@@ -1,5 +1,4 @@
1
- import { MAX_CAUSE_DEPTH, buildCauseChain, getErrorCause, isErrorLike, stackWithCauses } from "./error_serializer.mjs";
2
- import { SpanStatusCode } from "@opentelemetry/api";
1
+ import { MAX_CAUSE_DEPTH, getErrorCause, isErrorLike } from "./error_serializer.mjs";
3
2
  import { createInterface } from "node:readline";
4
3
  import { createReadStream } from "node:fs";
5
4
  import { parseStack } from "error-stack-parser-es/lite";
@@ -265,30 +264,6 @@ async function _buildCauseChainWithContext(err, seen, depth, contextLines) {
265
264
  if (cause) return [current, ...await _buildCauseChainWithContext(cause, seen, depth + 1, contextLines)];
266
265
  return [current];
267
266
  }
268
- /**
269
- * Record an exception on a span with context lines.
270
- * This replaces `span.recordException()` with an enriched version.
271
- */
272
- async function recordExceptionWithContext(options) {
273
- const { span, error, shouldReport = true, contextLines = DEFAULT_CONTEXT_LINES } = options;
274
- span.setStatus({
275
- code: SpanStatusCode.ERROR,
276
- message: error.message
277
- });
278
- span.setAttribute("monocle.exception.should_report", shouldReport);
279
- const causeChain = shouldReport ? await buildCauseChainWithContext(error, contextLines) : buildCauseChain(error);
280
- const hasCauses = causeChain.length > 1;
281
- const attributes = {
282
- "exception.type": error.name,
283
- "exception.message": error.message,
284
- "exception.stacktrace": hasCauses ? stackWithCauses(error) : error.stack || ""
285
- };
286
- const actualCauses = causeChain.slice(1);
287
- if (actualCauses.length > 0) attributes["monocle.exception.cause_chain"] = JSON.stringify(actualCauses);
288
- const mainFrames = causeChain[0]?.frames;
289
- if (mainFrames && mainFrames.length > 0) attributes["monocle.exception.frames"] = JSON.stringify(mainFrames);
290
- span.addEvent("exception", attributes);
291
- }
292
267
 
293
268
  //#endregion
294
- export { recordExceptionWithContext };
269
+ export { buildCauseChainWithContext };
@@ -0,0 +1,52 @@
1
+ import { buildCauseChain, stackWithCauses } from "./error_serializer.mjs";
2
+ import { buildCauseChainWithContext } from "./context_lines.mjs";
3
+ import { SpanStatusCode } from "@opentelemetry/api";
4
+ import is from "@sindresorhus/is";
5
+
6
+ //#region src/exception_reporter.ts
7
+ const DEFAULT_CONTEXT_LINES = 7;
8
+ /**
9
+ * Convert any unknown error to an HttpError-like object.
10
+ */
11
+ function toHttpError(error) {
12
+ const httpError = is.object(error) ? error : new Error(String(error));
13
+ if (!httpError.message) httpError.message = "Internal server error";
14
+ if (!httpError.status) httpError.status = 500;
15
+ return httpError;
16
+ }
17
+ /**
18
+ * Reports exceptions to OpenTelemetry spans with context lines and cause chains.
19
+ */
20
+ var ExceptionReporter = class {
21
+ /**
22
+ * Report an exception on the given span.
23
+ */
24
+ async report(options) {
25
+ const { span, shouldReport, contextLines = DEFAULT_CONTEXT_LINES } = options;
26
+ const error = toHttpError(options.error);
27
+ span.setStatus({
28
+ code: SpanStatusCode.ERROR,
29
+ message: error.message
30
+ });
31
+ span.setAttribute("monocle.exception.should_report", shouldReport);
32
+ const causeChain = shouldReport ? await buildCauseChainWithContext(error, contextLines) : buildCauseChain(error);
33
+ const attributes = this.#buildAttributes(error, causeChain);
34
+ span.addEvent("exception", attributes);
35
+ }
36
+ #buildAttributes(error, causeChain) {
37
+ const hasCauses = causeChain.length > 1;
38
+ const attributes = {
39
+ "exception.type": error.name || "Error",
40
+ "exception.message": error.message || "",
41
+ "exception.stacktrace": hasCauses ? stackWithCauses(error) : error.stack || ""
42
+ };
43
+ const actualCauses = causeChain.slice(1);
44
+ if (actualCauses.length > 0) attributes["monocle.exception.cause_chain"] = JSON.stringify(actualCauses);
45
+ const mainFrames = causeChain[0]?.frames;
46
+ if (mainFrames && mainFrames.length > 0) attributes["monocle.exception.frames"] = JSON.stringify(mainFrames);
47
+ return attributes;
48
+ }
49
+ };
50
+
51
+ //#endregion
52
+ export { ExceptionReporter, toHttpError };
@@ -1,7 +1,8 @@
1
1
  import { UserContextResult } from "@adonisjs/otel/types";
2
2
 
3
3
  //#region src/monocle.d.ts
4
- interface CaptureExceptionContext {
4
+ type MessageLevel = 'debug' | 'info' | 'warning' | 'error' | 'fatal';
5
+ interface CaptureContext {
5
6
  user?: {
6
7
  id: string;
7
8
  email?: string;
@@ -10,10 +11,15 @@ interface CaptureExceptionContext {
10
11
  tags?: Record<string, string>;
11
12
  extra?: Record<string, unknown>;
12
13
  }
14
+ interface CaptureMessageContext extends CaptureContext {
15
+ level?: MessageLevel;
16
+ }
17
+ type CaptureExceptionContext = CaptureContext;
13
18
  /**
14
19
  * Monocle helper class for manual instrumentation.
15
20
  */
16
21
  declare class Monocle {
22
+ #private;
17
23
  /**
18
24
  * Capture an exception and record it on the current active span.
19
25
  * If no span is active, the exception is silently ignored.
@@ -21,7 +27,12 @@ declare class Monocle {
21
27
  * This method is async to allow for source context extraction.
22
28
  * You can choose to await it or fire-and-forget.
23
29
  */
24
- static captureException(error: unknown, context?: CaptureExceptionContext): Promise<void>;
30
+ static captureException(error: unknown, ctx?: CaptureExceptionContext): Promise<void>;
31
+ /**
32
+ * Capture a message and record it on the current active span.
33
+ * If no span is active, the message is silently ignored.
34
+ */
35
+ static captureMessage(message: string, levelOrContext?: MessageLevel | CaptureMessageContext): void;
25
36
  /**
26
37
  * Set user information on the current active span.
27
38
  */
@@ -1,4 +1,4 @@
1
- import { recordExceptionWithContext } from "./context_lines.mjs";
1
+ import { ExceptionReporter } from "./exception_reporter.mjs";
2
2
  import { trace } from "@opentelemetry/api";
3
3
  import { setUser } from "@adonisjs/otel/helpers";
4
4
 
@@ -7,6 +7,18 @@ import { setUser } from "@adonisjs/otel/helpers";
7
7
  * Monocle helper class for manual instrumentation.
8
8
  */
9
9
  var Monocle = class {
10
+ static #applyUserToSpan(span, user) {
11
+ if (!user) return;
12
+ if (user.id) span.setAttribute("user.id", user.id);
13
+ if (user.email) span.setAttribute("user.email", user.email);
14
+ if (user.name) span.setAttribute("user.name", user.name);
15
+ }
16
+ static #buildContextAttributes(ctx) {
17
+ const attributes = {};
18
+ if (ctx?.tags) for (const [key, value] of Object.entries(ctx.tags)) attributes[`monocle.tag.${key}`] = value;
19
+ if (ctx?.extra) for (const [key, value] of Object.entries(ctx.extra)) attributes[`monocle.extra.${key}`] = JSON.stringify(value);
20
+ return attributes;
21
+ }
10
22
  /**
11
23
  * Capture an exception and record it on the current active span.
12
24
  * If no span is active, the exception is silently ignored.
@@ -14,20 +26,34 @@ var Monocle = class {
14
26
  * This method is async to allow for source context extraction.
15
27
  * You can choose to await it or fire-and-forget.
16
28
  */
17
- static async captureException(error, context$1) {
29
+ static async captureException(error, ctx) {
18
30
  const span = trace.getActiveSpan();
19
31
  if (!span) return;
20
- await recordExceptionWithContext({
32
+ await new ExceptionReporter().report({
21
33
  span,
22
- error: error instanceof Error ? error : new Error(String(error))
34
+ error,
35
+ shouldReport: true
23
36
  });
24
- if (context$1?.user) {
25
- if (context$1.user.id) span.setAttribute("user.id", context$1.user.id);
26
- if (context$1.user.email) span.setAttribute("user.email", context$1.user.email);
27
- if (context$1.user.name) span.setAttribute("user.name", context$1.user.name);
28
- }
29
- if (context$1?.tags) for (const [key, value] of Object.entries(context$1.tags)) span.setAttribute(`monocle.tag.${key}`, value);
30
- if (context$1?.extra) for (const [key, value] of Object.entries(context$1.extra)) span.setAttribute(`monocle.extra.${key}`, JSON.stringify(value));
37
+ this.#applyUserToSpan(span, ctx?.user);
38
+ const contextAttrs = this.#buildContextAttributes(ctx);
39
+ for (const [key, value] of Object.entries(contextAttrs)) span.setAttribute(key, value);
40
+ }
41
+ /**
42
+ * Capture a message and record it on the current active span.
43
+ * If no span is active, the message is silently ignored.
44
+ */
45
+ static captureMessage(message, levelOrContext) {
46
+ const span = trace.getActiveSpan();
47
+ if (!span) return;
48
+ const ctx = typeof levelOrContext === "string" ? { level: levelOrContext } : levelOrContext;
49
+ const level = ctx?.level ?? "info";
50
+ this.#applyUserToSpan(span, ctx?.user);
51
+ const attributes = {
52
+ "monocle.message.content": message,
53
+ "monocle.message.level": level,
54
+ ...this.#buildContextAttributes(ctx)
55
+ };
56
+ span.addEvent("monocle.message", attributes);
31
57
  }
32
58
  /**
33
59
  * Set user information on the current active span.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monocle.sh/adonisjs-agent",
3
- "version": "1.0.0-beta.6",
3
+ "version": "1.0.0-beta.8",
4
4
  "description": "Monocle agent for AdonisJS - sends telemetry to Monocle cloud",
5
5
  "keywords": [
6
6
  "adonisjs",
@@ -58,7 +58,7 @@
58
58
  "prepublishOnly": "pnpm run build"
59
59
  },
60
60
  "dependencies": {
61
- "@adonisjs/otel": "1.1.0",
61
+ "@adonisjs/otel": "^1.1.1",
62
62
  "@opentelemetry/api": "^1.9.0",
63
63
  "@opentelemetry/core": "^2.2.0",
64
64
  "@opentelemetry/exporter-metrics-otlp-http": "^0.208.0",
@@ -78,11 +78,11 @@
78
78
  "@japa/file-system": "^2.3.2",
79
79
  "@japa/runner": "^4.4.0",
80
80
  "@japa/snapshot": "^2.0.10",
81
- "release-it": "^19.2.2",
82
- "tsx": "^4.19.4"
81
+ "release-it": "^19.2.3",
82
+ "tsx": "^4.21.0"
83
83
  },
84
84
  "peerDependencies": {
85
85
  "@adonisjs/core": "^6.2.0 || ^7.0.0"
86
86
  },
87
- "packageManager": "pnpm@10.26.2"
87
+ "packageManager": "pnpm@10.27.0"
88
88
  }