Skip to content

Commit c617611

Browse files
authored
chore: observability status fix (#1724)
* chore: fix observability for status command * chore: add changeset
1 parent c77c8c8 commit c617611

6 files changed

Lines changed: 38 additions & 27 deletions

File tree

.changeset/empty-dryers-happen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lingo.dev": patch
3+
---
4+
5+
fix observability for status command

packages/cli/src/cli/cmd/i18n.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export default new Command()
125125
}
126126

127127
let hasErrors = false;
128-
let authId: string | null = null;
128+
let email: string | null = null;
129129
const errorDetails: ErrorDetail[] = [];
130130
try {
131131
ora.start("Loading configuration...");
@@ -141,15 +141,15 @@ export default new Command()
141141
const isByokMode = !!i18nConfig?.provider;
142142

143143
if (isByokMode) {
144-
authId = null;
144+
email = null;
145145
ora.succeed("Using external provider (BYOK mode)");
146146
} else {
147147
const auth = await validateAuth(settings);
148-
authId = auth.id;
148+
email = auth.email;
149149
ora.succeed(`Authenticated as ${auth.email}`);
150150
}
151151

152-
await trackEvent(authId, "cmd.i18n.start", {
152+
await trackEvent(email, "cmd.i18n.start", {
153153
i18nConfig,
154154
flags,
155155
});
@@ -569,7 +569,7 @@ export default new Command()
569569
console.log();
570570
if (!hasErrors) {
571571
ora.succeed("Localization completed.");
572-
await trackEvent(authId, "cmd.i18n.success", {
572+
await trackEvent(email, "cmd.i18n.success", {
573573
i18nConfig: {
574574
sourceLocale: i18nConfig!.locale.source,
575575
targetLocales: i18nConfig!.locale.targets,
@@ -583,7 +583,7 @@ export default new Command()
583583
await new Promise((resolve) => setTimeout(resolve, 50));
584584
} else {
585585
ora.warn("Localization completed with errors.");
586-
await trackEvent(authId, "cmd.i18n.error", {
586+
await trackEvent(email, "cmd.i18n.error", {
587587
flags,
588588
...aggregateErrorAnalytics(
589589
errorDetails,
@@ -615,7 +615,7 @@ export default new Command()
615615
};
616616
}
617617

618-
await trackEvent(authId, "cmd.i18n.error", {
618+
await trackEvent(email, "cmd.i18n.error", {
619619
flags,
620620
errorType,
621621
errorName: error.name || "Error",

packages/cli/src/cli/cmd/run/_utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { CmdRunContext } from "./_types";
22

33
/**
4-
* Determines the authentication ID for tracking purposes
4+
* Determines the user's email for tracking purposes.
5+
* Returns null if using BYOK mode or if authentication fails.
56
*/
6-
export async function determineAuthId(
7+
export async function determineEmail(
78
ctx: CmdRunContext,
89
): Promise<string | null> {
910
const isByokMode = !!ctx.config?.provider;

packages/cli/src/cli/cmd/run/index.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
renderSummary,
1919
} from "../../utils/ui";
2020
import trackEvent from "../../utils/observability";
21-
import { determineAuthId } from "./_utils";
21+
import { determineEmail } from "./_utils";
2222

2323
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2424

@@ -119,7 +119,7 @@ export default new Command()
119119
"Play audio feedback when translations complete (success or failure sounds)",
120120
)
121121
.action(async (args) => {
122-
let authId: string | null = null;
122+
let email: string | null = null;
123123
try {
124124
const ctx: CmdRunContext = {
125125
flags: flagsSchema.parse(args),
@@ -138,9 +138,9 @@ export default new Command()
138138

139139
await setup(ctx);
140140

141-
authId = await determineAuthId(ctx);
141+
email = await determineEmail(ctx);
142142

143-
await trackEvent(authId, "cmd.run.start", {
143+
await trackEvent(email, "cmd.run.start", {
144144
config: ctx.config,
145145
flags: ctx.flags,
146146
});
@@ -169,13 +169,17 @@ export default new Command()
169169
await watch(ctx);
170170
}
171171

172-
await trackEvent(authId, "cmd.run.success", {
172+
await trackEvent(email, "cmd.run.success", {
173173
config: ctx.config,
174174
flags: ctx.flags,
175175
});
176176
await new Promise((resolve) => setTimeout(resolve, 50));
177177
} catch (error: any) {
178-
await trackEvent(authId, "cmd.run.error", {});
178+
await trackEvent(email, "cmd.run.error", {
179+
flags: args,
180+
error: error.message,
181+
authenticated: !!email,
182+
});
179183
await new Promise((resolve) => setTimeout(resolve, 50));
180184
// Play sad sound if sound flag is enabled
181185
if (args.sound) {

packages/cli/src/cli/cmd/status.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default new Command()
6363
.action(async function (options) {
6464
const ora = Ora();
6565
const flags = parseFlags(options);
66-
let authId: string | null = null;
66+
let email: string | null = null;
6767

6868
try {
6969
ora.start("Loading configuration...");
@@ -76,7 +76,7 @@ export default new Command()
7676
ora.start("Checking authentication status...");
7777
const auth = await tryAuthenticate(settings);
7878
if (auth) {
79-
authId = auth.id;
79+
email = auth.email;
8080
ora.succeed(`Authenticated as ${auth.email}`);
8181
} else {
8282
ora.info(
@@ -92,10 +92,11 @@ export default new Command()
9292
ora.succeed("Localization configuration is valid");
9393

9494
// Track event with or without authentication
95-
trackEvent(authId, "cmd.status.start", {
95+
trackEvent(email, "cmd.status.start", {
9696
i18nConfig,
9797
flags,
9898
});
99+
await new Promise((resolve) => setTimeout(resolve, 50));
99100

100101
let buckets = getBuckets(i18nConfig!);
101102
if (flags.bucket?.length) {
@@ -628,22 +629,22 @@ export default new Command()
628629
}
629630

630631
// Track successful completion
631-
trackEvent(authId, "cmd.status.success", {
632+
trackEvent(email, "cmd.status.success", {
632633
i18nConfig,
633634
flags,
634635
totalSourceKeyCount,
635636
languageStats,
636637
totalWordsToTranslate,
637-
authenticated: !!authId,
638+
authenticated: !!email,
638639
});
639640
await new Promise((resolve) => setTimeout(resolve, 50));
640641
exitGracefully();
641642
} catch (error: any) {
642643
ora.fail(error.message);
643-
trackEvent(authId, "cmd.status.error", {
644+
trackEvent(email, "cmd.status.error", {
644645
flags,
645646
error: error.message,
646-
authenticated: !!authId,
647+
authenticated: !!email,
647648
});
648649
await new Promise((resolve) => setTimeout(resolve, 50));
649650
process.exit(1);

packages/cli/src/cli/utils/observability.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const POSTHOG_PATH = "/i/v0/e/";
99
const REQUEST_TIMEOUT_MS = 3000;
1010
const TRACKING_VERSION = "2.0";
1111

12-
function determineDistinctId(providedId: string | null | undefined): {
12+
function determineDistinctId(email: string | null | undefined): {
1313
distinct_id: string;
1414
distinct_id_source: string;
1515
project_id: string | null;
1616
} {
17-
if (providedId) {
17+
if (email) {
1818
const projectId = getRepositoryId();
1919
return {
20-
distinct_id: providedId,
20+
distinct_id: email,
2121
distinct_id_source: "email",
2222
project_id: projectId,
2323
};
@@ -46,7 +46,7 @@ function determineDistinctId(providedId: string | null | undefined): {
4646
}
4747

4848
export default function trackEvent(
49-
distinctId: string | null | undefined,
49+
email: string | null | undefined,
5050
event: string,
5151
properties?: Record<string, any>,
5252
): void {
@@ -56,7 +56,7 @@ export default function trackEvent(
5656

5757
setImmediate(() => {
5858
try {
59-
const identityInfo = determineDistinctId(distinctId);
59+
const identityInfo = determineDistinctId(email);
6060

6161
if (process.env.DEBUG === "true") {
6262
console.log(

0 commit comments

Comments
 (0)