stats_prom.go 3.2 KB

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