stats_manager.go 3.6 KB

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