stats_prom.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 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. 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(err string) {
  63. sm.pTotalExceptions.Inc()
  64. sm.DefaultStatManager.IncTotalExceptions(err)
  65. }
  66. func (sm *PrometheusStatManager) ProcessTimeEnd() {
  67. if !sm.processTimeStart.IsZero() {
  68. sm.processLatency = int64(time.Since(sm.processTimeStart) / time.Microsecond)
  69. sm.pProcessLatency.Set(float64(sm.processLatency))
  70. }
  71. }
  72. func (sm *PrometheusStatManager) SetBufferLength(l int64) {
  73. sm.bufferLength = l
  74. sm.pBufferLength.Set(float64(l))
  75. }