mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-19 01:25:36 -05:00
Remove TelemetryUtils and use IAdsTelemetryService directly (#8289)
* Remove TelemetryUtils and use IAdsTelemetryService directly * Fix event names and cleanup * Fix tests * Fix strict null check
This commit is contained in:
@@ -6,11 +6,9 @@
|
||||
import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement';
|
||||
import * as azdata from 'azdata';
|
||||
import * as TelemetryKeys from 'sql/platform/telemetry/common/telemetryKeys';
|
||||
import * as TelemetryUtils from 'sql/platform/telemetry/common/telemetryUtilities';
|
||||
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
|
||||
import { IBackupService, TaskExecutionMode } from 'sql/platform/backup/common/backupService';
|
||||
import { invalidProvider } from 'sql/base/common/errors';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
import { IAdsTelemetryService } from 'sql/platform/telemetry/common/telemetry';
|
||||
|
||||
export class BackupService implements IBackupService {
|
||||
|
||||
@@ -19,8 +17,7 @@ export class BackupService implements IBackupService {
|
||||
|
||||
constructor(
|
||||
@IConnectionManagementService private _connectionService: IConnectionManagementService,
|
||||
@ITelemetryService private _telemetryService: ITelemetryService,
|
||||
@ILogService private logService: ILogService
|
||||
@IAdsTelemetryService private _telemetryService: IAdsTelemetryService
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -45,7 +42,9 @@ export class BackupService implements IBackupService {
|
||||
return new Promise<azdata.BackupResponse>((resolve, reject) => {
|
||||
const providerResult = this.getProvider(connectionUri);
|
||||
if (providerResult) {
|
||||
TelemetryUtils.addTelemetry(this._telemetryService, this.logService, TelemetryKeys.BackupCreated, { provider: providerResult.providerName });
|
||||
this._telemetryService.createActionEvent(TelemetryKeys.TelemetryView.Shell, TelemetryKeys.BackupCreated)
|
||||
.withAdditionalProperties({ providerId: providerResult.providerName })
|
||||
.send();
|
||||
providerResult.provider.backup(connectionUri, backupInfo, taskExecutionMode).then(result => {
|
||||
resolve(result);
|
||||
}, error => {
|
||||
|
||||
@@ -9,12 +9,10 @@ import { createDecorator } from 'vs/platform/instantiation/common/instantiation'
|
||||
import { IDisposable } from 'vs/base/common/lifecycle';
|
||||
import * as azdata from 'azdata';
|
||||
import * as TelemetryKeys from 'sql/platform/telemetry/common/telemetryKeys';
|
||||
import * as TelemetryUtils from 'sql/platform/telemetry/common/telemetryUtilities';
|
||||
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
|
||||
import { Event, Emitter } from 'vs/base/common/event';
|
||||
import { keys } from 'vs/base/common/map';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
import { assign } from 'vs/base/common/objects';
|
||||
import { IAdsTelemetryService, ITelemetryEventProperties } from 'sql/platform/telemetry/common/telemetry';
|
||||
|
||||
export const SERVICE_ID = 'queryManagementService';
|
||||
|
||||
@@ -104,8 +102,7 @@ export class QueryManagementService implements IQueryManagementService {
|
||||
|
||||
constructor(
|
||||
@IConnectionManagementService private _connectionService: IConnectionManagementService,
|
||||
@ITelemetryService private _telemetryService: ITelemetryService,
|
||||
@ILogService private logService: ILogService
|
||||
@IAdsTelemetryService private _telemetryService: IAdsTelemetryService
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -169,18 +166,17 @@ export class QueryManagementService implements IQueryManagementService {
|
||||
}
|
||||
|
||||
private addTelemetry(eventName: string, ownerUri: string, runOptions?: azdata.ExecutionPlanOptions): void {
|
||||
let providerId: string = this._connectionService.getProviderIdFromUri(ownerUri);
|
||||
let data: TelemetryUtils.IConnectionTelemetryData = {
|
||||
const providerId: string = this._connectionService.getProviderIdFromUri(ownerUri);
|
||||
const data: ITelemetryEventProperties = {
|
||||
provider: providerId,
|
||||
};
|
||||
if (runOptions) {
|
||||
data = assign({}, data, {
|
||||
assign(data, {
|
||||
displayEstimatedQueryPlan: runOptions.displayEstimatedQueryPlan,
|
||||
displayActualQueryPlan: runOptions.displayActualQueryPlan
|
||||
});
|
||||
}
|
||||
// tslint:disable-next-line:no-floating-promises
|
||||
TelemetryUtils.addTelemetry(this._telemetryService, this.logService, eventName, data);
|
||||
this._telemetryService.createActionEvent(TelemetryKeys.TelemetryView.Shell, eventName).withAdditionalProperties(data).send();
|
||||
}
|
||||
|
||||
private _runAction<T>(uri: string, action: (handler: IQueryRequestHandler) => Promise<T>, fallBackToDefaultProvider: boolean = false): Promise<T> {
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
|
||||
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
|
||||
import * as types from 'vs/base/common/types';
|
||||
import * as azdata from 'azdata';
|
||||
@@ -20,11 +19,11 @@ import { IObjectExplorerService } from 'sql/workbench/services/objectExplorer/br
|
||||
import { ITaskService } from 'sql/platform/tasks/common/tasksService';
|
||||
import { TaskStatus, TaskNode } from 'sql/platform/tasks/common/tasksNode';
|
||||
import * as TelemetryKeys from 'sql/platform/telemetry/common/telemetryKeys';
|
||||
import * as TelemetryUtils from 'sql/platform/telemetry/common/telemetryUtilities';
|
||||
import { IConnectionManagementService } from 'sql/platform/connection/common/connectionManagement';
|
||||
import { invalidProvider } from 'sql/base/common/errors';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
import { find } from 'vs/base/common/arrays';
|
||||
import { IAdsTelemetryService } from 'sql/platform/telemetry/common/telemetry';
|
||||
|
||||
export class RestoreService implements IRestoreService {
|
||||
|
||||
@@ -33,8 +32,7 @@ export class RestoreService implements IRestoreService {
|
||||
|
||||
constructor(
|
||||
@IConnectionManagementService private _connectionService: IConnectionManagementService,
|
||||
@ITelemetryService private _telemetryService: ITelemetryService,
|
||||
@ILogService private logService: ILogService
|
||||
@IAdsTelemetryService private _telemetryService: IAdsTelemetryService
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -63,7 +61,10 @@ export class RestoreService implements IRestoreService {
|
||||
return new Promise<azdata.RestoreResponse>((resolve, reject) => {
|
||||
const providerResult = this.getProvider(connectionUri);
|
||||
if (providerResult) {
|
||||
TelemetryUtils.addTelemetry(this._telemetryService, this.logService, TelemetryKeys.RestoreRequested, { provider: providerResult.providerName }).catch((e) => this.logService.error(e));
|
||||
this._telemetryService.createActionEvent(TelemetryKeys.TelemetryView.Shell, TelemetryKeys.RestoreRequested)
|
||||
.withAdditionalProperties({
|
||||
provider: providerResult.providerName
|
||||
}).send();
|
||||
providerResult.provider.restore(connectionUri, restoreInfo).then(result => {
|
||||
resolve(result);
|
||||
}, error => {
|
||||
|
||||
@@ -55,6 +55,18 @@ class TelemetryEventImpl implements ITelemetryEvent {
|
||||
}
|
||||
}
|
||||
|
||||
class NullTelemetryEventImpl implements ITelemetryEvent {
|
||||
constructor() { }
|
||||
|
||||
public send(): void { }
|
||||
|
||||
public withAdditionalProperties(additionalProperties: ITelemetryEventProperties): ITelemetryEvent { return this; }
|
||||
|
||||
public withAdditionalMeasurements(additionalMeasurements: ITelemetryEventMeasures): ITelemetryEvent { return this; }
|
||||
|
||||
public withConnectionInfo(connectionInfo: ITelemetryConnectionInfo): ITelemetryEvent { return this; }
|
||||
}
|
||||
|
||||
export class AdsTelemetryService implements IAdsTelemetryService {
|
||||
|
||||
_serviceBrand: undefined;
|
||||
@@ -188,3 +200,31 @@ export class AdsTelemetryService implements IAdsTelemetryService {
|
||||
this.createTelemetryEvent(eventName, properties, measurements).send();
|
||||
}
|
||||
}
|
||||
|
||||
export class NullAdsTelemetryService implements IAdsTelemetryService {
|
||||
|
||||
_serviceBrand: undefined;
|
||||
|
||||
get isOptedIn(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
setEnabled(value: boolean): void { }
|
||||
getTelemetryInfo(): Promise<ITelemetryInfo> {
|
||||
return Promise.resolve({
|
||||
sessionId: '',
|
||||
machineId: '',
|
||||
instanceId: ''
|
||||
});
|
||||
}
|
||||
createViewEvent(view: string): ITelemetryEvent { return new NullTelemetryEventImpl(); }
|
||||
sendViewEvent(view: string): void { }
|
||||
createActionEvent(view: string, action: string, target?: string, source?: string, durationInMs?: number): ITelemetryEvent { return new NullTelemetryEventImpl(); }
|
||||
sendActionEvent(view: string, action: string, target?: string, source?: string, durationInMs?: number): void { }
|
||||
createMetricsEvent(metrics: ITelemetryEventMeasures, groupName: string): ITelemetryEvent { return new NullTelemetryEventImpl(); }
|
||||
sendMetricsEvent(metrics: ITelemetryEventMeasures, groupName: string): void { }
|
||||
createErrorEvent(view: string, name: string, errorCode?: string, errorType?: string): ITelemetryEvent { return new NullTelemetryEventImpl(); }
|
||||
sendErrorEvent(view: string, name: string, errorCode?: string, errorType?: string): void { }
|
||||
createTelemetryEvent(eventName: string, properties?: ITelemetryEventProperties, measurements?: ITelemetryEventMeasures): ITelemetryEvent { return new NullTelemetryEventImpl(); }
|
||||
sendTelemetryEvent(eventName: string, properties?: ITelemetryEventProperties, measurements?: ITelemetryEventMeasures): void { }
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ export const IAdsTelemetryService = createDecorator<IAdsTelemetryService>('adsTe
|
||||
* Holds additional properties to send along with an event.
|
||||
*/
|
||||
export interface ITelemetryEventProperties {
|
||||
[key: string]: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { ITelemetryService, ITelemetryData } from 'vs/platform/telemetry/common/telemetry';
|
||||
import { IConnectionProfile } from 'sql/platform/connection/common/interfaces';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
|
||||
export interface IConnectionTelemetryData extends ITelemetryData {
|
||||
provider?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the given telemetry service to log the telemetry event.
|
||||
* If the provider is not in the data, tries to get it from connection inside the data.
|
||||
* The connection in the data won't be included in the telemetry data
|
||||
* Note: userId is added to all telemetry events so no need to add it here
|
||||
* @param telemetryService Telemetry Service
|
||||
* @param telemetryEventName Telemetry event name
|
||||
* @param data Telemetry data
|
||||
*/
|
||||
export function addTelemetry(
|
||||
telemetryService: ITelemetryService,
|
||||
logService: ILogService,
|
||||
telemetryEventName: string,
|
||||
data?: IConnectionTelemetryData,
|
||||
connection?: IConnectionProfile
|
||||
): Promise<void> {
|
||||
return new Promise<void>(resolve => {
|
||||
try {
|
||||
let telData: ITelemetryData = data === undefined ? {} : data;
|
||||
|
||||
if (telData && telData.provider === undefined) {
|
||||
|
||||
let provider: string = '';
|
||||
if (connection) {
|
||||
provider = connection.providerName;
|
||||
}
|
||||
telData.provider = provider;
|
||||
}
|
||||
delete telData['connection'];
|
||||
if (telemetryService) {
|
||||
telemetryService.publicLog(telemetryEventName, telData).then(() => {
|
||||
resolve();
|
||||
}, telemetryServiceError => {
|
||||
if (logService) {
|
||||
logService.warn(`Failed to add telemetry. error: ${telemetryServiceError}`);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
} catch (error) {
|
||||
if (logService) {
|
||||
logService.warn(`Failed to add telemetry. error: ${error}`);
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as TelemetryUtils from 'sql/platform/telemetry/common/telemetryUtilities';
|
||||
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
|
||||
import * as TypeMoq from 'typemoq';
|
||||
import * as assert from 'assert';
|
||||
import { NullLogService } from 'vs/platform/log/common/log';
|
||||
import { TelemetryService } from 'vs/platform/telemetry/common/telemetryService';
|
||||
|
||||
suite('SQL Telemetry Utilities tests', () => {
|
||||
let telemetryService: TypeMoq.Mock<ITelemetryService>;
|
||||
let none: void;
|
||||
let providerName: string = 'provider name';
|
||||
let telemetryKey: string = 'tel key';
|
||||
|
||||
let connectionProfile = {
|
||||
connectionName: '',
|
||||
databaseName: '',
|
||||
serverName: '',
|
||||
authenticationType: '',
|
||||
getOptionsKey: () => '',
|
||||
matches: () => false,
|
||||
groupFullName: '',
|
||||
groupId: '',
|
||||
id: '',
|
||||
options: {},
|
||||
password: '',
|
||||
providerName: providerName,
|
||||
savePassword: true,
|
||||
saveProfile: true,
|
||||
userName: ''
|
||||
};
|
||||
|
||||
setup(() => {
|
||||
telemetryService = TypeMoq.Mock.ofType(TelemetryService, TypeMoq.MockBehavior.Strict, Object.create(null));
|
||||
telemetryService.setup(x => x.publicLog(TypeMoq.It.isAny(), TypeMoq.It.isAny())).returns(x => Promise.resolve(none));
|
||||
});
|
||||
|
||||
test('addTelemetry should add provider id using the connection', (done) => {
|
||||
let data: TelemetryUtils.IConnectionTelemetryData = {
|
||||
};
|
||||
const logService = new NullLogService();
|
||||
TelemetryUtils.addTelemetry(telemetryService.object, logService, telemetryKey, data, connectionProfile).then(() => {
|
||||
telemetryService.verify(x => x.publicLog(TypeMoq.It.is(a => a === telemetryKey), TypeMoq.It.is(b => b!.provider === providerName)), TypeMoq.Times.once());
|
||||
done();
|
||||
}).catch(err => {
|
||||
assert.fail(err);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
test('addTelemetry should pass the telemetry data to telemetry service', (done) => {
|
||||
let data: TelemetryUtils.IConnectionTelemetryData = {
|
||||
target: 'target',
|
||||
from: 'from'
|
||||
};
|
||||
data.test1 = '1';
|
||||
|
||||
const logService = new NullLogService();
|
||||
TelemetryUtils.addTelemetry(telemetryService.object, logService, telemetryKey, data, connectionProfile).then(() => {
|
||||
telemetryService.verify(x => x.publicLog(
|
||||
TypeMoq.It.is(a => a === telemetryKey),
|
||||
TypeMoq.It.is(b => b!.provider === providerName
|
||||
&& b!.from === data.from
|
||||
&& b!.target === data.target
|
||||
&& b!.test1 === data.test1
|
||||
&& b!.connection === undefined)), TypeMoq.Times.once());
|
||||
done();
|
||||
}).catch(err => {
|
||||
assert.fail(err);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
test('addTelemetry should not crash not given data', (done) => {
|
||||
|
||||
const logService = new NullLogService();
|
||||
TelemetryUtils.addTelemetry(telemetryService.object, logService, telemetryKey).then(() => {
|
||||
telemetryService.verify(x => x.publicLog(
|
||||
TypeMoq.It.is(a => a === telemetryKey),
|
||||
TypeMoq.It.is(b => b !== undefined)), TypeMoq.Times.once());
|
||||
done();
|
||||
}).catch(err => {
|
||||
assert.fail(err);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
test('addTelemetry should try to get the provider name from data first', (done) => {
|
||||
let data: TelemetryUtils.IConnectionTelemetryData = {
|
||||
connection: connectionProfile
|
||||
};
|
||||
data.provider = providerName + '1';
|
||||
|
||||
const logService = new NullLogService();
|
||||
TelemetryUtils.addTelemetry(telemetryService.object, logService, telemetryKey, data, connectionProfile).then(() => {
|
||||
telemetryService.verify(x => x.publicLog(TypeMoq.It.is(a => a === telemetryKey), TypeMoq.It.is(b => b!.provider === data.provider)), TypeMoq.Times.once());
|
||||
done();
|
||||
}).catch(err => {
|
||||
assert.fail(err);
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user