prometheus.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package nodes
  2. import (
  3. "github.com/prometheus/client_golang/prometheus"
  4. "sync"
  5. )
  6. const RecordsInTotal = "records_in_total"
  7. const RecordsOutTotal = "records_out_total"
  8. const ExceptionsTotal = "exceptions_total"
  9. const ProcessLatencyMs = "process_latency_ms"
  10. const LastInvocation = "last_invocation"
  11. const BufferLength = "buffer_length"
  12. var (
  13. MetricNames = []string{RecordsInTotal, RecordsOutTotal, ExceptionsTotal, ProcessLatencyMs, BufferLength, LastInvocation}
  14. prometheuseMetrics *PrometheusMetrics
  15. mutex sync.RWMutex
  16. )
  17. func GetPrometheusMetrics() *PrometheusMetrics {
  18. mutex.Lock()
  19. if prometheuseMetrics == nil {
  20. prometheuseMetrics = newPrometheusMetrics()
  21. }
  22. mutex.Unlock()
  23. return prometheuseMetrics
  24. }
  25. type MetricGroup struct {
  26. TotalRecordsIn *prometheus.CounterVec
  27. TotalRecordsOut *prometheus.CounterVec
  28. TotalExceptions *prometheus.CounterVec
  29. ProcessLatency *prometheus.GaugeVec
  30. BufferLength *prometheus.GaugeVec
  31. }
  32. type PrometheusMetrics struct {
  33. vecs []*MetricGroup
  34. }
  35. func newPrometheusMetrics() *PrometheusMetrics {
  36. var (
  37. labelNames = []string{"rule", "type", "op", "instance"}
  38. prefixes = []string{"kuiper_source", "kuiper_op", "kuiper_sink"}
  39. )
  40. var vecs []*MetricGroup
  41. for _, prefix := range prefixes {
  42. //prometheus initialization
  43. totalRecordsIn := prometheus.NewCounterVec(prometheus.CounterOpts{
  44. Name: prefix + "_" + RecordsInTotal,
  45. Help: "Total number of messages received by the operation of " + prefix,
  46. }, labelNames)
  47. totalRecordsOut := prometheus.NewCounterVec(prometheus.CounterOpts{
  48. Name: prefix + "_" + RecordsOutTotal,
  49. Help: "Total number of messages published by the operation of " + prefix,
  50. }, labelNames)
  51. totalExceptions := prometheus.NewCounterVec(prometheus.CounterOpts{
  52. Name: prefix + "_" + ExceptionsTotal,
  53. Help: "Total number of user exceptions of " + prefix,
  54. }, labelNames)
  55. processLatency := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  56. Name: prefix + "_" + ProcessLatencyMs,
  57. Help: "Process latency in millisecond of " + prefix,
  58. }, labelNames)
  59. bufferLength := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  60. Name: prefix + "_" + BufferLength,
  61. Help: "The length of the plan buffer which is shared by all instances of " + prefix,
  62. }, labelNames)
  63. prometheus.MustRegister(totalRecordsIn, totalRecordsOut, totalExceptions, processLatency, bufferLength)
  64. vecs = append(vecs, &MetricGroup{
  65. TotalRecordsIn: totalRecordsIn,
  66. TotalRecordsOut: totalRecordsOut,
  67. TotalExceptions: totalExceptions,
  68. ProcessLatency: processLatency,
  69. BufferLength: bufferLength,
  70. })
  71. }
  72. return &PrometheusMetrics{vecs: vecs}
  73. }
  74. func (m *PrometheusMetrics) GetMetricsGroup(opType string) *MetricGroup {
  75. switch opType {
  76. case "source":
  77. return m.vecs[0]
  78. case "op":
  79. return m.vecs[1]
  80. case "sink":
  81. return m.vecs[2]
  82. }
  83. return nil
  84. }