@rasputin-ai/elysia 0.13.0 → 0.14.0

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/dist/index.js CHANGED
@@ -4,21 +4,27 @@ import {
4
4
  createClient,
5
5
  isClientEnabled,
6
6
  parseDsn,
7
+ resolveClientActivation,
7
8
  resolveEndpointUrls,
8
9
  scheduleAnnounceDeploy
9
10
  } from "@rasputin-ai/core";
10
11
  import {
11
12
  bindAdaptiveRecorder,
13
+ bindExecutionMetrics,
14
+ bindFunctionMetrics,
15
+ createDynamicDurationSampleBuckets,
12
16
  createExecutionRecorder,
13
17
  installAutomaticExecutionRuntime,
14
18
  installGlobalHandlers,
19
+ releaseExecutionMetricsBindings,
20
+ releaseFunctionMetricsBindings,
15
21
  scheduleInstrumentationManifestUpload
16
22
  } from "@rasputin-ai/node/internal";
17
23
  import { Elysia } from "elysia";
18
24
 
19
25
  // src/sdk-meta.ts
20
26
  var SDK_NAME = "@rasputin-ai/elysia";
21
- var SDK_VERSION = "0.13.0";
27
+ var SDK_VERSION = "0.14.0";
22
28
 
23
29
  // src/rasputin-init.ts
