Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions packages/plugin-ext/src/plugin/telemetry-ext.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// *****************************************************************************
// Copyright (C) 2026 EclipseSource and others.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can put yourself/your company as copyright holder here.

//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import * as chai from 'chai';
import { TelemetryExtImpl } from './telemetry-ext';

const expect = chai.expect;

describe('TelemetryLogger', () => {
const sender = {
sendEventData: () => { },
sendErrorData: () => { }
};

it('reflects the current telemetry enabled state', () => {
const telemetry = new TelemetryExtImpl();
const logger = telemetry.createTelemetryLogger(sender);

expect(logger.telemetryEnabled).to.equal(false);
expect(logger.isUsageEnabled).to.equal(false);
expect(logger.isErrorsEnabled).to.equal(false);

telemetry.isTelemetryEnabled = true;

expect(logger.telemetryEnabled).to.equal(true);
expect(logger.isUsageEnabled).to.equal(true);
expect(logger.isErrorsEnabled).to.equal(true);

telemetry.isTelemetryEnabled = false;

expect(logger.telemetryEnabled).to.equal(false);
expect(logger.isUsageEnabled).to.equal(false);
expect(logger.isErrorsEnabled).to.equal(false);
});

it('emits enable-state changes when telemetry is toggled', () => {
const telemetry = new TelemetryExtImpl();
const logger = telemetry.createTelemetryLogger(sender);
let changes = 0;

logger.onDidChangeEnableStates(() => changes++);

telemetry.isTelemetryEnabled = true;
telemetry.isTelemetryEnabled = false;

expect(changes).to.equal(2);
});
});
27 changes: 23 additions & 4 deletions packages/plugin-ext/src/plugin/telemetry-ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class TelemetryLogger {
readonly options: TelemetryLoggerOptions | undefined;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly commonProperties: Record<string, any>;
telemetryEnabled: boolean;
private _telemetryEnabled: boolean;

private readonly onDidChangeEnableStatesEmitter: Emitter<TelemetryLogger> = new Emitter();
readonly onDidChangeEnableStates: Event<TelemetryLogger> = this.onDidChangeEnableStatesEmitter.event;
Expand All @@ -60,9 +60,20 @@ export class TelemetryLogger {
this.sender = sender;
this.options = options;
this.commonProperties = this.getCommonProperties();
this._isErrorsEnabled = true;
this._isUsageEnabled = true;
this.telemetryEnabled = telemetryEnabled;
this._telemetryEnabled = telemetryEnabled;
this._isErrorsEnabled = telemetryEnabled;
this._isUsageEnabled = telemetryEnabled;
}

get telemetryEnabled(): boolean {
return this._telemetryEnabled;
}

set telemetryEnabled(telemetryEnabled: boolean) {
if (this._telemetryEnabled !== telemetryEnabled) {
this._telemetryEnabled = telemetryEnabled;
this.updateEnableStates(telemetryEnabled, telemetryEnabled);
}
}

get isUsageEnabled(): boolean {
Expand All @@ -87,6 +98,14 @@ export class TelemetryLogger {
}
}

private updateEnableStates(isUsageEnabled: boolean, isErrorsEnabled: boolean): void {
if (this._isUsageEnabled !== isUsageEnabled || this._isErrorsEnabled !== isErrorsEnabled) {
this._isUsageEnabled = isUsageEnabled;
this._isErrorsEnabled = isErrorsEnabled;
this.onDidChangeEnableStatesEmitter.fire(this);
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
logUsage(eventName: string, data?: Record<string, any | TelemetryTrustedValue<any>>): void {
if (!this.telemetryEnabled || !this.isUsageEnabled) {
Expand Down
Loading