bug fixes

This commit is contained in:
Martin Ptáček
2023-06-14 11:03:38 +02:00
parent ad45302e9e
commit 7530cf6230
5 changed files with 32 additions and 22 deletions

View File

@ -255,6 +255,7 @@ class PromViewModel : ViewModel() {
// check port boundaries
val minPort = 1024
val maxPort = 65535
Log.d(TAG, "Prometheus server port, ${config.prometheusServerPort}")
val prometheusServerPort: Int = config.prometheusServerPort.toIntOrNull()
?: return displayConfigValidationDialog("Prometheus Server Port must be a number!")
if (prometheusServerPort < minPort || prometheusServerPort > maxPort) {

View File

@ -44,7 +44,7 @@ class PromWorker(
private fun performScrape(): String {
val writer = StringWriter()
TextFormat.write004(writer, CollectorRegistry.defaultRegistry.metricFamilySamples())
TextFormat.write004(writer, collectorRegistry.metricFamilySamples())
return writer.toString()
}

View File

@ -85,7 +85,9 @@ class PrometheusServer() {
call.respondText(getLandingPage())
}
get("/metrics") {
call.respondText(config.performScrape())
val response : String = config.performScrape()
Log.d(TAG, "Response: $response")
call.respondText(response)
config.countSuccessfulScrape()
Log.d(TAG, "Successful scrape")
}

View File

@ -95,6 +95,7 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) {
while (true) {
if (lastTimeRingBuffer.checkScrapeDidNotHappenInTime()) {
remoteWriteOn = true
performScrapeAndSaveIt(channel)
delay(config.scrapeInterval * 1000L)
@ -102,28 +103,13 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) {
delay(config.scrapeInterval * 1000L)
performScrapeAndSaveIt(channel)
}
remoteWriteOn = false
}
delay(checkDelay)
}
}
private suspend fun sendAll() {
Log.d(TAG, "sendAll")
if (!scrapesAreBeingSent) {
scrapesAreBeingSent = true
while (!storage.isEmpty()) {
val body = storage.getScrapedSamplesCompressedProtobuf(config.maxSamplesPerExport)
Log.d(TAG, "Exponential backoff to export remote write started")
ExponentialBackoff.runWithBackoff({
sendRequestToRemoteWrite(body, config.maxSamplesPerExport)
}, {}, false)
Log.d(TAG, "Exponential backoff to export remote write finish")
}
lastTimeRemoteWriteSent = System.currentTimeMillis()
}
}
private fun deviceHasInternet(): Boolean {
val connectivityManager = config.getContext()
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
@ -132,9 +118,11 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) {
val network = connectivityManager.getActiveNetworkCompat()
val cap = connectivityManager.getNetworkCapabilities(network)
if (cap != null && cap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
Log.d(TAG, "Device has internet: true")
return true
}
}
Log.d(TAG, "Device has internet: false")
return false
}
@ -150,8 +138,28 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) {
return storage.getLength() > config.maxSamplesPerExport
}
private suspend fun exportToRemoteWriteEndpoint() {
Log.d(TAG, "sendAll")
if (!scrapesAreBeingSent) {
scrapesAreBeingSent = true
while (!storage.isEmpty()) {
val body = storage.getScrapedSamplesCompressedProtobuf(config.maxSamplesPerExport)
Log.d(TAG, "Exponential backoff to export remote write started")
ExponentialBackoff.runWithBackoff({
sendRequestToRemoteWrite(body, config.maxSamplesPerExport)
}, {}, false)
Log.d(TAG, "Exponential backoff to export remote write finish")
}
lastTimeRemoteWriteSent = System.currentTimeMillis()
scrapesAreBeingSent = false
}
}
private suspend fun senderManager(channel: Channel<Unit>) {
while (true) {
Log.d(TAG, "Sender manager loop start")
if (storage.isEmpty()) {
// channel is conflated, one receive is enough
// suspend here until sending remote write is needed
@ -162,10 +170,11 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) {
while (remoteWriteOn || !storage.isEmpty()) {
if (conditionsForRemoteWrite()) {
sendAll()
exportToRemoteWriteEndpoint()
}
delay(1000)
}
Log.d(TAG, "Sender manager loop end")
}
}

View File

@ -159,8 +159,6 @@ abstract class RemoteWriteSenderStorage {
val result: WriteRequest = hashmapToProtobufWriteRequest(hashmap)
Log.v(TAG, result.timeseriesList.toString() + "<- protobuf")
return result
}