metrics.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdserver
  15. import (
  16. "log"
  17. "time"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/prometheus/client_golang/prometheus"
  19. "github.com/coreos/etcd/pkg/runtime"
  20. )
  21. var (
  22. // TODO: with label in v3?
  23. proposeDurations = prometheus.NewSummary(prometheus.SummaryOpts{
  24. Name: "etcdserver_proposal_durations_milliseconds",
  25. Help: "The latency distributions of committing proposal.",
  26. })
  27. proposePending = prometheus.NewGauge(prometheus.GaugeOpts{
  28. Name: "etcdserver_pending_proposal_total",
  29. Help: "The total number of pending proposals.",
  30. })
  31. // This is number of proposal failed in client's view.
  32. // The proposal might be later got committed in raft.
  33. proposeFailed = prometheus.NewCounter(prometheus.CounterOpts{
  34. Name: "etcdserver_proposal_failed_total",
  35. Help: "The total number of failed proposals.",
  36. })
  37. fileDescriptorUsed = prometheus.NewGauge(prometheus.GaugeOpts{
  38. Name: "file_descriptors_used",
  39. Help: "The number of file descriptors used",
  40. })
  41. )
  42. func init() {
  43. prometheus.MustRegister(proposeDurations)
  44. prometheus.MustRegister(proposePending)
  45. prometheus.MustRegister(proposeFailed)
  46. prometheus.MustRegister(fileDescriptorUsed)
  47. }
  48. func monitorFileDescriptor(done <-chan struct{}) {
  49. ticker := time.NewTicker(5 * time.Second)
  50. defer ticker.Stop()
  51. for {
  52. used, err := runtime.FDUsage()
  53. if err != nil {
  54. log.Printf("etcdserver: cannot monitor file descriptor usage (%v)", err)
  55. return
  56. }
  57. fileDescriptorUsed.Set(float64(used))
  58. limit, err := runtime.FDLimit()
  59. if err != nil {
  60. log.Printf("etcdserver: cannot monitor file descriptor usage (%v)", err)
  61. return
  62. }
  63. if used >= limit/5*4 {
  64. log.Printf("etcdserver: 80%% of the file descriptor limit is used [used = %d, limit = %d]", used, limit)
  65. }
  66. select {
  67. case <-ticker.C:
  68. case <-done:
  69. return
  70. }
  71. }
  72. }