@monocle.sh/adonisjs-agent 1.0.0-beta.5 → 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.
package/README.md CHANGED
@@ -85,7 +85,7 @@ import { defineConfig } from '@monocle.sh/adonisjs-agent'
85
85
  import env from '#start/env'
86
86
 
87
87
  export default defineConfig({
88
- // Required: Your Monocle API key
88
+ // Optional: Your Monocle API key
89
89
  apiKey: env.get('MONOCLE_API_KEY'),
90
90
 
91
91
  // Optional: Custom ingestion endpoint (for development)
@@ -123,7 +123,7 @@ export default defineConfig({
123
123
 
124
124
  | Variable | Description | Required |
125
125
  | ----------------- | ------------------------------------------------------ | -------- |
126
- | `MONOCLE_API_KEY` | Your Monocle API key | Yes |
126
+ | `MONOCLE_API_KEY` | Your Monocle API key | No |
127
127
  | `APP_NAME` | Service name for identification | Yes |
128
128
  | `APP_VERSION` | Service version (e.g., git sha, semver) | Yes |
129
129
  | `APP_ENV` | Environment: `development`, `staging`, or `production` | Yes |
@@ -53,7 +53,7 @@ async function configure(command) {
53
53
  APP_NAME: "Env.schema.string()",
54
54
  APP_VERSION: "Env.schema.string()",
55
55
  APP_ENV: `Env.schema.enum(['development', 'staging', 'production'] as const)`,
56
- MONOCLE_API_KEY: "Env.schema.string()"
56
+ MONOCLE_API_KEY: "Env.schema.string.optional()"
57
57
  } });
58
58
  }
59
59
 
package/dist/init.mjs CHANGED
@@ -69,9 +69,7 @@ async function init(dirname$1) {
69
69
  const manager = OtelManager.create(configWithProcessors);
70
70
  manager?.start();
71
71
  const shutdown = async () => {
72
- await manager?.shutdown().catch(() => {
73
- console.error("Error during OTEL shutdown");
74
- });
72
+ await manager?.shutdown().catch(() => {});
75
73
  };
76
74
  process.on("beforeExit", shutdown);
77
75
  process.on("SIGINT", async () => {
@@ -1,4 +1,4 @@
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
3
  import OtelMiddleware from "@adonisjs/otel/otel_middleware";
4
4
  import { OtelManager } from "@adonisjs/otel";
@@ -26,14 +26,16 @@ var OtelProvider = class {
26
26
  */
27
27
  #registerExceptionHandler() {
28
28
  const originalReport = ExceptionHandler.prototype.report;
29
+ const reporter = new ExceptionReporter();
29
30
  ExceptionHandler.macro("report", async function(error, ctx) {
30
31
  const span = getCurrentSpan();
31
- if (span && error instanceof Error) {
32
- const httpError = this.toHttpError(error);
33
- await recordExceptionWithContext({
32
+ if (span) {
33
+ const httpError = toHttpError(error);
34
+ const shouldReport = this.shouldReport(httpError);
35
+ await reporter.report({
34
36
  span,
35
37
  error,
36
- shouldReport: this.shouldReport(httpError)
38
+ shouldReport
37
39
  });
38
40
  }
39
41
  return originalReport.call(this, error, ctx);
@@ -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,29 +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
- if (causeChain.length > 0) attributes["monocle.exception.cause_chain"] = JSON.stringify(causeChain);
287
- const mainFrames = causeChain[0]?.frames;
288
- if (mainFrames && mainFrames.length > 0) attributes["monocle.exception.frames"] = JSON.stringify(mainFrames);
289
- span.addEvent("exception", attributes);
290
- }
291
267
 
292
268
  //#endregion
293
- 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.5",
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",
@@ -67,6 +67,7 @@
67
67
  "@opentelemetry/sdk-metrics": "^2.2.0",
68
68
  "@opentelemetry/sdk-trace-base": "^2.2.0",
69
69
  "@opentelemetry/semantic-conventions": "^1.38.0",
70
+ "@sindresorhus/is": "^7.2.0",
70
71
  "error-stack-parser-es": "^1.0.5",
71
72
  "import-in-the-middle": "^2.0.1"
72
73
  },