Skip to content

Commit 17430ee

Browse files
committed
refactor(execution): trace options
rename: - `traceHandler` to `onTraceEvent`
1 parent ff09c3c commit 17430ee

File tree

9 files changed

+73
-79
lines changed

9 files changed

+73
-79
lines changed

src/engine/executionEngine.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import { TraceableEngine } from './traceableEngine';
32
import { EngineTrace } from '../common/models/engineTrace.model';
43

src/engine/traceableEngine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { DEFAULT_TRACE_CONFIG, TraceOptions } from '../common/models/engineTrace
77
import { ExecutionTrace, ExecutionTraceExtractor, isExecutionTrace } from '../common/models/executionTrace.model';
88
import { Awaited } from '../common/utils/awaited';
99
import { extract } from '../common/utils/jsonQuery';
10+
import { executionTrace } from '../execution/trace';
1011
import { ExecutionTimer } from '../timer/executionTimer';
11-
import { executionTrace } from '../trace/trace';
1212

1313
/**
1414
* Represents a class for traceable execution of functions.

src/execution/memoize.decorator.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ describe('memoize decorator', () => {
9595
if (memoContext.isMemoized) {
9696
memoizedCalls++;
9797
}
98-
}) async throwData(name: string): Promise<string> {
98+
})
99+
async throwData(name: string): Promise<string> {
99100
totalFunctionCalls++;
100101
throw new Error(`hello ${name} but I throw!`);
101102
}

src/execution/memoize.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { execute } from './execute';
2-
import {
3-
memoizationDefaultTTL,
4-
memoizationKey,
5-
memoizationMaxTTL,
6-
MemoizeOptions
7-
} from '../common/models/executionMemoization.model';
2+
import { memoizationDefaultTTL, memoizationKey, memoizationMaxTTL, MemoizeOptions } from '../common/models/executionMemoization.model';
83
import { generateHashId } from '../common/utils/crypto';
94
import { extractFunctionMetadata } from '../common/utils/functionMetadata';
105

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { trace } from './traceDecorator';
1+
import { trace } from './trace.decorator';
22

33
describe('trace decorator', () => {
44
const executionTraceExpectation = {
@@ -21,15 +21,15 @@ describe('trace decorator', () => {
2121
};
2222

2323
describe('Tracing synchronous functions', () => {
24-
const traceHandlerMock = jest.fn();
25-
const traceHandlerMockThrows = jest.fn();
24+
const onTraceEventMock = jest.fn();
25+
const onTraceEventMockThrows = jest.fn();
2626

2727
class SyncClass {
2828
greetingFrom = ['hello from class'];
2929

3030
@trace(function (...args) {
3131
this.greetingFrom.push('hello from trace handler');
32-
traceHandlerMock(...args);
32+
onTraceEventMock(...args);
3333
expect(this.greetingFrom).toEqual(['hello from class', 'hello from method', 'hello from trace handler']);
3434
})
3535
helloWorld(): string {
@@ -38,7 +38,7 @@ describe('trace decorator', () => {
3838
}
3939

4040
@trace((...args) => {
41-
traceHandlerMockThrows(...args);
41+
onTraceEventMockThrows(...args);
4242
throw new Error('hello but I throw!');
4343
})
4444
helloWorldHandlerThrows(): string {
@@ -50,8 +50,8 @@ describe('trace decorator', () => {
5050
it('should trace a synchronous function and verify context', () => {
5151
const instance = new SyncClass();
5252
expect(instance.helloWorld()).toBe('Hello World');
53-
expect(traceHandlerMock).toHaveBeenCalledTimes(1);
54-
expect(traceHandlerMock).toHaveBeenCalledWith(
53+
expect(onTraceEventMock).toHaveBeenCalledTimes(1);
54+
expect(onTraceEventMock).toHaveBeenCalledWith(
5555
expect.objectContaining({
5656
metadata: expect.objectContaining({
5757
class: 'SyncClass',
@@ -69,8 +69,8 @@ describe('trace decorator', () => {
6969
it('should trace a synchronous function and verify helloWorldHandlerThrows', () => {
7070
const instance = new SyncClass();
7171
expect(() => instance.helloWorldHandlerThrows()).toThrowError('hello but I throw!');
72-
expect(traceHandlerMockThrows).toHaveBeenCalledTimes(1);
73-
expect(traceHandlerMockThrows).toHaveBeenCalledWith(
72+
expect(onTraceEventMockThrows).toHaveBeenCalledTimes(1);
73+
expect(onTraceEventMockThrows).toHaveBeenCalledWith(
7474
expect.objectContaining({
7575
metadata: expect.objectContaining({
7676
class: 'SyncClass',
@@ -124,11 +124,11 @@ describe('trace decorator', () => {
124124
});
125125
});
126126

127-
describe('Tracing function traceHandlerMock and traceContext', () => {
127+
describe('Tracing function onTraceEventMock and traceContext', () => {
128128
const traceContextDivision = { context: { metadata: { requestId: '12345' } } };
129129
const traceContextFetchData = { context: { metadata: { requestId: '6789' } } };
130-
const traceHandlerDivisionMock = jest.fn();
131-
const traceHandlerFetchDataMock = jest.fn();
130+
const onTraceEventDivisionMock = jest.fn();
131+
const onTraceEventFetchDataMock = jest.fn();
132132

133133
function empty(): MethodDecorator {
134134
return function (target: unknown, propertyKey: string, descriptor: PropertyDescriptor) {
@@ -140,7 +140,7 @@ describe('trace decorator', () => {
140140
}
141141

142142
class MyClass {
143-
@trace(traceHandlerDivisionMock, traceContextDivision, { contextKey: '__execution' })
143+
@trace(onTraceEventDivisionMock, traceContextDivision, { contextKey: '__execution' })
144144
divisionFunction(x: number, y: number, traceContext: Record<string, unknown> = {}): number {
145145
if (y === 0) {
146146
traceContext['narratives'] = [`Throwing because division of ${x} by ${y}`];
@@ -150,7 +150,7 @@ describe('trace decorator', () => {
150150
return x / y;
151151
}
152152

153-
@trace(traceHandlerFetchDataMock, traceContextFetchData, { contextKey: '__execution' })
153+
@trace(onTraceEventFetchDataMock, traceContextFetchData, { contextKey: '__execution' })
154154
async fetchDataFunction(url: string, traceContext: Record<string, unknown> = {}): Promise<{ data: string }> {
155155
traceContext['narratives'] = [`Fetching data from ${url}`];
156156
if (!url.startsWith('http')) {
@@ -160,24 +160,24 @@ describe('trace decorator', () => {
160160
return { data: 'Success' };
161161
}
162162

163-
@trace(traceHandlerDivisionMock, traceContextDivision)
163+
@trace(onTraceEventDivisionMock, traceContextDivision)
164164
@empty()
165165
divisionFunctionOnAnotherDecorator(x: number, y: number, traceContext: Record<string, unknown> = {}): number {
166166
return this.divisionFunction(x, y, traceContext);
167167
}
168168

169-
@trace(traceHandlerFetchDataMock, traceContextFetchData)
169+
@trace(onTraceEventFetchDataMock, traceContextFetchData)
170170
@empty()
171171
async fetchDataFunctionOnAnotherDecorator(
172172
url: string,
173173
traceContext: Record<string, unknown> = {}
174174
): Promise<{
175-
data: string;
176-
}> {
175+
data: string;
176+
}> {
177177
return this.fetchDataFunction(url, traceContext);
178178
}
179179

180-
@trace(traceHandlerFetchDataMock, traceContextFetchData, { errorStrategy: 'catch' })
180+
@trace(onTraceEventFetchDataMock, traceContextFetchData, { errorStrategy: 'catch' })
181181
@empty()
182182
async fetchDataFunctionOnAnotherDecoratorCoughtError(
183183
url: string,
@@ -188,14 +188,14 @@ describe('trace decorator', () => {
188188
}
189189

190190
beforeEach(() => {
191-
traceHandlerFetchDataMock.mockClear();
192-
traceHandlerDivisionMock.mockClear();
191+
onTraceEventFetchDataMock.mockClear();
192+
onTraceEventDivisionMock.mockClear();
193193
});
194194

195-
it('should sync trace successfully and pass correct traceHandlerMock and traceContext', () => {
195+
it('should sync trace successfully and pass correct onTraceEventMock and traceContext', () => {
196196
const classInstance = new MyClass();
197197
const response = classInstance.divisionFunction(1, 2);
198-
expect(traceHandlerDivisionMock).toHaveBeenCalledWith(
198+
expect(onTraceEventDivisionMock).toHaveBeenCalledWith(
199199
expect.objectContaining({
200200
metadata: expect.objectContaining({
201201
class: 'MyClass',
@@ -213,10 +213,10 @@ describe('trace decorator', () => {
213213
expect(response).toEqual(0.5);
214214
});
215215

216-
it('should sync trace errors and pass correct traceHandlerMock and traceContext', () => {
216+
it('should sync trace errors and pass correct onTraceEventMock and traceContext', () => {
217217
expect(() => new MyClass().divisionFunction(1, 0)).toThrow('Throwing because division by zero is not allowed.');
218218

219-
expect(traceHandlerDivisionMock).toHaveBeenCalledWith(
219+
expect(onTraceEventDivisionMock).toHaveBeenCalledWith(
220220
expect.objectContaining({
221221
metadata: expect.objectContaining({
222222
class: 'MyClass',
@@ -233,9 +233,9 @@ describe('trace decorator', () => {
233233
);
234234
});
235235

236-
it('should async trace successfully and pass correct traceHandlerMock and traceContext', async () => {
236+
it('should async trace successfully and pass correct onTraceEventMock and traceContext', async () => {
237237
const response = await new MyClass().fetchDataFunction('https://api.example.com/data');
238-
expect(traceHandlerFetchDataMock).toHaveBeenCalledWith(
238+
expect(onTraceEventFetchDataMock).toHaveBeenCalledWith(
239239
expect.objectContaining({
240240
metadata: expect.objectContaining({
241241
class: 'MyClass',
@@ -253,9 +253,9 @@ describe('trace decorator', () => {
253253
expect(response).toMatchObject({ data: 'Success' });
254254
});
255255

256-
it('should async trace errors and pass correct traceHandlerMock and traceContext', async () => {
256+
it('should async trace errors and pass correct onTraceEventMock and traceContext', async () => {
257257
await expect(new MyClass().fetchDataFunction('invalid-url')).rejects.toThrow('Invalid URL provided.');
258-
expect(traceHandlerFetchDataMock).toHaveBeenCalledWith(
258+
expect(onTraceEventFetchDataMock).toHaveBeenCalledWith(
259259
expect.objectContaining({
260260
metadata: expect.objectContaining({
261261
class: 'MyClass',
@@ -275,7 +275,7 @@ describe('trace decorator', () => {
275275
it('should async trace successfully divisionFunctionOnAnotherDecorator', async () => {
276276
const classInstance = new MyClass();
277277
const response = classInstance.divisionFunctionOnAnotherDecorator(1, 2);
278-
expect(traceHandlerDivisionMock).toHaveBeenNthCalledWith(
278+
expect(onTraceEventDivisionMock).toHaveBeenNthCalledWith(
279279
2,
280280
expect.objectContaining({
281281
metadata: expect.objectContaining({
@@ -297,7 +297,7 @@ describe('trace decorator', () => {
297297

298298
it('should async trace error divisionFunctionOnAnotherDecorator', async () => {
299299
expect(() => new MyClass().divisionFunctionOnAnotherDecorator(1, 0)).toThrow('Throwing because division by zero is not allowed.');
300-
expect(traceHandlerDivisionMock).toHaveBeenNthCalledWith(
300+
expect(onTraceEventDivisionMock).toHaveBeenNthCalledWith(
301301
2,
302302
expect.objectContaining({
303303
metadata: expect.objectContaining({
@@ -318,7 +318,7 @@ describe('trace decorator', () => {
318318

319319
it('should async trace successfully fetchDataFunctionOnAnotherDecorator', async () => {
320320
const response = await new MyClass().fetchDataFunctionOnAnotherDecorator('https://api.example.com/data');
321-
expect(traceHandlerFetchDataMock).toHaveBeenNthCalledWith(
321+
expect(onTraceEventFetchDataMock).toHaveBeenNthCalledWith(
322322
2,
323323
expect.objectContaining({
324324
metadata: expect.objectContaining({
@@ -340,7 +340,7 @@ describe('trace decorator', () => {
340340

341341
it('should async trace error fetchDataFunctionOnAnotherDecorator', async () => {
342342
await expect(new MyClass().fetchDataFunctionOnAnotherDecorator('invalid-url')).rejects.toThrow('Invalid URL provided.');
343-
expect(traceHandlerFetchDataMock).toHaveBeenNthCalledWith(
343+
expect(onTraceEventFetchDataMock).toHaveBeenNthCalledWith(
344344
2,
345345
expect.objectContaining({
346346
metadata: expect.objectContaining({
@@ -361,7 +361,7 @@ describe('trace decorator', () => {
361361

362362
it('should async trace error fetchDataFunctionOnAnotherDecorator cought', async () => {
363363
const response = await new MyClass().fetchDataFunctionOnAnotherDecoratorCoughtError('invalid-url');
364-
expect(traceHandlerFetchDataMock).toHaveBeenNthCalledWith(
364+
expect(onTraceEventFetchDataMock).toHaveBeenNthCalledWith(
365365
2,
366366
expect.objectContaining({
367367
metadata: expect.objectContaining({
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { isAsync } from '../common/utils/isAsync';
55
/**
66
* Method decorator to trace function execution, capturing metadata, inputs, outputs, and errors.
77
*
8-
* @param traceHandler - handle function of the trace context.
8+
* @param onTraceEvent - handle function of the trace context.
99
* @param additionalContext - Additional metadata to attach to the trace context.
1010
* @param options - Configuration options:
1111
* - `contextKey`: Key to store trace context on the instance.
@@ -14,7 +14,7 @@ import { isAsync } from '../common/utils/isAsync';
1414
* @returns A method decorator that wraps the original function with execution tracing.
1515
*/
1616
export function trace<O>(
17-
traceHandler: (traceContext: TraceContext<O>) => void,
17+
onTraceEvent: (traceContext: TraceContext<O>) => void,
1818
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1919
additionalContext: Record<string, any> = {},
2020
options: { contextKey?: string; errorStrategy?: 'catch' | 'throw' } = {
@@ -37,7 +37,7 @@ export function trace<O>(
3737
originalMethod.bind(this),
3838
args,
3939
(traceContext) => {
40-
return (traceHandler.bind(this) as typeof traceHandler)({ ...traceContext, ...thisTraceContext });
40+
return (onTraceEvent.bind(this) as typeof onTraceEvent)({ ...traceContext, ...thisTraceContext });
4141
},
4242
options
4343
)?.then((r) => (options?.errorStrategy === 'catch' ? r.errors : r.outputs));
@@ -46,7 +46,7 @@ export function trace<O>(
4646
originalMethod.bind(this) as () => O,
4747
args,
4848
(traceContext) => {
49-
return (traceHandler.bind(this) as typeof traceHandler)({ ...traceContext, ...thisTraceContext });
49+
return (onTraceEvent.bind(this) as typeof onTraceEvent)({ ...traceContext, ...thisTraceContext });
5050
},
5151
options
5252
);

0 commit comments

Comments
 (0)