mirror of
https://github.com/mii443/prometheus-android-exporter.git
synced 2025-08-22 15:15:35 +00:00
expose battery percentage
This commit is contained in:
@ -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<MetricFamilySamples> {
|
||||
val mfs: MutableList<MetricFamilySamples> = 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<MetricFamilySamples>) {
|
||||
fun collectBatteryStatus(mfs: MutableList<MetricFamilySamples>) {
|
||||
//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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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())
|
||||
|
@ -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)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user