@@ -89,6 +89,7 @@ public class Telemetry: Subscriber {
8989 private var seenErrors = [ String: Int] ( )
9090 internal var started = false
9191 private var rateLimitEndTime : TimeInterval = 0
92+ internal var flushFirstError = true
9293 private var telemetryQueue = DispatchQueue ( label: " telemetryQueue " )
9394 private var updateQueue = DispatchQueue ( label: " updateQueue " )
9495 private var telemetryTimer : QueueTimer ?
@@ -98,15 +99,10 @@ public class Telemetry: Subscriber {
9899 guard enable, !started, sampleRate > 0.0 && sampleRate <= 1.0 else { return }
99100 started = true
100101
102+ // Queue contents were sampled at the default 100%
103+ // the values on flush will be adjusted in the send function
101104 if Double . random ( in: 0 ... 1 ) > sampleRate {
102105 resetQueue ( )
103- } else {
104- telemetryQueue. async {
105- self . queue = self . queue. map { var metric = $0
106- metric. value = Int ( Double ( metric. value) / self . sampleRate)
107- return metric
108- }
109- }
110106 }
111107
112108 self . telemetryTimer = QueueTimer ( interval: . seconds( self . flushTimer) , queue: . main) { [ weak self] in
@@ -133,13 +129,12 @@ public class Telemetry: Subscriber {
133129 /// - buildTags: A closure to build the tags dictionary.
134130 func increment( metric: String , buildTags: ( inout [ String : String ] ) -> Void ) {
135131 guard enable, sampleRate > 0.0 && sampleRate <= 1.0 , metric. hasPrefix ( Telemetry . METRICS_BASE_TAG) , queueHasSpace ( ) else { return }
132+ if Double . random ( in: 0 ... 1 ) > sampleRate { return }
136133
137134 var tags = [ String: String] ( )
138135 buildTags ( & tags)
139136 guard !tags. isEmpty else { return }
140137
141- if Double . random ( in: 0 ... 1 ) > sampleRate { return }
142-
143138 addRemoteMetric ( metric: metric, tags: tags)
144139 }
145140
@@ -150,6 +145,7 @@ public class Telemetry: Subscriber {
150145 /// - buildTags: A closure to build the tags dictionary.
151146 func error( metric: String , log: String , buildTags: ( inout [ String : String ] ) -> Void ) {
152147 guard enable, sampleRate > 0.0 && sampleRate <= 1.0 , metric. hasPrefix ( Telemetry . METRICS_BASE_TAG) , queueHasSpace ( ) else { return }
148+ if Double . random ( in: 0 ... 1 ) > sampleRate { return }
153149
154150 var tags = [ String: String] ( )
155151 buildTags ( & tags)
@@ -165,19 +161,11 @@ public class Telemetry: Subscriber {
165161 logData = String ( log. prefix ( errorLogSizeMax) )
166162 }
167163
168- if let errorKey = tags [ " error " ] {
169- if let count = seenErrors [ errorKey] {
170- seenErrors [ errorKey] = count + 1
171- if Double . random ( in: 0 ... 1 ) > sampleRate { return }
172- addRemoteMetric ( metric: metric, tags: filteredTags, value: Int ( Double ( count) * sampleRate) , log: logData)
173- seenErrors [ errorKey] = 0
174- } else {
175- addRemoteMetric ( metric: metric, tags: filteredTags, log: logData)
176- flush ( )
177- seenErrors [ errorKey] = 0
178- }
179- } else {
180- addRemoteMetric ( metric: metric, tags: filteredTags, log: logData)
164+ addRemoteMetric ( metric: metric, tags: filteredTags, log: logData)
165+
166+ if ( flushFirstError) {
167+ flushFirstError = false
168+ flush ( )
181169 }
182170 }
183171
0 commit comments