@elysiumjs/argus 0.1.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.
@@ -0,0 +1,173 @@
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 { SpanStatusCode, trace } from '@opentelemetry/api';
16
+ import { afterEach, beforeEach, describe, expect, it, mock, spyOn } from 'bun:test';
17
+
18
+ import { Traced } from '../src/decorators/traced';
19
+
20
+ describe('@Traced decorator', () => {
21
+ let mockSpan: any;
22
+ let mockTracer: any;
23
+ let getTracerSpy: ReturnType<typeof spyOn>;
24
+
25
+ beforeEach(() => {
26
+ mockSpan = {
27
+ setStatus: mock(() => {}),
28
+ recordException: mock(() => {}),
29
+ end: mock(() => {})
30
+ };
31
+
32
+ mockTracer = {
33
+ startActiveSpan: mock(async (_name: string, _options: any, fn: any) => {
34
+ return fn(mockSpan);
35
+ })
36
+ };
37
+
38
+ getTracerSpy = spyOn(trace, 'getTracer').mockReturnValue(mockTracer);
39
+ });
40
+
41
+ afterEach(() => {
42
+ getTracerSpy.mockRestore();
43
+ });
44
+
45
+ it('should call the original method and return its result', async () => {
46
+ class MyService {
47
+ @Traced()
48
+ async greet(name: string): Promise<string> {
49
+ return `Hello, ${name}`;
50
+ }
51
+ }
52
+
53
+ const service = new MyService();
54
+ const result = await service.greet('World');
55
+ expect(result).toBe('Hello, World');
56
+ });
57
+
58
+ it('should create a span with default name', async () => {
59
+ class MyService {
60
+ @Traced()
61
+ async doWork(): Promise<void> {}
62
+ }
63
+
64
+ const service = new MyService();
65
+ await service.doWork();
66
+
67
+ expect(mockTracer.startActiveSpan).toHaveBeenCalled();
68
+ const [spanName] = mockTracer.startActiveSpan.mock.calls[0];
69
+ expect(spanName).toBe('MyService.doWork');
70
+ });
71
+
72
+ it('should create a span with custom name', async () => {
73
+ class MyService {
74
+ @Traced('custom.span.name')
75
+ async doWork(): Promise<void> {}
76
+ }
77
+
78
+ const service = new MyService();
79
+ await service.doWork();
80
+
81
+ const [spanName] = mockTracer.startActiveSpan.mock.calls[0];
82
+ expect(spanName).toBe('custom.span.name');
83
+ });
84
+
85
+ it('should set OK status on success', async () => {
86
+ class MyService {
87
+ @Traced()
88
+ async success(): Promise<string> {
89
+ return 'ok';
90
+ }
91
+ }
92
+
93
+ const service = new MyService();
94
+ await service.success();
95
+
96
+ expect(mockSpan.setStatus).toHaveBeenCalledWith({
97
+ code: SpanStatusCode.OK
98
+ });
99
+ expect(mockSpan.end).toHaveBeenCalled();
100
+ });
101
+
102
+ it('should set ERROR status and record exception on failure', async () => {
103
+ const error = new Error('test failure');
104
+
105
+ class MyService {
106
+ @Traced()
107
+ async failing(): Promise<void> {
108
+ throw error;
109
+ }
110
+ }
111
+
112
+ const service = new MyService();
113
+ await expect(service.failing()).rejects.toThrow('test failure');
114
+
115
+ expect(mockSpan.setStatus).toHaveBeenCalledWith({
116
+ code: SpanStatusCode.ERROR,
117
+ message: 'test failure'
118
+ });
119
+ expect(mockSpan.recordException).toHaveBeenCalledWith(error);
120
+ expect(mockSpan.end).toHaveBeenCalled();
121
+ });
122
+
123
+ it('should handle non-Error throws', async () => {
124
+ class MyService {
125
+ @Traced()
126
+ async throwString(): Promise<void> {
127
+ throw 'string error';
128
+ }
129
+ }
130
+
131
+ const service = new MyService();
132
+ try {
133
+ await service.throwString();
134
+ } catch {
135
+ // expected
136
+ }
137
+
138
+ expect(mockSpan.setStatus).toHaveBeenCalledWith({
139
+ code: SpanStatusCode.ERROR,
140
+ message: 'Unknown error'
141
+ });
142
+ });
143
+
144
+ it('should always end the span', async () => {
145
+ class MyService {
146
+ @Traced()
147
+ async mayFail(): Promise<void> {
148
+ throw new Error('fail');
149
+ }
150
+ }
151
+
152
+ const service = new MyService();
153
+ try {
154
+ await service.mayFail();
155
+ } catch {
156
+ // expected
157
+ }
158
+
159
+ expect(mockSpan.end).toHaveBeenCalledTimes(1);
160
+ });
161
+
162
+ it('should use the @elysiumjs/argus tracer', async () => {
163
+ class MyService {
164
+ @Traced()
165
+ async work(): Promise<void> {}
166
+ }
167
+
168
+ const service = new MyService();
169
+ await service.work();
170
+
171
+ expect(getTracerSpy).toHaveBeenCalledWith('@elysiumjs/argus');
172
+ });
173
+ });
@@ -0,0 +1,65 @@
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 { describe, expect, it } from 'bun:test';
16
+
17
+ import type { TracingConfig } from '../src/types';
18
+
19
+ describe('TracingConfig type', () => {
20
+ it('should accept a minimal config', () => {
21
+ const config: TracingConfig = {
22
+ enabled: true,
23
+ serviceName: 'my-service',
24
+ exporter: 'console'
25
+ };
26
+
27
+ expect(config.enabled).toBe(true);
28
+ expect(config.serviceName).toBe('my-service');
29
+ expect(config.exporter).toBe('console');
30
+ });
31
+
32
+ it('should accept otlp-http exporter with endpoint', () => {
33
+ const config: TracingConfig = {
34
+ enabled: true,
35
+ serviceName: 'my-service',
36
+ exporter: 'otlp-http',
37
+ endpoint: 'http://localhost:4318/v1/traces'
38
+ };
39
+
40
+ expect(config.exporter).toBe('otlp-http');
41
+ expect(config.endpoint).toBe('http://localhost:4318/v1/traces');
42
+ });
43
+
44
+ it('should accept samplingRate', () => {
45
+ const config: TracingConfig = {
46
+ enabled: true,
47
+ serviceName: 'my-service',
48
+ exporter: 'console',
49
+ samplingRate: 0.5
50
+ };
51
+
52
+ expect(config.samplingRate).toBe(0.5);
53
+ });
54
+
55
+ it('should allow optional fields to be undefined', () => {
56
+ const config: TracingConfig = {
57
+ enabled: false,
58
+ serviceName: 'test',
59
+ exporter: 'console'
60
+ };
61
+
62
+ expect(config.endpoint).toBeUndefined();
63
+ expect(config.samplingRate).toBeUndefined();
64
+ });
65
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "noEmit": true
5
+ }
6
+ }