Skip to content

Commit

Permalink
feat: init add tracer in callFn and computeACL, computeDRP
Browse files Browse the repository at this point in the history
  • Loading branch information
anhnd350309 committed Feb 18, 2025
1 parent 5e08e18 commit 51f688c
Show file tree
Hide file tree
Showing 12 changed files with 510 additions and 111 deletions.
39 changes: 20 additions & 19 deletions examples/grid/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
{
"name": "ts-drp-example-grid",
"version": "0.7.0",
"license": "MIT",
"scripts": {
"build": "vite build",
"clean": "rm -rf dist/ node_modules/",
"dev": "vite serve",
"start": "vite preview --host --port 5173"
},
"dependencies": {
"@ts-drp/node": "0.7.0",
"@ts-drp/object": "0.7.0"
},
"devDependencies": {
"@types/node": "^22.5.4",
"typescript": "^5.5.4",
"vite": "^6.0.9",
"vite-plugin-node-polyfills": "^0.22.0"
}
"name": "ts-drp-example-grid",
"version": "0.7.0",
"license": "MIT",
"scripts": {
"build": "vite build",
"clean": "rm -rf dist/ node_modules/",
"dev": "vite serve",
"start": "vite preview --host --port 5173"
},
"dependencies": {
"@ts-drp/node": "0.7.0",
"@ts-drp/object": "0.7.0",
"@ts-drp/tracer": "workspace:^"
},
"devDependencies": {
"@types/node": "^22.5.4",
"typescript": "^5.5.4",
"vite": "^6.0.9",
"vite-plugin-node-polyfills": "^0.22.0"
}
}
14 changes: 13 additions & 1 deletion examples/grid/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DRPNode } from "@ts-drp/node";
import { enableTracing, OpentelemetryMetrics } from "@ts-drp/tracer";

