From 766297a7412e84eefbe59c5e79049d404d85c7fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pt=C3=A1=C4=8Dek?= Date: Wed, 14 Jun 2023 22:18:31 +0200 Subject: [PATCH] wip --- .../exporter/worker/ExponentialBackoff.kt | 2 ++ .../android/exporter/worker/PromWorker.kt | 2 ++ .../exporter/worker/RemoteWriteSender.kt | 22 +++++++++++--- .../worker/RemoteWriteSenderStorage.kt | 29 +++++++++++++++---- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/ExponentialBackoff.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/ExponentialBackoff.kt index 1f04ea2..b40a811 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/ExponentialBackoff.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/ExponentialBackoff.kt @@ -38,6 +38,8 @@ class ExponentialBackoff { } } + Log.d(TAG, "Exception caught") + onException() // calculate new delay diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/PromWorker.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/PromWorker.kt index 89e7d2d..4215e89 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/PromWorker.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/PromWorker.kt @@ -11,6 +11,7 @@ import androidx.work.WorkerParameters import com.birdthedeveloper.prometheus.android.prometheus.android.exporter.R import com.birdthedeveloper.prometheus.android.prometheus.android.exporter.compose.PromConfiguration import io.prometheus.client.CollectorRegistry +import io.prometheus.client.exemplars.ExemplarConfig import io.prometheus.client.exporter.common.TextFormat import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi @@ -38,6 +39,7 @@ class PromWorker( init { val androidCustomExporter = AndroidCustomExporter(metricsEngine) androidCustomExporter.register(collectorRegistry) + ExemplarConfig.disableExemplars() // prometheus client library configuration } //TODO foreground notification diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSender.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSender.kt index 51adcee..17a0834 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSender.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSender.kt @@ -12,6 +12,7 @@ import io.ktor.client.request.post import io.ktor.client.request.setBody import io.ktor.http.HttpHeaders import io.ktor.http.HttpStatusCode +import io.prometheus.client.Collector.MetricFamilySamples import io.prometheus.client.CollectorRegistry import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.channels.BufferOverflow @@ -86,16 +87,26 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) { private suspend fun performScrapeAndSaveIt(channel: Channel) { Log.d(TAG, "performScrapeAndSaveIt start") val scrapedMetrics = config.collectorRegistry.metricFamilySamples() - storage.writeScrapedSample(scrapedMetrics) + val metricsScrape : MetricsScrape = MetricsScrape.fromMfs(scrapedMetrics) + + storage.writeScrapedSample(metricsScrape) channel.send(Unit) Log.d(TAG, "performScrapeAndSaveIt end") } + private fun insertInitialDummyScrape(){ + lastTimeRingBuffer.setLastTime(System.currentTimeMillis()) + } + private suspend fun scraper(channel: Channel) { - val checkDelay = 1000L + val checkDelay : Long = 1000L + + insertInitialDummyScrape() + while (true) { if (lastTimeRingBuffer.checkScrapeDidNotHappenInTime()) { remoteWriteOn = true + Log.d(TAG, "Turning remote write on") performScrapeAndSaveIt(channel) delay(config.scrapeInterval * 1000L) @@ -105,8 +116,10 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) { performScrapeAndSaveIt(channel) } + Log.d(TAG, "Turning remote write off") remoteWriteOn = false } + delay(checkDelay) } } @@ -140,7 +153,7 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) { } private suspend fun exportToRemoteWriteEndpoint() { - Log.d(TAG, "sendAll") + Log.d(TAG, "export To Remote Write Endpoint") if (!scrapesAreBeingSent) { scrapesAreBeingSent = true @@ -190,10 +203,12 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) { coroutineScope { //TODO this could be a problem launch { // check for outage in scrapes, save scrapes to storage + Log.d(TAG, "Launching scraper") scraper(channel) } launch { // send saved scrapes to remote write endpoint + Log.d(TAG, "Launching senderManager") senderManager(channel) } } @@ -207,7 +222,6 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) { } fun countSuccessfulScrape() { - Log.d(TAG, "Counting successful scrape") lastTimeRingBuffer.setLastTime(System.currentTimeMillis()) } diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSenderStorage.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSenderStorage.kt index a12ef09..5d88e84 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSenderStorage.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSenderStorage.kt @@ -18,12 +18,27 @@ private const val TAG: String = "REMOTE_WRITE_SENDER_STORAGE" // No need for locks as all operations are run on a single thread, defined in PromWorker // This class defines contract for RemoteWriteSender storage -typealias MetricsScrape = Enumeration +// the same structure as MetricFamilySamples +data class MetricsScrape( + val timeSeriesList : List +){ + companion object { + fun fromMfs(input : Enumeration) : MetricsScrape{ + for (family in input){ + for (sample in family.samples){ + + } + } + } + } +} -// HashMap -private typealias ConverterHashMap = HashMap, MutableList> +data class TimeSeries( + val sample : TimeSeriesSample, + val labels : List, +) -private data class TimeSeriesLabel( +data class TimeSeriesLabel( val name: String, val value: String, ) { @@ -35,7 +50,7 @@ private data class TimeSeriesLabel( } } -private data class TimeSeriesSample( +data class TimeSeriesSample( val timeStampMs: Long, val value: Double, ) { @@ -47,6 +62,10 @@ private data class TimeSeriesSample( } } +// HashMap +private typealias ConverterHashMap = HashMap, MutableList> + + abstract class RemoteWriteSenderStorage { private val remoteWriteLabel: TimeSeriesLabel = TimeSeriesLabel( name = "backfill",