зеркало из
1
0
Форкнуть 0
opensource-management-portal/lib/insights.ts

61 строка
1.9 KiB
TypeScript
Исходник Постоянная ссылка Обычный вид История

2016-08-04 19:04:53 +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.
//
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.
import type { TelemetryClient } from 'applicationinsights';
2022-10-08 01:25:28 +03:00
function createWrappedClient(propertiesToInsert: any, client: TelemetryClient): TelemetryClient {
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 {
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',
};
let props = '';
if (eventNameOrProperties && eventNameOrProperties.properties) {
props = ' ';
2022-10-12 04:20:41 +03:00
for (const [key, value] of Object.entries(eventNameOrProperties.properties)) {
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
);
};
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
export default createWrappedClient;