24
30
  var publicExecution = (runtime) => ({
@@ -34,7 +40,7 @@ var publicExecution = (runtime) => ({
34
40
  getStats: runtime.getStats
35
41
  });
36
42
  var withExecution = (client, runtime, installRuntime, manifest, uninstallHandlers = () => {
37
- }, policy) => {
43
+ }, policy, metrics, executionMetrics) => {
38
44
  const uninstallRuntime = installRuntime ? installAutomaticExecutionRuntime(runtime) : () => {
39
45
  };
40
46
  const manifestUpload = scheduleInstrumentationManifestUpload({
@@ -58,10 +64,14 @@ var withExecution = (client, runtime, installRuntime, manifest, uninstallHandler
58
64
  await client.flush(timeoutMs);
59
65
  if (timeoutMs === void 0) {
60
66
  await policy?.flush();
67
+ await metrics?.exporter.flush();
68
+ await executionMetrics?.exporter.flush();
61
69
  await manifestUpload.wait();
62
70
  return;
63
71
  }
64
72
  await policy?.flush(Math.max(0, timeoutMs - (Date.now() - startedAt)));
73
+ await metrics?.exporter.flush(Math.max(0, timeoutMs - (Date.now() - startedAt)));
74
+ await executionMetrics?.exporter.flush(Math.max(0, timeoutMs - (Date.now() - startedAt)));
65
75
  await manifestUpload.wait(Math.max(0, timeoutMs - (Date.now() - startedAt)));
66
76
  },
67
77
  close: async () => {
@@ -71,6 +81,8 @@ var withExecution = (client, runtime, installRuntime, manifest, uninstallHandler
71
81
  manifestUpload.disconnect();
72
82
  const startedAt = Date.now();
73
83
  await policy?.close();
84
+ await releaseFunctionMetricsBindings(metrics);
85
+ await releaseExecutionMetricsBindings(executionMetrics);
74
86
  await client.close();
75
87
  await manifestUpload.wait(Math.max(0, 2e3 - (Date.now() - startedAt)));
76
88
  }
@@ -114,7 +126,7 @@ var mergeRequestMetadata = (scope, request) => {
114
126
  } catch {
115
127
  }
116
128
  };
117
- var buildPlugin = (client, runtime, recorderEnabled) => {
129
+ var buildPlugin = (client, runtime, wrapRequests) => {
118
130
  const executionBoundary = ((handler, initialRequest) => {
119
131
  return (request) => {
120
132
  const scope = runtime.createScope({
@@ -145,7 +157,7 @@ var buildPlugin = (client, runtime, recorderEnabled) => {
145
157
  };
146
158
  });
147
159
  const plugin = new Elysia({ name: "rasputin" }).decorate("rasputin", client);
148
- if (recorderEnabled) plugin.wrap(executionBoundary);
160
+ if (wrapRequests) plugin.wrap(executionBoundary);
149
161
  return plugin.onBeforeHandle({ as: "global" }, ({ request, route }) => {
150
162
  mergeRequestMetadata(requestScopes.get(request), {
151
163
  method: request.method,
@@ -180,7 +192,37 @@ function RasputinInit(options, hooks = {}) {
180
192
  installTransportSignalHandlers = options.installTransportSignalHandlers
181
193
  } = hooks;
182
194
  const enabled = isClientEnabled(options);
195
+ const activation = resolveClientActivation({
196
+ enabled: options.enabled,
197
+ dsn: options.dsn,
198
+ projectApiKey: options.projectApiKey,
199
+ apiUrl: options.apiUrl,
200
+ release: options.release,
201
+ environment: options.environment
202
+ });
183
203
  const recorderEnabled = enabled && options.executionRecorder?.enabled !== false;
204
+ const durationSampling = options.dynamicDurationSampling === true;
205
+ const metrics = activation.enabled ? bindFunctionMetrics({
206
+ options: options.functionMetrics,
207
+ clientEnabled: true,
208
+ environment: activation.environment,
209
+ release: activation.release,
210
+ ingestToken: activation.ingestToken,
211
+ apiUrl: activation.apiUrl,
212
+ fetch,
213
+ sampledMeasurementFormat: durationSampling
214
+ }) : void 0;
215
+ const executionMetrics = activation.enabled ? bindExecutionMetrics({
216
+ options: options.executionMetrics,
217
+ clientEnabled: true,
218
+ environment: activation.environment,
219
+ release: activation.release,
220
+ ingestToken: activation.ingestToken,
221
+ apiUrl: activation.apiUrl,
222
+ fetch,
223
+ sampledMeasurementFormat: durationSampling
224
+ }) : void 0;
225
+ const durationSampler = durationSampling && (recorderEnabled || Boolean(metrics) || Boolean(executionMetrics)) ? createDynamicDurationSampleBuckets({ nowMs: () => Date.now() }) : void 0;
184
226
  const { runtime, policy } = bindAdaptiveRecorder({
185
227
  executionRecorder: options.executionRecorder,
186
228
  recorderEnabled,
@@ -193,7 +235,10 @@ function RasputinInit(options, hooks = {}) {
193
235
  enabled: options.enabled,
194
236
  sdkName: SDK_NAME,
195
237
  sdkVersion: SDK_VERSION,
196
- scrubPII: options.scrubPII
238
+ scrubPII: options.scrubPII,
239
+ functionMetrics: metrics?.collector,
240
+ executionMetrics: executionMetrics?.collector,
241
+ durationSampler
197
242
  });
198
243
  const client = createClient(options, {
199
244
  fetch,
@@ -209,7 +254,7 @@ function RasputinInit(options, hooks = {}) {
209
254
  const { client: wrappedClient, startManifestUpload } = withExecution(
210
255
  client,
211
256
  runtime,
212
- recorderEnabled,
257
+ recorderEnabled || Boolean(metrics),
213
258
  {
214
259
  projectApiKey: options.projectApiKey,
215
260
  release: options.release,
@@ -221,25 +266,29 @@ function RasputinInit(options, hooks = {}) {
221
266
  logSuccess: options.logSuccess
222
267
  },
223
268
  () => uninstallHandlers(),
224
- policy
269
+ policy,
270
+ metrics,
271
+ executionMetrics
225
272
  );
226
273
  if (enabled && installProcessHandlers) {
227
274
  uninstallHandlers = installGlobalHandlers(wrappedClient);
228
275
  }
229
- const plugin = buildPlugin(wrappedClient, runtime, recorderEnabled).onStart(() => {
230
- startManifestUpload();
231
- if (!enabled || announceDeploy === false) return;
232
- const parsed = parseDsn(options.dsn);
233
- const apiUrl = typeof options.apiUrl === "string" && options.apiUrl.trim() ? options.apiUrl.trim() : parsed?.apiUrl;
234
- if (!apiUrl) return;
235
- scheduleAnnounceDeploy({
236
- deployUrl: resolveEndpointUrls(apiUrl).deployUrl,
237
- projectApiKey: options.projectApiKey,
238
- environment: options.environment,
239
- release: options.release,
240
- fetch
241
- });
242
- });
276
+ const plugin = buildPlugin(wrappedClient, runtime, recorderEnabled || Boolean(metrics)).onStart(
277
+ () => {
278
+ startManifestUpload();
279
+ if (!enabled || announceDeploy === false) return;
280
+ const parsed = parseDsn(options.dsn);
281
+ const apiUrl = typeof options.apiUrl === "string" && options.apiUrl.trim() ? options.apiUrl.trim() : parsed?.apiUrl;
282
+ if (!apiUrl) return;
283
+ scheduleAnnounceDeploy({
284
+ deployUrl: resolveEndpointUrls(apiUrl).deployUrl,
285
+ projectApiKey: options.projectApiKey,
286
+ environment: options.environment,
287
+ release: options.release,
288
+ fetch
289
+ });
290
+ }
291
+ );
243
292
  return {
244
293
  client: wrappedClient,
245
294
  plugin
@@ -4,7 +4,7 @@
4
4
  * intentionally limited to lifecycle wiring; recording, serialization, and safety policy stay in
5
5
  * the framework-neutral Node recorder.
6
6
  */
7
- import { type RasputinOptions } from '@rasputin-ai/core';
7
+ import { type ExecutionMetricsOptions, type FunctionMetricsOptions, type RasputinOptions } from '@rasputin-ai/core';
8
8
  import type { ExecutionRecorderOptions, RasputinNodeClient } from '@rasputin-ai/node';
9
9
  import { type ExecutionRuntime } from '@rasputin-ai/node/internal';
10
10
  import { Elysia } from 'elysia';
@@ -34,8 +34,33 @@ export type RasputinElysiaOptions = RasputinOptions & {
34
34
  * Set false when the host owns process shutdown.
35
35
  */
36
36
  installTransportSignalHandlers?: boolean;
37
+ /**
38
+ * Measure durations on a progressive schedule shared by function metrics,
39
+ * execution metrics, and lightweight execution recording. Default false
40
+ * times every eligible call and execution. True counts every call,
41
+ * execution, and failure, and times a subset. Function durations are
42
+ * keyed by function, direct caller, and execution type. Execution
43
+ * durations use a separate counter per execution type so they cannot
44
+ * move function sampling. Counters reset hourly. After 5,000 samples
45
+ * the tail rate still samples. Selected sessions, activations, and full
46
+ * capture still record every duration; metric duration statistics follow
47
+ * this schedule so incident traffic does not bias aggregates. Invalid
48
+ * values are treated as false.
49
+ */
50
+ dynamicDurationSampling?: boolean;
51
+ /**
52
+ * Opt-in function-level aggregate metrics. Off unless `enabled` is `true`.
53
+ * Independent of `executionRecorder`.
54
+ */
55
+ functionMetrics?: FunctionMetricsOptions;
56
+ /**
57
+ * Execution-level counts, failures, and duration histograms. On with a
58
+ * valid client. Set `enabled: false` to turn them off. Independent of
59
+ * `executionRecorder` and `functionMetrics`.
60
+ */
61
+ executionMetrics?: ExecutionMetricsOptions;
37
62
  };
38
- declare const buildPlugin: (client: RasputinNodeClient, runtime: ExecutionRuntime, recorderEnabled: boolean) => Elysia<"", {
63
+ declare const buildPlugin: (client: RasputinNodeClient, runtime: ExecutionRuntime, wrapRequests: boolean) => Elysia<"", {
39
64
  decorator: {
40
65
  rasputin: RasputinNodeClient;
41
66
  };
@@ -73,8 +98,7 @@ declare const buildPlugin: (client: RasputinNodeClient, runtime: ExecutionRuntim
73
98
  * dsn: process.env.RASPUTIN_DSN!,
74
99
  * projectApiKey: process.env.RASPUTIN_PROJECT_API_KEY!,
75
100
  * release: process.env.RASPUTIN_RELEASE!,
76
- * environment: process.env.NODE_ENV ?? 'development',
77
- * enabled: ['staging', 'production'].includes(process.env.NODE_ENV ?? 'development'),
101
+ * environment: 'production',
78
102
  * });
79
103
  * app.use(plugin); // Chain this plugin as early as possible with .use(), before any other plugins or middleware.
80
104
  * client.captureException(err); // manual capture
@@ -1 +1 @@
1
- {"version":3,"file":"rasputin-init.d.ts","sourceRoot":"","sources":["../src/rasputin-init.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAON,KAAK,eAAe,EAGpB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EACX,wBAAwB,EAExB,kBAAkB,EAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAIN,KAAK,gBAAgB,EAKrB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIhC,iFAAiF;AACjF,MAAM,MAAM,oBAAoB,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAElE,MAAM,MAAM,cAAc,GAAG;IAC5B,8EAA8E;IAC9E,MAAM,EAAE,kBAAkB,CAAC;IAC3B,2EAA2E;IAC3E,MAAM,EAAE,oBAAoB,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,eAAe,GAAG;IACrD;;;OAGG;IACH,iBAAiB,CAAC,EAAE,wBAAwB,CAAC;IAC7C;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC;;;OAGG;IACH,8BAA8B,CAAC,EAAE,OAAO,CAAC;CACzC,CAAC;AAwIF,QAAA,MAAM,WAAW,GAChB,QAAQ,kBAAkB,EAC1B,SAAS,gBAAgB,EACzB,iBAAiB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAmExB,CAAC;AAEF;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,qBAAqB,GAAG,cAAc,CAAC"}
1
+ {"version":3,"file":"rasputin-init.d.ts","sourceRoot":"","sources":["../src/rasputin-init.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAIN,KAAK,uBAAuB,EAC5B,KAAK,sBAAsB,EAI3B,KAAK,eAAe,EAIpB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EACX,wBAAwB,EAExB,kBAAkB,EAClB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAQN,KAAK,gBAAgB,EAQrB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAIhC,iFAAiF;AACjF,MAAM,MAAM,oBAAoB,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAElE,MAAM,MAAM,cAAc,GAAG;IAC5B,8EAA8E;IAC9E,MAAM,EAAE,kBAAkB,CAAC;IAC3B,2EAA2E;IAC3E,MAAM,EAAE,oBAAoB,CAAC;CAC7B,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG,eAAe,GAAG;IACrD;;;OAGG;IACH,iBAAiB,CAAC,EAAE,wBAAwB,CAAC;IAC7C;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAChC;;;OAGG;IACH,8BAA8B,CAAC,EAAE,OAAO,CAAC;IACzC;;;;;;;;;;;;OAYG;IACH,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC;;;OAGG;IACH,eAAe,CAAC,EAAE,sBAAsB,CAAC;IACzC;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,uBAAuB,CAAC;CAC3C,CAAC;AAgJF,QAAA,MAAM,WAAW,GAChB,QAAQ,kBAAkB,EAC1B,SAAS,gBAAgB,EACzB,cAAc,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAmErB,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,qBAAqB,GAAG,cAAc,CAAC"}
@@ -1,4 +1,4 @@
1
1
  /** Generated by scripts/generate-sdk-meta.ts — do not edit. */
2
2
  export declare const SDK_NAME = "@rasputin-ai/elysia";
3
- export declare const SDK_VERSION = "0.13.0";
3
+ export declare const SDK_VERSION = "0.14.0";
4
4
  //# sourceMappingURL=sdk-meta.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rasputin-ai/elysia",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Official Rasputin AI SDK for Elysia.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -35,8 +35,8 @@
35
35
  "access": "public"
36
36
  },
37
37
  "dependencies": {
38
- "@rasputin-ai/core": "0.13.0",
39
- "@rasputin-ai/node": "0.13.0"
38
+ "@rasputin-ai/core": "0.14.0",
39
+ "@rasputin-ai/node": "0.14.0"
40
40
  },
41
41
  "peerDependencies": {
42
42
  "elysia": ">=1.4.0 <2"