2016-08-04 19:04:53 +03:00
|
|
|
//
|
2019-10-03 04:41:16 +03:00
|
|
|
// Copyright (c) Microsoft.
|
2016-08-04 19:04:53 +03:00
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
//
|
|
|
|
|
2019-08-07 02:53:50 +03:00
|
|
|
import Debug from 'debug';
|
2023-01-20 10:01:39 +03:00
|
|
|
const debug = Debug.debug('insights');
|
2016-08-04 19:04:53 +03:00
|
|
|
|
2018-05-02 20:28:35 +03:00
|
|
|
// This file was originally designed to wrap the pre-1.0.0 version of applicationinsights,
|
|
|
|
// and so is less important today.
|
|
|
|
|
2020-06-12 00:46:09 +03:00
|
|
|
import type { TelemetryClient } from 'applicationinsights';
|
|
|
|
|
2022-10-08 01:25:28 +03:00
|
|
|
function createWrappedClient(propertiesToInsert: any, client: TelemetryClient): TelemetryClient {
|
2020-06-12 00:46:09 +03:00
|
|
|
let c = client;
|
2016-08-04 19:04:53 +03:00
|
|
|
if (client) {
|
2018-05-02 20:28:35 +03:00
|
|
|
client.commonProperties = propertiesToInsert;
|
2016-08-04 19:04:53 +03:00
|
|
|
} else {
|
2020-06-12 00:46:09 +03:00
|
|
|
c = {
|
|
|
|
trackEvent: consoleHandler,
|
|
|
|
trackException: consoleHandler,
|
|
|
|
trackMetric: consoleMetric,
|
|
|
|
trackTrace: consoleHandler,
|
|
|
|
trackDependency: consoleHandler,
|
|
|
|
flush: (options) => {
|
|
|
|
options = options || {};
|
|
|
|
if (options.callback) {
|
|
|
|
return (options.callback as any)();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
} as TelemetryClient;
|
2016-08-04 19:04:53 +03:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-05-02 20:28:35 +03:00
|
|
|
const consoleHandler = (eventNameOrProperties) => {
|
2022-10-07 09:59:30 +03:00
|
|
|
eventNameOrProperties = eventNameOrProperties || {
|
|
|
|
name: 'Unknown event, may be from pre-v1.0.0 applicationinsights',
|
|
|
|
};
|
2019-08-07 02:53:50 +03:00
|
|
|
let props = '';
|
|
|
|
if (eventNameOrProperties && eventNameOrProperties.properties) {
|
|
|
|
props = ' ';
|
2022-10-12 04:20:41 +03:00
|
|
|
for (const [key, value] of Object.entries(eventNameOrProperties.properties)) {
|
2019-08-07 02:53:50 +03:00
|
|
|
props += `${key}=${value} `;
|
|
|
|
}
|
|
|
|
}
|
2022-10-07 09:59:30 +03:00
|
|
|
debug(
|
2022-10-08 01:25:28 +03:00
|
|
|
(typeof eventNameOrProperties === 'string' ? eventNameOrProperties : eventNameOrProperties.name) + props
|
2022-10-07 09:59:30 +03:00
|
|
|
);
|
2017-05-31 20:07:13 +03:00
|
|
|
};
|
2018-05-02 20:28:35 +03:00
|
|
|
const consoleMetric = (eventNameOrProperties) => {
|
2022-10-07 09:59:30 +03:00
|
|
|
if (typeof eventNameOrProperties === 'string') {
|
2022-10-08 01:25:28 +03:00
|
|
|
debug(`Legacy applicationinsights Metric ${eventNameOrProperties} was not recorded`);
|
2018-05-02 20:28:35 +03:00
|
|
|
} else {
|
2022-10-08 01:25:28 +03:00
|
|
|
eventNameOrProperties = eventNameOrProperties || { name: 'UnknownMetric', value: 0 };
|
|
|
|
debug(`Metric(${eventNameOrProperties.name}: ${eventNameOrProperties.value}`);
|
2016-08-04 19:04:53 +03:00
|
|
|
}
|
2018-05-02 20:28:35 +03:00
|
|
|
};
|
2016-08-04 19:04:53 +03:00
|
|
|
|
2020-06-12 00:46:09 +03:00
|
|
|
export default createWrappedClient;
|