Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
refactor: lower cost of metrics and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Feb 20, 2023
1 parent d2d30ed commit 49f5801
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,7 @@ func (p *pool) fetchWith(ctx context.Context, c cid.Cid, with string) (blk block
}

for i := 0; i < len(nodes); i++ {
blk, err = p.doFetch(ctx, nodes[i], c)
if err != nil {
goLogger.Debugw("fetch failed", "from", nodes[i], "of", c, "attempt", i, "error", err)
}
blk, err = p.doFetch(ctx, nodes[i], c, i)

var idx int
var nm *Member
Expand Down Expand Up @@ -345,7 +342,7 @@ func (p *pool) updateWeightUnlocked(node string, failure bool) (index int, membe
var saturnReqTmpl = "https://%s/ipfs/%s?format=raw"

// doFetch attempts to fetch a block from a given Saturn endpoint. It sends the retrieval logs to the logging endpoint upon a successful or failed attempt.
func (p *pool) doFetch(ctx context.Context, from string, c cid.Cid) (b blocks.Block, e error) {
func (p *pool) doFetch(ctx context.Context, from string, c cid.Cid, attempt int) (b blocks.Block, e error) {
requestId := uuid.NewString()
goLogger.Debugw("doing fetch", "from", from, "of", c, "requestId", requestId)
start := time.Now()
Expand All @@ -355,13 +352,15 @@ func (p *pool) doFetch(ctx context.Context, from string, c cid.Cid) (b blocks.Bl
respReq := &http.Request{}
received := 0
defer func() {
goLogger.Debugw("fetch result", "from", from, "of", c, "status", code, "size", received, "ttfb", int(fb.Sub(start).Milliseconds()), "duration", time.Since(start).Seconds())
ttfbMs := fb.Sub(start).Milliseconds()
durationSecs := time.Since(start).Seconds()
goLogger.Debugw("fetch result", "from", from, "of", c, "status", code, "size", received, "ttfb", int(ttfbMs), "duration", durationSecs, "attempt", attempt, "error", e)
fetchResponseMetric.WithLabelValues(fmt.Sprintf("%d", code)).Add(1)
if fb.After(start) {
fetchLatencyMetric.Observe(float64(fb.Sub(start).Milliseconds()))
fetchLatencyMetric.Observe(float64(ttfbMs))
}
if received > 0 {
fetchSpeedMetric.Observe(float64(received) / time.Since(start).Seconds())
fetchSpeedMetric.Observe(float64(received) / duration)
fetchSizeMetric.Observe(float64(received))
}
p.logger.queue <- log{
Expand All @@ -370,11 +369,11 @@ func (p *pool) doFetch(ctx context.Context, from string, c cid.Cid) (b blocks.Bl
LocalTime: start,
// TODO: does this include header sizes?
NumBytesSent: received,
RequestDuration: time.Since(start).Seconds(),
RequestDuration: durationSecs,
RequestID: requestId,
HTTPStatusCode: code,
HTTPProtocol: proto,
TTFBMS: int(fb.Sub(start).Milliseconds()),
TTFBMS: int(ttfbMs),
// my address
ClientAddress: "",
Range: "",
Expand Down

0 comments on commit 49f5801

Please sign in to comment.