prometheus.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "github.com/prometheus/client_golang/prometheus"
  18. "sync"
  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. ProcessLatency *prometheus.GaugeVec
  37. BufferLength *prometheus.GaugeVec
  38. }
  39. type PrometheusMetrics struct {
  40. vecs []*MetricGroup
  41. }
  42. func newPrometheusMetrics() *PrometheusMetrics {
  43. var (
  44. labelNames = []string{"rule", "type", "op", "instance"}
  45. prefixes = []string{"kuiper_source", "kuiper_op", "kuiper_sink"}
  46. )
  47. var vecs []*MetricGroup
  48. for _, prefix := range prefixes {
  49. //prometheus initialization
  50. totalRecordsIn := prometheus.NewCounterVec(prometheus.CounterOpts{
  51. Name: prefix + "_" + RecordsInTotal,
  52. Help: "Total number of messages received by the operation of " + prefix,
  53. }, labelNames)
  54. totalRecordsOut := prometheus.NewCounterVec(prometheus.CounterOpts{
  55. Name: prefix + "_" + RecordsOutTotal,
  56. Help: "Total number of messages published by the operation of " + prefix,
  57. }, labelNames)
  58. totalExceptions := prometheus.NewCounterVec(prometheus.CounterOpts{
  59. Name: prefix + "_" + ExceptionsTotal,
  60. Help: "Total number of user exceptions of " + prefix,
  61. }, labelNames)
  62. processLatency := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  63. Name: prefix + "_" + ProcessLatencyUs,
  64. Help: "Process latency in millisecond of " + prefix,
  65. }, labelNames)
  66. bufferLength := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  67. Name: prefix + "_" + BufferLength,
  68. Help: "The length of the plan buffer which is shared by all instances of " + prefix,
  69. }, labelNames)
  70. prometheus.MustRegister(totalRecordsIn, totalRecordsOut, totalExceptions, processLatency, bufferLength)
  71. vecs = append(vecs, &MetricGroup{
  72. TotalRecordsIn: totalRecordsIn,
  73. TotalRecordsOut: totalRecordsOut,
  74. TotalExceptions: totalExceptions,
  75. ProcessLatency: processLatency,
  76. BufferLength: bufferLength,
  77. })
  78. }
  79. return &PrometheusMetrics{vecs: vecs}
  80. }
  81. func (m *PrometheusMetrics) GetMetricsGroup(opType string) *MetricGroup {
  82. switch opType {
  83. case "source":
  84. return m.vecs[0]
  85. case "op":
  86. return m.vecs[1]
  87. case "sink":
  88. return m.vecs[2]
  89. }
  90. return nil
  91. }