Skip to content

Commit

Permalink
Enabling Telemetry and adding a couple things to track (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelGHSeg authored Nov 14, 2024
1 parent 97a016d commit 611c67b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ open class Analytics protected constructor(
it["cdnhost"] = configuration.cdnHost
it["flush"] =
"at:${configuration.flushAt} int:${configuration.flushInterval} pol:${configuration.flushPolicies.count()}"
it["config"] = "seg:${configuration.autoAddSegmentDestination}"
}

// Setup store
Expand Down
43 changes: 37 additions & 6 deletions core/src/main/java/com/segment/analytics/kotlin/core/Telemetry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ fun logError(err: Throwable) {
Analytics.reportInternalError(err)
}

/**
* A class for sending telemetry data to Segment.
* This system is used to gather usage and error data from the SDK for the purpose of improving the SDK.
* It can be disabled at any time by setting Telemetry.enable to false.
* Errors are sent with a write key, which can be disabled by setting Telemetry.sendWriteKeyOnError to false.
* All data is downsampled and no PII is collected.
*/
object Telemetry: Subscriber {
private const val METRICS_BASE_TAG = "analytics_mobile"
// Metric class for Analytics SDK
Expand All @@ -51,7 +58,10 @@ object Telemetry: Subscriber {
// Metric class for Analytics SDK plugin errors
const val INTEGRATION_ERROR_METRIC = "$METRICS_BASE_TAG.integration.invoke.error"

var enable: Boolean = false
/**
* Enables or disables telemetry.
*/
var enable: Boolean = true
set(value) {
field = value
if(value) {
Expand All @@ -62,10 +72,11 @@ object Telemetry: Subscriber {
start()
}
}

var host: String = Constants.DEFAULT_API_HOST
// 1.0 is 100%, will get set by Segment setting before start()
var sampleRate: Double = 0.0
var flushTimer: Int = 30 * 1000 // 30s
var sampleRate: Double = 1.0
var flushTimer: Int = 3 * 1000 // 30s
var httpClient: HTTPClient = HTTPClient("", MetricsRequestFactory())
var sendWriteKeyOnError: Boolean = true
var sendErrorLogData: Boolean = false
Expand Down Expand Up @@ -96,6 +107,11 @@ object Telemetry: Subscriber {
private var telemetryScope: CoroutineScope = CoroutineScope(SupervisorJob() + exceptionHandler)
private var telemetryDispatcher: ExecutorCoroutineDispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
private var telemetryJob: Job? = null

/**
* Starts the telemetry if it is enabled and not already started, and the sample rate is greater than 0.
* Called automatically when Telemetry.enable is set to true and when configuration data is received from Segment.
*/
fun start() {
if (!enable || started || sampleRate == 0.0) return
started = true
Expand Down Expand Up @@ -126,15 +142,23 @@ object Telemetry: Subscriber {
}
}

fun reset()
{
/**
* Resets the telemetry by canceling the telemetry job, clearing the error queue, and resetting the state.
*/
fun reset() {
telemetryJob?.cancel()
resetQueue()
seenErrors.clear()
started = false
rateLimitEndTime = 0
}

/**
* Increments a metric with the specified tags.
*
* @param metric The name of the metric to increment.
* @param buildTags A lambda function to build the tags for the metric.
*/
fun increment(metric: String, buildTags: (MutableMap<String, String>) -> Unit) {
val tags = mutableMapOf<String, String>()
buildTags(tags)
Expand All @@ -148,6 +172,13 @@ object Telemetry: Subscriber {
addRemoteMetric(metric, tags)
}

/**
* Logs an error metric with the specified tags and log data.
*
* @param metric The name of the error metric.
* @param log The log data associated with the error.
* @param buildTags A lambda function to build the tags for the error metric.
*/
fun error(metric:String, log: String, buildTags: (MutableMap<String, String>) -> Unit) {
val tags = mutableMapOf<String, String>()
buildTags(tags)
Expand Down Expand Up @@ -284,7 +315,7 @@ object Telemetry: Subscriber {
metric = metric,
value = value,
log = log?.let { mapOf("timestamp" to SegmentInstant.now(), "trace" to it) },
tags = tags + additionalTags
tags = fullTags
)
val newMetricSize = newMetric.toString().toByteArray().size
if (queueBytes + newMetricSize <= maxQueueBytes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ internal class Timeline {
// remove all plugins with this name in every category
plugins.forEach { (_, list) ->
list.remove(plugin)
Telemetry.increment(Telemetry.INTEGRATION_METRIC) {
it["message"] = "removed"
it["plugin"] = "${plugin.type.toString()}-${plugin.javaClass.toString()}"
}
}
}

Expand Down

0 comments on commit 611c67b

Please sign in to comment.