stats_manager.go 3.8 KB

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