-
Notifications
You must be signed in to change notification settings - Fork 235
/
AppInsightsCore.ts
116 lines (101 loc) · 5.26 KB
/
AppInsightsCore.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
/**
* AppInsightsCore.ts
* @author Abhilash Panwar (abpanwar) Hector Hernandez (hectorh)
* @copyright Microsoft 2018
*/
import dynamicProto from "@microsoft/dynamicproto-js";
import {
AppInsightsCore as InternalCore, IConfigDefaults, IDiagnosticLogger, INotificationManager, IPlugin, ITelemetryItem, _throwInternal,
createDynamicConfig, doPerf, dumpObj, eLoggingSeverity, throwError
} from "@microsoft/applicationinsights-core-js";
import { ITimerHandler, objDeepFreeze } from "@nevware21/ts-utils";
import { IExtendedConfiguration, IExtendedTelemetryItem, IPropertyStorageOverride } from "./DataModels";
import { EventLatencyValue, _eExtendedInternalMessageId } from "./Enums";
import { STR_DEFAULT_ENDPOINT_URL, STR_EMPTY, STR_VERSION } from "./InternalConstants";
import { FullVersionString, getTime, isLatency } from "./Utils";
/**
* The default settings for the config.
* WE MUST include all defaults here to ensure that the config is created with all of the properties
* defined as dynamic.
*/
const defaultConfig: IConfigDefaults<IExtendedConfiguration> = objDeepFreeze({
endpointUrl: STR_DEFAULT_ENDPOINT_URL,
propertyStorageOverride: { isVal: _chkPropOverride }
});
function _chkPropOverride(propertyStorageOverride: IPropertyStorageOverride) {
// Validate property storage override
if (propertyStorageOverride && (!propertyStorageOverride.getProperty || !propertyStorageOverride.setProperty)) {
throwError("Invalid property storage override passed.");
}
return true;
}
/**
* @group Classes
* @group Entrypoint
*/
export class AppInsightsCore<C extends IExtendedConfiguration = IExtendedConfiguration> extends InternalCore<C> {
constructor() {
super();
dynamicProto(AppInsightsCore, this, (_self, _base) => {
_self.initialize = (config: C, extensions: IPlugin[], logger?: IDiagnosticLogger, notificationManager?: INotificationManager) => {
doPerf(_self, () => "AppInsightsCore.initialize", () => {
try {
_base.initialize(createDynamicConfig<C>(config, defaultConfig as C, logger || _self.logger, false).cfg, extensions, logger, notificationManager);
} catch (e) {
let logger = _self.logger;
let message = dumpObj(e);
if (message.indexOf("channels") !== -1) {
// Add some additional context to the underlying reported error
message += "\n - Channels must be provided through config.channels only!";
}
_throwInternal(logger,
eLoggingSeverity.CRITICAL,
_eExtendedInternalMessageId.FailedToInitializeSDK, "SDK Initialization Failed - no telemetry will be sent: " + message
);
}
}, () => ({ config, extensions, logger, notificationManager }));
};
_self.track = (item: IExtendedTelemetryItem|ITelemetryItem) => {
doPerf(_self, () => "AppInsightsCore.track", () => {
let telemetryItem: IExtendedTelemetryItem = item as IExtendedTelemetryItem;
if (telemetryItem) {
telemetryItem.timings = telemetryItem.timings || {};
telemetryItem.timings.trackStart = getTime();
if (!isLatency(telemetryItem.latency)) {
telemetryItem.latency = EventLatencyValue.Normal;
}
let itemExt = telemetryItem.ext = telemetryItem.ext || {};
itemExt.sdk = itemExt.sdk || {};
itemExt.sdk.ver = FullVersionString;
let baseData = telemetryItem.baseData = telemetryItem.baseData || {};
baseData.properties = baseData.properties || {};
let itemProperties = baseData.properties;
itemProperties[STR_VERSION] = itemProperties[STR_VERSION] || _self.pluginVersionString || STR_EMPTY;
}
_base.track(telemetryItem);
}, () => ({ item: item }), !((item as any).sync));
};
_self.pollInternalLogs = (eventName?: string): ITimerHandler => {
return _base.pollInternalLogs(eventName || "InternalLog");
};
});
}
/**
* Initialize the sdk.
* @param config - The configuration to initialize the SDK.
* @param extensions - An array of extensions that are to be used by the core.
*/
public initialize(config: C, extensions: IPlugin[], logger?: IDiagnosticLogger, notificationManager?: INotificationManager) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
public track(item: IExtendedTelemetryItem|ITelemetryItem) {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
}
/**
* Periodically check logger.queue for
*/
public pollInternalLogs(eventName?: string): ITimerHandler {
// @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
return null;
}
}