Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add telemetry support #9

Merged
merged 3 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@types/express": "^5.0.0",
"@types/node": "^18.19.54",
"@types/prompts": "^2.4.9",
"@types/uuid": "^10.0.0",
"@types/yargs": "^17.0.33",
"nodemon": "^3.1.7",
"pkg": "^5.8.1",
Expand All @@ -81,6 +82,7 @@
"tree-sitter": "^0.21.1",
"tree-sitter-javascript": "^0.23.0",
"tree-sitter-typescript": "^0.23.0",
"uuid": "^10.0.0",
"yargs": "^17.7.2",
"zod": "^3.23.8"
}
Expand Down
42 changes: 25 additions & 17 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import { getApi } from "./api";
import annotateOpenAICommandHandler from "./commands/annotate";
import splitCommandHandler from "./commands/split";
import { findAvailablePort, openInBrowser } from "./helper/server";
import initCommandHandler from "./commands/init";
import splitCommandHandler from "./commands/split";
import { getConfigFromWorkDir, getOpenaiApiKeyFromConfig } from "./config";
import { findAvailablePort, openInBrowser } from "./helper/server";
import { TelemetryEvents, trackEvent } from "./telemetry";

// remove all warning.
// We need this because of some depreciation warning we have with 3rd party libraries
Expand All @@ -17,8 +18,13 @@ if (process.env.NODE_ENV !== "development") {
process.removeAllListeners("warning");
}

if (process.env.NAPI_DISABLE_TELEMETRY !== "true") {
trackEvent(TelemetryEvents.APP_START, {
message: "Napi started with Telemetry enabled",
});
}

yargs(hideBin(process.argv))
// Global options, used for all commands
.options({
workdir: {
type: "string",
Expand All @@ -27,30 +33,32 @@ yargs(hideBin(process.argv))
description: "working directory",
},
})
// Init command
.command(
"init",
"initialize a nanoapi project",
(yargs) => yargs,
(argv) => {
trackEvent(TelemetryEvents.INIT_COMMAND, { message: "Init command" });
initCommandHandler(argv.workdir);
},
)
// Annotate openai command
.command(
"annotate openai [entrypoint]",
"Annotate a program, needed for splitting",
(yargs) => {
return yargs.options({
(yargs) =>
yargs.options({
apiKey: {
type: "string",
default: "",
alias: "k",
description: "OpenAI API key",
},
});
},
}),
(argv) => {
trackEvent(TelemetryEvents.ANNOTATE_COMMAND, {
message: "Annotate command",
});
const napiConfig = getConfigFromWorkDir(argv.workdir);

if (!napiConfig) {
Expand Down Expand Up @@ -79,10 +87,11 @@ yargs(hideBin(process.argv))
.command(
"split [entrypoint]",
"Split a program into multiple ones",
(yargs) => {
return yargs;
},
(yargs) => yargs,
(argv) => {
trackEvent(TelemetryEvents.SPLIT_COMMAND, {
message: "Split command",
});
const napiConfig = getConfigFromWorkDir(argv.workdir);

if (!napiConfig) {
Expand All @@ -97,10 +106,12 @@ yargs(hideBin(process.argv))
.command(
"ui",
"open the NanoAPI UI",
(yargs) => {
return yargs;
},
(yargs) => yargs,
async (argv) => {
trackEvent(TelemetryEvents.UI_OPEN, {
message: "UI command",
});

const napiConfig = getConfigFromWorkDir(argv.workdir);

if (!napiConfig) {
Expand All @@ -109,15 +120,12 @@ yargs(hideBin(process.argv))
}

const app = express();

const api = getApi(napiConfig);

app.use(api);

if (process.env.NODE_ENV === "development") {
const targetServiceUrl =
process.env.APP_SERVICE_URL || "http://localhost:3001";

app.use(
"/",
createProxyMiddleware({
Expand Down
106 changes: 106 additions & 0 deletions packages/cli/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// telemetry.ts
import { EventEmitter } from "events";
import { existsSync, readFileSync, writeFileSync } from "fs";
import { request } from "http";
import { join } from "path";
import { URL } from "url";
import { v4 as uuidv4 } from "uuid";

export enum TelemetryEvents {
APP_START = "app_start",
INIT_COMMAND = "init_command",
ANNOTATE_COMMAND = "annotate_command",
SPLIT_COMMAND = "split_command",
UI_OPEN = "ui_open",
SERVER_STARTED = "server_started",
API_REQUEST = "api_request",
ERROR_OCCURRED = "error_occurred",
USER_ACTION = "user_action",
}

export interface TelemetryEvent {
sessionId: string;
eventId: TelemetryEvents;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: any;
timestamp: string;
}

const telemetry = new EventEmitter();
// TODO: @erbesharat: Deploy mongoose script to GCF and update the endpoint
const TELEMETRY_ENDPOINT =
process.env.TELEMETRY_ENDPOINT || "http://localhost:3000/telemetry";
const SESSION_FILE_PATH = join("/tmp", "napi_session_id");

// getSessionId generates a new session ID and cache it in SESSION_FILE_PATH
const getSessionId = (): string => {
if (existsSync(SESSION_FILE_PATH)) {
const fileContent = readFileSync(SESSION_FILE_PATH, "utf-8");
const [storedDate, sessionId] = fileContent.split(":");
const today = new Date().toISOString().slice(0, 10);

if (storedDate === today) {
return sessionId;
}
}

const newSessionId = uuidv4();
const today = new Date().toISOString().slice(0, 10);
writeFileSync(SESSION_FILE_PATH, `${today}:${newSessionId}`);
return newSessionId;
};

const SESSION_ID = getSessionId();

telemetry.on("event", (data) => {
sendTelemetryData(data);
});

const sendTelemetryData = (data: TelemetryEvent) => {
try {
const url = new URL(TELEMETRY_ENDPOINT);
const options = {
hostname: url.hostname,
port: url.port || (url.protocol === "https:" ? 443 : 80),
path: url.pathname,
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(JSON.stringify(data)),
"User-Agent": "napi",
},
};

const req = request(options, (res) => {
let responseData = "";
res.on("data", (chunk) => {
responseData += chunk;
});

res.on("end", () => {
console.info(`Telemetry response: ${res.statusCode} - ${responseData}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Telemetry tracking should be silent. Let’s not log or error any messages here except maybe in verbose debugging more

});
});

req.on("error", (error) => {
console.error(`Failed to send telemetry data: ${error}`);
});

req.write(JSON.stringify(data));
req.end();
} catch (error) {
console.error(`Error in telemetry setup: ${error}`);
}
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const trackEvent = (eventId: TelemetryEvents, eventData: any) => {
const telemetryPayload: TelemetryEvent = {
sessionId: SESSION_ID,
eventId,
data: eventData,
timestamp: new Date().toISOString(),
};

telemetry.emit("event", telemetryPayload);
};
Loading