stats_prom.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2022 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 node
  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. psm.pTotalRecordsIn = mg.TotalRecordsIn.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  35. psm.pTotalRecordsOut = mg.TotalRecordsOut.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  36. psm.pTotalExceptions = mg.TotalExceptions.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  37. psm.pProcessLatency = mg.ProcessLatency.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  38. psm.pBufferLength = mg.BufferLength.WithLabelValues(ctx.GetRuleId(), dsm.opType, dsm.opId, strInId)
  39. sm = psm
  40. } else {
  41. sm = &dsm
  42. }
  43. return sm, nil
  44. }
  45. type PrometheusStatManager struct {
  46. DefaultStatManager
  47. //prometheus metrics
  48. pTotalRecordsIn prometheus.Counter
  49. pTotalRecordsOut prometheus.Counter
  50. pTotalExceptions prometheus.Counter
  51. pProcessLatency prometheus.Gauge
  52. pBufferLength prometheus.Gauge
  53. }
  54. func (sm *PrometheusStatManager) IncTotalRecordsIn() {
  55. sm.totalRecordsIn++
  56. sm.pTotalRecordsIn.Inc()
  57. }
  58. func (sm *PrometheusStatManager) IncTotalRecordsOut() {
  59. sm.totalRecordsOut++
  60. sm.pTotalRecordsOut.Inc()
  61. }
  62. func (sm *PrometheusStatManager) IncTotalExceptions() {
  63. sm.totalExceptions++
  64. sm.pTotalExceptions.Inc()
  65. var t time.Time
  66. sm.processTimeStart = t
  67. }
  68. func (sm *PrometheusStatManager) ProcessTimeEnd() {
  69. if !sm.processTimeStart.IsZero() {
  70. sm.processLatency = int64(time.Since(sm.processTimeStart) / time.Microsecond)
  71. sm.pProcessLatency.Set(float64(sm.processLatency))
  72. }
  73. }
  74. func (sm *PrometheusStatManager) SetBufferLength(l int64) {
  75. sm.bufferLength = l
  76. sm.pBufferLength.Set(float64(l))
  77. }