mirror of
https://github.com/mii443/prometheus-android-exporter.git
synced 2025-08-22 23:25:40 +00:00
expose battery percentage
This commit is contained in:
@ -7,35 +7,27 @@ import kotlinx.coroutines.runBlocking
|
|||||||
import java.util.Arrays
|
import java.util.Arrays
|
||||||
import kotlinx.coroutines.*
|
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> {
|
override fun collect(): List<MetricFamilySamples> {
|
||||||
val mfs: MutableList<MetricFamilySamples> = ArrayList()
|
val mfs: MutableList<MetricFamilySamples> = ArrayList()
|
||||||
// With no labels.
|
|
||||||
mfs.add(GaugeMetricFamily("my_gauge", "help", 42.0))
|
// metrics definitions
|
||||||
// With labels
|
collectBatteryStatus(mfs)
|
||||||
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")
|
|
||||||
return mfs
|
return mfs
|
||||||
}
|
}
|
||||||
|
|
||||||
fun collectBaterryStatus(mfs: MutableList<MetricFamilySamples>) {
|
fun collectBatteryStatus(mfs: MutableList<MetricFamilySamples>) {
|
||||||
//TODO
|
//TODO
|
||||||
val baterryPercentageGauge = GaugeMetricFamily(
|
val batteryPercentageGauge = GaugeMetricFamily(
|
||||||
"baterry_percentage",
|
"battery_percentage",
|
||||||
"Baterry percentage", listOf(),
|
"Current battery percentage", listOf(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
val batteryPercentage : Double = metricsEngineRef.getBatteryPercentage().toDouble()
|
||||||
|
batteryPercentageGauge.addMetric(listOf(), batteryPercentage)
|
||||||
//baterryPercentageGauge.addMetric()
|
mfs.add(batteryPercentageGauge)
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter
|
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.text.BoringLayout.Metrics
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
@ -22,13 +23,14 @@ import java.io.StringWriter
|
|||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
|
|
||||||
// register custom prometheus exporter
|
private val collectorRegistry: CollectorRegistry = CollectorRegistry()
|
||||||
val collectorRegistry: CollectorRegistry = CollectorRegistry()
|
private lateinit var metricsEngine: MetricsEngine
|
||||||
val customExporter: AndroidCustomExporter = AndroidCustomExporter("dummy")
|
private lateinit var customExporter: AndroidCustomExporter
|
||||||
.register(collectorRegistry)
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
initialize()
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
PrometheusAndroidExporterTheme {
|
PrometheusAndroidExporterTheme {
|
||||||
// A surface container using the 'background' color from the theme
|
// 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{
|
fun CollectMetrics(): String{
|
||||||
val writer = StringWriter()
|
val writer = StringWriter()
|
||||||
TextFormat.write004(writer, collectorRegistry.metricFamilySamples())
|
TextFormat.write004(writer, collectorRegistry.metricFamilySamples())
|
||||||
|
@ -1,12 +1,25 @@
|
|||||||
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter
|
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.IntentFilter
|
import android.content.IntentFilter
|
||||||
|
import android.os.BatteryManager
|
||||||
|
|
||||||
class MetricsEngine {
|
class MetricsEngine(context: Context) {
|
||||||
// public fun getBatteryPercentage(){
|
private val contextRef = context
|
||||||
// val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { ifilter ->
|
public fun getBatteryPercentage() : Float{
|
||||||
// context.registerReceiver(null, ifilter)
|
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