1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224 | 1
1
1
1
1
1
1
1
1
1
6
6
6
6
2
13
12
6
6
6
6
6
6
6
6
6
6
6
6
6
1
1
6
6
1
6
6
18
6
6
6
6
6
6
6
6
6
6
6
5
3
1
2
1
1
5
1
| import 'reflect-metadata';
import {Enabled, PropEnabled} from '../../configurable';
import {Env, Props} from '../../env';
import {Providers} from '../../providers';
import {getTimeFactory, TimeFactory} from '../../time';
import {Duration, LazyDuration, TimeMeasurement} from '../../time/primitives';
import {joinFqn, Tags} from '../../util';
import {APM_ENV} from '../constants';
import {Settable} from '../settable';
import {TimerWrapper, TimerWrapperFactory} from '../timed';
import {Metric, ReadOnlyValue} from './metric';
export type Timer = Metric & {
start: (tags?: Tags) => TimeMeasurement | undefined;
end: (tags?: Tags) => Duration | undefined;
mark: (name: string, tags?: Tags) => Duration | undefined;
elapsed: () => Duration | undefined;
}
export class LocalTimer implements Timer, TimeFactory {
protected marks: Record<string, ReadOnlyValue<TimeMeasurement>> = {};
protected readonly timeFactory: TimeFactory;
protected done = false;
constructor(readonly name: string, readonly tags: Tags, timeFactory?: TimeFactory) {
this.timeFactory = timeFactory ? timeFactory : getTimeFactory();
}
get isDone() {
return this.done;
}
get values(): ReadOnlyValue<TimeMeasurement>[] {
return Object.values(this.marks);
}
asDuration(): Duration {
return this.marks.end?.value as LazyDuration;
}
reset(): void {
this.marks = {};
}
now(): TimeMeasurement {
return this.timeFactory.now();
}
elapsed(): Duration | undefined {
return this.marks.start && new LazyDuration(
this.marks.start.value.asNanoseconds(),
this.now().asNanoseconds(),
);
}
mark(name: string, tags: Tags = {}): Duration | undefined {
const result = this.elapsed();
Eif (result) {
this.marks[name] = new ReadOnlyValue<TimeMeasurement>(
joinFqn(name, this.name),
result,
{ ...this.tags, ...tags, mark: name },
);
}
return result;
}
end(tags?: Tags): Duration | undefined {
Eif (!this.done) {
this.done = true;
this.mark('end', tags);
}
return this.asDuration();
}
start(tags: Tags = {}): TimeMeasurement {
let result: TimeMeasurement;
Eif (!this.marks.start) {
result = this.now();
this.marks.start = new ReadOnlyValue<TimeMeasurement>(
joinFqn('start', this.name),
result,
{ ...this.tags, ...tags },
);
}
else {
result = this.marks.start.value;
}
return result;
}
}
export interface TimerFactory {
(name: string, tags?: Tags): Timer;
}
let timerFactory: TimerFactory;
export function setTimerFactory(factory: TimerFactory): TimerFactory {
timerFactory = factory;
return timerFactory;
}
export function getTimerFactory(): TimerFactory {
return timerFactory ?
timerFactory :
(name: string, tags: Tags = {}): Timer => new LocalTimer(name, tags, getTimeFactory());
}
export type LocalTimerOptions = {
prefix?: string;
suffix?: string;
tags?: Tags;
env?: Props;
parent?: LocalTimerWrapper<any, any>;
timerFactory?: TimerFactory;
}
export class LocalTimerWrapper<T, M extends Settable<Timer>> implements TimerWrapper<T>, Enabled {
readonly rawName: string;
readonly name: string;
readonly fqn: string;
protected readonly timerFactory: TimerFactory;
protected readonly _enabled: PropEnabled;
protected readonly options: LocalTimerOptions;
protected readonly tags: Tags;
constructor(name: string, tags: Tags, options: LocalTimerOptions, protected readonly store: M) {
this.rawName = name;
this.name = [options.prefix, name, options.suffix].filter(p => p).join('.');
this.fqn = joinFqn(this.name, options.parent);
this.timerFactory = options.timerFactory || getTimerFactory();
this.options = { ...options };
this.tags = {
...options.tags,
...tags,
};
this._enabled = options.parent?._enabled.extend(this.rawName, 'true') ||
new PropEnabled(
{
name: this.name,
global: APM_ENV.timer.envKey,
},
Providers.useOrProvide(Env, this.options.env)?.getConfig(this.fqn),
'true',
);
}
enabled(key?: string): boolean {
return this._enabled.enabled(key);
}
wrap(func: (...args: unknown[]) => T): (...args: unknown[]) => T {
const timer = this.timerFactory(this.name, this.tags);
this.store.set(timer, this.tags);
return function localTimerWrapper(...args: unknown[]): T {
timer.start();
let result: T;
try {
result = func(...args);
if (result instanceof Promise) {
result.then(() => timer.end()).catch(() => {
timer.end()
});
}
else {
timer.end();
}
}
catch (err) {
timer.end();
throw err;
}
return result;
}
}
}
export class LocalTimerFactory<M extends Settable<Timer>> implements TimerWrapperFactory {
protected readonly options: LocalTimerOptions;
constructor(options: LocalTimerOptions, protected readonly store: M) {
this.options = { ...options };
}
asyncTimer<T>(name: string, tags: Tags = {}): TimerWrapper<Promise<T>> {
return new LocalTimerWrapper(name, tags, this.options, this.store);
}
timer<T>(name: string, tags: Tags = {}): TimerWrapper<T> {
return new LocalTimerWrapper(name, tags, this.options, this.store);
}
}
|