stats_manager.go 3.5 KB

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