diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/AndroidCustomExporter.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/AndroidCustomExporter.kt index 734cea7..9ed96a7 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/AndroidCustomExporter.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/AndroidCustomExporter.kt @@ -7,35 +7,27 @@ import kotlinx.coroutines.runBlocking import java.util.Arrays import kotlinx.coroutines.* -class AndroidCustomExporter(name: String) : Collector() { +class AndroidCustomExporter(metricEngine: MetricsEngine) : Collector() { + private val metricsEngineRef = metricEngine - -//TODO pass context here, get baterry percentage from metrics engine override fun collect(): List { val mfs: MutableList = ArrayList() - // With no labels. - mfs.add(GaugeMetricFamily("my_gauge", "help", 42.0)) - // With labels - val labeledGauge = GaugeMetricFamily("my_other_gauge", "help", Arrays.asList("labelname")) - labeledGauge.addMetric(listOf("foo"), 4.0) - labeledGauge.addMetric(listOf("bar"), 5.0) - mfs.add(labeledGauge) - println("Start blocking") + + // metrics definitions + collectBatteryStatus(mfs) + return mfs } - fun collectBaterryStatus(mfs: MutableList) { + fun collectBatteryStatus(mfs: MutableList) { //TODO - val baterryPercentageGauge = GaugeMetricFamily( - "baterry_percentage", - "Baterry percentage", listOf(), + val batteryPercentageGauge = GaugeMetricFamily( + "battery_percentage", + "Current battery percentage", listOf(), ) - - - //baterryPercentageGauge.addMetric() - - + val batteryPercentage : Double = metricsEngineRef.getBatteryPercentage().toDouble() + batteryPercentageGauge.addMetric(listOf(), batteryPercentage) + mfs.add(batteryPercentageGauge) } - -} \ No newline at end of file +} diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/MainActivity.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/MainActivity.kt index 3512c12..2df3477 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/MainActivity.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/MainActivity.kt @@ -1,6 +1,7 @@ package com.birdthedeveloper.prometheus.android.prometheus.android.exporter import android.os.Bundle +import android.text.BoringLayout.Metrics import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.fillMaxSize @@ -22,13 +23,14 @@ import java.io.StringWriter class MainActivity : ComponentActivity() { - // register custom prometheus exporter - val collectorRegistry: CollectorRegistry = CollectorRegistry() - val customExporter: AndroidCustomExporter = AndroidCustomExporter("dummy") - .register(collectorRegistry) + private val collectorRegistry: CollectorRegistry = CollectorRegistry() + private lateinit var metricsEngine: MetricsEngine + private lateinit var customExporter: AndroidCustomExporter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + initialize() + setContent { PrometheusAndroidExporterTheme { // A surface container using the 'background' color from the theme @@ -42,6 +44,11 @@ class MainActivity : ComponentActivity() { } } + private fun initialize (){ + metricsEngine = MetricsEngine(this.applicationContext) + customExporter = AndroidCustomExporter(metricsEngine).register(collectorRegistry) + } + fun CollectMetrics(): String{ val writer = StringWriter() TextFormat.write004(writer, collectorRegistry.metricFamilySamples()) diff --git a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/MetricsEngine.kt b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/MetricsEngine.kt index 378ce15..0b9251c 100644 --- a/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/MetricsEngine.kt +++ b/client/app/src/main/java/com/birdthedeveloper/prometheus/android/prometheus/android/exporter/MetricsEngine.kt @@ -1,12 +1,25 @@ package com.birdthedeveloper.prometheus.android.prometheus.android.exporter +import android.content.Context import android.content.Intent import android.content.IntentFilter +import android.os.BatteryManager -class MetricsEngine { -// public fun getBatteryPercentage(){ -// val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { ifilter -> -// context.registerReceiver(null, ifilter) -// } -// } -} \ No newline at end of file +class MetricsEngine(context: Context) { + private val contextRef = context + public fun getBatteryPercentage() : Float{ + val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { intFilter -> + contextRef.registerReceiver(null, intFilter) + } + //val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1 + + val batteryPct: Float? = batteryStatus?.let { intent -> + val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) + val scale: Int = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1) + level * 100 / scale.toFloat() + } + + batteryPct?: return -1.0f + return batteryPct + } +}