prometheus.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2022-2023 EMQ Technologies Co., Ltd.
  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. //go:build prometheus || !core
  15. package metric
  16. import (
  17. "sync"
  18. "github.com/prometheus/client_golang/prometheus"
  19. )
  20. var (
  21. prometheuseMetrics *PrometheusMetrics
  22. mutex sync.RWMutex
  23. )
  24. func GetPrometheusMetrics() *PrometheusMetrics {
  25. mutex.Lock()
  26. if prometheuseMetrics == nil {
  27. prometheuseMetrics = newPrometheusMetrics()
  28. }
  29. mutex.Unlock()
  30. return prometheuseMetrics
  31. }
  32. type MetricGroup struct {
  33. TotalRecordsIn *prometheus.CounterVec
  34. TotalRecordsOut *prometheus.CounterVec
  35. TotalExceptions *prometheus.CounterVec
  36. ProcessLatencyHist *prometheus.HistogramVec
  37. ProcessLatency *prometheus.GaugeVec
  38. BufferLength *prometheus.GaugeVec
  39. }
  40. type PrometheusMetrics struct {
  41. vecs []*MetricGroup
  42. }
  43. func newPrometheusMetrics() *PrometheusMetrics {
  44. var (
  45. labelNames = []string{"rule", "type", "op", "instance"}
  46. prefixes = []string{"kuiper_source", "kuiper_op", "kuiper_sink"}
  47. )
  48. var vecs []*MetricGroup
  49. for _, prefix := range prefixes {
  50. // prometheus initialization
  51. totalRecordsIn := prometheus.NewCounterVec(prometheus.CounterOpts{
  52. Name: prefix + "_" + RecordsInTotal,
  53. Help: "Total number of messages received by the operation of " + prefix,
  54. }, labelNames)
  55. totalRecordsOut := prometheus.NewCounterVec(prometheus.CounterOpts{
  56. Name: prefix + "_" + RecordsOutTotal,
  57. Help: "Total number of messages published by the operation of " + prefix,
  58. }, labelNames)
  59. totalExceptions := prometheus.NewCounterVec(prometheus.CounterOpts{
  60. Name: prefix + "_" + ExceptionsTotal,
  61. Help: "Total number of user exceptions of " + prefix,
  62. }, labelNames)
  63. processLatency := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  64. Name: prefix + "_" + ProcessLatencyUs,
  65. Help: "Process latency in millisecond of " + prefix,
  66. }, labelNames)
  67. processLatencyHist := prometheus.NewHistogramVec(prometheus.HistogramOpts{
  68. Name: prefix + "_" + ProcessLatencyUsHist,
  69. Help: "Histograms of process latency in millisecond of " + prefix,
  70. Buckets: prometheus.ExponentialBuckets(10, 2, 20), // 10us ~ 5s
  71. }, labelNames)
  72. bufferLength := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  73. Name: prefix + "_" + BufferLength,
  74. Help: "The length of the plan buffer which is shared by all instances of " + prefix,
  75. }, labelNames)
  76. prometheus.MustRegister(totalRecordsIn, totalRecordsOut, totalExceptions, processLatency, processLatencyHist, bufferLength)
  77. vecs = append(vecs, &MetricGroup{
  78. TotalRecordsIn: totalRecordsIn,
  79. TotalRecordsOut: totalRecordsOut,
  80. TotalExceptions: totalExceptions,
  81. ProcessLatency: processLatency,
  82. ProcessLatencyHist: processLatencyHist,
  83. BufferLength: bufferLength,
  84. })
  85. }
  86. return &PrometheusMetrics{vecs: vecs}
  87. }
  88. func (m *PrometheusMetrics) GetMetricsGroup(opType string) *MetricGroup {
  89. switch opType {
  90. case "source":
  91. return m.vecs[0]
  92. case "op":
  93. return m.vecs[1]
  94. case "sink":
  95. return m.vecs[2]
  96. }
  97. return nil
  98. }