Skip to content

Commit

Permalink
feat: telemetry extension
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <[email protected]>
  • Loading branch information
dkwon17 committed Sep 26, 2022
1 parent 50efd76 commit 6269afb
Show file tree
Hide file tree
Showing 21 changed files with 3,157 additions and 2 deletions.
1 change: 1 addition & 0 deletions code/build/gulpfile.extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const compilations = [
'che-on-start/tsconfig.json',
'che-remote/tsconfig.json',
'che-resource-monitor/tsconfig.json',
'che-telemetry/tsconfig.json',
'che-github-authentication/tsconfig.json',
'configuration-editing/build/tsconfig.json',
'configuration-editing/tsconfig.json',
Expand Down
3 changes: 2 additions & 1 deletion code/extensions/che-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"inversify": "^6.0.1",
"js-yaml": "^4.1.0",
"reflect-metadata": "^0.1.13",
"vscode-nls": "^5.0.0"
"vscode-nls": "^5.0.0",
"@eclipse-che/workspace-telemetry-client": "latest"
},
"devDependencies": {
"@types/fs-extra": "^9.0.13",
Expand Down
2 changes: 2 additions & 0 deletions code/extensions/che-api/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import { DevfileService } from './devfile-service';
import { WorkspaceService } from './workspace-service';
import { GithubService } from './github-service';
import { TelemetryService } from './telemetry-service';

export interface Api {
getDevfileService(): DevfileService;
getWorkspaceService(): WorkspaceService;
getGithubService(): GithubService;
getTelemetryService(): TelemetryService;
}
23 changes: 23 additions & 0 deletions code/extensions/che-api/src/api/telemetry-service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**********************************************************************
* Copyright (c) 2022 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

export const TelemetryService = Symbol("TelemetryService");

export interface TelemetryService {
submitTelemetryEvent(
id: string,
ownerId: string,
ip: string,
agent: string,
resolution: string,
properties: [string, string][]
): Promise<void>;
submitTelemetryActivity(): Promise<void>;
}
9 changes: 8 additions & 1 deletion code/extensions/che-api/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { WorkspaceService } from './api/workspace-service';
import { K8sWorkspaceServiceImpl } from './impl/k8s-workspace-service-impl';
import { GithubService } from './api/github-service';
import { GithubServiceImpl } from './impl/github-service-impl';
import { TelemetryService } from './api/telemetry-service';
import { K8sTelemetryServiceImpl } from './impl/k8s-telemetry-service-impl';
import * as axios from 'axios';


Expand All @@ -39,10 +41,12 @@ export async function activate(_extensionContext: vscode.ExtensionContext): Prom
container.bind(Symbol.for('AxiosInstance')).toConstantValue(axios);
container.bind(GithubServiceImpl).toSelf().inSingletonScope();
container.bind(GithubService).to(GithubServiceImpl).inSingletonScope();
container.bind(TelemetryService).to(K8sTelemetryServiceImpl).inSingletonScope();

const devfileService = container.get(DevfileService) as DevfileService;
const workspaceService = container.get(WorkspaceService) as WorkspaceService;
const githubService = container.get(GithubService) as GithubService;
const telemetryService = container.get(TelemetryService) as TelemetryService;
const api: Api = {
getDevfileService(): DevfileService {
return devfileService;
Expand All @@ -52,7 +56,10 @@ export async function activate(_extensionContext: vscode.ExtensionContext): Prom
},
getGithubService(): GithubService {
return githubService;
}
},
getTelemetryService(): TelemetryService {
return telemetryService;
},
};

return api;
Expand Down
70 changes: 70 additions & 0 deletions code/extensions/che-api/src/impl/k8s-telemetry-service-impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**********************************************************************
* Copyright (c) 2021 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

import {
EventProperty,
TelemetryClient,
} from "@eclipse-che/workspace-telemetry-client";

import { TelemetryService } from "../api/telemetry-service";
import { injectable } from "inversify";

