@elysiumjs/argus 0.1.1 → 0.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@elysiumjs/argus",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "OpenTelemetry telemetry package for the Elysium.js framework.",
6
6
  "private": false,
@@ -24,13 +24,11 @@
24
24
  "type": "module",
25
25
  "main": "dist/index.js",
26
26
  "types": "dist/index.d.ts",
27
- "sideEffects": [
28
- "./src/sdk.ts"
29
- ],
27
+ "sideEffects": false,
30
28
  "scripts": {
31
29
  "format": "prettier --write \"./src/**/*.ts\"",
32
30
  "clean": "rimraf dist",
33
- "build:js": "bun build ./src/index.ts --outdir ./dist --target bun -e @elysiumjs/* -e @opentelemetry/* --root ./src --minify",
31
+ "build:js": "bun build ./src/index.ts --outdir ./dist --target bun -e @elysiumjs/* -e @opentelemetry/* --root ./src --minify-syntax --minify-whitespace",
34
32
  "build:ts": "tsc",
35
33
  "build": "bun run clean && bun run build:js && bun run build:ts",
36
34
  "test": "bun test",
@@ -39,20 +37,20 @@
39
37
  "prepublishOnly": "bun run build"
40
38
  },
41
39
  "dependencies": {
42
- "@opentelemetry/api": "^1.9.0",
43
- "@opentelemetry/sdk-node": "^0.57.0",
44
- "@opentelemetry/sdk-trace-base": "^1.30.0",
45
- "@opentelemetry/exporter-trace-otlp-http": "^0.57.0",
46
- "@opentelemetry/resources": "^1.30.0",
47
- "@opentelemetry/semantic-conventions": "^1.30.0"
40
+ "@opentelemetry/api": "^1.10.0",
41
+ "@opentelemetry/exporter-trace-otlp-http": "^0.300.0",
42
+ "@opentelemetry/resources": "^3.0.0",
43
+ "@opentelemetry/sdk-node": "^0.300.0",
44
+ "@opentelemetry/sdk-trace-base": "^2.12.0",
45
+ "@opentelemetry/semantic-conventions": "^1.43.0"
48
46
  },
49
47
  "devDependencies": {
50
- "@elysiumjs/core": "0.6.1",
51
- "@elysiumjs/hermes": "0.3.1"
48
+ "@elysiumjs/core": "0.8.0",
49
+ "@elysiumjs/hermes": "0.4.0"
52
50
  },
53
51
  "peerDependencies": {
54
- "@elysiumjs/core": "^0.6.1",
55
- "@elysiumjs/hermes": "^0.3.1"
52
+ "@elysiumjs/core": "^0.8.0",
53
+ "@elysiumjs/hermes": "^0.4.0"
56
54
  },
