metrics.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package proxy
  2. import (
  3. "github.com/prometheus/client_golang/prometheus"
  4. "github.com/cloudflare/cloudflared/connection"
  5. )
  6. // Metrics uses connection.MetricsNamespace(aka cloudflared) as namespace and connection.TunnelSubsystem
  7. // (tunnel) as subsystem to keep them consistent with the previous qualifier.
  8. var (
  9. totalRequests = prometheus.NewCounter(
  10. prometheus.CounterOpts{
  11. Namespace: connection.MetricsNamespace,
  12. Subsystem: connection.TunnelSubsystem,
  13. Name: "total_requests",
  14. Help: "Amount of requests proxied through all the tunnels",
  15. },
  16. )
  17. concurrentRequests = prometheus.NewGauge(
  18. prometheus.GaugeOpts{
  19. Namespace: connection.MetricsNamespace,
  20. Subsystem: connection.TunnelSubsystem,
  21. Name: "concurrent_requests_per_tunnel",
  22. Help: "Concurrent requests proxied through each tunnel",
  23. },
  24. )
  25. responseByCode = prometheus.NewCounterVec(
  26. prometheus.CounterOpts{
  27. Namespace: connection.MetricsNamespace,
  28. Subsystem: connection.TunnelSubsystem,
  29. Name: "response_by_code",
  30. Help: "Count of responses by HTTP status code",
  31. },
  32. []string{"status_code"},
  33. )
  34. requestErrors = prometheus.NewCounter(
  35. prometheus.CounterOpts{
  36. Namespace: connection.MetricsNamespace,
  37. Subsystem: connection.TunnelSubsystem,
  38. Name: "request_errors",
  39. Help: "Count of error proxying to origin",
  40. },
  41. )
  42. activeTCPSessions = prometheus.NewGauge(
  43. prometheus.GaugeOpts{
  44. Namespace: connection.MetricsNamespace,
  45. Subsystem: "tcp",
  46. Name: "active_sessions",
  47. Help: "Concurrent count of TCP sessions that are being proxied to any origin",
  48. },
  49. )
  50. totalTCPSessions = prometheus.NewCounter(
  51. prometheus.CounterOpts{
  52. Namespace: connection.MetricsNamespace,
  53. Subsystem: "tcp",
  54. Name: "total_sessions",
  55. Help: "Total count of TCP sessions that have been proxied to any origin",
  56. },
  57. )
  58. connectLatency = prometheus.NewHistogram(
  59. prometheus.HistogramOpts{
  60. Namespace: connection.MetricsNamespace,
  61. Subsystem: "proxy",
  62. Name: "connect_latency",
  63. Help: "Time it takes to establish and acknowledge connections in milliseconds",
  64. Buckets: []float64{1, 10, 25, 50, 100, 500, 1000, 5000},
  65. },
  66. )
  67. connectStreamErrors = prometheus.NewCounter(
  68. prometheus.CounterOpts{
  69. Namespace: connection.MetricsNamespace,
  70. Subsystem: "proxy",
  71. Name: "connect_streams_errors",
  72. Help: "Total count of failure to establish and acknowledge connections",
  73. },
  74. )
  75. )
  76. func init() {
  77. prometheus.MustRegister(
  78. totalRequests,
  79. concurrentRequests,
  80. responseByCode,
  81. requestErrors,
  82. activeTCPSessions,
  83. totalTCPSessions,
  84. connectLatency,
  85. connectStreamErrors,
  86. )
  87. }
  88. func incrementRequests() {
  89. totalRequests.Inc()
  90. concurrentRequests.Inc()
  91. }
  92. func decrementConcurrentRequests() {
  93. concurrentRequests.Dec()
  94. }
  95. func incrementTCPRequests() {
  96. incrementRequests()
  97. totalTCPSessions.Inc()
  98. activeTCPSessions.Inc()
  99. }
  100. func decrementTCPConcurrentRequests() {
  101. decrementConcurrentRequests()
  102. activeTCPSessions.Dec()
  103. }