From 3232b026df00dc5dc4db179347e0290ab9711619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Pt=C3=A1=C4=8Dek?= Date: Sun, 30 Jul 2023 14:53:29 +0200 Subject: [PATCH] final --- .../android/exporter/compose/Configuration.kt | 5 +- .../android/exporter/compose/PromViewModel.kt | 11 ++- .../exporter/compose/SettingsActivity.kt | 13 +++- .../exporter/worker/AndroidCustomExporter.kt | 52 +++++++------- .../android/exporter/worker/MetricsEngine.kt | 68 +++++++++--------- .../android/exporter/worker/PushProxClient.kt | 5 +- .../exporter/worker/RemoteWriteSender.kt | 7 +- .../worker/RemoteWriteSenderMemStorage.kt | 39 +++++----- .../exporter/worker/LastTimeRingBufferTest.kt | 2 + .../worker/RemoteWriteSenderStorageTest.kt | 72 ++++++++++--------- 10 files changed, 149 insertions(+), 125 deletions(-) diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/Configuration.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/Configuration.kt index 0605434..b9454bd 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/Configuration.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/Configuration.kt @@ -43,7 +43,8 @@ data class PromConfigFile( ?: defaultRemoteWriteMaxSamplesPerExport).toString(), remoteWriteExportInterval = (this.remote_write?.export_interval ?: defaultRemoteWriteExportInterval).toString(), - remoteWriteInstanceLabel = this.remote_write?.instance ?: defaultRemoteWriteInstanceLabel, + remoteWriteInstanceLabel = this.remote_write?.instance + ?: defaultRemoteWriteInstanceLabel, remoteWriteJobLabel = this.remote_write?.job ?: defaultRemoteWriteJobLabel, ) } @@ -112,7 +113,7 @@ data class PromConfiguration( } companion object { - // data/user/0/com.birdthedeveloper.prometheus.android.exporter/files + // data/user/0/com.birdthedeveloper.prometheus.android.exporter/files/ private const val filename: String = "config.yaml" private const val alternativeFilename: String = "config.yml" fun configFileExists(context: Context): Boolean { diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/PromViewModel.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/PromViewModel.kt index 43e6e66..6067075 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/PromViewModel.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/PromViewModel.kt @@ -252,6 +252,13 @@ class PromViewModel : ViewModel() { "Scrape interval must be smaller than Export interval!" ) } + + // check target labels + if (config.remoteWriteInstanceLabel.isEmpty() || config.remoteWriteJobLabel.isEmpty()) { + return displayConfigValidationDialog( + "Job target label and Instance target label must be set!" + ) + } } // validate settings for prometheus server @@ -391,7 +398,7 @@ class PromViewModel : ViewModel() { ) } - UpdatePromConfig.RemoteWriteInstanceLabel -> _uiState.update {current -> + UpdatePromConfig.RemoteWriteInstanceLabel -> _uiState.update { current -> current.copy( promConfig = current.promConfig.copy( remoteWriteInstanceLabel = value as String @@ -399,7 +406,7 @@ class PromViewModel : ViewModel() { ) } - UpdatePromConfig.RemoteWriteJobLabel -> _uiState.update {current -> + UpdatePromConfig.RemoteWriteJobLabel -> _uiState.update { current -> current.copy( promConfig = current.promConfig.copy( remoteWriteJobLabel = value as String diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/SettingsActivity.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/SettingsActivity.kt index d6d7a85..db0eb31 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/SettingsActivity.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/compose/SettingsActivity.kt @@ -38,8 +38,15 @@ fun SettingsPage( } ) - Text( modifier = Modifier.padding(all = 20.dp), - text = "This application is licensed under the Apache 2.0 license.", textAlign = TextAlign.Center,) - Text(modifier = Modifier.padding(all = 20.dp),text = "Author: Martin Ptacek, 2023", textAlign = TextAlign.Center,) + Text( + modifier = Modifier.padding(all = 20.dp), + text = "This application is licensed under the Apache 2.0 license.", + textAlign = TextAlign.Center, + ) + Text( + modifier = Modifier.padding(all = 20.dp), + text = "Author: Martin Ptacek, 2023", + textAlign = TextAlign.Center, + ) } } diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/AndroidCustomExporter.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/AndroidCustomExporter.kt index fe0f920..805fa5e 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/AndroidCustomExporter.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/AndroidCustomExporter.kt @@ -2,10 +2,8 @@ package com.birdthedeveloper.prometheus.android.exporter.worker -import android.os.CpuUsageInfo import android.util.Log import io.prometheus.client.Collector -import io.prometheus.client.Gauge import io.prometheus.client.GaugeMetricFamily private const val TAG = "ANDROID_EXPORTER" @@ -34,7 +32,7 @@ class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector return mfs } - private fun collectBatteryChargeRatio(mfs : MutableList){ + private fun collectBatteryChargeRatio(mfs: MutableList) { val gauge = GaugeMetricFamily( "android_battery_charge_ratio", "Current battery charge", @@ -44,7 +42,7 @@ class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector mfs.add(gauge) } - private fun collectUptimeInSeconds(mfs : MutableList){ + private fun collectUptimeInSeconds(mfs: MutableList) { val gauge = GaugeMetricFamily( "android_uptime_seconds", "Android uptime in seconds", @@ -54,11 +52,11 @@ class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector mfs.add(gauge) } - private fun collectHasWiFiConnection(mfs : MutableList){ + private fun collectHasWiFiConnection(mfs: MutableList) { metricEngine.getHasWiFiConnected()?.let { - val result : Double = if (it) { + val result: Double = if (it) { 1.0 - }else{ + } else { 0.0 } @@ -72,10 +70,10 @@ class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector } } - private fun collectBatteryIsCharging(mfs: MutableList){ - val result = if (metricEngine.getBatteryIsCharging()){ + private fun collectBatteryIsCharging(mfs: MutableList) { + val result = if (metricEngine.getBatteryIsCharging()) { 1.0 - }else{ + } else { 0.0 } @@ -88,11 +86,11 @@ class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector mfs.add(gauge) } - private fun collectHasCellularConnection(mfs : MutableList){ + private fun collectHasCellularConnection(mfs: MutableList) { metricEngine.getHasCellularConnected()?.let { - val result : Double = if (it) { + val result: Double = if (it) { 1.0 - }else{ + } else { 0.0 } @@ -106,22 +104,24 @@ class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector } } - private fun collectAndroidInfo(mfs : MutableList){ + private fun collectAndroidInfo(mfs: MutableList) { val gauge = GaugeMetricFamily( "android_system_info", "Static information about the android phone", - listOf("manufacturer", "model", "os_release","cpu_core_count") + listOf("manufacturer", "model", "os_release", "cpu_core_count") + ) + gauge.addMetric( + listOf( + metricEngine.getAndroidManufacturer(), + metricEngine.getAndroidModel(), + metricEngine.getAndroidOsVersion(), + metricEngine.getNumberOfCpuCores().toString(), + ), 1.0 ) - gauge.addMetric(listOf( - metricEngine.getAndroidManufacturer(), - metricEngine.getAndroidModel(), - metricEngine.getAndroidOsVersion(), - metricEngine.getNumberOfCpuCores().toString(), - ), 1.0) mfs.add(gauge) } - private fun collectHardwareSensors(mfs : MutableList){ + private fun collectHardwareSensors(mfs: MutableList) { metricEngine.hwSensorsValues().headingDegrees?.let { val gauge = GaugeMetricFamily( "android_sensor_heading_degrees", @@ -284,7 +284,7 @@ class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector "Data from the Android Rotation Vector sensor, how is the device rotated, without a unit", listOf(), ) - gauge.addMetric(listOf(),it) + gauge.addMetric(listOf(), it) mfs.add(gauge) } @@ -294,12 +294,12 @@ class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector "Accuracy of the Android rotation vector sensor, in radians", listOf(), ) - gauge.addMetric(listOf(),it) + gauge.addMetric(listOf(), it) mfs.add(gauge) } } - private fun collectScrapeDuration(mfs : MutableList, startTime : Long){ + private fun collectScrapeDuration(mfs: MutableList, startTime: Long) { val gauge = GaugeMetricFamily( "android_scrape_duration_seconds", "Duration of the metric scrape", @@ -311,7 +311,7 @@ class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector mfs.add(gauge) } - private fun addAxisSpecificGauge(gauge: GaugeMetricFamily, data : AxisSpecificGauge){ + private fun addAxisSpecificGauge(gauge: GaugeMetricFamily, data: AxisSpecificGauge) { gauge.addMetric(listOf("x"), data.x) gauge.addMetric(listOf("y"), data.y) gauge.addMetric(listOf("z"), data.z) diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/MetricsEngine.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/MetricsEngine.kt index 2d65f6e..95df8d6 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/MetricsEngine.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/MetricsEngine.kt @@ -24,35 +24,35 @@ import android.util.Log private const val TAG = "METRICS_ENGINE" data class AxisSpecificGauge( - val x : Double, - val y : Double, - val z : Double, + val x: Double, + val y: Double, + val z: Double, ) class HwSensorsCache( - var headingDegrees : Double? = null, - var headingAccuracyDegrees : Double? = null, - var hingeAngleDegrees : Double? = null, - var offbodyDetect : Double? = null, - var ambientTemperatureCelsius : Double? = null, - var relativeHumidityPercent : Double? = null, + var headingDegrees: Double? = null, + var headingAccuracyDegrees: Double? = null, + var hingeAngleDegrees: Double? = null, + var offbodyDetect: Double? = null, + var ambientTemperatureCelsius: Double? = null, + var relativeHumidityPercent: Double? = null, - var accelerometer : AxisSpecificGauge? = null, - var magneticFieldMicroTesla : AxisSpecificGauge? = null, + var accelerometer: AxisSpecificGauge? = null, + var magneticFieldMicroTesla: AxisSpecificGauge? = null, var gyroscopeRadiansPerSecond: AxisSpecificGauge? = null, - var ambientLightLux : Double? = null, - var pressureHectoPascal : Double? = null, - var proximityCentimeters : Double? = null, + var ambientLightLux: Double? = null, + var pressureHectoPascal: Double? = null, + var proximityCentimeters: Double? = null, - var gravityAcceleration : AxisSpecificGauge? = null, - var linearAcceleration : AxisSpecificGauge? = null, - var rotationVectorValues : AxisSpecificGauge? = null, - var rotationVectorCosinusThetaHalf : Double? = null, - var rotationVectorAccuracyRadians : Double? = null, + var gravityAcceleration: AxisSpecificGauge? = null, + var linearAcceleration: AxisSpecificGauge? = null, + var rotationVectorValues: AxisSpecificGauge? = null, + var rotationVectorCosinusThetaHalf: Double? = null, + var rotationVectorAccuracyRadians: Double? = null, ); -private val supportedSensors : List = listOf( +private val supportedSensors: List = listOf( Sensor.TYPE_HEADING, Sensor.TYPE_HINGE_ANGLE, Sensor.TYPE_LOW_LATENCY_OFFBODY_DETECT, @@ -69,18 +69,19 @@ private val supportedSensors : List = listOf( Sensor.TYPE_ROTATION_VECTOR, ) -val temperatureTypes : Map = mapOf( +val temperatureTypes: Map = mapOf( HardwarePropertiesManager.DEVICE_TEMPERATURE_BATTERY to "battery", HardwarePropertiesManager.DEVICE_TEMPERATURE_CPU to "cpu", HardwarePropertiesManager.DEVICE_TEMPERATURE_GPU to "gpu", HardwarePropertiesManager.DEVICE_TEMPERATURE_SKIN to "skin", -) + ) class MetricsEngine(private val context: Context) : SensorEventListener { private val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager private val hwSensorsCache = HwSensorsCache() - private val hwPropertiesManager = context.getSystemService(Context.HARDWARE_PROPERTIES_SERVICE) as HardwarePropertiesManager + private val hwPropertiesManager = + context.getSystemService(Context.HARDWARE_PROPERTIES_SERVICE) as HardwarePropertiesManager init { registerAllHwEventHandlers() @@ -204,7 +205,6 @@ class MetricsEngine(private val context: Context) : SensorEventListener { return batteryRatio.toDouble() } - //TODO fun getBatteryIsCharging(): Boolean { val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { intFilter -> context.registerReceiver(null, intFilter) @@ -222,7 +222,7 @@ class MetricsEngine(private val context: Context) : SensorEventListener { return SystemClock.elapsedRealtime() / 1000.0 } - fun getAndroidOsVersion(): String{ + fun getAndroidOsVersion(): String { return Build.VERSION.RELEASE } @@ -230,38 +230,40 @@ class MetricsEngine(private val context: Context) : SensorEventListener { return Build.MODEL } - fun getAndroidManufacturer() : String { + fun getAndroidManufacturer(): String { return Build.MANUFACTURER } - fun getHasCellularConnected() : Boolean? { + fun getHasCellularConnected(): Boolean? { val connectivityManager = context .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager? - return if (connectivityManager != null){ + return if (connectivityManager != null) { val network = connectivityManager.activeNetwork val cap = connectivityManager.getNetworkCapabilities(network) cap != null && cap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) - }else{ + } else { null } } - fun getHasWiFiConnected() : Boolean? { + fun getHasWiFiConnected(): Boolean? { val connectivityManager = context .getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager? - return if (connectivityManager != null){ + return if (connectivityManager != null) { val network = connectivityManager.activeNetwork val cap = connectivityManager.getNetworkCapabilities(network) - cap != null && (cap.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || cap.hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE)) + cap != null && (cap.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || cap.hasTransport( + NetworkCapabilities.TRANSPORT_WIFI_AWARE + )) - }else{ + } else { null } } diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/PushProxClient.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/PushProxClient.kt index 9ce722f..ba568de 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/PushProxClient.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/PushProxClient.kt @@ -7,7 +7,6 @@ import android.util.Log import io.ktor.client.HttpClient import io.ktor.client.call.body import io.ktor.client.engine.android.Android -import io.ktor.client.engine.cio.CIO import io.ktor.client.request.post import io.ktor.client.request.request import io.ktor.client.request.setBody @@ -179,7 +178,7 @@ class PushProxClient(private val pushProxConfig: PushProxConfig) { // push metrics to pushprox // only try to push metrics if device is connected to the network - if (Util.deviceIsConnectedToInternet(context.getContext())){ + if (Util.deviceIsConnectedToInternet(context.getContext())) { try { val scrapeId: String = getIdFromResponseBody(pollResponseBody) val pushRequestBody: String = composeRequestBody(scrapedMetrics, scrapeId) @@ -199,7 +198,7 @@ class PushProxClient(private val pushProxConfig: PushProxConfig) { Log.v(TAG, "Push exception $e") return } - }else{ + } else { counters.pushError() Log.d(TAG, "device is not connected to any network") } diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/RemoteWriteSender.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/RemoteWriteSender.kt index 5e33dc3..001ca0c 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/RemoteWriteSender.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/RemoteWriteSender.kt @@ -87,7 +87,8 @@ data class RemoteWriteConfiguration( class RemoteWriteSender(private val config: RemoteWriteConfiguration) { private val lastTimeRingBuffer = LastTimeRingBuffer(config.scrapeInterval) - private val storage: RemoteWriteSenderStorage = RemoteWriteSenderSimpleMemoryStorage(config.targetLabels) + private val storage: RemoteWriteSenderStorage = + RemoteWriteSenderSimpleMemoryStorage(config.targetLabels) private var scrapesAreBeingSent: Boolean = false private lateinit var client: HttpClient private var lastTimeRemoteWriteSent: Long = 0 @@ -232,7 +233,7 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) { // only send the request if device is online, otherwise throw exception // ExponentialBackoff will catch the exception - if (Util.deviceIsConnectedToInternet(config.getContext())){ + if (Util.deviceIsConnectedToInternet(config.getContext())) { val response = client.post(config.remoteWriteEndpoint) { setBody(body) headers { @@ -261,7 +262,7 @@ class RemoteWriteSender(private val config: RemoteWriteConfiguration) { throw TryExportMetricsAgainException("Status code: ${response.status.description}") } } - }else{ + } else { throw TryExportMetricsAgainException("Device is not connected to any network") } } diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/RemoteWriteSenderMemStorage.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/RemoteWriteSenderMemStorage.kt index 3837ffa..c9c6cc0 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/RemoteWriteSenderMemStorage.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/exporter/worker/RemoteWriteSenderMemStorage.kt @@ -12,29 +12,32 @@ private typealias ConverterHashMap = HashMap, MutableList< private const val TAG: String = "REMOTE_WRITE_SENDER_MEMORY_SIMPLE_STORAGE" -class RemoteWriteSenderSimpleMemoryStorage(val targetLabels: Map) : RemoteWriteSenderStorage() { +class RemoteWriteSenderSimpleMemoryStorage(val targetLabels: Map) : + RemoteWriteSenderStorage() { private val data: Queue = LinkedList() - private fun filterExpiredMetrics(metrics: MutableList) { - val now: Long = System.currentTimeMillis() - val oldestMetricTimeMs: Long = now - maxMetricsAge * 1000 - var howManyMetricsRemove = 0 + companion object { + fun filterExpiredMetrics(metrics: MutableList) { + val now: Long = System.currentTimeMillis() + val oldestMetricTimeMs: Long = now - maxMetricsAge * 1000 + var howManyMetricsRemove = 0 - // count how many metrics to remove - for (i in 0 until metrics.size) { - val scrape: MetricsScrape = metrics[i] - if (scrape.timeSeriesList.isNotEmpty()) { - if (scrape.timeSeriesList.first().sample.timeStampMs < oldestMetricTimeMs) { - howManyMetricsRemove++ - } else { - break // I suppose scrapes were performed one after another + // count how many metrics to remove + for (i in 0 until metrics.size) { + val scrape: MetricsScrape = metrics[i] + if (scrape.timeSeriesList.isNotEmpty()) { + if (scrape.timeSeriesList.first().sample.timeStampMs < oldestMetricTimeMs) { + howManyMetricsRemove++ + } else { + break // I suppose scrapes were performed one after another + } } } - } - // remove metrics - for (i in 1..howManyMetricsRemove) { - metrics.removeFirst() + // remove metrics + for (i in 1..howManyMetricsRemove) { + metrics.removeFirst() + } } } @@ -107,7 +110,7 @@ class RemoteWriteSenderSimpleMemoryStorage(val targetLabels: Map return hashmapToProtobufWriteRequest(hashmap) } - private fun addTargetLabels(labels: MutableList){ + private fun addTargetLabels(labels: MutableList) { targetLabels.forEach { val label = TimeSeriesLabel( value = it.value, diff --git a/client/app/src/test/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/LastTimeRingBufferTest.kt b/client/app/src/test/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/LastTimeRingBufferTest.kt index 5b20b2b..7741069 100644 --- a/client/app/src/test/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/LastTimeRingBufferTest.kt +++ b/client/app/src/test/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/LastTimeRingBufferTest.kt @@ -1,3 +1,5 @@ +// Author: Martin Ptacek + package com.birdthedeveloper.prometheus.android.exporter.worker import org.junit.Assert diff --git a/client/app/src/test/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSenderStorageTest.kt b/client/app/src/test/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSenderStorageTest.kt index 6949714..df38bd1 100644 --- a/client/app/src/test/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSenderStorageTest.kt +++ b/client/app/src/test/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/worker/RemoteWriteSenderStorageTest.kt @@ -1,41 +1,43 @@ +// Author: Martin Ptacek + package com.birdthedeveloper.prometheus.android.exporter.worker -import org.junit.Assert.* +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue import org.junit.Test -class RemoteWriteSenderStorageTest{ -// @Test -// fun `basic test filterExpiredMetrics`(){ -// val metrics : MutableList = mutableListOf( -// // MetricSamples must be ordered -// createDummyMetricsScrape(70), -// createDummyMetricsScrape(50), -// createDummyMetricsScrape(30), -// ) -// -// // execute SUT -// RemoteWriteSenderStorage.filterExpiredMetrics(metrics) -// -// assertEquals(2, metrics.size) -// -// // assert the right order -// val firstTimeStamp = metrics[0].timeSeriesList[0].sample.timeStampMs -// val secondTimeStamp = metrics[1].timeSeriesList[0].sample.timeStampMs -// assertTrue(firstTimeStamp < secondTimeStamp) -// } +class RemoteWriteSenderStorageTest { + @Test + fun `basic test filterExpiredMetrics`() { + val metrics: MutableList = mutableListOf( + // MetricSamples must be ordered + createDummyMetricsScrape(70), + createDummyMetricsScrape(50), + createDummyMetricsScrape(30), + ) -// private fun createDummyMetricsScrape(ageInMinutes : Int) : MetricsScrape{ -// return MetricsScrape( -// timeSeriesList = listOf( -// StorageTimeSeries( -// labels = listOf(), -// sample = TimeSeriesSample( -// // too old -// timeStampMs = System.currentTimeMillis() - ageInMinutes * 60 * 1000L, -// value = 0.0, -// ), -// ) -// ) -// ) -// } + RemoteWriteSenderSimpleMemoryStorage.filterExpiredMetrics(metrics) + + assertEquals(2, metrics.size) + + // assert the right order + val firstTimeStamp = metrics[0].timeSeriesList[0].sample.timeStampMs + val secondTimeStamp = metrics[1].timeSeriesList[0].sample.timeStampMs + assertTrue(firstTimeStamp < secondTimeStamp) + } + + private fun createDummyMetricsScrape(ageInMinutes: Int): MetricsScrape { + return MetricsScrape( + timeSeriesList = listOf( + StorageTimeSeries( + labels = listOf(), + sample = TimeSeriesSample( + // too old + timeStampMs = System.currentTimeMillis() - ageInMinutes * 60 * 1000L, + value = 0.0, + ), + ) + ) + ) + } }