@monocle.sh/adonisjs-agent 1.2.1 → 1.2.2

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.
@@ -52,7 +52,7 @@ function extractUserResponseHook(httpConfig) {
52
52
  * Returns undefined if no API key is provided (telemetry will be disabled).
53
53
  */
54
54
  function defineConfig(config) {
55
- const isDev = config.dev ?? process.env.NODE_ENV === "development";
55
+ const isDev = config.dev;
56
56
  if (!isDev && !config.apiKey) return;
57
57
  if (isDev) process.env.OTEL_EXPORTER_OTLP_PROTOCOL = "http/json";
58
58
  const endpoint = isDev ? config.endpoint || "http://localhost:4200" : config.endpoint || "https://ingest.monocle.sh";
@@ -5,6 +5,7 @@ import { SpanKind, SpanStatusCode, context, trace } from "@opentelemetry/api";
5
5
  import { pathToFileURL } from "node:url";
6
6
  import { InstrumentationBase } from "@opentelemetry/instrumentation";
7
7
  //#region src/instrumentations/mail/instrumentation.ts
8
+ const mailerPatchState = Symbol("monocle.mail.patchState");
8
9
  /**
9
10
  * OpenTelemetry instrumentation for AdonisJS Mail.
10
11
  *
@@ -54,10 +55,16 @@ var MailInstrumentation = class extends InstrumentationBase {
54
55
  };
55
56
  }
56
57
  /**
58
+ * Apply attributes to a span.
59
+ */
60
+ setSpanAttributes(span, attributes) {
61
+ for (const [key, value] of Object.entries(attributes)) span.setAttribute(key, value);
62
+ }
63
+ /**
57
64
  * Wraps an async function with an OpenTelemetry span.
58
65
  */
59
66
  async wrapWithSpan(options) {
60
- const { spanName, attributes, fn, onSuccess } = options;
67
+ const { spanName, attributes, fn, onSuccess, onSettled } = options;
61
68
  const span = this.tracer.startSpan(spanName, {
62
69
  kind: SpanKind.CLIENT,
63
70
  attributes
@@ -77,6 +84,7 @@ var MailInstrumentation = class extends InstrumentationBase {
77
84
  });
78
85
  throw error;
79
86
  } finally {
87
+ await onSettled?.(span);
80
88
  span.end();
81
89
  }
82
90
  });
@@ -93,6 +101,9 @@ var MailInstrumentation = class extends InstrumentationBase {
93
101
  fn: () => original.call(this, mail, sendConfig),
94
102
  onSuccess: (span, response) => {
95
103
  if (response?.messageId) span.setAttribute(EmailAttributes.MESSAGE_ID, response.messageId);
104
+ },
105
+ onSettled: (span) => {
106
+ instrumentation.setSpanAttributes(span, instrumentation.extractMessageAttributes(mail.message, this.name, "send"));
96
107
  }
97
108
  });
98
109
  };
@@ -111,6 +122,28 @@ var MailInstrumentation = class extends InstrumentationBase {
111
122
  };
112
123
  }
113
124
  /**
125
+ * Patch a Mailer class prototype with tracing wrappers.
126
+ */
127
+ patchMailerClass(mailerClass) {
128
+ const patchedMailerClass = mailerClass;
129
+ const existingPatch = patchedMailerClass.prototype[mailerPatchState];
130
+ this.mailerClass = mailerClass;
131
+ this.patched = true;
132
+ if (existingPatch) {
133
+ this.originalSendCompiled = existingPatch.sendCompiled;
134
+ this.originalSendLaterCompiled = existingPatch.sendLaterCompiled;
135
+ return;
136
+ }
137
+ this.originalSendCompiled = patchedMailerClass.prototype.sendCompiled;
138
+ this.originalSendLaterCompiled = patchedMailerClass.prototype.sendLaterCompiled;
139
+ patchedMailerClass.prototype[mailerPatchState] = {
140
+ sendCompiled: this.originalSendCompiled,
141
+ sendLaterCompiled: this.originalSendLaterCompiled
142
+ };
143
+ patchedMailerClass.prototype.sendCompiled = this.#createSendCompiledWrapper(this.originalSendCompiled);
144
+ patchedMailerClass.prototype.sendLaterCompiled = this.#createSendLaterCompiledWrapper(this.originalSendLaterCompiled);
145
+ }
146
+ /**
114
147
  * Patches Mailer.prototype methods with tracing wrappers.
115
148
  *
116
149
  * Note: InstrumentationBase constructor calls enable() synchronously
@@ -132,11 +165,8 @@ var MailInstrumentation = class extends InstrumentationBase {
132
165
  return;
133
166
  }
134
167
  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);
168
+ const mailModule = await import(pathToFileURL(mailerPath).href);
169
+ this.patchMailerClass(mailModule.Mailer);
140
170
  } catch {
141
171
  this.patched = false;
142
172
  }
@@ -146,8 +176,13 @@ var MailInstrumentation = class extends InstrumentationBase {
146
176
  */
147
177
  disable() {
148
178
  if (!this.patched || !this.mailerClass) return;
149
- if (this.originalSendCompiled) this.mailerClass.prototype.sendCompiled = this.originalSendCompiled;
150
- if (this.originalSendLaterCompiled) this.mailerClass.prototype.sendLaterCompiled = this.originalSendLaterCompiled;
179
+ const patchedMailerClass = this.mailerClass;
180
+ const existingPatch = patchedMailerClass.prototype[mailerPatchState];
181
+ if (existingPatch?.sendCompiled) patchedMailerClass.prototype.sendCompiled = existingPatch.sendCompiled;
182
+ else if (this.originalSendCompiled) this.mailerClass.prototype.sendCompiled = this.originalSendCompiled;
183
+ if (existingPatch?.sendLaterCompiled) patchedMailerClass.prototype.sendLaterCompiled = existingPatch.sendLaterCompiled;
184
+ else if (this.originalSendLaterCompiled) this.mailerClass.prototype.sendLaterCompiled = this.originalSendLaterCompiled;
185
+ delete patchedMailerClass.prototype[mailerPatchState];
151
186
  this.patched = false;
152
187
  this.mailerClass = void 0;
153
188
  this.originalSendCompiled = void 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@monocle.sh/adonisjs-agent",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Monocle agent for AdonisJS - sends telemetry to Monocle cloud",
5
5
  "keywords": [
6
6
  "adonisjs",