Skip to content

Commit

Permalink
add centrifuge_client_ping_pong_duration_seconds histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Jul 12, 2024
1 parent 1f009b9 commit 7e8ecd5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func getPingData(uni bool, protoType ProtocolType) []byte {

func (c *Client) sendPing() {
c.mu.Lock()
c.lastPing = time.Now().Unix()
c.lastPing = time.Now().UnixNano()
c.mu.Unlock()
unidirectional := c.transport.Unidirectional()
_ = c.transportEnqueue(getPingData(unidirectional, c.transport.Protocol()), "", protocol.FrameTypeServerPing)
Expand All @@ -518,6 +518,7 @@ func (c *Client) checkPong() {
}
lastSeen := c.lastSeen
c.mu.RUnlock()
c.node.metrics.observePingPongDuration(time.Duration(lastSeen-lastPing) * time.Nanosecond)
if lastSeen < lastPing {
go func() { c.Disconnect(DisconnectNoPong) }()
return
Expand Down Expand Up @@ -1180,7 +1181,7 @@ func (c *Client) dispatchCommand(cmd *protocol.Command, cmdSize int) (*Disconnec
// upon receiving pong we change a sign of lastPing value. This way we can handle
// unnecessary pongs sent by the client and still use lastPing value in Client.checkPong.
c.lastPing = -c.lastPing
c.lastSeen = time.Now().Unix()
c.lastSeen = time.Now().UnixNano()
c.mu.Unlock()
return nil, true
}
Expand Down
19 changes: 19 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type metrics struct {

pubSubLagHistogram prometheus.Histogram
broadcastDurationHistogram prometheus.Histogram
pingPongDurationHistogram prometheus.Histogram
}

func (m *metrics) observeCommandDuration(frameType protocol.FrameType, d time.Duration) {
Expand Down Expand Up @@ -131,6 +132,10 @@ func (m *metrics) observeBroadcastDuration(started time.Time) {
m.broadcastDurationHistogram.Observe(time.Since(started).Seconds())
}

func (m *metrics) observePingPongDuration(duration time.Duration) {
m.pingPongDurationHistogram.Observe(duration.Seconds())
}

func (m *metrics) setBuildInfo(version string) {
m.buildInfoGauge.WithLabelValues(version).Set(1)
}
Expand Down Expand Up @@ -438,6 +443,17 @@ func initMetricsRegistry(registry prometheus.Registerer, metricsNamespace string
Help: "Count of recover operations.",
}, []string{"recovered"})

m.pingPongDurationHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Namespace: metricsNamespace,
Subsystem: "client",
Name: "ping_pong_duration_seconds",
Help: "Ping/Pong duration in seconds",
Buckets: []float64{
0.000100, 0.000250, 0.000500, // Microsecond resolution.
0.001, 0.005, 0.010, 0.025, 0.050, 0.100, 0.250, 0.500, // Millisecond resolution.
1.0, 2.5, 5.0, 10.0, // Second resolution.
}})

m.transportConnectCount = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: metricsNamespace,
Subsystem: "transport",
Expand Down Expand Up @@ -582,6 +598,9 @@ func initMetricsRegistry(registry prometheus.Registerer, metricsNamespace string
if err := registry.Register(m.recoverCount); err != nil && !errors.As(err, &alreadyRegistered) {
return nil, err
}
if err := registry.Register(m.pingPongDurationHistogram); err != nil && !errors.As(err, &alreadyRegistered) {
return nil, err
}
if err := registry.Register(m.transportConnectCount); err != nil && !errors.As(err, &alreadyRegistered) {
return nil, err
}
Expand Down

0 comments on commit 7e8ecd5

Please sign in to comment.