@injectable()
export class K8sTelemetryServiceImpl implements TelemetryService {
private readonly telemetryClient: TelemetryClient | undefined;

constructor() {
if (process.env.DEVWORKSPACE_TELEMETRY_BACKEND_PORT === undefined) {
console.error(
'Unable to create telemetry client: "DEVWORKSPACE_TELEMETRY_BACKEND_PORT" is not set.'
);
} else {
this.telemetryClient = new TelemetryClient(
undefined,
"http://localhost:" + process.env.DEVWORKSPACE_TELEMETRY_BACKEND_PORT
);
}
}

async submitTelemetryEvent(
id: string,
ownerId: string,
ip: string,
agent: string,
resolution: string,
properties: [string, string][]
): Promise<void> {
if (!this.telemetryClient) {
return;
}

await this.telemetryClient.event({
id: id,
ip: ip,
ownerId: ownerId,
agent: agent,
resolution: resolution,
properties: properties.map((prop: [string, string]) => {
const eventProp: EventProperty = {
id: prop[0],
value: prop[1],
};
return eventProp;
}),
});
}

async submitTelemetryActivity(): Promise<void> {
if (!this.telemetryClient) {
return;
}
await this.telemetryClient.activity();
}
}
25 changes: 25 additions & 0 deletions code/extensions/che-api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,19 @@
request "^2.81.0"
rewire "^3.0.2"

"@eclipse-che/api@^7.38.1":
version "7.54.0"
resolved "https://registry.yarnpkg.com/@eclipse-che/api/-/api-7.54.0.tgz#10305df3fca96e4149a6e9d99b107f89f916d1ec"
integrity sha512-/7zO0IVmtMsaoEY6jybejDT3fq68t+Y4pSkDGG3+nJIK1qcT89yUyKMQGUIP4xjIZBZQvCddijscPlhJWXwndg==

"@eclipse-che/workspace-telemetry-client@latest":
version "0.0.1-1654006444"
resolved "https://registry.yarnpkg.com/@eclipse-che/workspace-telemetry-client/-/workspace-telemetry-client-0.0.1-1654006444.tgz#62a4a03573061a3452266392510729843e89d0b9"
integrity sha512-WQ+zh54t/Kt83Ex9eH05w/4hmhkXAgXC6wauXt92JHhrPbox9E1CWgDVdP/T9FHdCST42qcbPEnXK3zpdgkSBg==
dependencies:
"@eclipse-che/api" "^7.38.1"
axios "^0.24.0"

"@istanbuljs/load-nyc-config@^1.0.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced"
Expand Down Expand Up @@ -898,6 +911,13 @@ [email protected]:
dependencies:
follow-redirects "^1.14.0"

axios@^0.24.0:
version "0.24.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.24.0.tgz#804e6fa1e4b9c5288501dd9dff56a7a0940d20d6"
integrity sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==
dependencies:
follow-redirects "^1.14.4"

babel-code-frame@^6.26.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-code-frame/-/babel-code-frame-6.26.0.tgz#63fd43f7dc1e3bb7ce35947db8fe369a3f58c74b"
Expand Down Expand Up @@ -1633,6 +1653,11 @@ follow-redirects@^1.14.0:
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7"
integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==

follow-redirects@^1.14.4:
version "1.15.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==

