-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathlogger.ts
1001 lines (925 loc) · 29.7 KB
/
logger.ts
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Copyright (c) Microsoft Corporation and contributors. All rights reserved.
* Licensed under the MIT License.
*/
import { performance } from "@fluid-internal/client-utils";
import {
type ITelemetryBaseEvent,
type ITelemetryBaseLogger,
LogLevel,
type Tagged,
type TelemetryBaseEventPropertyType,
} from "@fluidframework/core-interfaces";
import {
CachedConfigProvider,
loggerIsMonitoringContext,
mixinMonitoringContext,
} from "./config.js";
import {
extractLogSafeErrorProperties,
generateStack,
isILoggingError,
isTaggedTelemetryPropertyValue,
} from "./errorLogging.js";
import type {
ITelemetryErrorEventExt,
ITelemetryEventExt,
ITelemetryGenericEventExt,
ITelemetryLoggerExt,
ITelemetryPerformanceEventExt,
ITelemetryPropertiesExt,
TelemetryEventCategory,
TelemetryEventPropertyTypeExt,
} from "./telemetryTypes.js";
/**
* Broad classifications to be applied to individual properties as they're prepared to be logged to telemetry.
*
* @privateRemarks Please do not modify existing entries, to maintain backwards compatibility.
*
* @internal
*/
export enum TelemetryDataTag {
/**
* Data containing terms or IDs from code packages that may have been dynamically loaded
*/
CodeArtifact = "CodeArtifact",
/**
* Personal data of a variety of classifications that pertains to the user
*/
UserData = "UserData",
}
/**
* @legacy
* @alpha
*/
export type TelemetryEventPropertyTypes = ITelemetryPropertiesExt[string];
/**
* @legacy
* @alpha
*/
export type ITelemetryLoggerPropertyBag = Record<
string,
TelemetryEventPropertyTypes | (() => TelemetryEventPropertyTypes)
>;
/**
* @legacy
* @alpha
*/
export interface ITelemetryLoggerPropertyBags {
all?: ITelemetryLoggerPropertyBag;
error?: ITelemetryLoggerPropertyBag;
}
/**
* Attempts to parse number from string.
* If it fails, it will return the original string.
*
* @remarks
* Used to make telemetry data typed (and support math operations, like comparison),
* in places where we do expect numbers (like contentsize/duration property in http header).
*
* @internal
*/
// eslint-disable-next-line @rushstack/no-new-null
export function numberFromString(str: string | null | undefined): string | number | undefined {
if (str === undefined || str === null) {
return undefined;
}
const num = Number(str);
return Number.isNaN(num) ? str : num;
}
// TODO: add docs
// eslint-disable-next-line jsdoc/require-description
/**
* @internal
*/
export function formatTick(tick: number): number {
return Math.floor(tick);
}
/**
* String used to concatenate the namespace of parent loggers and their child loggers.
* @internal
*/
export const eventNamespaceSeparator = ":" as const;
/**
* TelemetryLogger class contains various helper telemetry methods,
* encoding in one place schemas for various types of Fluid telemetry events.
* Creates sub-logger that appends properties to all events
*/
export abstract class TelemetryLogger implements ITelemetryLoggerExt {
/**
* {@inheritDoc eventNamespaceSeparator}
*/
public static readonly eventNamespaceSeparator = eventNamespaceSeparator;
public static sanitizePkgName(name: string): string {
return name.replace("@", "").replace("/", "-");
}
/**
* Take an unknown error object and add the appropriate info from it to the event. Message and stack will be copied
* over from the error object, along with other telemetry properties if it's an ILoggingError.
* @param event - Event being logged
* @param error - Error to extract info from
* @param fetchStack - Whether to fetch the current callstack if error.stack is undefined
*/
public static prepareErrorObject(
event: ITelemetryBaseEvent,
error: unknown,
fetchStack: boolean,
): void {
const { message, errorType, stack } = extractLogSafeErrorProperties(
error,
true /* sanitizeStack */,
);
// First, copy over error message, stack, and errorType directly (overwrite if present on event)
event.stack = stack;
event.error = message; // Note that the error message goes on the 'error' field
event.errorType = errorType;
if (isILoggingError(error)) {
// Add any other telemetry properties from the LoggingError
const telemetryProp = error.getTelemetryProperties();
for (const key of Object.keys(telemetryProp)) {
if (event[key] !== undefined) {
// Don't overwrite existing properties on the event
continue;
}
event[key] = telemetryProp[key];
}
}
// Collect stack if we were not able to extract it from error
if (event.stack === undefined && fetchStack) {
event.stack = generateStack();
}
}
public constructor(
protected readonly namespace?: string,
protected readonly properties?: ITelemetryLoggerPropertyBags,
) {}
/**
* Send an event with the logger
*
* @param event - the event to send
*/
public abstract send(event: ITelemetryBaseEvent, logLevel?: LogLevel): void;
/**
* Send a telemetry event with the logger
*
* @param event - the event to send
* @param error - optional error object to log
* @param logLevel - optional level of the log. It category of event is set as error,
* then the logLevel will be upgraded to be an error.
*/
public sendTelemetryEvent(
event: ITelemetryGenericEventExt,
error?: unknown,
logLevel: typeof LogLevel.verbose | typeof LogLevel.default = LogLevel.default,
): void {
this.sendTelemetryEventCore(
{ ...event, category: event.category ?? "generic" },
error,
event.category === "error" ? LogLevel.error : logLevel,
);
}
/**
* Send a telemetry event with the logger
*
* @param event - the event to send
* @param error - optional error object to log
* @param logLevel - optional level of the log.
*/
protected sendTelemetryEventCore(
event: ITelemetryGenericEventExt & { category: TelemetryEventCategory },
error?: unknown,
logLevel?: LogLevel,
): void {
const newEvent = convertToBaseEvent(event);
if (error !== undefined) {
TelemetryLogger.prepareErrorObject(newEvent, error, false);
}
// Will include Nan & Infinity, but probably we do not care
if (typeof newEvent.duration === "number") {
newEvent.duration = formatTick(newEvent.duration);
}
this.send(newEvent, logLevel);
}
/**
* Send an error telemetry event with the logger
*
* @param event - the event to send
* @param error - optional error object to log
*/
public sendErrorEvent(event: ITelemetryErrorEventExt, error?: unknown): void {
this.sendTelemetryEventCore(
{
// ensure the error field has some value,
// this can and will be overridden by event, or error
error: event.eventName,
...event,
category: "error",
},
error,
LogLevel.error,
);
}
/**
* Send a performance telemetry event with the logger
*
* @param event - Event to send
* @param error - optional error object to log
* @param logLevel - optional level of the log. It category of event is set as error,
* then the logLevel will be upgraded to be an error.
*/
public sendPerformanceEvent(
event: ITelemetryPerformanceEventExt,
error?: unknown,
logLevel: typeof LogLevel.verbose | typeof LogLevel.default = LogLevel.default,
): void {
const perfEvent = {
...event,
category: event.category ?? "performance",
};
this.sendTelemetryEventCore(
perfEvent,
error,
perfEvent.category === "error" ? LogLevel.error : logLevel,
);
}
protected prepareEvent(event: ITelemetryBaseEvent): ITelemetryBaseEvent {
const includeErrorProps = event.category === "error" || event.error !== undefined;
const newEvent: ITelemetryBaseEvent = {
...event,
};
if (this.namespace !== undefined) {
newEvent.eventName = `${this.namespace}${TelemetryLogger.eventNamespaceSeparator}${newEvent.eventName}`;
}
return this.extendProperties(newEvent, includeErrorProps);
}
private extendProperties<
T extends ITelemetryLoggerPropertyBag = ITelemetryLoggerPropertyBag,
>(toExtend: T, includeErrorProps: boolean): T {
const eventLike: ITelemetryLoggerPropertyBag = toExtend;
if (this.properties) {
const properties: (undefined | ITelemetryLoggerPropertyBag)[] = [];
properties.push(this.properties.all);
if (includeErrorProps) {
properties.push(this.properties.error);
}
for (const props of properties) {
if (props !== undefined) {
for (const [key, getterOrValue] of Object.entries(props)) {
if (eventLike[key] !== undefined) {
continue;
}
// If this throws, hopefully it is handled elsewhere
const value =
typeof getterOrValue === "function" ? getterOrValue() : getterOrValue;
if (value !== undefined) {
eventLike[key] = value;
}
}
}
}
}
return toExtend;
}
}
/**
* @deprecated 0.56, remove TaggedLoggerAdapter once its usage is removed from
* container-runtime. Issue: #8191
* TaggedLoggerAdapter class can add tag handling to your logger.
*
* @internal
*/
export class TaggedLoggerAdapter implements ITelemetryBaseLogger {
public constructor(private readonly logger: ITelemetryBaseLogger) {}
/**
* {@inheritDoc @fluidframework/core-interfaces#ITelemetryBaseLogger.send}
*/
public send(eventWithTagsMaybe: ITelemetryBaseEvent): void {
const newEvent: ITelemetryBaseEvent = {
category: eventWithTagsMaybe.category,
eventName: eventWithTagsMaybe.eventName,
};
for (const [key, taggableProp] of Object.entries(eventWithTagsMaybe)) {
const { value, tag } =
typeof taggableProp === "object"
? taggableProp
: { value: taggableProp, tag: undefined };
switch (tag) {
case undefined: {
// No tag means we can log plainly
newEvent[key] = value;
break;
}
case "PackageData": // For back-compat
case TelemetryDataTag.CodeArtifact: {
// For Microsoft applications, CodeArtifact is safe for now
// (we don't load 3P code in 1P apps)
newEvent[key] = value;
break;
}
case TelemetryDataTag.UserData: {
// Strip out anything tagged explicitly as UserData.
// Alternate strategy would be to hash these props
newEvent[key] = "REDACTED (UserData)";
break;
}
default: {
// If we encounter a tag we don't recognize
// then we must assume we should scrub.
newEvent[key] = "REDACTED (unknown tag)";
break;
}
}
}
this.logger.send(newEvent);
}
}
/**
* Create a child logger based on the provided props object.
*
* @remarks
* Passing in no props object (i.e. undefined) will return a logger that is effectively a no-op.
*
* @param props - logger is the base logger the child will log to after it's processing, namespace will be prefixed to all event names, properties are default properties that will be applied events.
*
* @legacy
* @alpha
*/
export function createChildLogger(props?: {
logger?: ITelemetryBaseLogger;
namespace?: string;
properties?: ITelemetryLoggerPropertyBags;
}): ITelemetryLoggerExt {
return ChildLogger.create(props?.logger, props?.namespace, props?.properties);
}
/**
* ChildLogger class contains various helper telemetry methods,
* encoding in one place schemas for various types of Fluid telemetry events.
* Creates sub-logger that appends properties to all events.
*/
export class ChildLogger extends TelemetryLogger {
/**
* Create child logger
* @param baseLogger - Base logger to use to output events. If undefined, proper child logger
* is created, but it does not send telemetry events anywhere.
* @param namespace - Telemetry event name prefix to add to all events
* @param properties - Base properties to add to all events
*/
public static create(
baseLogger?: ITelemetryBaseLogger,
namespace?: string,
properties?: ITelemetryLoggerPropertyBags,
): TelemetryLogger {
// if we are creating a child of a child, rather than nest, which will increase
// the callstack overhead, just generate a new logger that includes everything from the previous
if (baseLogger instanceof ChildLogger) {
const combinedProperties: ITelemetryLoggerPropertyBags = {};
for (const extendedProps of [baseLogger.properties, properties]) {
if (extendedProps !== undefined) {
if (extendedProps.all !== undefined) {
combinedProperties.all = {
...combinedProperties.all,
...extendedProps.all,
};
}
if (extendedProps.error !== undefined) {
combinedProperties.error = {
...combinedProperties.error,
...extendedProps.error,
};
}
}
}
const combinedNamespace =
baseLogger.namespace === undefined
? namespace
: namespace === undefined
? baseLogger.namespace
: `${baseLogger.namespace}${TelemetryLogger.eventNamespaceSeparator}${namespace}`;
const child = new ChildLogger(
baseLogger.baseLogger,
combinedNamespace,
combinedProperties,
);
if (!loggerIsMonitoringContext(child) && loggerIsMonitoringContext(baseLogger)) {
mixinMonitoringContext(child, baseLogger.config);
}
return child;
}
return new ChildLogger(baseLogger ?? { send(): void {} }, namespace, properties);
}
private constructor(
protected readonly baseLogger: ITelemetryBaseLogger,
namespace: string | undefined,
properties: ITelemetryLoggerPropertyBags | undefined,
) {
super(namespace, properties);
// propagate the monitoring context
if (loggerIsMonitoringContext(baseLogger)) {
mixinMonitoringContext(this, new CachedConfigProvider(this, baseLogger.config));
}
}
public get minLogLevel(): LogLevel | undefined {
return this.baseLogger.minLogLevel;
}
private shouldFilterOutEvent(event: ITelemetryBaseEvent, logLevel?: LogLevel): boolean {
const eventLogLevel = logLevel ?? LogLevel.default;
const configLogLevel = this.baseLogger.minLogLevel ?? LogLevel.default;
// Filter out in case event log level is below what is wanted in config.
return eventLogLevel < configLogLevel;
}
/**
* Send an event with the logger
*
* @param event - the event to send
*/
public send(event: ITelemetryBaseEvent, logLevel?: LogLevel): void {
if (this.shouldFilterOutEvent(event, logLevel)) {
return;
}
this.baseLogger.send(this.prepareEvent(event), logLevel);
}
}
/**
* Input properties for {@link createMultiSinkLogger}.
*
* @internal
*/
export interface MultiSinkLoggerProperties {
/**
* Will be prefixed to all event names.
*/
namespace?: string;
/**
* Default properties that will be applied to all events flowing through this logger.
*/
properties?: ITelemetryLoggerPropertyBags;
/**
* The base loggers that this logger will forward the logs to, after it processes them.
*/
loggers?: (ITelemetryBaseLogger | undefined)[];
/**
* If true, the logger will attempt to copy the custom properties (if they are of a known type, i.e. one from this package) of all the base loggers passed to it, to apply them itself to logs that flow through.
*/
tryInheritProperties?: true;
}
/**
* Create a logger which logs to multiple other loggers based on the provided props object.
*
* @internal
*/
export function createMultiSinkLogger(props: MultiSinkLoggerProperties): ITelemetryLoggerExt {
return new MultiSinkLogger(
props.namespace,
props.properties,
props.loggers?.filter((l): l is ITelemetryBaseLogger => l !== undefined),
props.tryInheritProperties,
);
}
/**
* Multi-sink logger
* Takes multiple ITelemetryBaseLogger objects (sinks) and logs all events into each sink
*/
export class MultiSinkLogger extends TelemetryLogger {
protected loggers: ITelemetryBaseLogger[];
// This is minimum of minLlogLevel of all loggers.
private _minLogLevelOfAllLoggers: LogLevel;
/**
* Create multiple sink logger (i.e. logger that sends events to multiple sinks)
* @param namespace - Telemetry event name prefix to add to all events
* @param properties - Base properties to add to all events
* @param loggers - The list of loggers to use as sinks
* @param tryInheritProperties - Will attempted to copy those loggers properties to this loggers if they are of a known type e.g. one from this package
*/
public constructor(
namespace?: string,
properties?: ITelemetryLoggerPropertyBags,
loggers: ITelemetryBaseLogger[] = [],
tryInheritProperties?: true,
) {
let realProperties = properties === undefined ? undefined : { ...properties };
if (tryInheritProperties === true) {
const merge = (realProperties ??= {});
loggers
.filter((l): l is this => l instanceof TelemetryLogger)
.map((l) => l.properties ?? {})
// eslint-disable-next-line unicorn/no-array-for-each
.forEach((cv) => {
// eslint-disable-next-line unicorn/no-array-for-each
Object.keys(cv).forEach((k) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
merge[k] = { ...cv[k], ...merge?.[k] };
});
});
}
super(namespace, realProperties);
this.loggers = loggers;
this._minLogLevelOfAllLoggers = LogLevel.default;
this.calculateMinLogLevel();
}
public get minLogLevel(): LogLevel {
return this._minLogLevelOfAllLoggers;
}
private calculateMinLogLevel(): void {
if (this.loggers.length > 0) {
const logLevels: LogLevel[] = [];
for (const logger of this.loggers) {
logLevels.push(logger.minLogLevel ?? LogLevel.default);
}
this._minLogLevelOfAllLoggers = Math.min(...logLevels) as LogLevel;
}
}
/**
* Add logger to send all events to
* @param logger - Logger to add
*/
public addLogger(logger?: ITelemetryBaseLogger): void {
if (logger !== undefined && logger !== null) {
this.loggers.push(logger);
// Update in case the logLevel of added logger is less than the current.
this.calculateMinLogLevel();
}
}
/**
* Send an event to the loggers
*
* @param event - the event to send to all the registered logger
*/
public send(event: ITelemetryBaseEvent): void {
const newEvent = this.prepareEvent(event);
for (const logger of this.loggers) {
logger.send(newEvent);
}
}
}
/**
* Describes what events {@link PerformanceEvent} should log.
*
* @remarks
* By default, all events are logged, but the client can override this behavior.
*
* For example, there is rarely a need to record a start event, as we're really after
* success / failure tracking, including duration (on success).
*
* @internal
*/
export interface IPerformanceEventMarkers {
start?: true;
end?: true;
cancel?: "generic" | "error"; // tells wether to issue "generic" or "error" category cancel event
}
/**
* Helper class to log performance events.
*
* @internal
*/
export class PerformanceEvent {
/**
* Creates an instance of {@link PerformanceEvent} and starts measurements
* @param logger - the logger to be used for publishing events
* @param event - the logging event details which will be published with the performance measurements
* @param markers - See {@link IPerformanceEventMarkers}
* @param recordHeapSize - whether or not to also record memory performance
* @param emitLogs - should this instance emit logs. If set to false, logs will not be emitted to the logger,
* but measurements will still be performed and any specified markers will be generated.
* @returns An instance of {@link PerformanceEvent}
*/
public static start(
logger: ITelemetryLoggerExt,
event: ITelemetryGenericEventExt,
markers?: IPerformanceEventMarkers,
emitLogs: boolean = true,
): PerformanceEvent {
return new PerformanceEvent(logger, event, markers, emitLogs);
}
/**
* Measure a synchronous task
* @param logger - the logger to be used for publishing events
* @param event - the logging event details which will be published with the performance measurements
* @param callback - the task to be executed and measured
* @param markers - See {@link IPerformanceEventMarkers}
* @param sampleThreshold - events with the same name and category will be sent to the logger
* only when we hit this many executions of the task. If unspecified, all events will be sent.
* @returns The results of the executed task
*
* @remarks Note that if the "same" event (category + eventName) would be emitted by different
* tasks (`callback`), `sampleThreshold` is still applied only based on the event's category + eventName,
* so executing either of the tasks will increase the internal counter and they
* effectively "share" the sampling rate for the event.
*/
public static timedExec<T>(
logger: ITelemetryLoggerExt,
event: ITelemetryGenericEventExt,
callback: (event: PerformanceEvent) => T,
markers?: IPerformanceEventMarkers,
sampleThreshold: number = 1,
): T {
const perfEvent = PerformanceEvent.start(
logger,
event,
markers,
PerformanceEvent.shouldReport(event, sampleThreshold),
);
try {
const ret = callback(perfEvent);
perfEvent.autoEnd();
return ret;
} catch (error) {
perfEvent.cancel(undefined, error);
throw error;
}
}
/**
* Measure an asynchronous task
* @param logger - the logger to be used for publishing events
* @param event - the logging event details which will be published with the performance measurements
* @param callback - the task to be executed and measured
* @param markers - See {@link IPerformanceEventMarkers}
* @param recordHeapSize - whether or not to also record memory performance
* @param sampleThreshold - events with the same name and category will be sent to the logger
* only when we hit this many executions of the task. If unspecified, all events will be sent.
* @returns The results of the executed task
*
* @remarks Note that if the "same" event (category + eventName) would be emitted by different
* tasks (`callback`), `sampleThreshold` is still applied only based on the event's category + eventName,
* so executing either of the tasks will increase the internal counter and they
* effectively "share" the sampling rate for the event.
*/
public static async timedExecAsync<T>(
logger: ITelemetryLoggerExt,
event: ITelemetryGenericEventExt,
callback: (event: PerformanceEvent) => Promise<T>,
markers?: IPerformanceEventMarkers,
sampleThreshold: number = 1,
): Promise<T> {
const perfEvent = PerformanceEvent.start(
logger,
event,
markers,
PerformanceEvent.shouldReport(event, sampleThreshold),
);
try {
const ret = await callback(perfEvent);
perfEvent.autoEnd();
return ret;
} catch (error) {
perfEvent.cancel(undefined, error);
throw error;
}
}
public get duration(): number {
return performance.now() - this.startTime;
}
private event?: ITelemetryGenericEventExt;
private readonly startTime = performance.now();
private startMark?: string;
protected constructor(
private readonly logger: ITelemetryLoggerExt,
event: ITelemetryGenericEventExt,
private readonly markers: IPerformanceEventMarkers = { end: true, cancel: "generic" },
private readonly emitLogs: boolean = true,
) {
this.event = { ...event };
if (this.markers.start) {
this.reportEvent("start");
}
if (
typeof window === "object" &&
window?.performance?.mark !== undefined &&
window?.performance?.mark !== null
) {
this.startMark = `${event.eventName}-start`;
window.performance.mark(this.startMark);
}
}
public reportProgress(
props?: ITelemetryPropertiesExt,
eventNameSuffix: string = "update",
): void {
this.reportEvent(eventNameSuffix, props);
}
private autoEnd(): void {
// Event might have been cancelled or ended in the callback
if (this.event && this.markers.end) {
this.reportEvent("end");
}
this.performanceEndMark();
// To prevent the event from being reported again later
this.event = undefined;
}
public end(props?: ITelemetryPropertiesExt): void {
this.reportEvent("end", props);
this.performanceEndMark();
// To prevent the event from being reported again later
this.event = undefined;
}
private performanceEndMark(): void {
if (this.startMark !== undefined && this.event) {
const endMark = `${this.event.eventName}-end`;
window.performance.mark(endMark);
window.performance.measure(`${this.event.eventName}`, this.startMark, endMark);
this.startMark = undefined;
}
}
public cancel(props?: ITelemetryPropertiesExt, error?: unknown): void {
if (this.markers.cancel !== undefined) {
this.reportEvent("cancel", { category: this.markers.cancel, ...props }, error);
}
// To prevent the event from being reported again later
this.event = undefined;
}
/**
* Report the event, if it hasn't already been reported.
*/
public reportEvent(
eventNameSuffix: string,
props?: ITelemetryPropertiesExt,
error?: unknown,
): void {
// If the caller invokes cancel or end directly inside the callback for timedExec[Async],
// then it's possible to come back through reportEvent twice. Only the first time counts.
if (!this.event) {
return;
}
if (!this.emitLogs) {
return;
}
const event: ITelemetryPerformanceEventExt = { ...this.event, ...props };
event.eventName = `${event.eventName}_${eventNameSuffix}`;
if (eventNameSuffix !== "start") {
event.duration = this.duration;
}
this.logger.sendPerformanceEvent(event, error);
}
private static readonly eventHits = new Map<string, number>();
private static shouldReport(
event: ITelemetryGenericEventExt,
sampleThreshold: number,
): boolean {
const eventKey = `.${event.category}.${event.eventName}`;
const hitCount = PerformanceEvent.eventHits.get(eventKey) ?? 0;
PerformanceEvent.eventHits.set(eventKey, hitCount >= sampleThreshold ? 1 : hitCount + 1);
return hitCount % sampleThreshold === 0;
}
}
/**
* Takes in an event object, and converts all of its values to a basePropertyType.
* In the case of an invalid property type, the value will be converted to an error string.
* @param event - Event with fields you want to stringify.
*/
function convertToBaseEvent({
category,
eventName,
...props
}: ITelemetryEventExt): ITelemetryBaseEvent {
const newEvent: ITelemetryBaseEvent = { category, eventName };
for (const key of Object.keys(props)) {
newEvent[key] = convertToBasePropertyType(props[key]);
}
return newEvent;
}
/**
* Takes in value, and does one of 4 things.
* if value is of primitive type - returns the original value.
* If the value is a flat array or object - returns a stringified version of the array/object.
* If the value is an object of type Tagged<TelemetryBaseEventPropertyType> - returns the object
* with its values recursively converted to base property Type.
* If none of these cases are reached - returns an error string
* @param x - value passed in to convert to a base property type
*/
export function convertToBasePropertyType(
x: TelemetryEventPropertyTypeExt | Tagged<TelemetryEventPropertyTypeExt>,
): TelemetryBaseEventPropertyType | Tagged<TelemetryBaseEventPropertyType> {
return isTaggedTelemetryPropertyValue(x)
? {
value: convertToBasePropertyTypeUntagged(x.value),
tag: x.tag,
}
: convertToBasePropertyTypeUntagged(x);
}
function convertToBasePropertyTypeUntagged(
x: TelemetryEventPropertyTypeExt,
): TelemetryBaseEventPropertyType {
switch (typeof x) {
case "string":
case "number":
case "boolean":
case "undefined": {
return x;
}
case "object": {
// We assume this is an array or flat object based on the input types
return JSON.stringify(x);
}
default: {
// should never reach this case based on the input types
console.error(
`convertToBasePropertyTypeUntagged: INVALID PROPERTY (typed as ${typeof x})`,
);
return `INVALID PROPERTY (typed as ${typeof x})`;
}
}
}
/**
* Tags all given `values` with the same `tag`.
*
* @param tag - The tag with which all `values` will be annotated.
* @param values - The values to be tagged.
*
* @remarks
* It supports properties of type {@link @fluidframework/core-interfaces#TelemetryBaseEventPropertyType},
* as well as callbacks that return that type.
*
* @example Sample usage
* ```typescript
* {
* // ...Other properties being added to a telemetry event
* ...tagData("someTag", {foo: 1, bar: 2}),
* // ...
* }
* ```
* This will result in `foo` and `bar` added to the event with their values tagged.
*
* @internal
*/
export const tagData = <
T extends TelemetryDataTag,
V extends Record<
string,
TelemetryBaseEventPropertyType | (() => TelemetryBaseEventPropertyType)
>,
>(
tag: T,
values: V,
): {
[P in keyof V]:
| (V[P] extends () => TelemetryBaseEventPropertyType
? () => {
value: ReturnType<V[P]>;
tag: T;
}
: {
value: Exclude<V[P], undefined>;
tag: T;
})
| (V[P] extends undefined ? undefined : never);
} =>
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
Object.entries(values)
.filter((e) => e[1] !== undefined)
// eslint-disable-next-line unicorn/no-array-reduce
.reduce((pv, cv) => {
const [key, value] = cv;
// The ternary form is less legible in this case.
// eslint-disable-next-line unicorn/prefer-ternary
if (typeof value === "function") {
pv[key] = () => {
return { tag, value: value() };
};
} else {
pv[key] = { tag, value };
}
return pv;
}, {}) as ReturnType<typeof tagData>;
/**
* Tags all provided `values` as {@link TelemetryDataTag.CodeArtifact}.
*
* @param values - The values to be tagged.
*
* @remarks
* It supports properties of type {@link @fluidframework/core-interfaces#TelemetryBaseEventPropertyType},
* as well as callbacks that return that type.
*
* @example Sample usage
* ```typescript
* {
* // ...Other properties being added to a telemetry event
* ...tagCodeArtifacts("someTag", {foo: 1, bar: 2}),
* // ...
* }
* ```
* This will result in `foo` and `bar` added to the event with their values tagged as {@link TelemetryDataTag.CodeArtifact}.
*
* @see {@link tagData}
*
* @internal
*/
export const tagCodeArtifacts = <
T extends Record<
string,
TelemetryBaseEventPropertyType | (() => TelemetryBaseEventPropertyType)
>,
>(
values: T,
): {
[P in keyof T]:
| (T[P] extends () => TelemetryBaseEventPropertyType
? () => {
value: ReturnType<T[P]>;
tag: TelemetryDataTag.CodeArtifact;
}
: {
value: Exclude<T[P], undefined>;
tag: TelemetryDataTag.CodeArtifact;
})
| (T[P] extends undefined ? undefined : never);