Skip to content

Commit

Permalink
feat: Identify client type in metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
oliversalzburg committed Aug 28, 2024
1 parent a181e2a commit 34cd602
Show file tree
Hide file tree
Showing 24 changed files with 122 additions and 66 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

44 changes: 28 additions & 16 deletions packages/kitten-analysts/source/KittenAnalysts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export interface KittenAnalystsMessage<
*/
data?: TData;

client_type: "backend" | "browser" | "headless";

/**
* The HTTP URL that identifies the context of the client that sent the message.
*/
Expand Down Expand Up @@ -181,7 +183,12 @@ export class KittenAnalysts {

this.ws.onopen = () => {
cinfo("WS connection established.");
this.postMessage({ type: "connected", location: this.location, guid: game.telemetry.guid });
this.postMessage({
type: "connected",
client_type: this.location.includes("headless.html") ? "headless" : "browser",
location: this.location,
guid: game.telemetry.guid,
});
};

this.ws.onmessage = event => {
Expand Down Expand Up @@ -246,11 +253,12 @@ export class KittenAnalysts {
);

return {
type: message.type,
location: this.location,
client_type: this.location.includes("headless.html") ? "headless" : "browser",
data: [...bonfire, ...space, ...religion],
guid: game.telemetry.guid,
location: this.location,
responseId: message.responseId,
data: [...bonfire, ...space, ...religion],
type: message.type,
};

break;
Expand All @@ -265,11 +273,12 @@ export class KittenAnalysts {
}));

return {
type: message.type,
location: this.location,
client_type: this.location.includes("headless.html") ? "headless" : "browser",
data,
guid: game.telemetry.guid,
location: this.location,
responseId: message.responseId,
data,
type: message.type,
};

break;
Expand All @@ -285,11 +294,12 @@ export class KittenAnalysts {
);

return {
type: message.type,
location: this.location,
client_type: this.location.includes("headless.html") ? "headless" : "browser",
data,
guid: game.telemetry.guid,
location: this.location,
responseId: message.responseId,
data,
type: message.type,
};