forever-agent@~0.6.1:
version "0.6.1"
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
Expand Down
7 changes: 7 additions & 0 deletions code/extensions/che-telemetry/.vscodeignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build/**
tests/**
coverage/**
out/**
tsconfig.json
extension.webpack.config.js
yarn.lock
13 changes: 13 additions & 0 deletions code/extensions/che-telemetry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Eclipse Che Telemetry Extension for Visual Studio Code

**Notice:** This extension is bundled with Visual Studio Code. It can be disabled but not uninstalled.

## Features
This extension detects and sends the following events to a backend telemetry plugin listening on `http://localhost:${DEVWORKSPACE_TELEMETRY_BACKEND_PORT}`.

This extension will activate on Che Code startup.

| Event ID | Description |
|------------------|------------------------------------------------------------|
| WORKSPACE_OPENED | Sent when the telemetry extension activates |
| EDITOR_USED | Sent on the vscode.workspace.onDidChangeTextDocument event |
11 changes: 11 additions & 0 deletions code/extensions/che-telemetry/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**********************************************************************
* Copyright (c) 2022 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

export type EventId = "WORKSPACE_OPENED" | "EDITOR_USED";
60 changes: 60 additions & 0 deletions code/extensions/che-telemetry/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**********************************************************************
* Copyright (c) 2022 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/
import * as vscode from 'vscode';
import { TelemetryEventService } from "./telemetry-event-service";

let telemetryEventService: TelemetryEventService;

export async function activate(context: vscode.ExtensionContext) {
const telemetryService = await getTelemetryService();
telemetryEventService = new TelemetryEventService(telemetryService);

telemetryEventService.sendEvent(
"WORKSPACE_OPENED",
context.extensionPath,
[]
);

context.subscriptions.push(
vscode.workspace.onDidChangeTextDocument(
(e: vscode.TextDocumentChangeEvent) => {
if (e.document.uri.scheme !== "output") {
telemetryEventService.sendEvent(
"EDITOR_USED",
context.extensionPath,
[["programming language", e.document.languageId]]
);
}
}
)
);
}

async function getTelemetryService(): Promise<any> {
const CHE_API = "eclipse-che.api";
const extensionApi = vscode.extensions.getExtension(CHE_API);
if (!extensionApi) {
throw Error(
`Failed to get workspace service. Extension ${CHE_API} is not installed.`
);
}

try {
await extensionApi.activate();
const cheApi: any = extensionApi?.exports;
return cheApi.getTelemetryService();
} catch {
throw Error(
`Failed to get telemetry service. Could not activate and retrieve exports from extension ${CHE_API}.`
);
}
}

export function deactivate() {}
35 changes: 35 additions & 0 deletions code/extensions/che-telemetry/extension.webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**********************************************************************
* Copyright (c) 2022 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/

/* eslint-disable header/header */

//@ts-check

"use strict";

const withDefaults = require("../shared.webpack.config");
const webpack = require("webpack");

module.exports = withDefaults({
context: __dirname,
resolve: {
mainFields: ["module", "main"],
},
entry: {
extension: "./src/extension.ts",
},
plugins: [
new webpack.ContextReplacementPlugin(/keyv/), // needs to exclude the package to ignore warnings https://github.com/jaredwray/keyv/issues/45
],
externals: {
bufferutil: "commonjs bufferutil", // ignored
"utf-8-validate": "commonjs utf-8-validate", // ignored
},
});
Binary file added code/extensions/che-telemetry/images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions code/extensions/che-telemetry/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "telemetry",
"displayName": "%displayName%",
"description": "%description%",
"publisher": "eclipse-che",
"license": "EPL-2.0",
"version": "0.0.1",
"engines": {
"vscode": "^1.63.0"
},
"icon": "images/icon.png",
"categories": [
"Other"
],
"activationEvents": [
"*"
],
"capabilities": {
"virtualWorkspaces": true,
"untrustedWorkspaces": {
"supported": true
}
},
"main": "./out/extension.js",
"scripts": {
"compile": "gulp compile-extension:che-telemetry",
"watch": "gulp watch-extension:che-telemetry",
"vscode:prepublish": "npm run compile",
"test": "jest",
"lint:fix": "eslint --fix --cache=true --no-error-on-unmatched-pattern=true \"{src,tests}/**/*.{ts,tsx}\""
},
"devDependencies": {
"@types/fs-extra": "^9.0.13",
"@types/jest": "^27.4.1",
"@types/js-yaml": "^4.0.5",
"@types/node": "14.x",
"add": "^2.0.6",
"jest": "27.3.1",
"ts-jest": "^27.1.4",
"yarn": "^1.22.18"
},
"dependencies": {
"axios": "0.21.2",
"inversify": "^6.0.1"
},
"repository": {
"type": "git",
"url": "https://github.com/che-incubator/che-code.git"
},
"extensionDependencies": [
"eclipse-che.api"
]
}
4 changes: 4 additions & 0 deletions code/extensions/che-telemetry/package.nls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"displayName": "Eclipse Che Telemetry Extension",
"description": "Eclipse Che Telemetry Extension"
}
Loading

0 comments on commit 6269afb

Please sign in to comment.