|
| 1 | +package dogstatsd |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync/atomic" |
| 5 | + |
| 6 | + "github.com/DataDog/datadog-go/v5/statsd" |
| 7 | + |
| 8 | + "github.com/vmware/vmware-go-kcl-v2/logger" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + // Labels attached to metric submissions |
| 13 | + streamNameLabel = "kinesis.stream" |
| 14 | + shardIdLabel = "kinesis.shardId" |
| 15 | + workerIdLabel = "kcl.workerId" |
| 16 | + applicationNameLabel = "kcl.application-name" |
| 17 | + |
| 18 | + // Names for the metrics submitted below |
| 19 | + recordsProcessedMetric = "kcl.records_processed" |
| 20 | + bytesProcessedMetric = "kcl.bytes_processed" |
| 21 | + millisBehindLatestMetric = "kcl.millis_behind_latest" |
| 22 | + shardLeasesOwnedMetric = "kcl.shard_leases_owned" |
| 23 | + getRecordsTimeMetric = "kcl.get_records_time" |
| 24 | + processRecordsTimeMetric = "kcl.process_records_time" |
| 25 | +) |
| 26 | + |
| 27 | +// MonitoringService publishes KCL metrics to Datadog using the dogstatsd client |
| 28 | +// package. |
| 29 | +type MonitoringService struct { |
| 30 | + Client statsd.ClientInterface // Client used to push metrics |
| 31 | + SamplingRate float64 // Sampling rate for all metrics |
| 32 | + Logger logger.Logger // Logger used for metric push errors |
| 33 | + |
| 34 | + leaseCount atomic.Int64 // Number of leases current held |
| 35 | + tags []string // List of tags to send with every metric |
| 36 | +} |
| 37 | + |
| 38 | +// New creates a new dogstatsd monitoring service. The given sampling rate will |
| 39 | +// be used for all submitted metrics. The logger is only used if submissions |
| 40 | +// fail. |
| 41 | +func New(client statsd.ClientInterface, samplingRate float64, logger logger.Logger) *MonitoringService { |
| 42 | + return &MonitoringService{ |
| 43 | + Client: client, |
| 44 | + SamplingRate: samplingRate, |
| 45 | + Logger: logger, |
| 46 | + leaseCount: atomic.Int64{}, |
| 47 | + tags: []string{}, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (s *MonitoringService) Init(appName, streamName, workerID string) (err error) { |
| 52 | + s.tags = []string{ |
| 53 | + applicationNameLabel + ":" + appName, |
| 54 | + streamNameLabel + ":" + streamName, |
| 55 | + workerIdLabel + ":" + workerID, |
| 56 | + } |
| 57 | + return err |
| 58 | +} |
| 59 | + |
| 60 | +func (s *MonitoringService) Start() error { |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func (s *MonitoringService) Shutdown() {} |
| 65 | + |
| 66 | +// If the error is non-nil, log it. This should be inlined by the |
| 67 | +// compiler so no overhead, and simplifies the metrics methods below, |
| 68 | +// since the log entry is essentially always the same. |
| 69 | +func (s *MonitoringService) logFailure(shard string, metric string, err error) { |
| 70 | + if err != nil { |
| 71 | + s.Logger.WithFields(logger.Fields{ |
| 72 | + "error": err, |
| 73 | + "shardId": shard, |
| 74 | + "metric": metric, |
| 75 | + }).Errorf("failed to push metric") |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Add the tags for a specific metric to the global monitoring service tags |
| 80 | +func (s *MonitoringService) buildTags(tags ...string) []string { |
| 81 | + return append(tags, s.tags...) |
| 82 | +} |
| 83 | + |
| 84 | +func (s *MonitoringService) IncrRecordsProcessed(shard string, count int) { |
| 85 | + err := s.Client.Count( |
| 86 | + recordsProcessedMetric, |
| 87 | + int64(count), |
| 88 | + s.buildTags(shardIdLabel+":"+shard), |
| 89 | + s.SamplingRate, |
| 90 | + ) |
| 91 | + s.logFailure(shard, recordsProcessedMetric, err) |
| 92 | +} |
| 93 | + |
| 94 | +func (s *MonitoringService) IncrBytesProcessed(shard string, count int64) { |
| 95 | + err := s.Client.Count( |
| 96 | + bytesProcessedMetric, |
| 97 | + count, |
| 98 | + s.buildTags(shardIdLabel+":"+shard), |
| 99 | + s.SamplingRate, |
| 100 | + ) |
| 101 | + s.logFailure(shard, recordsProcessedMetric, err) |
| 102 | +} |
| 103 | + |
| 104 | +func (s *MonitoringService) MillisBehindLatest(shard string, millis float64) { |
| 105 | + err := s.Client.Gauge( |
| 106 | + millisBehindLatestMetric, |
| 107 | + millis, |
| 108 | + s.buildTags(shardIdLabel+":"+shard), |
| 109 | + s.SamplingRate, |
| 110 | + ) |
| 111 | + s.logFailure(shard, millisBehindLatestMetric, err) |
| 112 | +} |
| 113 | + |
| 114 | +func (s *MonitoringService) DeleteMetricMillisBehindLatest(shard string) { |
| 115 | + s.MillisBehindLatest(shard, 0) |
| 116 | +} |
| 117 | + |
| 118 | +func (s *MonitoringService) LeaseGained(shard string) { |
| 119 | + leaseCount := s.leaseCount.Add(1) |
| 120 | + err := s.Client.Gauge( |
| 121 | + shardLeasesOwnedMetric, |
| 122 | + float64(leaseCount), |
| 123 | + s.buildTags(shardIdLabel+":"+shard), |
| 124 | + s.SamplingRate, |
| 125 | + ) |
| 126 | + s.logFailure(shard, shardLeasesOwnedMetric, err) |
| 127 | +} |
| 128 | + |
| 129 | +func (s *MonitoringService) LeaseLost(shard string) { |
| 130 | + leaseCount := s.leaseCount.Add(-1) |
| 131 | + err := s.Client.Gauge( |
| 132 | + shardLeasesOwnedMetric, |
| 133 | + float64(leaseCount), |
| 134 | + s.buildTags(shardIdLabel+":"+shard), |
| 135 | + s.SamplingRate, |
| 136 | + ) |
| 137 | + s.logFailure(shard, shardLeasesOwnedMetric, err) |
| 138 | +} |
| 139 | + |
| 140 | +func (s *MonitoringService) LeaseRenewed(shard string) { |
| 141 | + leaseCount := s.leaseCount.Load() |
| 142 | + err := s.Client.Gauge( |
| 143 | + shardLeasesOwnedMetric, |
| 144 | + float64(leaseCount), |
| 145 | + s.buildTags(shardIdLabel+":"+shard), |
| 146 | + s.SamplingRate, |
| 147 | + ) |
| 148 | + s.logFailure(shard, shardLeasesOwnedMetric, err) |
| 149 | +} |
| 150 | + |
| 151 | +func (s *MonitoringService) RecordGetRecordsTime(shard string, time float64) { |
| 152 | + err := s.Client.Count( |
| 153 | + getRecordsTimeMetric, |
| 154 | + int64(time), |
| 155 | + s.buildTags(shardIdLabel+":"+shard), |
| 156 | + s.SamplingRate, |
| 157 | + ) |
| 158 | + s.logFailure(shard, getRecordsTimeMetric, err) |
| 159 | +} |
| 160 | + |
| 161 | +func (s *MonitoringService) RecordProcessRecordsTime(shard string, time float64) { |
| 162 | + err := s.Client.Count( |
| 163 | + processRecordsTimeMetric, |
| 164 | + int64(time), |
| 165 | + s.buildTags(shardIdLabel+":"+shard), |
| 166 | + s.SamplingRate, |
| 167 | + ) |
| 168 | + s.logFailure(shard, processRecordsTimeMetric, err) |
| 169 | +} |
0 commit comments