import { Grid } from "./objects/grid";
import { render, enableUIControls, renderInfo } from "./render";
Expand Down Expand Up @@ -77,10 +78,14 @@ async function createConnectHandlers() {
async function run() {
enableUIControls();
renderInfo();
const metrics = new OpentelemetryMetrics("grid-service-2");

const button_create = <HTMLButtonElement>document.getElementById("createGrid");
button_create.addEventListener("click", async () => {
gridState.drpObject = await gridState.node.createObject({ drp: new Grid() });
gridState.drpObject = await gridState.node.createObject({
drp: new Grid(),
metrics,
});
gridState.gridDRP = gridState.drpObject.drp as Grid;
await createConnectHandlers();
await addUser();
Expand All @@ -94,6 +99,7 @@ async function run() {
gridState.drpObject = await gridState.node.connectObject({
id: drpId,
drp: new Grid(),
metrics,
});
gridState.gridDRP = gridState.drpObject.drp as Grid;
await createConnectHandlers();
Expand Down Expand Up @@ -127,6 +133,12 @@ async function run() {
}

async function main() {
enableTracing({
provider: {
serviceName: "grid-service-tracer",
},
});

const networkConfig = getNetworkConfigFromEnv();
gridState.node = new DRPNode(networkConfig ? { network_config: networkConfig } : undefined);
await gridState.node.start();
Expand Down
4 changes: 4 additions & 0 deletions packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@
"@libp2p/dcutr": "^2.0.6",
"@libp2p/devtools-metrics": "^1.1.5",
"@libp2p/identify": "^3.0.6",
"@libp2p/opentelemetry-metrics": "^1.0.0",
"@libp2p/ping": "2.0.11",
"@libp2p/pubsub-peer-discovery": "^11.0.0",
"@libp2p/webrtc": "^5.0.9",
"@libp2p/websockets": "^9.1.1",
"@libp2p/webtransport": "^5.0.9",
"@multiformats/multiaddr": "^12.3.1",
"@multiformats/multiaddr-matcher": "^1.6.0",
"@opentelemetry/exporter-prometheus": "^0.57.2",
"@opentelemetry/exporter-trace-otlp-http": "^0.57.1",
"@opentelemetry/sdk-node": "^0.57.2",
"@ts-drp/logger": "^0.7.0",
"it-length-prefixed": "^10.0.0",
"it-map": "^3.1.1",
Expand Down
11 changes: 8 additions & 3 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
"devDependencies": {
"@bufbuild/protobuf": "^2.0.0",
"@types/node": "^22.5.4",
"race-event": "^1.3.0",
"tsx": "4.19.1",
"typescript": "^5.5.4",
"race-event": "^1.3.0",
"vitest": "^3.0.5"
},
"dependencies": {
Expand All @@ -46,12 +46,17 @@
"@grpc/reflection": "^1.0.4",
"@libp2p/crypto": "^5.0.5",
"@libp2p/interface": "^2.1.3",
"dotenv": "^16.4.5",
"@libp2p/opentelemetry-metrics": "^1.0.0",
"@opentelemetry/exporter-prometheus": "^0.57.2",
"@opentelemetry/exporter-trace-otlp-http": "^0.57.1",
"@opentelemetry/sdk-node": "^0.57.2",
"@ts-drp/blueprints": "0.7.0",
"@ts-drp/logger": "0.7.0",
"@ts-drp/network": "0.7.0",
"@ts-drp/object": "0.7.0",
"@ts-drp/logger": "0.7.0",
"@ts-drp/tracer": "workspace:^",
"commander": "^13.0.0",
"dotenv": "^16.4.5",
"uint8arrays": "^5.1.0"
}
}
12 changes: 11 additions & 1 deletion packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { EventCallback, StreamHandler } from "@libp2p/interface";
import { Logger, type LoggerOptions } from "@ts-drp/logger";
import { DRPNetworkNode, type DRPNetworkNodeConfig, NetworkPb } from "@ts-drp/network";
import { type ACL, type DRP, DRPObject } from "@ts-drp/object";
import { IMetrics } from "@ts-drp/tracer";

import { drpMessagesHandler } from "./handlers.js";
import * as operations from "./operations.js";
Expand Down Expand Up @@ -88,13 +89,15 @@ export class DRPNode {
enabled: boolean;
peerId?: string;
};
metrics?: IMetrics;
}) {
const object = new DRPObject({
peerId: this.networkNode.peerId,
publicCredential: options.acl ? undefined : this.credentialStore.getPublicCredential(),
acl: options.acl,
drp: options.drp,
id: options.id,
metrics: options.metrics,
});
operations.createObject(this, object);
await operations.subscribeObject(this, object.id);
Expand All @@ -117,8 +120,15 @@ export class DRPNode {
sync?: {
peerId?: string;
};
metrics?: IMetrics;
}) {
const object = operations.connectObject(this, options.id, options.drp, options.sync?.peerId);
const object = operations.connectObject(
this,
options.id,
options.drp,
options.sync?.peerId,
options.metrics
);
return object;
}

Expand Down
22 changes: 15 additions & 7 deletions packages/node/src/operations.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { NetworkPb } from "@ts-drp/network";
import { type DRP, DRPObject, HashGraph } from "@ts-drp/object";
import { IMetrics } from "@ts-drp/tracer";

import { drpMessagesHandler, drpObjectChangesHandler } from "./handlers.js";
import { type DRPNode, log } from "./index.js";

export function createObject(node: DRPNode, object: DRPObject) {
console.log("Creating object", object.id);
node.objectStore.put(object.id, object);
object.subscribe((obj, originFn, vertices) =>
drpObjectChangesHandler(node, obj, originFn, vertices)
);
object.subscribe((obj, originFn, vertices) => {
console.log("Perform", Date.now());
drpObjectChangesHandler(node, obj, originFn, vertices);
});
}

export async function connectObject(
node: DRPNode,
id: string,
drp?: DRP,
peerId?: string
peerId?: string,
metrics?: IMetrics
): Promise<DRPObject> {
const object = DRPObject.createObject({
peerId: node.networkNode.peerId,
id,
drp,
metrics,
});
node.objectStore.put(id, object);

Expand All @@ -30,9 +35,9 @@ export async function connectObject(
if (object.acl) {
await syncObject(node, id, peerId);
await subscribeObject(node, id);
object.subscribe((obj, originFn, vertices) =>
drpObjectChangesHandler(node, obj, originFn, vertices)
);
object.subscribe((obj, originFn, vertices) => {
drpObjectChangesHandler(node, obj, originFn, vertices);
});
clearInterval(retry);
}
}, 1000);
Expand All @@ -41,6 +46,7 @@ export async function connectObject(

/* data: { id: string } */
export async function subscribeObject(node: DRPNode, objectId: string) {
console.log("Subscribing to object", objectId);
node.networkNode.subscribe(objectId);
node.networkNode.addGroupMessageHandler(
objectId,
Expand All @@ -54,6 +60,7 @@ export function unsubscribeObject(node: DRPNode, objectId: string, purge?: boole
}

export async function fetchState(node: DRPNode, objectId: string, peerId?: string) {
console.log("fetching state", objectId);
const data = NetworkPb.FetchState.create({
objectId,
vertexHash: HashGraph.rootHash,
Expand All @@ -75,6 +82,7 @@ export async function fetchState(node: DRPNode, objectId: string, peerId?: strin
data: { vertex_hashes: string[] }
*/
export async function syncObject(node: DRPNode, objectId: string, peerId?: string) {
console.log("Syncing object", objectId);
const object: DRPObject | undefined = node.objectStore.get(objectId);
if (!object) {
log.error("::syncObject: Object not found");
Expand Down
1 change: 1 addition & 0 deletions packages/object/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@chainsafe/bls": "^8.1.0",
"@msgpack/msgpack": "^3.0.1",
"@ts-drp/logger": "^0.7.0",
"@ts-drp/tracer": "workspace:^",
"es-toolkit": "1.30.1",
"fast-deep-equal": "^3.1.3",
"fast-equals": "^5.2.2",
Expand Down
13 changes: 12 additions & 1 deletion packages/object/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Logger, type LoggerOptions } from "@ts-drp/logger";
import { IMetrics } from "@ts-drp/tracer";
import { cloneDeep } from "es-toolkit";
import { deepEqual } from "fast-equals";
import * as crypto from "node:crypto";
Expand Down Expand Up @@ -55,6 +56,7 @@ export class DRPObject implements ObjectPb.DRPObjectBase {
drp?: DRP;
id?: string;
config?: DRPObjectConfig;
metrics?: IMetrics;
}) {
if (!options.acl && !options.publicCredential) {
throw new Error("Either publicCredential or acl must be provided to create a DRPObject");
Expand Down Expand Up @@ -90,6 +92,14 @@ export class DRPObject implements ObjectPb.DRPObjectBase {
this.finalityStore = new FinalityStore(options.config?.finality_config);
this.originalObjectACL = cloneDeep(objAcl);
this.originalDRP = cloneDeep(options.drp);
this.callFn =
options.metrics?.traceFunc("drpObject.callFn", this.callFn.bind(this)) ?? this.callFn;
this._computeObjectACL =
options.metrics?.traceFunc("drpObject.computeObjectACL", this._computeObjectACL.bind(this)) ??
this._computeObjectACL;
this._computeDRP =
options.metrics?.traceFunc("drpObject.computeDRP", this._computeDRP.bind(this)) ??
this._computeDRP;
}

private _initLocalDrpInstance(drp: DRP, acl: DRP) {
Expand All @@ -108,7 +118,7 @@ export class DRPObject implements ObjectPb.DRPObjectBase {
this.vertices = this.hashGraph.getAllVertices();
}

static createObject(options: { peerId: string; id?: string; drp?: DRP }) {
static createObject(options: { peerId: string; id?: string; drp?: DRP; metrics?: IMetrics }) {
const aclObj = new ObjectACL({
admins: new Map(),
permissionless: true,
Expand All @@ -118,6 +128,7 @@ export class DRPObject implements ObjectPb.DRPObjectBase {
id: options.id,
acl: aclObj,
drp: options.drp,
metrics: options.metrics,
});
return object;
}
Expand Down
Loading

0 comments on commit 51f688c

Please sign in to comment.