• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Unleash / unleash-android / 11297355926

11 Oct 2024 06:19PM UTC coverage: 82.543% (-0.1%) from 82.638%
11297355926

push

github

web-flow
docs: use defaults for polling and metrics intervals (#92)

chore: use defaults for polling and metrics intervals

214 of 292 branches covered (73.29%)

Branch coverage included in aggregate %.

656 of 762 relevant lines covered (86.09%)

5.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.97
/unleashandroidsdk/src/main/java/io/getunleash/android/metrics/MetricsSender.kt
1
package io.getunleash.android.metrics
2

3
import android.util.Log
4
import io.getunleash.android.UnleashConfig
5
import io.getunleash.android.data.Parser.metricsBodyAdapter
6
import io.getunleash.android.data.Variant
7
import io.getunleash.android.http.Throttler
8
import okhttp3.Call
9
import okhttp3.Callback
10
import okhttp3.Headers.Companion.toHeaders
11
import okhttp3.HttpUrl.Companion.toHttpUrl
12
import okhttp3.MediaType.Companion.toMediaType
13
import okhttp3.OkHttpClient
14
import okhttp3.Request
15
import okhttp3.RequestBody.Companion.toRequestBody
16
import okhttp3.Response
17
import java.io.IOException
18
import java.util.Date
19
import java.util.concurrent.TimeUnit
20

21
class MetricsSender(
11✔
22
    private val config: UnleashConfig,
3✔
23
    private val httpClient: OkHttpClient,
3✔
24
    private val applicationHeaders: Map<String, String> = config.getApplicationHeaders(config.metricsStrategy)
8✔
25
): MetricsHandler {
26
    companion object {
27
        private const val TAG: String = "MetricsSender"
28
    }
29

30
    private val metricsUrl = config.proxyUrl?.toHttpUrl()?.newBuilder()?.addPathSegment("client")
22!
31
        ?.addPathSegment("metrics")?.build()
12!
32
    private var bucket: CountBucket = CountBucket(start = Date())
13✔
33
    private val throttler =
34
        Throttler(
5✔
35
            TimeUnit.MILLISECONDS.toSeconds(config.metricsStrategy.interval),
6✔
36
            longestAcceptableIntervalSeconds = 300,
1✔
37
            metricsUrl.toString()
3✔
38
        )
39

40
    override suspend fun sendMetrics() {
41
        if (metricsUrl == null) {
3!
42
            Log.d(TAG, "No proxy URL configured, skipping metrics reporting")
×
43
            return
×
44
        }
45
        if (bucket.isEmpty()) {
4✔
46
            Log.d(TAG, "No metrics to report")
4✔
47
            return
2✔
48
        }
49
        if (throttler.performAction()) {
4!
50
            val toReport = swapAndFreeze()
3✔
51
            val payload = MetricsPayload(
4✔
52
                appName = config.appName,
3✔
53
                instanceId = config.instanceId,
3✔
54
                bucket = toReport
1✔
55
            )
56
            val request = Request.Builder()
4✔
57
                .headers(applicationHeaders.toHeaders())
5✔
58
                .url(metricsUrl).post(
4✔
59
                    metricsBodyAdapter.toJson(payload)
7✔
60
                        .toRequestBody("application/json".toMediaType())
5✔
61
                ).build()
1✔
62
            httpClient.newCall(request).enqueue(object : Callback {
15✔
63
                override fun onFailure(call: Call, e: IOException) {
64
                    Log.i(TAG, "Failed to report metrics for interval", e)
×
65
                }
×
66

67
                override fun onResponse(call: Call, response: Response) {
68
                    Log.d(
×
69
                        TAG,
×
70
                        "Received status code ${response.code} from ${request.method} $metricsUrl"
×
71
                    )
72
                    throttler.handle(response.code)
×
73
                    response.body.use { // Need to consume body to ensure we don't keep connection open
×
74
                    }
75
                }
×
76
            })
77
        } else {
78
            throttler.skipped()
×
79
        }
80
    }
2✔
81

82
    private fun swapAndFreeze(): Bucket {
83
        val bucketRef = bucket
3✔
84
        bucket = CountBucket(start = Date())
13✔
85
        return bucketRef.copy().toBucket(bucket.start)
13✔
86
    }
87

88
    override fun count(featureName: String, enabled: Boolean): Boolean {
89
        return bucket.count(featureName, enabled)
6✔
90
    }
91

92
    override fun countVariant(featureName: String, variant: Variant): Variant {
93
        return bucket.countVariant(featureName, variant)
6✔
94
    }
95
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc