metrics.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package rpc
  17. import (
  18. "fmt"
  19. "time"
  20. "github.com/ethereum/go-ethereum/metrics"
  21. )
  22. var (
  23. rpcRequestGauge = metrics.NewRegisteredGauge("rpc/requests", nil)
  24. successfulRequestGauge = metrics.NewRegisteredGauge("rpc/success", nil)
  25. failedRequestGauge = metrics.NewRegisteredGauge("rpc/failure", nil)
  26. // serveTimeHistName is the prefix of the per-request serving time histograms.
  27. serveTimeHistName = "rpc/duration"
  28. rpcServingTimer = metrics.NewRegisteredTimer("rpc/duration/all", nil)
  29. )
  30. // updateServeTimeHistogram tracks the serving time of a remote RPC call.
  31. func updateServeTimeHistogram(method string, success bool, elapsed time.Duration) {
  32. note := "success"
  33. if !success {
  34. note = "failure"
  35. }
  36. h := fmt.Sprintf("%s/%s/%s", serveTimeHistName, method, note)
  37. sampler := func() metrics.Sample {
  38. return metrics.ResettingSample(
  39. metrics.NewExpDecaySample(1028, 0.015),
  40. )
  41. }
  42. metrics.GetOrRegisterHistogramLazy(h, nil, sampler).Update(elapsed.Microseconds())
  43. }