metrics.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package zookeeperreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zookeeperreceiver"
  4. import (
  5. "fmt"
  6. "go.opentelemetry.io/collector/pdata/pcommon"
  7. "go.uber.org/zap"
  8. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zookeeperreceiver/internal/metadata"
  9. )
  10. // Constants to define entries in the output of "mntr" command.
  11. const (
  12. avgLatencyMetricKey = "zk_avg_latency"
  13. maxLatencyMetricKey = "zk_max_latency"
  14. minLatencyMetricKey = "zk_min_latency"
  15. packetsReceivedMetricKey = "zk_packets_received"
  16. packetsSentMetricKey = "zk_packets_sent"
  17. numAliveConnectionsMetricKey = "zk_num_alive_connections"
  18. outstandingRequestsMetricKey = "zk_outstanding_requests"
  19. zNodeCountMetricKey = "zk_znode_count"
  20. watchCountMetricKey = "zk_watch_count"
  21. ephemeralsCountMetricKey = "zk_ephemerals_count"
  22. approximateDataSizeMetricKey = "zk_approximate_data_size"
  23. openFileDescriptorCountMetricKey = "zk_open_file_descriptor_count"
  24. maxFileDescriptorCountMetricKey = "zk_max_file_descriptor_count"
  25. fSyncThresholdExceedCountMetricKey = "zk_fsync_threshold_exceed_count"
  26. followersMetricKey = "zk_followers"
  27. syncedFollowersMetricKey = "zk_synced_followers"
  28. pendingSyncsMetricKey = "zk_pending_syncs"
  29. serverStateKey = "zk_server_state"
  30. zkVersionKey = "zk_version"
  31. ruokKey = "ruok"
  32. )
  33. // metricCreator handles generation of metric and metric recording
  34. type metricCreator struct {
  35. computedMetricStore map[string]int64
  36. mb *metadata.MetricsBuilder
  37. // Temporary feature gates while transitioning to metrics without a direction attribute
  38. }
  39. func newMetricCreator(mb *metadata.MetricsBuilder) *metricCreator {
  40. return &metricCreator{
  41. computedMetricStore: make(map[string]int64),
  42. mb: mb,
  43. }
  44. }
  45. func (m *metricCreator) recordDataPointsFunc(metric string) func(ts pcommon.Timestamp, val int64) {
  46. switch metric {
  47. case followersMetricKey:
  48. return func(ts pcommon.Timestamp, val int64) {
  49. m.computedMetricStore[followersMetricKey] = val
  50. }
  51. case syncedFollowersMetricKey:
  52. return func(ts pcommon.Timestamp, val int64) {
  53. m.computedMetricStore[syncedFollowersMetricKey] = val
  54. m.mb.RecordZookeeperFollowerCountDataPoint(ts, val, metadata.AttributeStateSynced)
  55. }
  56. case pendingSyncsMetricKey:
  57. return m.mb.RecordZookeeperSyncPendingDataPoint
  58. case avgLatencyMetricKey:
  59. return m.mb.RecordZookeeperLatencyAvgDataPoint
  60. case maxLatencyMetricKey:
  61. return m.mb.RecordZookeeperLatencyMaxDataPoint
  62. case minLatencyMetricKey:
  63. return m.mb.RecordZookeeperLatencyMinDataPoint
  64. case numAliveConnectionsMetricKey:
  65. return m.mb.RecordZookeeperConnectionActiveDataPoint
  66. case outstandingRequestsMetricKey:
  67. return m.mb.RecordZookeeperRequestActiveDataPoint
  68. case zNodeCountMetricKey:
  69. return m.mb.RecordZookeeperZnodeCountDataPoint
  70. case watchCountMetricKey:
  71. return m.mb.RecordZookeeperWatchCountDataPoint
  72. case ephemeralsCountMetricKey:
  73. return m.mb.RecordZookeeperDataTreeEphemeralNodeCountDataPoint
  74. case approximateDataSizeMetricKey:
  75. return m.mb.RecordZookeeperDataTreeSizeDataPoint
  76. case openFileDescriptorCountMetricKey:
  77. return m.mb.RecordZookeeperFileDescriptorOpenDataPoint
  78. case maxFileDescriptorCountMetricKey:
  79. return m.mb.RecordZookeeperFileDescriptorLimitDataPoint
  80. case fSyncThresholdExceedCountMetricKey:
  81. return m.mb.RecordZookeeperFsyncExceededThresholdCountDataPoint
  82. case ruokKey:
  83. return m.mb.RecordZookeeperRuokDataPoint
  84. case packetsReceivedMetricKey:
  85. return func(ts pcommon.Timestamp, val int64) {
  86. m.mb.RecordZookeeperPacketCountDataPoint(ts, val, metadata.AttributeDirectionReceived)
  87. }
  88. case packetsSentMetricKey:
  89. return func(ts pcommon.Timestamp, val int64) {
  90. m.mb.RecordZookeeperPacketCountDataPoint(ts, val, metadata.AttributeDirectionSent)
  91. }
  92. }
  93. return nil
  94. }
  95. func (m *metricCreator) generateComputedMetrics(logger *zap.Logger, ts pcommon.Timestamp) {
  96. // not_synced Followers Count
  97. if err := m.computeNotSyncedFollowersMetric(ts); err != nil {
  98. logger.Debug("metric computation failed", zap.Error(err))
  99. }
  100. }
  101. func (m *metricCreator) computeNotSyncedFollowersMetric(ts pcommon.Timestamp) error {
  102. followersTotal, ok := m.computedMetricStore[followersMetricKey]
  103. if !ok {
  104. return fmt.Errorf("could not compute not_synced follower.count, missing %s", followersMetricKey)
  105. }
  106. syncedFollowers, ok := m.computedMetricStore[syncedFollowersMetricKey]
  107. if !ok {
  108. return fmt.Errorf("could not compute not_synced follower.count, missing %s", syncedFollowersMetricKey)
  109. }
  110. val := followersTotal - syncedFollowers
  111. m.mb.RecordZookeeperFollowerCountDataPoint(ts, val, metadata.AttributeStateUnsynced)
  112. return nil
  113. }