break;
Expand All @@ -308,20 +318,22 @@ export class KittenAnalysts {
reportFrameListener = (event: Event): void => {
const location = window.location.toString().replace(/#$/, "");
this.postMessage({
type: "reportFrame",
location,
guid: game.telemetry.guid,
client_type: location.includes("headless.html") ? "headless" : "browser",
data: (event as CustomEvent<unknown>).detail,
guid: game.telemetry.guid,
location,
type: "reportFrame",
});
};

reportSavegameListener = (event: Event): void => {
const location = window.location.toString().replace(/#$/, "");
this.postMessage({
type: "reportSavegame",
location,
guid: game.telemetry.guid,
client_type: location.includes("headless.html") ? "headless" : "browser",
data: (event as CustomEvent<unknown>).detail,
guid: game.telemetry.guid,
location,
type: "reportSavegame",
});
};

Expand Down
32 changes: 23 additions & 9 deletions packages/kitten-analysts/source/entrypoint-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const ks_iterate_duration = new Histogram({
name: "ks_iterate_duration",
help: "How long each iteration of KS took.",
buckets: [...linearBuckets(0, 1, 100), ...exponentialBuckets(100, 1.125, 30)],
labelNames: ["guid", "location", "manager"],
labelNames: ["client_type", "guid", "location", "manager"],
});

// KGNet Savegame Storage
Expand All @@ -68,8 +68,10 @@ interface KGNetSaveFromGame {
}
interface KGNetSaveUpdate {
guid: string;
"metadata[archived]"?: string;
"metadata[label]"?: string;
metadata?: {
archived: string;
label: string;
};
}
interface KGNetSaveFromAnalysts {
telemetry: {
Expand Down Expand Up @@ -187,7 +189,12 @@ export class KittensGameRemote {
console.info(`=> Received frame report (${message.location}).`, delta);

ks_iterate_duration.observe(
{ guid: message.guid, location: message.location, manager: "all" },
{
client_type: message.location.includes("headless.html") ? "headless" : "browser",
guid: message.guid,
location: message.location,
manager: "all",
},
delta,
);
for (const [measurement, timeTaken] of Object.entries(payload.measurements)) {
Expand All @@ -196,7 +203,12 @@ export class KittensGameRemote {
}

ks_iterate_duration.observe(
{ guid: message.guid, location: message.location, manager: measurement },
{
client_type: message.location.includes("headless.html") ? "headless" : "browser",
guid: message.guid,
location: message.location,
manager: measurement,
},
timeTaken,
);
}
Expand Down Expand Up @@ -256,12 +268,13 @@ export class KittensGameRemote {
}

sendMessage<TMessage extends KittenAnalystsMessageId>(
message: Omit<KittenAnalystsMessage<TMessage>, "location" | "guid">,
message: Omit<KittenAnalystsMessage<TMessage>, "client_type" | "location" | "guid">,
): Promise<Array<KittenAnalystsMessage<TMessage> | null>> {
const clientRequests = [...this.sockets.values()].map(socket =>
this.#sendMessageToSocket(
{
...message,
client_type: "backend",
guid: "ka-backend",
location: this.location,
},
Expand Down Expand Up @@ -466,6 +479,7 @@ routerNetwork.post("/kgnet/save/upload", context => {
.toHeadless({
type: "injectSavegame",
data: savegame,
client_type: "backend",
location: `ws://${(remote.wss.address() as AddressInfo | null)?.address ?? "localhost"}:9093/`,
guid: "ka-backend",
})
Expand All @@ -487,12 +501,12 @@ routerNetwork.post("/kgnet/save/update", context => {
const gameGUID = gameSave.guid;
const existingSave = saveStore.get(gameGUID);
if (isNil(existingSave)) {
console.warn(`Couldn't find existing savegame with ID '${gameGUID}'!`);
console.warn(`=> Couldn't find existing savegame with ID '${gameGUID}'! Update is ignored.`);
return;
}

existingSave.archived = gameSave["metadata[archived]"] === "true";
existingSave.label = gameSave["metadata[label]"] ?? existingSave.label;
existingSave.archived = gameSave.metadata?.archived === "true";
existingSave.label = gameSave.metadata?.label ?? existingSave.label;
writeFileSync(`${LOCAL_STORAGE_PATH}/${gameGUID}.json`, JSON.stringify(existingSave));
saveStore.set(gameGUID, existingSave);
console.debug(`=> Savegame persisted to disc.`);
Expand Down
16 changes: 14 additions & 2 deletions packages/kitten-analysts/source/metrics/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ export const gaugeFactory = <
name: string;
labelNames: Array<string>;
require: TMessage;
extract: (guid: string, location: string, element: TData[number], subject: Gauge) => void;
extract: (
client_type: "backend" | "browser" | "headless",
guid: string,
location: string,
element: TData[number],
subject: Gauge,
) => void;
}) =>
new Gauge({
help: instructions.help,
Expand All @@ -46,7 +52,13 @@ export const gaugeFactory = <
continue;
}
for (const entity of mustExist(clientResponse.data) as TData) {
instructions.extract(clientResponse.guid, clientResponse.location, entity, this);
instructions.extract(
clientResponse.client_type,
clientResponse.guid,
clientResponse.location,
entity,
this,
);
}
}
},
Expand Down
5 changes: 3 additions & 2 deletions packages/kitten-analysts/source/metrics/kg_building_on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ export const kg_building_on = (cache: MessageCache, remote: KittensGameRemote) =
remote,
help: "How many of the given building are turned on.",
name: "kg_building_on",
labelNames: ["guid", "name", "label", "location", "tab"],
labelNames: ["client_type", "client_type", "guid", "name", "label", "location", "tab"],
require: "getBuildings",
extract(guid, location, element, subject) {
extract(client_type, guid, location, element, subject) {
subject.set(
{
client_type,
guid,
label: ucfirst(element.label),
location,
Expand Down
5 changes: 3 additions & 2 deletions packages/kitten-analysts/source/metrics/kg_building_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ export const kg_building_value = (cache: MessageCache, remote: KittensGameRemote
remote,
help: "How many of the given building have been built.",
name: "kg_building_value",
labelNames: ["guid", "name", "label", "location", "tab"],
labelNames: ["client_type", "guid", "name", "label", "location", "tab"],
require: "getBuildings",
extract(guid, location, element, subject) {
extract(client_type, guid, location, element, subject) {
subject.set(
{
client_type,
guid,
label: ucfirst(element.label),
location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ export const kg_buildings_constructed = (cache: MessageCache, remote: KittensGam
remote,
help: "How many buildings you have constructed.",
name: "kg_buildings_constructed",
labelNames: ["guid", "label", "location", "type"],
labelNames: ["client_type", "guid", "label", "location", "type"],
require: "getStatistics",
extract(guid, location, element, subject) {
extract(client_type, guid, location, element, subject) {
if (element.name !== "buildingsConstructed") {
return;
}

subject.set(
{
client_type,
guid,
label: ucfirst(element.label),
location,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ export const kg_challenges_completed_total = (cache: MessageCache, remote: Kitte
remote,
help: "Amount of challenges you have completed.",
name: "kg_challenges_completed_total",
labelNames: ["guid", "label", "location", "type"],
labelNames: ["client_type", "guid", "label", "location", "type"],
require: "getStatistics",
extract(guid, location, element, subject) {
extract(client_type, guid, location, element, subject) {
if (element.name !== "totalChallengesCompleted") {
return;
}

subject.set(
{
client_type,
guid,
label: ucfirst(element.label),
location,
Expand Down
5 changes: 3 additions & 2 deletions packages/kitten-analysts/source/metrics/kg_clicks_total.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ export const kg_clicks_total = (cache: MessageCache, remote: KittensGameRemote)
remote,
help: "How many times you have clicked.",
name: "kg_clicks_total",
labelNames: ["guid", "label", "location", "type"],
labelNames: ["client_type", "guid", "label", "location", "type"],
require: "getStatistics",
extract(guid, location, element, subject) {
extract(client_type, guid, location, element, subject) {
if (element.name !== "totalClicks") {
return;
}

subject.set(
{
client_type,
guid,
label: ucfirst(element.label),
location,
Expand Down
5 changes: 3 additions & 2 deletions packages/kitten-analysts/source/metrics/kg_crafts_total.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ export const kg_crafts_total = (cache: MessageCache, remote: KittensGameRemote)
remote,
help: "How many times you have crafted.",
name: "kg_crafts_total",
labelNames: ["guid", "label", "location", "type"],
labelNames: ["client_type", "guid", "label", "location", "type"],
require: "getStatistics",
extract(guid, location, element, subject) {
extract(client_type, guid, location, element, subject) {
if (element.name !== "totalCrafts") {
return;
}

subject.set(
{
client_type,
guid,
label: ucfirst(element.label),
location,
Expand Down
5 changes: 3 additions & 2 deletions packages/kitten-analysts/source/metrics/kg_events_observed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ export const kg_events_observed = (cache: MessageCache, remote: KittensGameRemot
remote,
help: "How many rare events you have observed.",
name: "kg_events_observed",
labelNames: ["guid", "label", "location", "type"],
labelNames: ["client_type", "guid", "label", "location", "type"],
require: "getStatistics",
extract(guid, location, element, subject) {
extract(client_type, guid, location, element, subject) {
if (element.name !== "eventsObserved") {
return;
}

subject.set(
{
client_type,
guid,
label: ucfirst(element.label),
location,
Expand Down
5 changes: 3 additions & 2 deletions packages/kitten-analysts/source/metrics/kg_kittens_average.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ export const kg_kittens_average = (cache: MessageCache, remote: KittensGameRemot
remote,
help: "How many kittens were born per century at average.",
name: "kg_kittens_average",
labelNames: ["guid", "label", "location", "type"],
labelNames: ["client_type", "guid", "label", "location", "type"],
require: "getStatistics",
extract(guid, location, element, subject) {
extract(client_type, guid, location, element, subject) {
if (element.name !== "averageKittens") {
return;
}

subject.set(
{
client_type,
guid,
label: ucfirst(element.label),
location,
Expand Down
5 changes: 3 additions & 2 deletions packages/kitten-analysts/source/metrics/kg_kittens_dead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ export const kg_kittens_dead = (cache: MessageCache, remote: KittensGameRemote)
remote,
help: "How many kittens have died :(",
name: "kg_kittens_dead",
labelNames: ["guid", "label", "location", "type"],
labelNames: ["client_type", "guid", "label", "location", "type"],
require: "getStatistics",
extract(guid, location, element, subject) {
extract(client_type, guid, location, element, subject) {
if (element.name !== "kittensDead") {
return;
}

subject.set(
{
client_type,
guid,
label: ucfirst(element.label),
location,
Expand Down
Loading

0 comments on commit 34cd602

Please sign in to comment.