Skip to content

Commit

Permalink
feat: track time to publish a message (#451)
Browse files Browse the repository at this point in the history
* feat: track time to publish a message

* fix: add topic label to metric

* fix: add more buckets
  • Loading branch information
twoeths authored Jul 20, 2023
1 parent 89c82f6 commit 83b8e61
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
* For messages not from us, this class uses `forwardMessage`.
*/
async publish(topic: TopicStr, data: Uint8Array, opts?: PublishOpts): Promise<PublishResult> {
const startMs = Date.now()
const transformedData = this.dataTransform ? this.dataTransform.outboundTransform(topic, data) : data

if (this.publishConfig == null) {
Expand Down Expand Up @@ -2099,7 +2100,14 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
}
}

this.metrics?.onPublishMsg(topic, tosendCount, tosend.size, rawMsg.data != null ? rawMsg.data.length : 0)
const durationMs = Date.now() - startMs
this.metrics?.onPublishMsg(
topic,
tosendCount,
tosend.size,
rawMsg.data != null ? rawMsg.data.length : 0,
durationMs
)

// Dispatch the message to the user if we are subscribed to the topic
if (willSendToSelf) {
Expand Down
16 changes: 15 additions & 1 deletion src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,13 @@ export function getMetrics(
help: 'Total count of msg publish data.length bytes',
labelNames: ['topic']
}),
/** Total time in millisecond to publish a message */
msgPublishMs: register.histogram<{ topic: TopicLabel }>({
name: 'gossipsub_msg_publish_ms',
help: 'Total time in millisecond to publish a message',
buckets: [1, 2, 5, 10, 100, 500, 1000],
labelNames: ['topic']
}),

/** Total count of msg forwarded by topic */
msgForwardCount: register.gauge<{ topic: TopicLabel }>({
Expand Down Expand Up @@ -683,7 +690,13 @@ export function getMetrics(
this.msgForwardPeers.inc({ topic }, tosendCount)
},

onPublishMsg(topicStr: TopicStr, tosendGroupCount: ToSendGroupCount, tosendCount: number, dataLen: number): void {
onPublishMsg(
topicStr: TopicStr,
tosendGroupCount: ToSendGroupCount,
tosendCount: number,
dataLen: number,
ms: number
): void {
const topic = this.toTopic(topicStr)
this.msgPublishCount.inc({ topic }, 1)
this.msgPublishBytes.inc({ topic }, tosendCount * dataLen)
Expand All @@ -692,6 +705,7 @@ export function getMetrics(
this.msgPublishPeersByGroup.inc({ peerGroup: 'floodsub' }, tosendGroupCount.floodsub)
this.msgPublishPeersByGroup.inc({ peerGroup: 'mesh' }, tosendGroupCount.mesh)
this.msgPublishPeersByGroup.inc({ peerGroup: 'fanout' }, tosendGroupCount.fanout)
this.msgPublishMs.observe({ topic }, ms)
},

onMsgRecvPreValidation(topicStr: TopicStr): void {
Expand Down

0 comments on commit 83b8e61

Please sign in to comment.