prometheus.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2021 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. package node
  15. import (
  16. "github.com/prometheus/client_golang/prometheus"
  17. "sync"
  18. )
  19. const RecordsInTotal = "records_in_total"
  20. const RecordsOutTotal = "records_out_total"
  21. const ExceptionsTotal = "exceptions_total"
  22. const ProcessLatencyUs = "process_latency_us"
  23. const LastInvocation = "last_invocation"
  24. const BufferLength = "buffer_length"
  25. var (
  26. MetricNames = []string{RecordsInTotal, RecordsOutTotal, ExceptionsTotal, ProcessLatencyUs, BufferLength, LastInvocation}
  27. prometheuseMetrics *PrometheusMetrics
  28. mutex sync.RWMutex
  29. )
  30. func GetPrometheusMetrics() *PrometheusMetrics {
  31. mutex.Lock()
  32. if prometheuseMetrics == nil {
  33. prometheuseMetrics = newPrometheusMetrics()
  34. }
  35. mutex.Unlock()
  36. return prometheuseMetrics
  37. }
  38. type MetricGroup struct {
  39. TotalRecordsIn *prometheus.CounterVec
  40. TotalRecordsOut *prometheus.CounterVec
  41. TotalExceptions *prometheus.CounterVec
  42. ProcessLatency *prometheus.GaugeVec
  43. BufferLength *prometheus.GaugeVec
  44. }
  45. type PrometheusMetrics struct {
  46. vecs []*MetricGroup
  47. }
  48. func newPrometheusMetrics() *PrometheusMetrics {
  49. var (
  50. labelNames = []string{"rule", "type", "op", "instance"}
  51. prefixes = []string{"kuiper_source", "kuiper_op", "kuiper_sink"}
  52. )
  53. var vecs []*MetricGroup
  54. for _, prefix := range prefixes {
  55. //prometheus initialization
  56. totalRecordsIn := prometheus.NewCounterVec(prometheus.CounterOpts{
  57. Name: prefix + "_" + RecordsInTotal,
  58. Help: "Total number of messages received by the operation of " + prefix,
  59. }, labelNames)
  60. totalRecordsOut := prometheus.NewCounterVec(prometheus.CounterOpts{
  61. Name: prefix + "_" + RecordsOutTotal,
  62. Help: "Total number of messages published by the operation of " + prefix,
  63. }, labelNames)
  64. totalExceptions := prometheus.NewCounterVec(prometheus.CounterOpts{
  65. Name: prefix + "_" + ExceptionsTotal,
  66. Help: "Total number of user exceptions of " + prefix,
  67. }, labelNames)
  68. processLatency := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  69. Name: prefix + "_" + ProcessLatencyUs,
  70. Help: "Process latency in millisecond of " + prefix,
  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, bufferLength)
  77. vecs = append(vecs, &MetricGroup{
  78. TotalRecordsIn: totalRecordsIn,
  79. TotalRecordsOut: totalRecordsOut,
  80. TotalExceptions: totalExceptions,
  81. ProcessLatency: processLatency,
  82. BufferLength: bufferLength,
  83. })
  84. }
  85. return &PrometheusMetrics{vecs: vecs}
  86. }
  87. func (m *PrometheusMetrics) GetMetricsGroup(opType string) *MetricGroup {
  88. switch opType {
  89. case "source":
  90. return m.vecs[0]
  91. case "op":
  92. return m.vecs[1]
  93. case "sink":
  94. return m.vecs[2]
  95. }
  96. return nil
  97. }