Skip to content

Commit 611c67b

Browse files
authored
Enabling Telemetry and adding a couple things to track (#242)
1 parent 97a016d commit 611c67b

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

core/src/main/java/com/segment/analytics/kotlin/core/Analytics.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ open class Analytics protected constructor(
123123
it["cdnhost"] = configuration.cdnHost
124124
it["flush"] =
125125
"at:${configuration.flushAt} int:${configuration.flushInterval} pol:${configuration.flushPolicies.count()}"
126+
it["config"] = "seg:${configuration.autoAddSegmentDestination}"
126127
}
127128

128129
// Setup store

core/src/main/java/com/segment/analytics/kotlin/core/Telemetry.kt

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ fun logError(err: Throwable) {
4040
Analytics.reportInternalError(err)
4141
}
4242

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+
*/
4350
object Telemetry: Subscriber {
4451
private const val METRICS_BASE_TAG = "analytics_mobile"
4552
// Metric class for Analytics SDK
@@ -51,7 +58,10 @@ object Telemetry: Subscriber {
5158
// Metric class for Analytics SDK plugin errors
5259
const val INTEGRATION_ERROR_METRIC = "$METRICS_BASE_TAG.integration.invoke.error"
5360

54-
var enable: Boolean = false
61+
/**
62+
* Enables or disables telemetry.
63+
*/
64+
var enable: Boolean = true
5565
set(value) {
5666
field = value
5767
if(value) {
@@ -62,10 +72,11 @@ object Telemetry: Subscriber {
6272
start()
6373
}
6474
}
75+
6576
var host: String = Constants.DEFAULT_API_HOST
6677
// 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
6980
var httpClient: HTTPClient = HTTPClient("", MetricsRequestFactory())
7081
var sendWriteKeyOnError: Boolean = true
7182
var sendErrorLogData: Boolean = false
@@ -96,6 +107,11 @@ object Telemetry: Subscriber {
96107
private var telemetryScope: CoroutineScope = CoroutineScope(SupervisorJob() + exceptionHandler)
97108
private var telemetryDispatcher: ExecutorCoroutineDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
98109
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+
*/
99115
fun start() {
100116
if (!enable || started || sampleRate == 0.0) return
101117
started = true
@@ -126,15 +142,23 @@ object Telemetry: Subscriber {
126142
}
127143
}
128144

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() {
131149
telemetryJob?.cancel()
132150
resetQueue()
133151
seenErrors.clear()
134152
started = false
135153
rateLimitEndTime = 0
136154
}
137155

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+
*/
138162
fun increment(metric: String, buildTags: (MutableMap<String, String>) -> Unit) {
139163
val tags = mutableMapOf<String, String>()
140164
buildTags(tags)
@@ -148,6 +172,13 @@ object Telemetry: Subscriber {
148172
addRemoteMetric(metric, tags)
149173
}
150174

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+
*/
151182
fun error(metric:String, log: String, buildTags: (MutableMap<String, String>) -> Unit) {
152183
val tags = mutableMapOf<String, String>()
153184
buildTags(tags)
@@ -284,7 +315,7 @@ object Telemetry: Subscriber {
284315
metric = metric,
285316
value = value,
286317
log = log?.let { mapOf("timestamp" to SegmentInstant.now(), "trace" to it) },
287-
tags = tags + additionalTags
318+
tags = fullTags
288319
)
289320
val newMetricSize = newMetric.toString().toByteArray().size
290321
if (queueBytes + newMetricSize <= maxQueueBytes) {

core/src/main/java/com/segment/analytics/kotlin/core/platform/Timeline.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ internal class Timeline {
107107
// remove all plugins with this name in every category
108108
plugins.forEach { (_, list) ->
109109
list.remove(plugin)
110+
Telemetry.increment(Telemetry.INTEGRATION_METRIC) {
111+
it["message"] = "removed"
112+
it["plugin"] = "${plugin.type.toString()}-${plugin.javaClass.toString()}"
113+
}
110114
}
111115
}
112116

0 commit comments

Comments
 (0)