mirror of
https://github.com/mii443/prometheus-android-exporter.git
synced 2025-08-22 15:15:35 +00:00
runBlocking -> Coroutine scope
This commit is contained in:
@ -13,13 +13,16 @@ import com.birdthedeveloper.prometheus.android.prometheus.android.exporter.compo
|
||||
import io.prometheus.client.CollectorRegistry
|
||||
import io.prometheus.client.exporter.common.TextFormat
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.newFixedThreadPoolContext
|
||||
import kotlinx.coroutines.newSingleThreadContext
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.StringWriter
|
||||
|
||||
private const val TAG : String = "Worker"
|
||||
private const val TAG : String = "PROM_WORKER"
|
||||
|
||||
class PromWorker(
|
||||
private val context: Context,
|
||||
@ -53,11 +56,12 @@ class PromWorker(
|
||||
remoteWriteSender?.countSuccessfulScrape()
|
||||
}
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
@OptIn(DelicateCoroutinesApi::class, ExperimentalCoroutinesApi::class)
|
||||
private suspend fun startServicesInOneThread(config: PromConfiguration) {
|
||||
val threadContext = newSingleThreadContext("PromWorkerThreadContext")
|
||||
val backgroundDispatcher = newFixedThreadPoolContext(2, "Prom worker")
|
||||
val threadContext = backgroundDispatcher.limitedParallelism(2)
|
||||
|
||||
coroutineScope {
|
||||
try{
|
||||
withContext(threadContext) {
|
||||
|
||||
if (config.remoteWriteEnabled) {
|
||||
@ -99,17 +103,24 @@ class PromWorker(
|
||||
countSuccessfulScrape = ::countSuccessfulScrape,
|
||||
)
|
||||
)
|
||||
Log.d(TAG, "PushProx launching now") //TODO is singleThreadContext a problem??
|
||||
launch {
|
||||
Log.d(TAG, "PushProx launched")
|
||||
pushProxClient.start()
|
||||
}
|
||||
}
|
||||
}
|
||||
}finally {
|
||||
withContext(NonCancellable) {
|
||||
Log.v(TAG, "Canceling prom worker")
|
||||
backgroundDispatcher.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun doWork(): Result {
|
||||
val inputConfiguration: PromConfiguration = PromConfiguration.fromWorkData(inputData)
|
||||
Log.d(TAG, "Launching PromWorker with the following config: $inputConfiguration")
|
||||
|
||||
// set foreground - //TODO is this right for the use case?
|
||||
//setForeground(createForegroundInfo())
|
||||
|
@ -86,7 +86,6 @@ class PrometheusServer() {
|
||||
}
|
||||
get("/metrics") {
|
||||
val response : String = config.performScrape()
|
||||
Log.d(TAG, "Response: $response")
|
||||
call.respondText(response)
|
||||
config.countSuccessfulScrape()
|
||||
Log.d(TAG, "Successful scrape")
|
||||
|
@ -10,6 +10,7 @@ import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.http.HttpMethod
|
||||
import io.prometheus.client.CollectorRegistry
|
||||
import io.prometheus.client.Counter
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@ -164,6 +165,9 @@ class PushProxClient(private val pushProxConfig: PushProxConfig) {
|
||||
|
||||
pushProxConfig.countSuccessfulScrape()
|
||||
} catch (e: Exception) {
|
||||
if (e is CancellationException){
|
||||
throw e
|
||||
}
|
||||
counters.pushError()
|
||||
Log.v(TAG, "Push exception $e")
|
||||
return
|
||||
@ -172,14 +176,14 @@ class PushProxClient(private val pushProxConfig: PushProxConfig) {
|
||||
|
||||
private suspend fun loop(context: PushProxContext) {
|
||||
while (true) {
|
||||
Log.v(TAG, "PushProxClient main loop start")
|
||||
Log.d(TAG, "PushProxClient main loop start")
|
||||
|
||||
ExponentialBackoff.runWithBackoff(
|
||||
function = { doPoll(context) },
|
||||
onException = { counters.pollError() }
|
||||
)
|
||||
|
||||
Log.v(TAG, "PushProxClient main loop end")
|
||||
Log.d(TAG, "PushProxClient main loop end")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,7 @@ import io.prometheus.client.CollectorRegistry
|
||||
import kotlinx.coroutines.NonCancellable
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
@ -185,7 +186,8 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) {
|
||||
|
||||
client = HttpClient()
|
||||
try {
|
||||
runBlocking {
|
||||
//TODO test this being coroutine scope
|
||||
coroutineScope { //TODO this could be a problem
|
||||
launch {
|
||||
// check for outage in scrapes, save scrapes to storage
|
||||
scraper(channel)
|
||||
|
Reference in New Issue
Block a user