stats_prom.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/lf-edge/ekuiper/internal/conf"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "github.com/prometheus/client_golang/prometheus"
  20. "strconv"
  21. "time"
  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.BufferLength.DeleteLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  38. psm.pTotalRecordsIn = mg.TotalRecordsIn.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  39. psm.pTotalRecordsOut = mg.TotalRecordsOut.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  40. psm.pTotalExceptions = mg.TotalExceptions.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  41. psm.pProcessLatency = mg.ProcessLatency.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  42. psm.pBufferLength = mg.BufferLength.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  43. sm = psm
  44. } else {
  45. sm = &dsm
  46. }
  47. return sm, nil
  48. }
  49. type PrometheusStatManager struct {
  50. DefaultStatManager
  51. //prometheus metrics
  52. pTotalRecordsIn prometheus.Counter
  53. pTotalRecordsOut prometheus.Counter
  54. pTotalExceptions prometheus.Counter
  55. pProcessLatency prometheus.Gauge
  56. pBufferLength prometheus.Gauge
  57. }
  58. func (sm *PrometheusStatManager) IncTotalRecordsIn() {
  59. sm.totalRecordsIn++
  60. sm.pTotalRecordsIn.Inc()
  61. }
  62. func (sm *PrometheusStatManager) IncTotalRecordsOut() {
  63. sm.totalRecordsOut++
  64. sm.pTotalRecordsOut.Inc()
  65. }
  66. func (sm *PrometheusStatManager) IncTotalExceptions(err string) {
  67. sm.pTotalExceptions.Inc()
  68. sm.DefaultStatManager.IncTotalExceptions(err)
  69. }
  70. func (sm *PrometheusStatManager) ProcessTimeEnd() {
  71. if !sm.processTimeStart.IsZero() {
  72. sm.processLatency = int64(time.Since(sm.processTimeStart) / time.Microsecond)
  73. sm.pProcessLatency.Set(float64(sm.processLatency))
  74. }
  75. }
  76. func (sm *PrometheusStatManager) SetBufferLength(l int64) {
  77. sm.bufferLength = l
  78. sm.pBufferLength.Set(float64(l))
  79. }
  80. func (sm *PrometheusStatManager) Clean(ruleId string) {
  81. if conf.Config != nil && conf.Config.Basic.Prometheus {
  82. mg := GetPrometheusMetrics().GetMetricsGroup(sm.opType)
  83. strInId := strconv.Itoa(sm.instanceId)
  84. mg.TotalRecordsIn.DeleteLabelValues(ruleId, sm.opType, sm.opId, strInId)
  85. mg.TotalRecordsOut.DeleteLabelValues(ruleId, sm.opType, sm.opId, strInId)
  86. mg.TotalExceptions.DeleteLabelValues(ruleId, sm.opType, sm.opId, strInId)
  87. mg.ProcessLatency.DeleteLabelValues(ruleId, sm.opType, sm.opId, strInId)
  88. mg.BufferLength.DeleteLabelValues(ruleId, sm.opType, sm.opId, strInId)
  89. }
  90. }