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

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 };
@@ -21,7 +21,7 @@ declare class Monocle {
21
21
  * This method is async to allow for source context extraction.
22
22
  * You can choose to await it or fire-and-forget.
23
23
  */
24
- static captureException(error: unknown, context?: CaptureExceptionContext): Promise<void>;
24
+ static captureException(error: unknown, ctx?: CaptureExceptionContext): Promise<void>;
25
25
  /**
26
26
  * Set user information on the current active span.
27
27
  */
@@ -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
 
@@ -14,20 +14,21 @@ var Monocle = class {
14
14
  * This method is async to allow for source context extraction.
15
15
  * You can choose to await it or fire-and-forget.
16
16
  */
17
- static async captureException(error, context$1) {
17
+ static async captureException(error, ctx) {
18
18
  const span = trace.getActiveSpan();
19
19
  if (!span) return;
20
- await recordExceptionWithContext({
20
+ await new ExceptionReporter().report({
21
21
  span,
22
- error: error instanceof Error ? error : new Error(String(error))
22
+ error,
23
+ shouldReport: true
23
24
  });
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);
25
+ if (ctx?.user) {
26
+ if (ctx.user.id) span.setAttribute("user.id", ctx.user.id);
27
+ if (ctx.user.email) span.setAttribute("user.email", ctx.user.email);
28
+ if (ctx.user.name) span.setAttribute("user.name", ctx.user.name);
28
29
  }
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));
30
+ if (ctx?.tags) for (const [key, value] of Object.entries(ctx.tags)) span.setAttribute(`monocle.tag.${key}`, value);
31
+ if (ctx?.extra) for (const [key, value] of Object.entries(ctx.extra)) span.setAttribute(`monocle.extra.${key}`, JSON.stringify(value));
31
32
  }
32
33
  /**
33
34
  * 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.7",
4
4
  "description": "Monocle agent for AdonisJS - sends telemetry to Monocle cloud",
5
5
  "keywords": [
6
6
  "adonisjs",