stats_manager.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. package metric
  15. import (
  16. "fmt"
  17. "time"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. )
  20. const RecordsInTotal = "records_in_total"
  21. const RecordsOutTotal = "records_out_total"
  22. const ProcessLatencyUs = "process_latency_us"
  23. const LastInvocation = "last_invocation"
  24. const BufferLength = "buffer_length"
  25. const ExceptionsTotal = "exceptions_total"
  26. const LastException = "last_exception"
  27. const LastExceptionTime = "last_exception_time"
  28. var MetricNames = []string{RecordsInTotal, RecordsOutTotal, ProcessLatencyUs, BufferLength, LastInvocation, ExceptionsTotal, LastException, LastExceptionTime}
  29. type StatManager interface {
  30. IncTotalRecordsIn()
  31. IncTotalRecordsOut()
  32. IncTotalExceptions(err string)
  33. ProcessTimeStart()
  34. ProcessTimeEnd()
  35. SetBufferLength(l int64)
  36. SetProcessTimeStart(t time.Time)
  37. GetMetrics() []interface{}
  38. // Clean remove all metrics history
  39. Clean(ruleId string)
  40. }
  41. // DefaultStatManager The statManager is not thread safe. Make sure it is used in only one instance
  42. type DefaultStatManager struct {
  43. //metrics
  44. totalRecordsIn int64
  45. totalRecordsOut int64
  46. processLatency int64
  47. lastInvocation time.Time
  48. bufferLength int64
  49. totalExceptions int64
  50. lastException string
  51. lastExceptionTime time.Time
  52. //configs
  53. opType string //"source", "op", "sink"
  54. prefix string
  55. processTimeStart time.Time
  56. opId string
  57. instanceId int
  58. }
  59. func NewStatManager(ctx api.StreamContext, opType string) (StatManager, error) {
  60. var prefix string
  61. switch opType {
  62. case "source":
  63. prefix = "source_"
  64. case "op":
  65. prefix = "op_"
  66. case "sink":
  67. prefix = "sink_"
  68. default:
  69. return nil, fmt.Errorf("invalid opType %s, must be \"source\", \"sink\" or \"op\"", opType)
  70. }
  71. ds := DefaultStatManager{
  72. opType: opType,
  73. prefix: prefix,
  74. opId: ctx.GetOpId(),
  75. instanceId: ctx.GetInstanceId(),
  76. }
  77. return getStatManager(ctx, ds)
  78. }
  79. func (sm *DefaultStatManager) IncTotalRecordsIn() {
  80. sm.totalRecordsIn++
  81. }
  82. func (sm *DefaultStatManager) IncTotalRecordsOut() {
  83. sm.totalRecordsOut++
  84. }
  85. func (sm *DefaultStatManager) IncTotalExceptions(err string) {
  86. sm.totalExceptions++
  87. var t time.Time
  88. sm.processTimeStart = t
  89. sm.lastException = err
  90. sm.lastExceptionTime = time.Now()
  91. }
  92. func (sm *DefaultStatManager) ProcessTimeStart() {
  93. sm.lastInvocation = time.Now()
  94. sm.processTimeStart = sm.lastInvocation
  95. }
  96. func (sm *DefaultStatManager) ProcessTimeEnd() {
  97. if !sm.processTimeStart.IsZero() {
  98. sm.processLatency = int64(time.Since(sm.processTimeStart) / time.Microsecond)
  99. }
  100. }
  101. func (sm *DefaultStatManager) SetBufferLength(l int64) {
  102. sm.bufferLength = l
  103. }
  104. func (sm *DefaultStatManager) SetProcessTimeStart(t time.Time) {
  105. sm.processTimeStart = t
  106. sm.lastInvocation = t
  107. }
  108. func (sm *DefaultStatManager) GetMetrics() []interface{} {
  109. result := []interface{}{
  110. sm.totalRecordsIn,
  111. sm.totalRecordsOut,
  112. sm.processLatency,
  113. sm.bufferLength,
  114. 0,
  115. sm.totalExceptions,
  116. sm.lastException,
  117. 0,
  118. }
  119. if !sm.lastInvocation.IsZero() {
  120. result[4] = sm.lastInvocation.Format("2006-01-02T15:04:05.999999")
  121. }
  122. if !sm.lastExceptionTime.IsZero() {
  123. result[7] = sm.lastExceptionTime.Format("2006-01-02T15:04:05.999999")
  124. }
  125. return result
  126. }
  127. func (sm *DefaultStatManager) Clean(_ string) {
  128. // do nothing
  129. }