Skip to content

Commit 34cd602

Browse files
feat: Identify client type in metrics
1 parent a181e2a commit 34cd602

24 files changed

+122
-66
lines changed

packages/kitten-analysts/examples/2108acbb-2feb-427b-92ca-add74a2a8c10.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/kitten-analysts/examples/a708b0ad-5f94-4466-8a2e-1d381117d0e0.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/kitten-analysts/examples/ka-internal-savestate.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/kitten-analysts/source/KittenAnalysts.ts

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export interface KittenAnalystsMessage<
6464
*/
6565
data?: TData;
6666

67+
client_type: "backend" | "browser" | "headless";
68+
6769
/**
6870
* The HTTP URL that identifies the context of the client that sent the message.
6971
*/
@@ -181,7 +183,12 @@ export class KittenAnalysts {
181183

182184
this.ws.onopen = () => {
183185
cinfo("WS connection established.");
184-
this.postMessage({ type: "connected", location: this.location, guid: game.telemetry.guid });
186+
this.postMessage({
187+
type: "connected",
188+
client_type: this.location.includes("headless.html") ? "headless" : "browser",
189+
location: this.location,
190+
guid: game.telemetry.guid,
191+
});
185192
};
186193

187194
this.ws.onmessage = event => {
@@ -246,11 +253,12 @@ export class KittenAnalysts {
246253
);
247254

248255
return {
249-
type: message.type,
250-
location: this.location,
256+
client_type: this.location.includes("headless.html") ? "headless" : "browser",
257+
data: [...bonfire, ...space, ...religion],
251258
guid: game.telemetry.guid,
259+
location: this.location,
252260
responseId: message.responseId,
253-
data: [...bonfire, ...space, ...religion],
261+
type: message.type,
254262
};
255263

256264
break;
@@ -265,11 +273,12 @@ export class KittenAnalysts {
265273
}));
266274

267275
return {
268-
type: message.type,
269-
location: this.location,
276+
client_type: this.location.includes("headless.html") ? "headless" : "browser",
277+
data,
270278
guid: game.telemetry.guid,
279+
location: this.location,
271280
responseId: message.responseId,
272-
data,
281+
type: message.type,
273282
};
274283

275284
break;
@@ -285,11 +294,12 @@ export class KittenAnalysts {
285294
);
286295

287296
return {
288-
type: message.type,
289-
location: this.location,
297+
client_type: this.location.includes("headless.html") ? "headless" : "browser",
298+
data,
290299
guid: game.telemetry.guid,
300+
location: this.location,
291301
responseId: message.responseId,
292-
data,
302+
type: message.type,
293303
};
294304

