stats_manager.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2021 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. package node
  15. import (
  16. "fmt"
  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. type StatManager interface {
  24. IncTotalRecordsIn()
  25. IncTotalRecordsOut()
  26. IncTotalExceptions()
  27. ProcessTimeStart()
  28. ProcessTimeEnd()
  29. SetBufferLength(l int64)
  30. GetMetrics() []interface{}
  31. }
  32. //The statManager is not thread safe. Make sure it is used in only one instance
  33. type DefaultStatManager struct {
  34. //metrics
  35. totalRecordsIn int64
  36. totalRecordsOut int64
  37. totalExceptions int64
  38. processLatency int64
  39. lastInvocation time.Time
  40. bufferLength int64
  41. //configs
  42. opType string //"source", "op", "sink"
  43. prefix string
  44. processTimeStart time.Time
  45. opId string
  46. instanceId int
  47. }
  48. type PrometheusStatManager struct {
  49. DefaultStatManager
  50. //prometheus metrics
  51. pTotalRecordsIn prometheus.Counter
  52. pTotalRecordsOut prometheus.Counter
  53. pTotalExceptions prometheus.Counter
  54. pProcessLatency prometheus.Gauge
  55. pBufferLength prometheus.Gauge
  56. }
  57. func NewStatManager(opType string, ctx api.StreamContext) (StatManager, error) {
  58. var prefix string
  59. switch opType {
  60. case "source":
  61. prefix = "source_"
  62. case "op":
  63. prefix = "op_"
  64. case "sink":
  65. prefix = "sink_"
  66. default:
  67. return nil, fmt.Errorf("invalid opType %s, must be \"source\", \"sink\" or \"op\"", opType)
  68. }
  69. var sm StatManager
  70. if conf.Config != nil && conf.Config.Basic.Prometheus {
  71. ctx.GetLogger().Debugf("Create prometheus stat manager")
  72. psm := &PrometheusStatManager{
  73. DefaultStatManager: DefaultStatManager{
  74. opType: opType,
  75. prefix: prefix,
  76. opId: ctx.GetOpId(),
  77. instanceId: ctx.GetInstanceId(),
  78. },
  79. }
  80. //assign prometheus
  81. mg := GetPrometheusMetrics().GetMetricsGroup(opType)
  82. strInId := strconv.Itoa(ctx.GetInstanceId())
  83. psm.pTotalRecordsIn = mg.TotalRecordsIn.WithLabelValues(ctx.GetRuleId(), opType, ctx.GetOpId(), strInId)
  84. psm.pTotalRecordsOut = mg.TotalRecordsOut.WithLabelValues(ctx.GetRuleId(), opType, ctx.GetOpId(), strInId)
  85. psm.pTotalExceptions = mg.TotalExceptions.WithLabelValues(ctx.GetRuleId(), opType, ctx.GetOpId(), strInId)
  86. psm.pProcessLatency = mg.ProcessLatency.WithLabelValues(ctx.GetRuleId(), opType, ctx.GetOpId(), strInId)
  87. psm.pBufferLength = mg.BufferLength.WithLabelValues(ctx.GetRuleId(), opType, ctx.GetOpId(), strInId)
  88. sm = psm
  89. } else {
  90. sm = &DefaultStatManager{
  91. opType: opType,
  92. prefix: prefix,
  93. opId: ctx.GetOpId(),
  94. instanceId: ctx.GetInstanceId(),
  95. }
  96. }
  97. return sm, nil
  98. }
  99. func (sm *DefaultStatManager) IncTotalRecordsIn() {
  100. sm.totalRecordsIn++
  101. }
  102. func (sm *DefaultStatManager) IncTotalRecordsOut() {
  103. sm.totalRecordsOut++
  104. }
  105. func (sm *DefaultStatManager) IncTotalExceptions() {
  106. sm.totalExceptions++
  107. var t time.Time
  108. sm.processTimeStart = t
  109. }
  110. func (sm *DefaultStatManager) ProcessTimeStart() {
  111. sm.lastInvocation = time.Now()
  112. sm.processTimeStart = sm.lastInvocation
  113. }
  114. func (sm *DefaultStatManager) ProcessTimeEnd() {
  115. if !sm.processTimeStart.IsZero() {
  116. sm.processLatency = int64(time.Since(sm.processTimeStart) / time.Microsecond)
  117. }
  118. }
  119. func (sm *DefaultStatManager) SetBufferLength(l int64) {
  120. sm.bufferLength = l
  121. }
  122. func (sm *PrometheusStatManager) IncTotalRecordsIn() {
  123. sm.totalRecordsIn++
  124. sm.pTotalRecordsIn.Inc()
  125. }
  126. func (sm *PrometheusStatManager) IncTotalRecordsOut() {
  127. sm.totalRecordsOut++
  128. sm.pTotalRecordsOut.Inc()
  129. }
  130. func (sm *PrometheusStatManager) IncTotalExceptions() {
  131. sm.totalExceptions++
  132. sm.pTotalExceptions.Inc()
  133. var t time.Time
  134. sm.processTimeStart = t
  135. }
  136. func (sm *PrometheusStatManager) ProcessTimeEnd() {
  137. if !sm.processTimeStart.IsZero() {
  138. sm.processLatency = int64(time.Since(sm.processTimeStart) / time.Microsecond)
  139. sm.pProcessLatency.Set(float64(sm.processLatency))
  140. }
  141. }
  142. func (sm *PrometheusStatManager) SetBufferLength(l int64) {
  143. sm.bufferLength = l
  144. sm.pBufferLength.Set(float64(l))
  145. }
  146. func (sm *DefaultStatManager) GetMetrics() []interface{} {
  147. result := []interface{}{
  148. sm.totalRecordsIn, sm.totalRecordsOut, sm.totalExceptions, sm.processLatency, sm.bufferLength,
  149. }
  150. if !sm.lastInvocation.IsZero() {
  151. result = append(result, sm.lastInvocation.Format("2006-01-02T15:04:05.999999"))
  152. } else {
  153. result = append(result, 0)
  154. }
  155. return result
  156. }