prometheus.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2022 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. // +build prometheus !core
  16. package node
  17. import (
  18. "github.com/prometheus/client_golang/prometheus"
  19. "sync"
  20. )
  21. var (
  22. prometheuseMetrics *PrometheusMetrics
  23. mutex sync.RWMutex
  24. )
  25. func GetPrometheusMetrics() *PrometheusMetrics {
  26. mutex.Lock()
  27. if prometheuseMetrics == nil {
  28. prometheuseMetrics = newPrometheusMetrics()
  29. }
  30. mutex.Unlock()
  31. return prometheuseMetrics
  32. }
  33. type MetricGroup struct {
  34. TotalRecordsIn *prometheus.CounterVec
  35. TotalRecordsOut *prometheus.CounterVec
  36. TotalExceptions *prometheus.CounterVec
  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. bufferLength := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  68. Name: prefix + "_" + BufferLength,
  69. Help: "The length of the plan buffer which is shared by all instances of " + prefix,
  70. }, labelNames)
  71. prometheus.MustRegister(totalRecordsIn, totalRecordsOut, totalExceptions, processLatency, bufferLength)
  72. vecs = append(vecs, &MetricGroup{
  73. TotalRecordsIn: totalRecordsIn,
  74. TotalRecordsOut: totalRecordsOut,
  75. TotalExceptions: totalExceptions,
  76. ProcessLatency: processLatency,
  77. BufferLength: bufferLength,
  78. })
  79. }
  80. return &PrometheusMetrics{vecs: vecs}
  81. }
  82. func (m *PrometheusMetrics) GetMetricsGroup(opType string) *MetricGroup {
  83. switch opType {
  84. case "source":
  85. return m.vecs[0]
  86. case "op":
  87. return m.vecs[1]
  88. case "sink":
  89. return m.vecs[2]
  90. }
  91. return nil
  92. }