@@ -40,6 +40,13 @@ fun logError(err: Throwable) {
40
40
Analytics .reportInternalError(err)
41
41
}
42
42
43
+ /* *
44
+ * A class for sending telemetry data to Segment.
45
+ * This system is used to gather usage and error data from the SDK for the purpose of improving the SDK.
46
+ * It can be disabled at any time by setting Telemetry.enable to false.
47
+ * Errors are sent with a write key, which can be disabled by setting Telemetry.sendWriteKeyOnError to false.
48
+ * All data is downsampled and no PII is collected.
49
+ */
43
50
object Telemetry: Subscriber {
44
51
private const val METRICS_BASE_TAG = " analytics_mobile"
45
52
// Metric class for Analytics SDK
@@ -51,7 +58,10 @@ object Telemetry: Subscriber {
51
58
// Metric class for Analytics SDK plugin errors
52
59
const val INTEGRATION_ERROR_METRIC = " $METRICS_BASE_TAG .integration.invoke.error"
53
60
54
- var enable: Boolean = false
61
+ /* *
62
+ * Enables or disables telemetry.
63
+ */
64
+ var enable: Boolean = true
55
65
set(value) {
56
66
field = value
57
67
if (value) {
@@ -62,10 +72,11 @@ object Telemetry: Subscriber {
62
72
start()
63
73
}
64
74
}
75
+
65
76
var host: String = Constants .DEFAULT_API_HOST
66
77
// 1.0 is 100%, will get set by Segment setting before start()
67
- var sampleRate: Double = 0 .0
68
- var flushTimer: Int = 30 * 1000 // 30s
78
+ var sampleRate: Double = 1 .0
79
+ var flushTimer: Int = 3 * 1000 // 30s
69
80
var httpClient: HTTPClient = HTTPClient (" " , MetricsRequestFactory ())
70
81
var sendWriteKeyOnError: Boolean = true
71
82
var sendErrorLogData: Boolean = false
@@ -96,6 +107,11 @@ object Telemetry: Subscriber {
96
107
private var telemetryScope: CoroutineScope = CoroutineScope (SupervisorJob () + exceptionHandler)
97
108
private var telemetryDispatcher: ExecutorCoroutineDispatcher = Executors .newSingleThreadExecutor().asCoroutineDispatcher()
98
109
private var telemetryJob: Job ? = null
110
+
111
+ /* *
112
+ * Starts the telemetry if it is enabled and not already started, and the sample rate is greater than 0.
113
+ * Called automatically when Telemetry.enable is set to true and when configuration data is received from Segment.
114
+ */
99
115
fun start () {
100
116
if (! enable || started || sampleRate == 0.0 ) return
101
117
started = true
@@ -126,15 +142,23 @@ object Telemetry: Subscriber {
126
142
}
127
143
}
128
144
129
- fun reset ()
130
- {
145
+ /* *
146
+ * Resets the telemetry by canceling the telemetry job, clearing the error queue, and resetting the state.
147
+ */
148
+ fun reset () {
131
149
telemetryJob?.cancel()
132
150
resetQueue()
133
151
seenErrors.clear()
134
152
started = false
135
153
rateLimitEndTime = 0
136
154
}
137
155
156
+ /* *
157
+ * Increments a metric with the specified tags.
158
+ *
159
+ * @param metric The name of the metric to increment.
160
+ * @param buildTags A lambda function to build the tags for the metric.
161
+ */
138
162
fun increment (metric : String , buildTags : (MutableMap <String , String >) -> Unit ) {
139
163
val tags = mutableMapOf<String , String >()
140
164
buildTags(tags)
@@ -148,6 +172,13 @@ object Telemetry: Subscriber {
148
172
addRemoteMetric(metric, tags)
149
173
}
150
174
175
+ /* *
176
+ * Logs an error metric with the specified tags and log data.
177
+ *
178
+ * @param metric The name of the error metric.
179
+ * @param log The log data associated with the error.
180
+ * @param buildTags A lambda function to build the tags for the error metric.
181
+ */
151
182
fun error (metric : String , log : String , buildTags : (MutableMap <String , String >) -> Unit ) {
152
183
val tags = mutableMapOf<String , String >()
153
184
buildTags(tags)
@@ -284,7 +315,7 @@ object Telemetry: Subscriber {
284
315
metric = metric,
285
316
value = value,
286
317
log = log?.let { mapOf (" timestamp" to SegmentInstant .now(), " trace" to it) },
287
- tags = tags + additionalTags
318
+ tags = fullTags
288
319
)
289
320
val newMetricSize = newMetric.toString().toByteArray().size
290
321
if (queueBytes + newMetricSize <= maxQueueBytes) {
0 commit comments