57
55
  "peerDependenciesMeta": {
58
56
  "@elysiumjs/hermes": {
@@ -0,0 +1,55 @@
1
+ // Copyright (c) 2026-present Workbud Technologies Inc. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ import type { TracingConfig } from './types';
16
+
17
+ import { Module } from '@elysiumjs/core';
18
+
19
+ import { argusPlugin } from './plugin';
20
+ import { ArgusSDK } from './sdk';
21
+
22
+ /**
23
+ * Configurable module that starts the {@link ArgusSDK} on init, installs the request-span
24
+ * plugin on the root app when serving, and shuts the SDK down on destroy.
25
+ *
26
+ * If the SDK was already initialised (e.g. by a preload), the module leaves it running and
27
+ * does not shut it down.
28
+ *
29
+ * `serviceName` has no sensible default and is required.
30
+ * @author Axel Nana <axel.nana@workbud.com>
31
+ */
32
+ @Module.register({})
33
+ export class ArgusModule extends Module.configurable<TracingConfig>({
34
+ defaults: { enabled: true, exporter: 'console' },
35
+ required: ['serviceName']
36
+ }) {
37
+ #started = false;
38
+
39
+ public override onInit(): void {
40
+ // A preload (or anything else) already owns the SDK: leave it alone.
41
+ if (ArgusSDK.isInitialized()) return;
42
+ ArgusSDK.initialize(this.config);
43
+ this.#started = true;
44
+ }
45
+
46
+ public override onServe(app: Parameters<Module['onServe']>[0]): void {
47
+ app.use(argusPlugin());
48
+ }
49
+
50
+ public override async onDestroy(): Promise<void> {
51
+ if (!this.#started) return;
52
+ this.#started = false;
53
+ await ArgusSDK.shutdown();
54
+ }
55
+ }
package/src/index.ts CHANGED
@@ -18,3 +18,4 @@ export { ArgusSDK } from './sdk';
18
18
  export { Traced } from './decorators/traced';
19
19
  export { withTraceContext } from './correlation';
20
20
  export { argusPlugin } from './plugin';
21
+ export { ArgusModule } from './argus.module';
package/src/sdk.ts CHANGED
@@ -15,7 +15,7 @@
15
15
  import type { TracingConfig } from './types';
16
16
 
17
17
  import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
18
- import { Resource } from '@opentelemetry/resources';
18
+ import { resourceFromAttributes } from '@opentelemetry/resources';
19
19
  import { NodeSDK } from '@opentelemetry/sdk-node';
20
20
  import { ConsoleSpanExporter, TraceIdRatioBasedSampler } from '@opentelemetry/sdk-trace-base';
21
21
  import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
@@ -23,10 +23,13 @@ import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
23
23
  /**
24
24
  * ArgusSDK initializes OpenTelemetry for the Elysium framework.
25
25
  *
26
- * CRITICAL: This MUST be called before any application code runs.
27
- * Auto-instrumentation patches modules at import time; if app code
28
- * imports modules before OTel is initialized, those imports won't
29
- * be instrumented.
26
+ * CRITICAL: For full auto-instrumentation this MUST be called before any
27
+ * application code runs (e.g. from a `--preload` file). Auto-instrumentation
28
+ * patches modules at import time; modules imported before OTel is initialized
29
+ * won't be instrumented. `ArgusModule` initialises the SDK in `onInit`, which
30
+ * only covers modules loaded afterwards; when the SDK was already initialised
31
+ * (e.g. by a preload), the module detects it with {@link ArgusSDK.isInitialized}
32
+ * and leaves it alone.
30
33
  *
31
34
  * @example
32
35
  * ```typescript
@@ -44,11 +47,19 @@ import { ATTR_SERVICE_NAME } from '@opentelemetry/semantic-conventions';
44
47
  * @author Axel Nana <axel.nana@workbud.com>
45
48
  */
46
49
  export class ArgusSDK {
47
- private static instance: ArgusSDK;
50
+ private static instance: ArgusSDK | undefined;
48
51
  private sdk: NodeSDK | null = null;
49
52
 
50
53
  private constructor() {}
51
54
 
55
+ /**
56
+ * Whether {@link ArgusSDK.initialize} has already been called.
57
+ * @author Axel Nana <axel.nana@workbud.com>
58
+ */
59
+ public static isInitialized(): boolean {
60
+ return ArgusSDK.instance !== undefined;
61
+ }
62
+
52
63
  /**
53
64
  * Initializes the OpenTelemetry SDK.
54
65
  * Can only be called once — subsequent calls are ignored with a warning.
@@ -68,7 +79,7 @@ export class ArgusSDK {
68
79
  const exporter = ArgusSDK.createExporter(config);
69
80
 
70
81
  ArgusSDK.instance.sdk = new NodeSDK({
71
- resource: new Resource({
82
+ resource: resourceFromAttributes({
72
83
  [ATTR_SERVICE_NAME]: config.serviceName
73
84
  }),
74
85
  traceExporter: exporter,
@@ -106,5 +117,6 @@ export class ArgusSDK {
106
117
  if (ArgusSDK.instance?.sdk) {
107
118
  await ArgusSDK.instance.sdk.shutdown();
108
119
  }
120
+ ArgusSDK.instance = undefined;
109
121
  }
110
122
  }
@@ -0,0 +1,120 @@
1
+ // Copyright (c) 2026-present Workbud Technologies Inc. All rights reserved.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ import { readFileSync } from 'node:fs';
16
+ import { join } from 'node:path';
17
+
18
+ import { Application, MissingModuleConfigError, Module } from '@elysiumjs/core';
19
+ import { afterEach, describe, expect, it, mock, spyOn } from 'bun:test';
20
+ import { Elysia } from 'elysia';
21
+
22
+ import { ArgusModule, argusPlugin, ArgusSDK } from '../src';
23
+
24
+ const withConfig = (config: Record<string, unknown>) => {
25
+ @Module.register({ imports: [ArgusModule.with(config as any)] })
26
+ class AppModule extends Module {}
27
+ return AppModule;
28
+ };
29
+
30
+ describe('ArgusModule', () => {
31
+ afterEach(() => {
32
+ mock.restore();
33
+ (ArgusSDK as any).instance = undefined;
34
+ });
35
+
36
+ it('has no bare side-effect imports in its entry point', () => {
37
+ const source = readFileSync(join(import.meta.dir, '../src/index.ts'), 'utf8');
38
+ const bare = source.match(/^import\s+['"][^'"]+['"]/gm) ?? [];
39
+ expect(bare).toEqual([]);
40
+ });
41
+
42
+ it('requires a serviceName', async () => {
43
+ @Module.register({ imports: [ArgusModule] })
44
+ class Bare extends Module {}
45
+ await expect(Application.testing({ modules: [Bare] })).rejects.toBeInstanceOf(
46
+ MissingModuleConfigError
47
+ );
48
+ });
49
+
50
+ it('starts the SDK with the resolved config on init', async () => {
51
+ const init = spyOn(ArgusSDK, 'initialize').mockImplementation(() => {});
52
+ const app = await Application.testing({
53
+ modules: [withConfig({ serviceName: 'svc' })],
54
+ boot: 'eager'
55
+ });
56
+ try {
57
+ expect(init).toHaveBeenCalledWith(
58
+ expect.objectContaining({ serviceName: 'svc', enabled: true, exporter: 'console' })
59
+ );
60
+ } finally {
61
+ await app.shutdown();
62
+ }
63
+ });
64
+
65
+ it('installs the argus plugin on serve', async () => {
66
+ spyOn(ArgusSDK, 'initialize').mockImplementation(() => {});
67
+ const app = await Application.testing({
68
+ modules: [withConfig({ serviceName: 'svc' })],
69
+ boot: 'eager'
70
+ });
71
+ try {
72
+ const use = mock((_plugin: unknown) => {});
73
+ const instance = app.kernel.moduleInstance(app.kernel.graph.node(ArgusModule)!);
74
+ await instance.onServe({ use } as any);
75
+ expect(use).toHaveBeenCalledTimes(1);
76
+ const installed = (use.mock.calls[0] as unknown[])[0] as any;
77
+ expect(installed).toBeInstanceOf(Elysia);
78
+ expect(installed.config.name).toBe((argusPlugin() as any).config.name);
79
+ } finally {
80
+ await app.shutdown();
81
+ }
82
+ });
83
+
84
+ it('shuts the SDK down on destroy when it was started', async () => {
85
+ spyOn(ArgusSDK, 'initialize').mockImplementation(() => {});
86
+ const shutdown = spyOn(ArgusSDK, 'shutdown').mockResolvedValue(undefined);
87
+ const app = await Application.testing({
88
+ modules: [withConfig({ serviceName: 'svc' })],
89
+ boot: 'eager'
90
+ });
91
+ await app.shutdown();
92
+ expect(shutdown).toHaveBeenCalledTimes(1);
93
+ });
94
+
95
+ it('leaves a pre-initialised SDK alone', async () => {
96
+ const init = spyOn(ArgusSDK, 'initialize').mockImplementation(() => {});
97
+ const shutdown = spyOn(ArgusSDK, 'shutdown').mockResolvedValue(undefined);
98
+ spyOn(ArgusSDK, 'isInitialized').mockReturnValue(true);
99
+ const app = await Application.testing({
100
+ modules: [withConfig({ serviceName: 'svc' })],
101
+ boot: 'eager'
102
+ });
103
+ await app.shutdown();
104
+ expect(init).not.toHaveBeenCalled();
105
+ expect(shutdown).not.toHaveBeenCalled();
106
+ });
107
+
108
+ it('does not shut the SDK down when its init failed', async () => {
109
+ spyOn(ArgusSDK, 'initialize').mockImplementation(() => {
110
+ throw new Error('boom');
111
+ });
112
+ const shutdown = spyOn(ArgusSDK, 'shutdown').mockResolvedValue(undefined);
113
+ const app = await Application.testing({
114
+ modules: [withConfig({ serviceName: 'svc' })],
115
+ boot: 'eager'
116
+ }).catch(() => undefined);
117
+ await app?.shutdown();
118
+ expect(shutdown).not.toHaveBeenCalled();
119
+ });
120
+ });
package/tests/setup.ts CHANGED
@@ -11,14 +11,3 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
-
15
- import 'reflect-metadata';
16
-
17
- import { mock } from 'bun:test';
18
-
19
- mock.module('node:async_hooks', () => ({
20
- AsyncLocalStorage: class {
21
- public run = mock((_map: any, callback: any) => callback());
22
- public disable = mock();
23
- }
24
- }));