@@ -89,6 +89,7 @@ public class Telemetry: Subscriber {
89
89
private var seenErrors = [ String: Int] ( )
90
90
internal var started = false
91
91
private var rateLimitEndTime : TimeInterval = 0
92
+ internal var flushFirstError = true
92
93
private var telemetryQueue = DispatchQueue ( label: " telemetryQueue " )
93
94
private var updateQueue = DispatchQueue ( label: " updateQueue " )
94
95
private var telemetryTimer : QueueTimer ?
@@ -98,15 +99,10 @@ public class Telemetry: Subscriber {
98
99
guard enable, !started, sampleRate > 0.0 && sampleRate <= 1.0 else { return }
99
100
started = true
100
101
102
+ // Queue contents were sampled at the default 100%
103
+ // the values on flush will be adjusted in the send function
101
104
if Double . random ( in: 0 ... 1 ) > sampleRate {
102
105
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
- }
110
106
}
111
107
112
108
self . telemetryTimer = QueueTimer ( interval: . seconds( self . flushTimer) , queue: . main) { [ weak self] in
@@ -133,13 +129,12 @@ public class Telemetry: Subscriber {
133
129
/// - buildTags: A closure to build the tags dictionary.
134
130
func increment( metric: String , buildTags: ( inout [ String : String ] ) -> Void ) {
135
131
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 }
136
133
137
134
var tags = [ String: String] ( )
138
135
buildTags ( & tags)
139
136
guard !tags. isEmpty else { return }
140
137
141
- if Double . random ( in: 0 ... 1 ) > sampleRate { return }
142
-
143
138
addRemoteMetric ( metric: metric, tags: tags)
144
139
}
145
140
@@ -150,6 +145,7 @@ public class Telemetry: Subscriber {
150
145
/// - buildTags: A closure to build the tags dictionary.
151
146
func error( metric: String , log: String , buildTags: ( inout [ String : String ] ) -> Void ) {
152
147
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 }
153
149
154
150
var tags = [ String: String] ( )
155
151
buildTags ( & tags)
@@ -165,19 +161,11 @@ public class Telemetry: Subscriber {
165
161
logData = String ( log. prefix ( errorLogSizeMax) )
166
162
}
167
163
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 ( )
181
169
}
182
170
}
183
171
0 commit comments