Skip to content

Commit 816d57d

Browse files
authored
Merge pull request #750 from atlassian/alyssat2/emit-latency-metric
Emit latency metric for forwarder requests
2 parents 29bd4f6 + c0dcf6d commit 816d57d

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

METRICS.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Metric types:
1111
| gauge (cumulative) | An internal counter sent as a gauge with the value never resetting
1212
| gauge (sparse) | The same as a cumulative gauge, but data is only sent on change
1313
| counter | An internal counter, reset on flush
14-
| reported | The type will be emitted as a guage for services in stand alone mode, otherwise as counters for forwarders.
14+
| reported | The type will be emitted as a gauge for services in stand alone mode, otherwise as counters for forwarders.
1515

1616

1717
Metrics:
@@ -60,6 +60,8 @@ Metrics:
6060
| http.forwarder.sent | counter | | The number of batches successfully forwarded
6161
| http.forwarder.retried | counter | | The number of retries sending a batch
6262
| http.forwarder.dropped | counter | | The number of batches dropped due to inability to forward upstream
63+
| http.forwarder.post_latency.sum | counter | | The total of the time taken to forward a batch in the flush interval
64+
| http.forwarder.post_latency.max | gauge (flush) | | The maximum time taken to forward a batch in the flush interval
6365
| http.incoming | counter | server-name, result, failure | The number of batches forwarded to the server, and the results of processing them
6466
| http.incoming.metrics | counter | server-name | The number of metrics received over http
6567

pkg/statsd/handler_http_forwarder_v2.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,15 @@ const (
4444

4545
// HttpForwarderHandlerV2 is a PipelineHandler which sends metrics to another gostatsd instance
4646
type HttpForwarderHandlerV2 struct {
47-
postId uint64 // atomic - used for an id in logs
48-
messagesInvalid uint64 // atomic - messages which failed to be created
49-
messagesCreated uint64 // atomic - messages which were created
50-
messagesSent uint64 // atomic - messages successfully sent
51-
messagesRetried uint64 // atomic - retries (first send is not a retry, final failure is not a retry)
52-
messagesDropped uint64 // atomic - final failure
47+
postId uint64 // atomic - used for an id in logs
48+
messagesInvalid uint64 // atomic - messages which failed to be created
49+
messagesCreated uint64 // atomic - messages which were created
50+
messagesSent uint64 // atomic - messages successfully sent
51+
messagesRetried uint64 // atomic - retries (first send is not a retry, final failure is not a retry)
52+
messagesDropped uint64 // atomic - final failure
53+
postLatencyTotal atomic.Int64 // total of the time taken to send messages in a flush interval
54+
postLatencyMax atomic.Int64 // maximum time taken to send a message in a flush interval
55+
5356
lastSuccessfulSend atomic.Int64
5457

5558
logger logrus.FieldLogger
@@ -299,6 +302,11 @@ func (hfh *HttpForwarderHandlerV2) emitMetrics(statser stats.Statser) {
299302
statser.Report("http.forwarder.sent", &hfh.messagesSent, nil)
300303
statser.Report("http.forwarder.retried", &hfh.messagesRetried, nil)
301304
statser.Report("http.forwarder.dropped", &hfh.messagesDropped, nil)
305+
306+
postLatencyMax := hfh.postLatencyMax.Swap(0)
307+
postLatencyTotal := hfh.postLatencyTotal.Swap(0)
308+
statser.Gauge("http.forwarder.post_latency.max", float64(postLatencyMax), nil)
309+
statser.Count("http.forwarder.post_latency.sum", float64(postLatencyTotal), nil)
302310
}
303311

304312
// sendNop sends an empty metric map downstream. It's used to "prime the pump" for the deepcheck.
@@ -465,9 +473,18 @@ func (hfh *HttpForwarderHandlerV2) post(ctx context.Context, message proto.Messa
465473
b.MaxElapsedTime = hfh.maxRequestElapsedTime
466474

467475
for {
476+
startTime := clock.Now(ctx)
468477
if err = post(); err == nil {
469478
atomic.AddUint64(&hfh.messagesSent, 1)
470479
hfh.lastSuccessfulSend.Store(clock.Now(ctx).UnixNano())
480+
481+
postLatency := clock.Since(ctx, startTime).Milliseconds()
482+
hfh.postLatencyTotal.Add(postLatency)
483+
for old := hfh.postLatencyMax.Load(); old < postLatency; old = hfh.postLatencyMax.Load() {
484+
if hfh.postLatencyMax.CompareAndSwap(old, postLatency) {
485+
break
486+
}
487+
}
471488
return
472489
}
473490

0 commit comments

Comments
 (0)