Skip to content

fix crash on android 5 and 6 #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions src/main/kotlin/io/getunleash/metrics/MetricsReporter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,32 +109,64 @@ class HttpMetricsReporter(

}

override fun log(featureName: String, enabled: Boolean): Boolean {
val count = if (enabled) {
EvaluationCount(1, 0)
} else {
EvaluationCount(0, 1)
}
bucket.toggles.merge(featureName, count) { old: EvaluationCount?, new: EvaluationCount ->
old?.copy(yes = old.yes + new.yes, no = old.no + new.no) ?: new
override fun log(featureName: String, enabled: Boolean): Boolean {
try {
val count = if (enabled) {
EvaluationCount(1, 0)
} else {
EvaluationCount(0, 1)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
bucket.toggles?.merge(
featureName,
count
) { old: EvaluationCount?, new: EvaluationCount ->
old?.copy(yes = old.yes + new.yes, no = old.no + new.no) ?: new
}
} else {
// handle for android 5,6
try {
bucket.toggles?.remove(featureName)
bucket.toggles?.set(featureName, count)
} catch (e: Exception) {
}
}
return enabled
} catch (e: Exception) {
}
return enabled
return false
}

override fun logVariant(featureName: String, variant: Variant): Variant {
bucket.toggles.compute(featureName) { _, count ->
val evaluationCount = count ?: EvaluationCount(0, 0)
evaluationCount.variants.merge(variant.name, 1) { old, value ->
old + value
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
bucket.toggles?.compute(featureName) { _, count ->
val evaluationCount = count ?: EvaluationCount(0, 0)
evaluationCount.variants.merge(variant.name, 1) { old, value ->
old + value
}
evaluationCount
}
} else {
//handle for android 5,6
try {
val count: EvaluationCount =
bucket.toggles?.get(featureName) ?: EvaluationCount(0, 0)
bucket.toggles?.remove(featureName)
bucket.toggles?.set(featureName, count)
} catch (e: Exception) {
}
}
evaluationCount
return variant
} catch (e: Exception) {
e.printStackTrace()
}
return variant
return Variant()
}

override fun close() {
httpClient.dispatcher.executorService.shutdownNow()
httpClient.connectionPool.evictAll()
httpClient.cache?.closeQuietly()
}
}
}