295305
break;
@@ -308,20 +318,22 @@ export class KittenAnalysts {
308318
reportFrameListener = (event: Event): void => {
309319
const location = window.location.toString().replace(/#$/, "");
310320
this.postMessage({
311-
type: "reportFrame",
312-
location,
313-
guid: game.telemetry.guid,
321+
client_type: location.includes("headless.html") ? "headless" : "browser",
314322
data: (event as CustomEvent<unknown>).detail,
323+
guid: game.telemetry.guid,
324+
location,
325+
type: "reportFrame",
315326
});
316327
};
317328

318329
reportSavegameListener = (event: Event): void => {
319330
const location = window.location.toString().replace(/#$/, "");
320331
this.postMessage({
321-
type: "reportSavegame",
322-
location,
323-
guid: game.telemetry.guid,
332+
client_type: location.includes("headless.html") ? "headless" : "browser",
324333
data: (event as CustomEvent<unknown>).detail,
334+
guid: game.telemetry.guid,
335+
location,
336+
type: "reportSavegame",
325337
});
326338
};
327339

packages/kitten-analysts/source/entrypoint-backend.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const ks_iterate_duration = new Histogram({
4848
name: "ks_iterate_duration",
4949
help: "How long each iteration of KS took.",
5050
buckets: [...linearBuckets(0, 1, 100), ...exponentialBuckets(100, 1.125, 30)],
51-
labelNames: ["guid", "location", "manager"],
51+
labelNames: ["client_type", "guid", "location", "manager"],
5252
});
5353

5454
// KGNet Savegame Storage
@@ -68,8 +68,10 @@ interface KGNetSaveFromGame {
6868
}
6969
interface KGNetSaveUpdate {
7070
guid: string;
71-
"metadata[archived]"?: string;
72-
"metadata[label]"?: string;
71+
metadata?: {
72+
archived: string;
73+
label: string;
74+
};
7375
}
7476
interface KGNetSaveFromAnalysts {
7577
telemetry: {
@@ -187,7 +189,12 @@ export class KittensGameRemote {
187189
console.info(`=> Received frame report (${message.location}).`, delta);
188190

189191
ks_iterate_duration.observe(
190-
{ guid: message.guid, location: message.location, manager: "all" },
192+
{
193+
client_type: message.location.includes("headless.html") ? "headless" : "browser",
194+
guid: message.guid,
195+
location: message.location,
196+
manager: "all",
197+
},
191198
delta,
192199
);
193200
for (const [measurement, timeTaken] of Object.entries(payload.measurements)) {
@@ -196,7 +203,12 @@ export class KittensGameRemote {
196203
}
197204

198205
ks_iterate_duration.observe(
199-
{ guid: message.guid, location: message.location, manager: measurement },
206+
{
207+
client_type: message.location.includes("headless.html") ? "headless" : "browser",
208+
guid: message.guid,
209+
location: message.location,
210+
manager: measurement,
211+
},
200212
timeTaken,
201213
);
202214
}
@@ -256,12 +268,13 @@ export class KittensGameRemote {
256268
}
257269

258270
sendMessage<TMessage extends KittenAnalystsMessageId>(
259-
message: Omit<KittenAnalystsMessage<TMessage>, "location" | "guid">,
271+
message: Omit<KittenAnalystsMessage<TMessage>, "client_type" | "location" | "guid">,
260272
): Promise<Array<KittenAnalystsMessage<TMessage> | null>> {
261273
const clientRequests = [...this.sockets.values()].map(socket =>
262274
this.#sendMessageToSocket(
263275
{
264276
...message,
277+
client_type: "backend",
265278
guid: "ka-backend",
266279
location: this.location,
267280
},
@@ -466,6 +479,7 @@ routerNetwork.post("/kgnet/save/upload", context => {
466479
.toHeadless({
467480
type: "injectSavegame",
468481
data: savegame,
482+
client_type: "backend",
469483
location: `ws://${(remote.wss.address() as AddressInfo | null)?.address ?? "localhost"}:9093/`,
470484
guid: "ka-backend",
471485
})
@@ -487,12 +501,12 @@ routerNetwork.post("/kgnet/save/update", context => {
487501
const gameGUID = gameSave.guid;
488502
const existingSave = saveStore.get(gameGUID);
489503
if (isNil(existingSave)) {
490-
console.warn(`Couldn't find existing savegame with ID '${gameGUID}'!`);
504+
console.warn(`=> Couldn't find existing savegame with ID '${gameGUID}'! Update is ignored.`);
491505
return;
492506
}
493507

494-
existingSave.archived = gameSave["metadata[archived]"] === "true";
495-
existingSave.label = gameSave["metadata[label]"] ?? existingSave.label;
508+
existingSave.archived = gameSave.metadata?.archived === "true";
509+
existingSave.label = gameSave.metadata?.label ?? existingSave.label;
496510
writeFileSync(`${LOCAL_STORAGE_PATH}/${gameGUID}.json`, JSON.stringify(existingSave));
497511
saveStore.set(gameGUID, existingSave);
498512
console.debug(`=> Savegame persisted to disc.`);

packages/kitten-analysts/source/metrics/factory.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ export const gaugeFactory = <
2323
name: string;
2424
labelNames: Array<string>;
2525
require: TMessage;
26-
extract: (guid: string, location: string, element: TData[number], subject: Gauge) => void;
26+
extract: (
27+
client_type: "backend" | "browser" | "headless",
28+
guid: string,
29+
location: string,
30+
element: TData[number],
31+
subject: Gauge,
32+
) => void;
2733
}) =>
2834
new Gauge({
2935
help: instructions.help,
@@ -46,7 +52,13 @@ export const gaugeFactory = <
4652
continue;
4753
}
4854
for (const entity of mustExist(clientResponse.data) as TData) {
49-
instructions.extract(clientResponse.guid, clientResponse.location, entity, this);
55+
instructions.extract(
56+
clientResponse.client_type,
57+
clientResponse.guid,
58+
clientResponse.location,
59+
entity,
60+
this,
61+
);
5062
}
5163
}
5264
},

packages/kitten-analysts/source/metrics/kg_building_on.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ export const kg_building_on = (cache: MessageCache, remote: KittensGameRemote) =
88
remote,
99
help: "How many of the given building are turned on.",
1010
name: "kg_building_on",
11-
labelNames: ["guid", "name", "label", "location", "tab"],
11+
labelNames: ["client_type", "client_type", "guid", "name", "label", "location", "tab"],
1212
require: "getBuildings",
13-
extract(guid, location, element, subject) {
13+
extract(client_type, guid, location, element, subject) {
1414
subject.set(
1515
{
16+
client_type,
1617
guid,
1718
label: ucfirst(element.label),
1819
location,

packages/kitten-analysts/source/metrics/kg_building_value.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ export const kg_building_value = (cache: MessageCache, remote: KittensGameRemote
88
remote,
99
help: "How many of the given building have been built.",
1010
name: "kg_building_value",
11-
labelNames: ["guid", "name", "label", "location", "tab"],
11+
labelNames: ["client_type", "guid", "name", "label", "location", "tab"],
1212
require: "getBuildings",
13-
extract(guid, location, element, subject) {
13+
extract(client_type, guid, location, element, subject) {
1414
subject.set(
1515
{
16+
client_type,
1617
guid,
1718
label: ucfirst(element.label),
1819
location,

packages/kitten-analysts/source/metrics/kg_buildings_constructed.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ export const kg_buildings_constructed = (cache: MessageCache, remote: KittensGam
88
remote,
99
help: "How many buildings you have constructed.",
1010
name: "kg_buildings_constructed",
11-
labelNames: ["guid", "label", "location", "type"],
11+
labelNames: ["client_type", "guid", "label", "location", "type"],
1212
require: "getStatistics",
13-
extract(guid, location, element, subject) {
13+
extract(client_type, guid, location, element, subject) {
1414
if (element.name !== "buildingsConstructed") {
1515
return;
1616
}
1717

1818
subject.set(
1919
{
20+
client_type,
2021
guid,
2122
label: ucfirst(element.label),
2223
location,

packages/kitten-analysts/source/metrics/kg_challenges_completed_total.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ export const kg_challenges_completed_total = (cache: MessageCache, remote: Kitte
88
remote,
99
help: "Amount of challenges you have completed.",
1010
name: "kg_challenges_completed_total",
11-
labelNames: ["guid", "label", "location", "type"],
11+
labelNames: ["client_type", "guid", "label", "location", "type"],
1212
require: "getStatistics",
13-
extract(guid, location, element, subject) {
13+
extract(client_type, guid, location, element, subject) {
1414
if (element.name !== "totalChallengesCompleted") {
1515
return;
1616
}
1717

1818
subject.set(
1919
{
20+
client_type,
2021
guid,
2122
label: ucfirst(element.label),
2223
location,

0 commit comments

Comments
 (0)