Skip to content

Commit

Permalink
Use public facing gateway for telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
erbesharat committed Oct 27, 2024
1 parent 6aa8d74 commit feae744
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 39 deletions.
15 changes: 13 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"vitest": "^2.1.1"
},
"dependencies": {
"axios": "^1.7.7",
"dependency-tree": "^11.0.1",
"express": "^4.21.1",
"http-proxy-middleware": "^3.0.3",
Expand Down
8 changes: 3 additions & 5 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ 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",
});
}
trackEvent(TelemetryEvents.APP_START, {
message: "Napi started with Telemetry enabled",
});

yargs(hideBin(process.argv))
.options({
Expand Down
39 changes: 7 additions & 32 deletions packages/cli/src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// telemetry.ts
import axios from "axios";
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 {
Expand All @@ -27,9 +25,9 @@ export interface TelemetryEvent {
}

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";
process.env.TELEMETRY_ENDPOINT ||
"https://napi-watchdog-api-gateway-33ge7a49.nw.gateway.dev/telemetryHandler";
const SESSION_FILE_PATH = join("/tmp", "napi_session_id");

// getSessionId generates a new session ID and cache it in SESSION_FILE_PATH
Expand All @@ -56,40 +54,17 @@ telemetry.on("event", (data) => {
sendTelemetryData(data);
});

const sendTelemetryData = (data: TelemetryEvent) => {
const sendTelemetryData = async (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",
await axios.post(TELEMETRY_ENDPOINT, data, {
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}`);
});
timeout: 5000,
});

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}`);
console.error(`Failed to send telemetry data: ${error}`);
}
};

Expand Down

0 comments on commit feae744

Please sign in to comment.