feat: use new telemetry (#88)

This commit is contained in:
Sasha 2024-09-17 15:11:06 +02:00 committed by GitHub
parent 50d3cac3a8
commit a7755a7167
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 65 additions and 70 deletions

View File

@ -5,17 +5,16 @@ import {
DecodedMessage, DecodedMessage,
waitForRemotePeer, waitForRemotePeer,
LightNode, LightNode,
utils utils,
} from "@waku/sdk"; } from "@waku/sdk";
import { Type, Field } from "protobufjs"; import { Type, Field } from "protobufjs";
import { import {
TelemetryClient, TelemetryClient,
TelemetryPushError,
TelemetryPushFilter, TelemetryPushFilter,
TelemetryType, TelemetryType,
} from "./telemetry_client"; } from "./telemetry_client";
import { generateRandomNumber, hashNumber } from "./util"; import { generateRandomNumber, sha256 } from "./util";
const DEFAULT_CONTENT_TOPIC = "/js-waku-examples/1/message-ratio/utf8"; const DEFAULT_CONTENT_TOPIC = "/js-waku-examples/1/message-ratio/utf8";
const TELEMETRY_URL = const TELEMETRY_URL =
@ -55,14 +54,19 @@ export async function app(telemetryClient: TelemetryClient) {
numMessages: number, numMessages: number,
period: number = 3000 period: number = 3000
) => { ) => {
const sequenceHash = await hashNumber(generateRandomNumber()); const sequenceHash = await sha256(generateRandomNumber());
const sequenceTotal = numMessages; const sequenceTotal = numMessages;
let sequenceIndex = 0; let sequenceIndex = 0;
const sendMessage = async () => { const sendMessage = async () => {
try { try {
// TODO(weboko): replace with @waku/message-hash ideally
const reportingHash = await sha256(`${sequenceHash}-${sequenceIndex}-${sequenceTotal}`);
const timestamp = Math.floor(new Date().getTime() / 1000);
const message = ProtoSequencedMessage.create({ const message = ProtoSequencedMessage.create({
hash: sequenceHash, hash: reportingHash,
seqHash: sequenceHash,
total: sequenceTotal, total: sequenceTotal,
index: sequenceIndex, index: sequenceIndex,
sender: peerId, sender: peerId,
@ -83,16 +87,19 @@ export async function app(telemetryClient: TelemetryClient) {
// Push to telemetry client // Push to telemetry client
telemetryClient.push<TelemetryPushFilter>([ telemetryClient.push<TelemetryPushFilter>([
{ {
messageType: TelemetryType.LIGHT_PUSH_FILTER, type: TelemetryType.LIGHT_PUSH_FILTER,
timestamp: Math.floor(new Date().getTime() / 1000), protocol: "lightPush",
peerIdSender: peerId, timestamp: timestamp,
peerIdReporter: peerId, createdAt: timestamp,
sequenceHash: sequenceHash, seenTimestamp: timestamp,
sequenceTotal: sequenceTotal, peerId,
sequenceIndex: sequenceIndex,
contentTopic: DEFAULT_CONTENT_TOPIC, contentTopic: DEFAULT_CONTENT_TOPIC,
pubsubTopic: utils.contentTopicToPubsubTopic(DEFAULT_CONTENT_TOPIC), pubsubTopic: utils.contentTopicToPubsubTopic(DEFAULT_CONTENT_TOPIC),
}, ephemeral: false,
messageHash: reportingHash,
errorMessage: "",
extraData: "",
}
]); ]);
// Update ui // Update ui
@ -111,14 +118,20 @@ export async function app(telemetryClient: TelemetryClient) {
sequenceIndex++; sequenceIndex++;
} }
if (result.failures.length > 0) { if (result.failures.length > 0) {
telemetryClient.push<TelemetryPushError>( telemetryClient.push<TelemetryPushFilter>(
result.failures.map((failure) => ({ result.failures.map((failure) => ({
messageType: TelemetryType.LIGHT_PUSH_ERROR, type: TelemetryType.LIGHT_PUSH_FILTER,
timestamp: Math.floor(new Date().getTime() / 1000), protocol: "lightPush",
peerId: peerId, timestamp: timestamp,
peerIdRemote: failure.peerId?.toString(), createdAt: timestamp,
errorMessage: failure.error.toString(), seenTimestamp: timestamp,
peerId,
contentTopic: DEFAULT_CONTENT_TOPIC, contentTopic: DEFAULT_CONTENT_TOPIC,
pubsubTopic: utils.contentTopicToPubsubTopic(DEFAULT_CONTENT_TOPIC),
ephemeral: false,
messageHash: reportingHash,
errorMessage: failure.error.toString(),
extraData: "",
})) }))
); );
} }
@ -140,30 +153,35 @@ export async function app(telemetryClient: TelemetryClient) {
const messagesReceived = document.getElementById("messagesReceived"); const messagesReceived = document.getElementById("messagesReceived");
const subscriptionCallback = (message: DecodedMessage) => { const subscriptionCallback = (message: DecodedMessage) => {
const sequencedMessage: any = ProtoSequencedMessage.decode( const decodedMessage: any = ProtoSequencedMessage.decode(
message.payload message.payload
); );
// Don't bother reporting messages sent by this same node // Don't bother reporting messages sent by this same node
if (sequencedMessage.sender === peerId) { if (decodedMessage.sender === peerId) {
return; return;
} }
const timestamp = Math.floor(new Date().getTime() / 1000);
telemetryClient.push<TelemetryPushFilter>([ telemetryClient.push<TelemetryPushFilter>([
{ {
messageType: TelemetryType.LIGHT_PUSH_FILTER, type: TelemetryType.LIGHT_PUSH_FILTER,
timestamp: Math.floor(new Date().getTime() / 1000), protocol: "filter",
peerIdSender: sequencedMessage.sender, timestamp,
peerIdReporter: peerId, createdAt: Math.floor(message.timestamp.getTime() / 1000),
sequenceHash: sequencedMessage.hash, seenTimestamp: timestamp,
sequenceTotal: sequencedMessage.total, peerId: decodedMessage.sender,
sequenceIndex: sequencedMessage.index, contentTopic: message.contentTopic,
contentTopic: DEFAULT_CONTENT_TOPIC, pubsubTopic: message.pubsubTopic,
pubsubTopic: utils.contentTopicToPubsubTopic(DEFAULT_CONTENT_TOPIC), ephemeral: message.ephemeral,
messageHash: decodedMessage.hash,
errorMessage: "",
extraData: "",
}, },
]); ]);
const messageElement = document.createElement("div"); const messageElement = document.createElement("div");
messageElement.textContent = `Message: ${sequencedMessage.hash} ${sequencedMessage.index} of ${sequencedMessage.total}`; messageElement.textContent = `Message: ${decodedMessage.hash} ${decodedMessage.index} of ${decodedMessage.total}`;
messagesReceived.appendChild(messageElement); messagesReceived.appendChild(messageElement);
messagesReceived.appendChild(document.createElement("br")); messagesReceived.appendChild(document.createElement("br"));

View File

@ -1,50 +1,27 @@
export enum TelemetryType { export enum TelemetryType {
LIGHT_PUSH_FILTER = "LightPushFilter", LIGHT_PUSH_FILTER = "LightPushFilter",
LIGHT_PUSH_ERROR = "LightPushError",
GENERIC = "Generic"
} }
// Top level structure of a telemetry request interface TelemetryMessage {
export interface TelemetryRequest { type: string;
id: number;
telemetryType: TelemetryType;
telemetryData: any; // Using 'any' to represent the raw JSON data
}
// Common to all telemetry messages
export interface TelemetryMessage {
timestamp: number; timestamp: number;
messageType: TelemetryType; contentTopic: string;
pubsubTopic: string;
peerId: string;
errorMessage: string;
extraData: string;
} }
export interface TelemetryPushFilter extends TelemetryMessage { export interface TelemetryPushFilter extends TelemetryMessage {
peerIdSender: string; type: "LightPushFilter",
peerIdReporter: string; protocol: string;
sequenceHash: string; ephemeral: boolean;
sequenceTotal: number; seenTimestamp: number;
sequenceIndex: number; createdAt: number;
contentTopic: string; messageHash: string;
pubsubTopic: string;
} }
export interface TelemetryPushError extends TelemetryMessage {
peerId: string;
errorMessage: string;
peerIdRemote?: string;
contentTopic?: string;
pubsubTopic?: string;
}
export interface TelemetryGeneric extends TelemetryMessage {
peerId: string;
metricType: string;
contentTopic?: string;
pubsubTopic?: string;
genericData?: string;
errorMessage?: string;
}
export class TelemetryClient { export class TelemetryClient {
constructor( constructor(
private readonly url: string, private readonly url: string,
@ -82,7 +59,7 @@ export class TelemetryClient {
private async send<T extends TelemetryMessage>(messages: T[]) { private async send<T extends TelemetryMessage>(messages: T[]) {
const telemetryRequests = messages.map((message) => ({ const telemetryRequests = messages.map((message) => ({
id: ++this.requestId, id: ++this.requestId,
telemetryType: message.messageType.toString(), telemetryType: message.type.toString(),
telemetryData: message telemetryData: message
})); }));

View File

@ -2,7 +2,7 @@ export const generateRandomNumber = (): number => {
return Math.floor(Math.random() * 1000000); return Math.floor(Math.random() * 1000000);
}; };
export const hashNumber = async (number: number): Promise<string> => { export const sha256 = async (number: number | string ): Promise<string> => {
const encoder = new TextEncoder(); const encoder = new TextEncoder();
const data = encoder.encode(number.toString()); const data = encoder.encode(number.toString());
const buffer = await crypto.subtle.digest("SHA-256", data); const buffer = await crypto.subtle.digest("SHA-256", data);