Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #829 from yeya24/feature/add-download-bytes-metrics
Browse files Browse the repository at this point in the history
feature: add supernode piece downloaded size metrics
  • Loading branch information
starnop authored Sep 12, 2019
2 parents f3f6ece + 7f2fc6d commit ccc7ba7
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/user_guide/metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This doc contains all the metrics that Dragonfly components currently support. N
- dragonfly_supernode_cdn_cache_hit_total{} - total times of hitting cdn cache. counter type.
- dragonfly_supernode_cdn_download_total{} - total times of cdn downloading. counter type.
- dragonfly_supernode_cdn_download_failed_total{} - total failure times of cdn downloading. counter type.
- dragonfly_supernode_pieces_downloaded_size_bytes{} - total size of pieces downloaded from supernode in bytes. counter type.

## Dfdaemon

Expand Down
5 changes: 5 additions & 0 deletions supernode/server/0.3_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ func (s *Server) reportPiece(ctx context.Context, rw http.ResponseWriter, req *h
return err
}

// If piece is downloaded from supernode, add metrics.
if s.Config.IsSuperCID(dstCID) {
m.pieceDownloadedBytes.WithLabelValues().Add(float64(sutil.CalculatePieceSize(pieceRange)))
}

request := &types.PieceUpdateRequest{
ClientID: srcCID,
DstPID: dstDfgetTask.PeerID,
Expand Down
12 changes: 8 additions & 4 deletions supernode/server/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import (

// metrics defines some prometheus metrics for monitoring supernode
type metrics struct {
requestCounter *prometheus.CounterVec
requestDuration *prometheus.HistogramVec
requestSize *prometheus.HistogramVec
responseSize *prometheus.HistogramVec
requestCounter *prometheus.CounterVec
requestDuration *prometheus.HistogramVec
requestSize *prometheus.HistogramVec
responseSize *prometheus.HistogramVec
pieceDownloadedBytes *prometheus.CounterVec
}

func newMetrics(register prometheus.Registerer) *metrics {
Expand All @@ -51,6 +52,9 @@ func newMetrics(register prometheus.Registerer) *metrics {
"Histogram of response size for HTTP requests.", []string{"handler"},
prometheus.ExponentialBuckets(100, 10, 8), register,
),
pieceDownloadedBytes: metricsutils.NewCounter(config.SubsystemSupernode, "pieces_downloaded_size_bytes",
"total bytes of pieces downloaded from supernode", []string{}, register,
),
}
}

Expand Down
25 changes: 24 additions & 1 deletion supernode/util/range_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ const (
separator = "-"
)

// CalculatePieceSize calculates the size of piece
// according to the parameter range.
func CalculatePieceSize(rangeStr string) int64 {
ranges := strings.Split(rangeStr, separator)
if len(ranges) != 2 {
return 0
}

startIndex, err := strconv.ParseInt(ranges[0], 10, 64)
if err != nil {
return 0
}
endIndex, err := strconv.ParseInt(ranges[1], 10, 64)
if err != nil {
return 0
}
if endIndex < startIndex {
return 0
}

pieceSize := endIndex - startIndex + 1
return pieceSize
}

// CalculatePieceNum calculates the number of piece
// according to the parameter range.
func CalculatePieceNum(rangeStr string) int {
Expand All @@ -47,7 +71,6 @@ func CalculatePieceNum(rangeStr string) int {
}

pieceSize := endIndex - startIndex + 1

return int(startIndex / pieceSize)
}

Expand Down
41 changes: 41 additions & 0 deletions supernode/util/range_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,47 @@ func init() {
check.Suite(&RangeUtilSuite{})
}

func (suite *RangeUtilSuite) TestCalculatePieceSize(c *check.C) {
var cases = []struct {
rangeStr string
expected int64
}{
{
rangeStr: "foo",
expected: 0,
},
{
rangeStr: "aaa-bbb",
expected: 0,
},
{
rangeStr: "3-2",
expected: 0,
},
{
rangeStr: "1 -3",
expected: 0,
},
{
rangeStr: "0-0",
expected: 1,
},
{
rangeStr: "6-8",
expected: 3,
},
{
rangeStr: "0-40000",
expected: 40001,
},
}

for _, v := range cases {
result := CalculatePieceSize(v.rangeStr)
c.Assert(result, check.Equals, v.expected)
}
}

func (suite *RangeUtilSuite) TestCalculatePieceNum(c *check.C) {
var cases = []struct {
rangeStr string
Expand Down

0 comments on commit ccc7ba7

Please sign in to comment.