Skip to content
Merged
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
14 changes: 11 additions & 3 deletions src/utils/telemetry/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PostHog } from "posthog-node"
import type { PostHog } from "posthog-node"
import type { TelemetryConfig } from "./types"

const POSTHOG_API_KEY = process.env.POSTHOG_API_KEY || ""
Expand Down Expand Up @@ -29,15 +29,23 @@ export class TelemetryClient {
private sessionStartTime: number | null = null

/* c8 ignore start -- requires PostHog API key */
init(config: TelemetryConfig): void {
async init(config: TelemetryConfig): Promise<void> {
if (this.initialized || !config.isEnabled() || !POSTHOG_API_KEY) return

this.config = config
this.userId = config.userId
this.sessionId = crypto.randomUUID()
this.sessionStartTime = Date.now()

this.posthog = new PostHog(POSTHOG_API_KEY, {
let PostHogClass: typeof PostHog
try {
;({ PostHog: PostHogClass } = await import("posthog-node"))
} catch {
// posthog-node is unavailable (e.g. browser/web context)
return
}

this.posthog = new PostHogClass(POSTHOG_API_KEY, {
host: POSTHOG_HOST,
disableGeoip: true,
flushAt: FLUSH_AT,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/telemetry/vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function initVSCodeTelemetry(
vscode.extensions.getExtension("FastAPILabs.fastapi-vscode")?.packageJSON
?.version ?? "unknown"

client.init({
await client.init({
userId,
clientInfo: getClientInfo(),
extensionVersion,
Expand Down