swarm: finalise metrics after feedback

This commit is contained in:
Fabio Barone 2018-01-30 14:42:01 -05:00 committed by Anton Evangelatov
parent e05e4cab48
commit 5af18e3e6b
3 changed files with 73 additions and 47 deletions

View file

@ -55,8 +55,6 @@ var (
apiRmFileFail = metrics.NewCounter("api.removefile.fail") apiRmFileFail = metrics.NewCounter("api.removefile.fail")
apiAppendFileCount = metrics.NewCounter("api.appendfile.count") apiAppendFileCount = metrics.NewCounter("api.appendfile.count")
apiAppendFileFail = metrics.NewCounter("api.appendfile.fail") apiAppendFileFail = metrics.NewCounter("api.appendfile.fail")
apiBuildDirTreeCount = metrics.NewCounter("api.builddirtree.count")
apiBuildDirTreeFail = metrics.NewCounter("api.builddirtree.fail")
) )
type Resolver interface { type Resolver interface {
@ -184,7 +182,6 @@ func (self *Api) Resolve(uri *URI) (storage.Key, error) {
isHash := hashMatcher.MatchString(uri.Addr) isHash := hashMatcher.MatchString(uri.Addr)
if uri.Immutable() || uri.DeprecatedImmutable() { if uri.Immutable() || uri.DeprecatedImmutable() {
if !isHash { if !isHash {
apiResolveFail.Inc(1)
return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr) return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr)
} }
return common.Hex2Bytes(uri.Addr), nil return common.Hex2Bytes(uri.Addr), nil
@ -467,23 +464,19 @@ func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, conte
} }
func (self *Api) BuildDirectoryTree(mhash string, nameresolver bool) (key storage.Key, manifestEntryMap map[string]*manifestTrieEntry, err error) { func (self *Api) BuildDirectoryTree(mhash string, nameresolver bool) (key storage.Key, manifestEntryMap map[string]*manifestTrieEntry, err error) {
apiBuildDirTreeCount.Inc(1)
uri, err := Parse("bzz:/" + mhash) uri, err := Parse("bzz:/" + mhash)
if err != nil { if err != nil {
apiBuildDirTreeFail.Inc(1)
return nil, nil, err return nil, nil, err
} }
key, err = self.Resolve(uri) key, err = self.Resolve(uri)
if err != nil { if err != nil {
apiBuildDirTreeFail.Inc(1)
return nil, nil, err return nil, nil, err
} }
quitC := make(chan bool) quitC := make(chan bool)
rootTrie, err := loadManifest(self.dpa, key, quitC) rootTrie, err := loadManifest(self.dpa, key, quitC)
if err != nil { if err != nil {
apiBuildDirTreeFail.Inc(1)
return nil, nil, fmt.Errorf("can't load manifest %v: %v", key.String(), err) return nil, nil, fmt.Errorf("can't load manifest %v: %v", key.String(), err)
} }
@ -493,7 +486,6 @@ func (self *Api) BuildDirectoryTree(mhash string, nameresolver bool) (key storag
}) })
if err != nil { if err != nil {
apiBuildDirTreeFail.Inc(1)
return nil, nil, fmt.Errorf("list with prefix failed %v: %v", key.String(), err) return nil, nil, fmt.Errorf("list with prefix failed %v: %v", key.String(), err)
} }
return key, manifestEntryMap, nil return key, manifestEntryMap, nil

View file

@ -61,6 +61,8 @@ var (
getListCount = metrics.NewCounter("api.http.get.list.count") getListCount = metrics.NewCounter("api.http.get.list.count")
getListFail = metrics.NewCounter("api.http.get.list.fail") getListFail = metrics.NewCounter("api.http.get.list.fail")
requestCount = metrics.NewCounter("http.request.count") requestCount = metrics.NewCounter("http.request.count")
htmlRequestCount = metrics.NewCounter("http.request.html.count")
jsonRequestCount = metrics.NewCounter("http.request.json.count")
requestTimer = metrics.NewResettingTimer("http.request.time") requestTimer = metrics.NewResettingTimer("http.request.time")
) )
@ -643,8 +645,19 @@ func (s *Server) HandleGetFile(w http.ResponseWriter, r *Request) {
} }
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if metrics.Enabled {
//The increment for request count and request timer themselves have a flag check
//for metrics.Enabled. Nevertheless, we introduce the if here because we
//are looking into the header just to see what request type it is (json/html).
//So let's take advantage and add all metrics related stuff here
requestCount.Inc(1) requestCount.Inc(1)
defer requestTimer.UpdateSince(time.Now()) defer requestTimer.UpdateSince(time.Now())
if r.Header.Get("Accept") == "application/json" {
jsonRequestCount.Inc(1)
} else {
htmlRequestCount.Inc(1)
}
}
s.logDebug("HTTP %s request URL: '%s', Host: '%s', Path: '%s', Referer: '%s', Accept: '%s'", r.Method, r.RequestURI, r.URL.Host, r.URL.Path, r.Referer(), r.Header.Get("Accept")) s.logDebug("HTTP %s request URL: '%s', Host: '%s', Path: '%s', Referer: '%s', Accept: '%s'", r.Method, r.RequestURI, r.URL.Host, r.URL.Path, r.Referer(), r.Header.Get("Accept"))
uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/")) uri, err := api.Parse(strings.TrimLeft(r.URL.Path, "/"))

