Skip to content

Commit

Permalink
Add telemetry using the extension API
Browse files Browse the repository at this point in the history
This adds telemetry to the extension, utilizing the functionality
provided by Podman Desktop.
  • Loading branch information
gbraad committed May 4, 2023
1 parent dcf5f11 commit 93e26ec
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 24 deletions.
11 changes: 11 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
# and commit this file to your remote git repository to share the goodness with others.

# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart

tasks:
- init: yarn install && yarn run build
command: yarn run watch


13 changes: 12 additions & 1 deletion src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface ProviderCommand extends extensionApi.MenuItem {

export class CommandManager {
private extensionContext: extensionApi.ExtensionContext;
private telemetryLogger: extensionApi.TelemetryLogger;
private commands: ProviderCommand[] = [];
private disposables = new Map<string, Disposable>();
constructor() {
Expand Down Expand Up @@ -69,14 +70,24 @@ export class CommandManager {
const disposable = extensionApi.tray.registerProviderMenuItem(providerId, command);
this.disposables.set(command.id, disposable);
this.commands.push(command);
this.extensionContext.subscriptions.push(extensionApi.commands.registerCommand(command.id, command.callback));

this.extensionContext.subscriptions.push(
extensionApi.commands.registerCommand(command.id, () => {
this.telemetryLogger.logUsage(command.id);
command.callback();
}),
);
}

setExtContext(extensionContext: extensionApi.ExtensionContext): void {
this.extensionContext = extensionContext;
extensionContext.subscriptions.push(extensionApi.Disposable.from(this));
}

setTelemetryLogger(telemetryLogger: extensionApi.TelemetryLogger): void {
this.telemetryLogger = telemetryLogger;
}

dispose(): void {
// dispose all commands registered
for (const disposable of this.disposables.values()) {
Expand Down
8 changes: 6 additions & 2 deletions src/crc-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ interface Auths {

const missingPullSecret = 'Failed to ask for pull secret';

export async function startCrc(logger: extensionApi.Logger): Promise<void> {
export async function startCrc(
logger: extensionApi.Logger,
telemetryLogger: extensionApi.TelemetryLogger,
): Promise<void> {
telemetryLogger.logUsage('crc.start');
try {
// call crc setup to prepare bundle, before start
if (isNeedSetup) {
Expand All @@ -58,7 +62,7 @@ export async function startCrc(logger: extensionApi.Logger): Promise<void> {
// ask user to provide pull secret
if (await askAndStorePullSecret(logger)) {
// if pull secret provided try to start again
return startCrc(logger);
return startCrc(logger, telemetryLogger);
}
return;
} else if (err.name === 'RequestError' && err.code === 'ECONNRESET') {
Expand Down
4 changes: 3 additions & 1 deletion src/crc-stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/

import type * as extensionApi from '@podman-desktop/api';
import { commander } from './daemon-commander';
import { crcLogProvider } from './log-provider';

export async function stopCrc(): Promise<void> {
export async function stopCrc(telemetryLogger: extensionApi.TelemetryLogger): Promise<void> {
telemetryLogger.logUsage('crc.stop');
console.log('extension:crc: receive the call stop');
try {
await commander.stop();
Expand Down
29 changes: 17 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
const crcInstaller = new CrcInstall();
extensionApi.configuration.getConfiguration();
const crcVersion = await getCrcVersion();
const telemetryLogger = extensionApi.env.createTelemetryLogger();

const detectionChecks: extensionApi.ProviderDetectionCheck[] = [];
let status: extensionApi.ProviderStatus = 'not-installed';
Expand Down Expand Up @@ -75,13 +76,14 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
extensionContext.subscriptions.push(provider);

const providerLifecycle: extensionApi.ProviderLifecycle = {
status: () => crcStatus.getProviderStatus(),

status: () => {
return crcStatus.getProviderStatus();
},
start: context => {
return startCrc(context.log);
return startCrc(context.log, telemetryLogger);
},
stop: () => {
return stopCrc();
return stopCrc(telemetryLogger);
},
};

Expand All @@ -92,7 +94,7 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
if (hasSetupFinished) {
await needSetup();
connectToCrc();
presetChanged(provider, extensionContext);
presetChanged(provider, extensionContext, telemetryLogger);
}
},
}),
Expand All @@ -101,17 +103,18 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
extensionContext.subscriptions.push(provider.registerLifecycle(providerLifecycle));

commandManager.setExtContext(extensionContext);
commandManager.setTelemetryLogger(telemetryLogger);

registerOpenTerminalCommand();
registerOpenConsoleCommand();
registerLogInCommands();
registerDeleteCommand();

syncPreferences(extensionContext);
syncPreferences(extensionContext, telemetryLogger);

if (!isNeedSetup) {
// initial preset check
presetChanged(provider, extensionContext);
presetChanged(provider, extensionContext, telemetryLogger);
}

if (crcInstaller.isAbleToInstall()) {
Expand All @@ -125,7 +128,7 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
return;
}
await connectToCrc();
presetChanged(provider, extensionContext);
presetChanged(provider, extensionContext, telemetryLogger);
});
},
});
Expand Down Expand Up @@ -170,6 +173,7 @@ async function registerOpenShiftLocalCluster(
name,
provider: extensionApi.Provider,
extensionContext: extensionApi.ExtensionContext,
telemetryLogger: extensionApi.TelemetryLogger,
): Promise<void> {
const status = () => crcStatus.getConnectionStatus();
const apiURL = 'https://api.crc.testing:6443';
Expand All @@ -184,10 +188,10 @@ async function registerOpenShiftLocalCluster(
return deleteCrc();
},
start: ctx => {
return startCrc(ctx.log);
return startCrc(ctx.log, telemetryLogger);
},
stop: () => {
return stopCrc();
return stopCrc(telemetryLogger);
},
},
};
Expand Down Expand Up @@ -230,6 +234,7 @@ async function connectToCrc(): Promise<void> {
async function presetChanged(
provider: extensionApi.Provider,
extensionContext: extensionApi.ExtensionContext,
telemetryLogger: extensionApi.TelemetryLogger,
): Promise<void> {
// TODO: handle situation if some cluster/connection was registered already

Expand All @@ -239,8 +244,8 @@ async function presetChanged(
// podman connection
registerPodmanConnection(provider, extensionContext);
} else if (preset === 'OpenShift') {
registerOpenShiftLocalCluster('OpenShift Local', provider, extensionContext);
registerOpenShiftLocalCluster('OpenShift Local', provider, extensionContext, telemetryLogger);
} else if (preset === 'MicroShift') {
registerOpenShiftLocalCluster('MicroShift', provider, extensionContext);
registerOpenShiftLocalCluster('MicroShift', provider, extensionContext, telemetryLogger);
}
}
24 changes: 16 additions & 8 deletions src/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ interface ConfigEntry {

let initialCrcConfig: Configuration;

export async function syncPreferences(context: extensionApi.ExtensionContext): Promise<void> {
export async function syncPreferences(
context: extensionApi.ExtensionContext,
telemetryLogger: extensionApi.TelemetryLogger,
): Promise<void> {
try {
initialCrcConfig = await commander.configGet();

Expand All @@ -62,7 +65,7 @@ export async function syncPreferences(context: extensionApi.ExtensionContext): P

context.subscriptions.push(
extensionApi.configuration.onDidChangeConfiguration(e => {
configChanged(e);
configChanged(e, telemetryLogger);
}),
);

Expand Down Expand Up @@ -120,7 +123,10 @@ async function handleProxyChange(proxy?: extensionApi.ProxySettings): Promise<vo
}
}

async function configChanged(e: extensionApi.ConfigurationChangeEvent): Promise<void> {
async function configChanged(
e: extensionApi.ConfigurationChangeEvent,
telemetryLogger: extensionApi.TelemetryLogger,
): Promise<void> {
const currentConfig = await commander.configGet();

const extConfig = extensionApi.configuration.getConfiguration();
Expand Down Expand Up @@ -167,7 +173,7 @@ async function configChanged(e: extensionApi.ConfigurationChangeEvent): Promise<
if (!isEmpty(newConfig)) {
await commander.configSet(newConfig);
if (needRecreateCrc) {
await handleRecreate();
await handleRecreate(telemetryLogger);
}
}
} catch (err) {
Expand Down Expand Up @@ -209,7 +215,7 @@ function validateRam(newVal: string | number): string | undefined {
}
}

async function handleRecreate(): Promise<void> {
async function handleRecreate(telemetryLogger: extensionApi.TelemetryLogger): Promise<void> {
const needDelete = crcStatus.status.CrcStatus !== 'No Cluster';
const needStop = crcStatus.getProviderStatus() === 'started' || crcStatus.getProviderStatus() === 'starting';

Expand All @@ -228,13 +234,15 @@ async function handleRecreate(): Promise<void> {
...buttons,
);

// we might wanna log what user clicked on.
// for now we infer from the logged events
if (result === 'Stop and Delete') {
await stopCrc();
await stopCrc(telemetryLogger);
await deleteCrc();
} else if (result === 'Delete and Restart') {
await stopCrc();
await stopCrc(telemetryLogger);
await deleteCrc();
await startCrc(defaultLogger);
await startCrc(defaultLogger, telemetryLogger);
} else if (result === 'Delete') {
await deleteCrc();
}
Expand Down

0 comments on commit 93e26ec

Please sign in to comment.