From f0780ad9cc2d4990a21520fd852d517c596a2b95 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 14 Jun 2018 18:57:23 -0400 Subject: [PATCH 1/4] frontend: isolate prometheus logic to one file --- frontend/http/frontend.go | 40 ------------------------------- frontend/http/prometheus.go | 48 +++++++++++++++++++++++++++++++++++++ frontend/udp/frontend.go | 41 ------------------------------- frontend/udp/prometheus.go | 48 +++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 81 deletions(-) create mode 100644 frontend/http/prometheus.go create mode 100644 frontend/udp/prometheus.go diff --git a/frontend/http/frontend.go b/frontend/http/frontend.go index fc4957e..3cf553f 100644 --- a/frontend/http/frontend.go +++ b/frontend/http/frontend.go @@ -10,52 +10,12 @@ import ( "time" "github.com/julienschmidt/httprouter" - "github.com/prometheus/client_golang/prometheus" "github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/frontend" "github.com/chihaya/chihaya/pkg/log" ) -func init() { - prometheus.MustRegister(promResponseDurationMilliseconds) -} - -var promResponseDurationMilliseconds = prometheus.NewHistogramVec( - prometheus.HistogramOpts{ - Name: "chihaya_http_response_duration_milliseconds", - Help: "The duration of time it takes to receive and write a response to an API request", - Buckets: prometheus.ExponentialBuckets(9.375, 2, 10), - }, - []string{"action", "address_family", "error"}, -) - -// recordResponseDuration records the duration of time to respond to a Request -// in milliseconds . -func recordResponseDuration(action string, af *bittorrent.AddressFamily, err error, duration time.Duration) { - var errString string - if err != nil { - if _, ok := err.(bittorrent.ClientError); ok { - errString = err.Error() - } else { - errString = "internal error" - } - } - - var afString string - if af == nil { - afString = "Unknown" - } else if *af == bittorrent.IPv4 { - afString = "IPv4" - } else if *af == bittorrent.IPv6 { - afString = "IPv6" - } - - promResponseDurationMilliseconds. - WithLabelValues(action, afString, errString). - Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond)) -} - // Config represents all of the configurable options for an HTTP BitTorrent // Frontend. type Config struct { diff --git a/frontend/http/prometheus.go b/frontend/http/prometheus.go new file mode 100644 index 0000000..e82b1e1 --- /dev/null +++ b/frontend/http/prometheus.go @@ -0,0 +1,48 @@ +package http + +import ( + "time" + + "github.com/prometheus/client_golang/prometheus" + + "github.com/chihaya/chihaya/bittorrent" +) + +func init() { + prometheus.MustRegister(promResponseDurationMilliseconds) +} + +var promResponseDurationMilliseconds = prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Name: "chihaya_http_response_duration_milliseconds", + Help: "The duration of time it takes to receive and write a response to an API request", + Buckets: prometheus.ExponentialBuckets(9.375, 2, 10), + }, + []string{"action", "address_family", "error"}, +) + +// recordResponseDuration records the duration of time to respond to a Request +// in milliseconds. +func recordResponseDuration(action string, af *bittorrent.AddressFamily, err error, duration time.Duration) { + var errString string + if err != nil { + if _, ok := err.(bittorrent.ClientError); ok { + errString = err.Error() + } else { + errString = "internal error" + } + } + + var afString string + if af == nil { + afString = "Unknown" + } else if *af == bittorrent.IPv4 { + afString = "IPv4" + } else if *af == bittorrent.IPv6 { + afString = "IPv6" + } + + promResponseDurationMilliseconds. + WithLabelValues(action, afString, errString). + Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond)) +} diff --git a/frontend/udp/frontend.go b/frontend/udp/frontend.go index 929e2ae..8dea946 100644 --- a/frontend/udp/frontend.go +++ b/frontend/udp/frontend.go @@ -12,8 +12,6 @@ import ( "sync" "time" - "github.com/prometheus/client_golang/prometheus" - "github.com/chihaya/chihaya/bittorrent" "github.com/chihaya/chihaya/frontend" "github.com/chihaya/chihaya/frontend/udp/bytepool" @@ -24,45 +22,6 @@ import ( var allowedGeneratedPrivateKeyRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") -func init() { - prometheus.MustRegister(promResponseDurationMilliseconds) -} - -var promResponseDurationMilliseconds = prometheus.NewHistogramVec( - prometheus.HistogramOpts{ - Name: "chihaya_udp_response_duration_milliseconds", - Help: "The duration of time it takes to receive and write a response to an API request", - Buckets: prometheus.ExponentialBuckets(9.375, 2, 10), - }, - []string{"action", "address_family", "error"}, -) - -// recordResponseDuration records the duration of time to respond to a UDP -// Request in milliseconds . -func recordResponseDuration(action string, af *bittorrent.AddressFamily, err error, duration time.Duration) { - var errString string - if err != nil { - if _, ok := err.(bittorrent.ClientError); ok { - errString = err.Error() - } else { - errString = "internal error" - } - } - - var afString string - if af == nil { - afString = "Unknown" - } else if *af == bittorrent.IPv4 { - afString = "IPv4" - } else if *af == bittorrent.IPv6 { - afString = "IPv6" - } - - promResponseDurationMilliseconds. - WithLabelValues(action, afString, errString). - Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond)) -} - // Config represents all of the configurable options for a UDP BitTorrent // Tracker. type Config struct { diff --git a/frontend/udp/prometheus.go b/frontend/udp/prometheus.go new file mode 100644 index 0000000..4b1f5ba --- /dev/null +++ b/frontend/udp/prometheus.go @@ -0,0 +1,48 @@ +package udp + +import ( + "time" + + "github.com/prometheus/client_golang/prometheus" + + "github.com/chihaya/chihaya/bittorrent" +) + +func init() { + prometheus.MustRegister(promResponseDurationMilliseconds) +} + +var promResponseDurationMilliseconds = prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Name: "chihaya_udp_response_duration_milliseconds", + Help: "The duration of time it takes to receive and write a response to an API request", + Buckets: prometheus.ExponentialBuckets(9.375, 2, 10), + }, + []string{"action", "address_family", "error"}, +) + +// recordResponseDuration records the duration of time to respond to a UDP +// Request in milliseconds. +func recordResponseDuration(action string, af *bittorrent.AddressFamily, err error, duration time.Duration) { + var errString string + if err != nil { + if _, ok := err.(bittorrent.ClientError); ok { + errString = err.Error() + } else { + errString = "internal error" + } + } + + var afString string + if af == nil { + afString = "Unknown" + } else if *af == bittorrent.IPv4 { + afString = "IPv4" + } else if *af == bittorrent.IPv6 { + afString = "IPv6" + } + + promResponseDurationMilliseconds. + WithLabelValues(action, afString, errString). + Observe(float64(duration.Nanoseconds()) / float64(time.Millisecond)) +} From 3c052ec98d2850d7b2eba6d7811f679626008923 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 14 Jun 2018 18:58:40 -0400 Subject: [PATCH 2/4] storage/memory: multi-line call to New() --- storage/memory/peer_store_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/storage/memory/peer_store_test.go b/storage/memory/peer_store_test.go index 86b4477..1a2aee2 100644 --- a/storage/memory/peer_store_test.go +++ b/storage/memory/peer_store_test.go @@ -9,7 +9,12 @@ import ( ) func createNew() s.PeerStore { - ps, err := New(Config{ShardCount: 1024, GarbageCollectionInterval: 10 * time.Minute, PrometheusReportingInterval: 10 * time.Minute, PeerLifetime: 30 * time.Minute}) + ps, err := New(Config{ + ShardCount: 1024, + GarbageCollectionInterval: 10 * time.Minute, + PrometheusReportingInterval: 10 * time.Minute, + PeerLifetime: 30 * time.Minute, + }) if err != nil { panic(err) } From 734c11c6ed80dc84ce10f362daec37644724566c Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 14 Jun 2018 19:02:28 -0400 Subject: [PATCH 3/4] bittorrent: anonymous fields for IPs in test table --- bittorrent/bittorrent_test.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/bittorrent/bittorrent_test.go b/bittorrent/bittorrent_test.go index c69d299..504d5b1 100644 --- a/bittorrent/bittorrent_test.go +++ b/bittorrent/bittorrent_test.go @@ -19,22 +19,16 @@ var peerStringTestCases = []struct { }{ { input: Peer{ - ID: PeerIDFromBytes(b), - IP: IP{ - IP: net.IPv4(10, 11, 12, 1), - AddressFamily: IPv4, - }, + ID: PeerIDFromBytes(b), + IP: IP{net.IPv4(10, 11, 12, 1), IPv4}, Port: 1234, }, expected: fmt.Sprintf("%s@[10.11.12.1]:1234", expected), }, { input: Peer{ - ID: PeerIDFromBytes(b), - IP: IP{ - IP: net.ParseIP("2001:db8::ff00:42:8329"), - AddressFamily: IPv6, - }, + ID: PeerIDFromBytes(b), + IP: IP{net.ParseIP("2001:db8::ff00:42:8329"), IPv6}, Port: 1234, }, expected: fmt.Sprintf("%s@[2001:db8::ff00:42:8329]:1234", expected), From 84ee1d66583288086f0b68c9b1703af1894f1b96 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Thu, 14 Jun 2018 19:06:01 -0400 Subject: [PATCH 4/4] storage/memorybysubnet: fix mispellings --- storage/memorybysubnet/peer_store.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/storage/memorybysubnet/peer_store.go b/storage/memorybysubnet/peer_store.go index 206074e..cda08ef 100644 --- a/storage/memorybysubnet/peer_store.go +++ b/storage/memorybysubnet/peer_store.go @@ -520,7 +520,7 @@ func (ps *peerStore) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant // Append the rest of the leechers. if numWant > 0 { for subnet := range shard.swarms[ih].leechers { - // Already appended from this subnet explictly first. + // Already appended from this subnet explicitly first. if subnet == preferredSubnet { continue } @@ -568,7 +568,7 @@ func (ps *peerStore) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant // Append as the rest of the seeders. if numWant > 0 { for subnet := range shard.swarms[ih].seeders { - // Already appended from this subnet explictly first. + // Already appended from this subnet explicitly first. if subnet == preferredSubnet { continue } @@ -587,7 +587,7 @@ func (ps *peerStore) AnnouncePeers(ih bittorrent.InfoHash, seeder bool, numWant // Append the rest of the leechers. if numWant > 0 { for subnet := range shard.swarms[ih].leechers { - // Already appended from this subnet explictly first. + // Already appended from this subnet explicitly first. if subnet == preferredSubnet { continue }