remote write wip

This commit is contained in:
Martin Ptáček
2023-06-04 21:23:17 +02:00
parent 7988660eb4
commit 2378145dc4
4 changed files with 114 additions and 4 deletions

View File

@ -31,6 +31,7 @@ class PromWorker(
private val metricsEngine : MetricsEngine = MetricsEngine(context)
private lateinit var androidCustomExporter : AndroidCustomExporter
private lateinit var pushProxClient : PushProxClient
private lateinit var remoteWriteSender : RemoteWriteSender
//TODO foreground notification
private val notificationManager =
@ -75,6 +76,16 @@ class PromWorker(
if(config.remoteWriteEnabled){
//DO something
Log.v(TAG, "Remote write service started.")
remoteWriteSender = RemoteWriteSender(
RemoteWriteConfiguration(
config.remoteWriteScrapeInterval,
config.remoteWriteEndpoint,
::performScrape,
)
)
//TODO
remoteWriteSender.sendTestRequest()
}
}

View File

@ -42,7 +42,7 @@ private class PushProxCounter(registry: CollectorRegistry) {
.help("Number of push errors")
.register(registry)
fun scrapeError(){ scrapeErrorCounter.inc()}
fun scrapeError(){ scrapeErrorCounter.inc() }
fun pushError(){ pushErrorCounter.inc() }
fun pollError(){ pollErrorCounter.inc() }
}

View File

@ -1,18 +1,70 @@
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter.worker
import android.util.Log
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.header
import io.ktor.client.request.headers
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.http.HttpHeaders
import remote.write.RemoteWrite
import remote.write.RemoteWrite.Label
import remote.write.RemoteWrite.TimeSeries
import remote.write.RemoteWrite.WriteRequest
private const val TAG : String = "REMOTE_WRITE_SENDER"
data class RemoteWriteConfiguration(
val scrape_interval : Int,
val remote_write_endpoint : String,
val performScrape : () -> String, //TODO this class needs it structured in objects
)
class RemoteWriteSender(config : RemoteWriteConfiguration) {
class RemoteWriteSender(private val config : RemoteWriteConfiguration) {
//TODO implement this thing
fun test(){
var request : WriteRequest
private fun getRequestBody() : ByteArray {
val label : Label = Label.newBuilder()
.setName("labelNameTest")
.setValue("labelValueTest").build()
val sample : RemoteWrite.Sample = RemoteWrite.Sample.newBuilder()
.setValue(55.0)
.setTimestamp(System.currentTimeMillis()).build()
val timeSeries: TimeSeries = TimeSeries.newBuilder()
.addLabels(label)
.addSamples(sample)
.build()
val request : WriteRequest = WriteRequest.newBuilder()
.addTimeseries(timeSeries)
.build()
return request.toByteArray()
}
private fun encdeWithSnappy(data : ByteArray) : ByteArray {
// TODO implement this
// github.com/xerial/snappy-java
return data
}
suspend fun sendTestRequest(){
val client = HttpClient()
val response = client.post(config.remote_write_endpoint){
setBody(getRequestBody())
headers{
append(HttpHeaders.ContentEncoding, "snappy")
append(HttpHeaders.ContentType, "application/protobuf")
append(HttpHeaders.UserAgent, "Prometheus Android Exporter")
header("X-Prometheus-Remote-Write-Version", "0.1.0")
}
}
Log.v(TAG, "Response status: ${response.status.toString()}")
client.close()
}
}

View File

@ -0,0 +1,47 @@
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter.worker
typealias MetricsScrape = String
// define the contract for Remote Write Sender storage
abstract class RemoteWriteSenderStorage {
abstract fun writeScrapedSample(metricsScrape: MetricsScrape)
abstract fun getNumberOfScrapedSamples(number: Int) : List<MetricsScrape>
abstract fun removeNumberOfScrapedSamples(number : Int)
abstract fun isEmpty() : Boolean
}
class RemoteWriteSenderMemoryStorage : RemoteWriteSenderStorage() {
override fun getNumberOfScrapedSamples(number: Int): List<MetricsScrape> {
TODO("Not yet implemented")
}
override fun removeNumberOfScrapedSamples(number: Int) {
TODO("Not yet implemented")
}
override fun writeScrapedSample(metricsScrape: MetricsScrape) {
TODO("Not yet implemented")
}
override fun isEmpty(): Boolean {
TODO("Not yet implemented")
}
}
class RemoteWriteSenterDatabaseStorage : RemoteWriteSenderStorage() {
override fun getNumberOfScrapedSamples(number: Int): List<MetricsScrape> {
TODO("Not yet implemented")
}
override fun removeNumberOfScrapedSamples(number: Int) {
TODO("Not yet implemented")
}
override fun writeScrapedSample(metricsScrape: MetricsScrape) {
TODO("Not yet implemented")
}
override fun isEmpty(): Boolean {
TODO("Not yet implemented")
}
}