View file

@ -25,12 +25,16 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics" "github.com/ethereum/go-ethereum/metrics"
gometrics "github.com/ethersphere/go-metrics"
) )
//metrics variables //metrics variables
//For metrics, we want to count how many times peers are added/removed
//at a certain index. Thus we do that with an array of counters with
//entry for each index
var ( var (
bucketOnIndexGauge = metrics.NewGauge("network.kademlia.bucket.onindex") bucketAddIndexCount []gometrics.Counter
bucketOffIndexGauge = metrics.NewGauge("network.kademlia.bucket.offindex") bucketRmIndexCount []gometrics.Counter
) )
const ( const (
@ -95,12 +99,17 @@ type Node interface {
// params is KadParams configuration // params is KadParams configuration
func New(addr Address, params *KadParams) *Kademlia { func New(addr Address, params *KadParams) *Kademlia {
buckets := make([][]Node, params.MaxProx+1) buckets := make([][]Node, params.MaxProx+1)
return &Kademlia{ kad := &Kademlia{
addr: addr, addr: addr,
KadParams: params, KadParams: params,
buckets: buckets, buckets: buckets,
db: newKadDb(addr, params), db: newKadDb(addr, params),
} }
//if metrics are enabled, initialise the array of counters
if metrics.Enabled {
kad.initMetricsVariables()
}
return kad
} }
// accessor for KAD base address // accessor for KAD base address
@ -145,7 +154,7 @@ func (self *Kademlia) On(node Node, cb func(*NodeRecord, Node) error) (err error
// TODO: give priority to peers with active traffic // TODO: give priority to peers with active traffic
if len(bucket) < self.BucketSize { // >= allows us to add peers beyond the bucketsize limitation if len(bucket) < self.BucketSize { // >= allows us to add peers beyond the bucketsize limitation
self.buckets[index] = append(bucket, node) self.buckets[index] = append(bucket, node)
bucketOnIndexGauge.Update(int64(index)) bucketAddIndexCount[index].Inc(1)
log.Debug(fmt.Sprintf("add node %v to table", node)) log.Debug(fmt.Sprintf("add node %v to table", node))
self.setProxLimit(index, true) self.setProxLimit(index, true)
record.node = node record.node = node
@ -186,7 +195,7 @@ func (self *Kademlia) Off(node Node, cb func(*NodeRecord, Node)) (err error) {
defer self.lock.Unlock() defer self.lock.Unlock()
index := self.proximityBin(node.Addr()) index := self.proximityBin(node.Addr())
bucketOffIndexGauge.Update(int64(index)) bucketRmIndexCount[index].Inc(1)
bucket := self.buckets[index] bucket := self.buckets[index]
for i := 0; i < len(bucket); i++ { for i := 0; i < len(bucket); i++ {
if node.Addr() == bucket[i].Addr() { if node.Addr() == bucket[i].Addr() {
@ -435,3 +444,15 @@ func (self *Kademlia) String() string {
rows = append(rows, "=========================================================================") rows = append(rows, "=========================================================================")
return strings.Join(rows, "\n") return strings.Join(rows, "\n")
} }
//We have to build up the array of counters for each index
func (self *Kademlia) initMetricsVariables() {
//create the arrays
bucketAddIndexCount = make([]gometrics.Counter, self.MaxProx+1)
bucketRmIndexCount = make([]gometrics.Counter, self.MaxProx+1)
//at each index create a metrics counter
for i := 0; i < (self.KadParams.MaxProx); i++ {
bucketAddIndexCount[i] = metrics.NewCounter(fmt.Sprintf("network.kademlia.bucket.add.%d.index", i))
bucketRmIndexCount[i] = metrics.NewCounter(fmt.Sprintf("network.kademlia.bucket.rm.%d.index", i))
}
}