Skip to content

Commit

Permalink
feat: track extra data in dogfooding app (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
weboko authored Sep 19, 2024
1 parent a7755a7 commit 427be33
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 32 deletions.
148 changes: 117 additions & 31 deletions examples/dogfooding/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
TelemetryPushFilter,
TelemetryType,
} from "./telemetry_client";
import { generateRandomNumber, sha256 } from "./util";
import { generateRandomNumber, sha256, buildExtraData, DEFAULT_EXTRA_DATA_STR } from "./util";

const DEFAULT_CONTENT_TOPIC = "/js-waku-examples/1/message-ratio/utf8";
const DEFAULT_PUBSUB_TOPIC = utils.contentTopicToPubsubTopic(DEFAULT_CONTENT_TOPIC);
const TELEMETRY_URL =
process.env.TELEMETRY_URL || "http://localhost:8080/waku-metrics";

Expand Down Expand Up @@ -83,25 +84,56 @@ export async function app(telemetryClient: TelemetryClient) {
console.log("light push failures: ", result.failures.length);
console.log(result.failures);

if (result.successes.length > 0) {
// Push to telemetry client
telemetryClient.push<TelemetryPushFilter>([
{
const successEvents = result
.successes
.map(async (peerId) => {
const extraData = await buildExtraData(node, peerId.toString());
return {
type: TelemetryType.LIGHT_PUSH_FILTER,
protocol: "lightPush",
timestamp: timestamp,
createdAt: timestamp,
seenTimestamp: timestamp,
peerId,
peerId: peerId.toString(),
contentTopic: DEFAULT_CONTENT_TOPIC,
pubsubTopic: utils.contentTopicToPubsubTopic(DEFAULT_CONTENT_TOPIC),
pubsubTopic: DEFAULT_PUBSUB_TOPIC,
ephemeral: false,
messageHash: reportingHash,
errorMessage: "",
extraData: "",
}
]);
extraData,
};
});

const failureEvents = result
.failures
.map(async (fail) => {
const extraData = await buildExtraData(node, fail.peerId.toString());
return {
type: TelemetryType.LIGHT_PUSH_FILTER,
protocol: "lightPush",
timestamp: timestamp,
createdAt: timestamp,
seenTimestamp: timestamp,
peerId: fail.peerId.toString(),
contentTopic: DEFAULT_CONTENT_TOPIC,
pubsubTopic: DEFAULT_PUBSUB_TOPIC,
ephemeral: false,
messageHash: reportingHash,
errorMessage: fail.error.toString(),
extraData,
};
});

const events = await Promise.all([
...successEvents,
...failureEvents,
]);

if (events.length > 0) {
telemetryClient.push<TelemetryPushFilter>(events);
}

if (result.successes.length > 0) {
// Update ui
const messageElement = document.createElement("div");
const messagesSent = document.getElementById("messagesSent");
Expand All @@ -117,24 +149,6 @@ export async function app(telemetryClient: TelemetryClient) {
// Increment sequence
sequenceIndex++;
}
if (result.failures.length > 0) {
telemetryClient.push<TelemetryPushFilter>(
result.failures.map((failure) => ({
type: TelemetryType.LIGHT_PUSH_FILTER,
protocol: "lightPush",
timestamp: timestamp,
createdAt: timestamp,
seenTimestamp: timestamp,
peerId,
contentTopic: DEFAULT_CONTENT_TOPIC,
pubsubTopic: utils.contentTopicToPubsubTopic(DEFAULT_CONTENT_TOPIC),
ephemeral: false,
messageHash: reportingHash,
errorMessage: failure.error.toString(),
extraData: "",
}))
);
}
if (sequenceIndex < sequenceTotal) {
setTimeout(sendMessage, period); // Schedule the next send
} else {
Expand All @@ -152,7 +166,7 @@ export async function app(telemetryClient: TelemetryClient) {
const decoder = createDecoder(DEFAULT_CONTENT_TOPIC);

const messagesReceived = document.getElementById("messagesReceived");
const subscriptionCallback = (message: DecodedMessage) => {
const subscriptionCallback = async (message: DecodedMessage) => {
const decodedMessage: any = ProtoSequencedMessage.decode(
message.payload
);
Expand All @@ -162,6 +176,7 @@ export async function app(telemetryClient: TelemetryClient) {
return;
}

const extraData = await buildExtraData(node, peerId);
const timestamp = Math.floor(new Date().getTime() / 1000);
telemetryClient.push<TelemetryPushFilter>([
{
Expand All @@ -176,7 +191,7 @@ export async function app(telemetryClient: TelemetryClient) {
ephemeral: message.ephemeral,
messageHash: decodedMessage.hash,
errorMessage: "",
extraData: "",
extraData,
},
]);

Expand All @@ -188,7 +203,78 @@ export async function app(telemetryClient: TelemetryClient) {
document.dispatchEvent(messageReceivedEvent);
};

await node.filter.subscribe(decoder, subscriptionCallback);
const result = await node.filter.subscribe(decoder, subscriptionCallback);

let errorEvent = [];
if (result.error) {
const timestamp = Math.floor(new Date().getTime() / 1000);
errorEvent.push({
type: TelemetryType.LIGHT_PUSH_FILTER,
protocol: "filterCreateSubscription",
timestamp,
createdAt: timestamp,
seenTimestamp: timestamp,
peerId: peerId,
contentTopic: DEFAULT_CONTENT_TOPIC,
pubsubTopic: DEFAULT_PUBSUB_TOPIC,
ephemeral: false,
messageHash: "",
errorMessage: result.error,
extraData: DEFAULT_EXTRA_DATA_STR,
});
}

const failEvents = result.results.failures.map(async (fail) => {
const extraData = await buildExtraData(node, fail.peerId.toString());
const timestamp = Math.floor(new Date().getTime() / 1000);
return {
type: TelemetryType.LIGHT_PUSH_FILTER,
protocol: "filterCreateSubscription",
timestamp,
createdAt: timestamp,
seenTimestamp: timestamp,
peerId: fail.peerId.toString(),
contentTopic: DEFAULT_CONTENT_TOPIC,
pubsubTopic: DEFAULT_PUBSUB_TOPIC,
ephemeral: false,
messageHash: "",
errorMessage: fail.error,
extraData,
};
});

const successEvents = result.results.successes.map(async (peerId) => {
const extraData = await buildExtraData(node, peerId.toString());
const timestamp = Math.floor(new Date().getTime() / 1000);
return {
type: TelemetryType.LIGHT_PUSH_FILTER,
protocol: "filterCreateSubscription",
timestamp,
createdAt: timestamp,
seenTimestamp: timestamp,
peerId: peerId.toString(),
contentTopic: DEFAULT_CONTENT_TOPIC,
pubsubTopic: DEFAULT_PUBSUB_TOPIC,
ephemeral: false,
messageHash: "",
errorMessage: "",
extraData,
};
});

const resolvedEvents = await Promise.all([
...failEvents,
...successEvents,
]);

const events = [
...errorEvent,
...resolvedEvents,
];

if (events.length > 0) {
telemetryClient.push<TelemetryPushFilter>(events);
}
};

return {
Expand Down
23 changes: 22 additions & 1 deletion examples/dogfooding/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { peerIdFromString } from "@libp2p/peer-id";
import type { Waku } from "@waku/interfaces";

export const generateRandomNumber = (): number => {
return Math.floor(Math.random() * 1000000);
};
Expand All @@ -9,4 +12,22 @@ export const sha256 = async (number: number | string ): Promise<string> => {
return Array.from(new Uint8Array(buffer))
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
};
};

const DEFAULT_EXTRA_DATA = { sdk: "0.0.28" };
export const DEFAULT_EXTRA_DATA_STR = JSON.stringify(DEFAULT_EXTRA_DATA);

export const buildExtraData = async (node: Waku, peerId: string): Promise<string> => {
const peer = await node.libp2p.peerStore.get(peerIdFromString(peerId));
const hasWebsockes = peer
.addresses
.map(addr => addr.multiaddr.toString())
.some(addr => addr.includes("ws") || addr.includes("wss"));

return JSON.stringify({
...DEFAULT_EXTRA_DATA,
peerId,
hasWebsockes,
enabledProtocols: peer.protocols,
});
};

0 comments on commit 427be33

Please sign in to comment.