stats_prom.go 4.0 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. "strconv"
  18. "time"
  19. "github.com/prometheus/client_golang/prometheus"
  20. "github.com/lf-edge/ekuiper/internal/conf"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. )
  23. func getStatManager(ctx api.StreamContext, dsm DefaultStatManager) (StatManager, error) {
  24. ctx.GetLogger().Debugf("Create prometheus stat manager")
  25. var sm StatManager
  26. if conf.Config != nil && conf.Config.Basic.Prometheus {
  27. psm := &PrometheusStatManager{
  28. DefaultStatManager: dsm,
  29. }
  30. // assign prometheus
  31. mg := GetPrometheusMetrics().GetMetricsGroup(dsm.opType)
  32. strInId := strconv.Itoa(dsm.instanceId)
  33. mg.TotalRecordsIn.DeleteLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  34. mg.TotalRecordsOut.DeleteLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  35. mg.TotalExceptions.DeleteLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  36. mg.ProcessLatency.DeleteLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  37. mg.ProcessLatencyHist.DeleteLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  38. mg.BufferLength.DeleteLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  39. psm.pTotalRecordsIn = mg.TotalRecordsIn.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  40. psm.pTotalRecordsOut = mg.TotalRecordsOut.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  41. psm.pTotalExceptions = mg.TotalExceptions.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  42. psm.pProcessLatency = mg.ProcessLatency.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  43. psm.pProcessLatencyHist = mg.ProcessLatencyHist.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  44. psm.pBufferLength = mg.BufferLength.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  45. sm = psm
  46. } else {
  47. sm = &dsm
  48. }
  49. return sm, nil
  50. }
  51. type PrometheusStatManager struct {
  52. DefaultStatManager
  53. // prometheus metrics
  54. pTotalRecordsIn prometheus.Counter
  55. pTotalRecordsOut prometheus.Counter
  56. pTotalExceptions prometheus.Counter
  57. pProcessLatency prometheus.Gauge
  58. pProcessLatencyHist prometheus.Observer
  59. pBufferLength prometheus.Gauge
  60. }
  61. func (sm *PrometheusStatManager) IncTotalRecordsIn() {
  62. sm.totalRecordsIn++
  63. sm.pTotalRecordsIn.Inc()
  64. }
  65. func (sm *PrometheusStatManager) IncTotalRecordsOut() {
  66. sm.totalRecordsOut++
  67. sm.pTotalRecordsOut.Inc()
  68. }
  69. func (sm *PrometheusStatManager) IncTotalExceptions(err string) {
  70. sm.pTotalExceptions.Inc()
  71. sm.DefaultStatManager.IncTotalExceptions(err)
  72. }
  73. func (sm *PrometheusStatManager) ProcessTimeEnd() {
  74. if !sm.processTimeStart.IsZero() {
  75. sm.processLatency = int64(time.Since(sm.processTimeStart) / time.Microsecond)
  76. sm.pProcessLatency.Set(float64(sm.processLatency))
  77. sm.pProcessLatencyHist.Observe(float64(sm.processLatency))
  78. }
  79. }
  80. func (sm *PrometheusStatManager) SetBufferLength(l int64) {
  81. sm.bufferLength = l
  82. sm.pBufferLength.Set(float64(l))
  83. }
  84. func (sm *PrometheusStatManager) Clean(ruleId string) {
  85. if conf.Config != nil && conf.Config.Basic.Prometheus {
  86. mg := GetPrometheusMetrics().GetMetricsGroup(sm.opType)
  87. strInId := strconv.Itoa(sm.instanceId)
  88. mg.TotalRecordsIn.DeleteLabelValues(ruleId, sm.opType, sm.opId, strInId)
  89. mg.TotalRecordsOut.DeleteLabelValues(ruleId, sm.opType, sm.opId, strInId)
  90. mg.TotalExceptions.DeleteLabelValues(ruleId, sm.opType, sm.opId, strInId)
  91. mg.ProcessLatency.DeleteLabelValues(ruleId, sm.opType, sm.opId, strInId)
  92. mg.BufferLength.DeleteLabelValues(ruleId, sm.opType, sm.opId, strInId)
  93. }
  94. }