max metrics added

This commit is contained in:
Martin Ptáček
2023-07-30 11:32:20 +02:00
parent c532bf1d8b
commit e75200e6a3
3 changed files with 33 additions and 64 deletions

View File

@ -95,14 +95,11 @@ $ ansible-playbook ansible_playbook.yaml --tags config
`android_cellular_network_connected` - Whether cellular network is connected
`android_wifi_connected` - Whether WiFi is connected
### Miscellaneous
`android_battery_charge_ratio` - Current battery charge
`android_battery_is_charging` - Whether the battery is charging
`android_system_info{manufacturer, model, os_release, cpu_core_count}` - Information about Android system
`android_uptime_seconds` - Phone uptime in seconds
`android_cpu_active_seconds{core}` - Active CPU time in seconds since last time system booted
`android_cpu_total_seconds{core}` - Total CPU time in seconds since last time system booted
`android_system_temperature_celsius{where}` - Temperature on the device
`android_scrape_duration_seconds` - Duration of the metric scrape
### PushProx client mode specific metrics

View File

@ -13,23 +13,24 @@ private const val TAG = "ANDROID_EXPORTER"
class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector() {
override fun collect(): List<MetricFamilySamples> {
val startTime = System.currentTimeMillis()
Log.d(TAG, "Collecting metrics now")
val mfs: MutableList<MetricFamilySamples> = ArrayList()
val startTime = System.currentTimeMillis()
// metrics definitions
collectBatteryChargeRatio(mfs)
collectUptimeInSeconds(mfs)
// collectDeviceTemperatures(mfs)
// collectCpuUsage(mfs)
collectHasWiFiConnection(mfs)
collectHasCellularConnection(mfs)
collectAndroidInfo(mfs)
collectBatteryIsCharging(mfs)
collectHardwareSensors(mfs)
collectScrapeDuration(mfs, startTime)
Log.d(TAG, "Metrics collected")
collectScrapeDuration(mfs, startTime)
return mfs
}
@ -53,43 +54,6 @@ class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector
mfs.add(gauge)
}
//TODO this does not work
private fun collectCpuUsage(mfs : MutableList<MetricFamilySamples>){
var coreIndex= 0
val cpuUsage : Array<CpuUsageInfo> = metricEngine.getCpuUsage()
val gaugeActive = GaugeMetricFamily(
"android_cpu_active_seconds",
"Active CPU time in seconds since last system booted",
listOf("core"),
)
val gaugeTotal = GaugeMetricFamily(
"android_cpu_total_seconds",
"Total CPU time in seconds since last system booted",
listOf("core")
)
cpuUsage.forEach {
gaugeActive.addMetric(listOf((coreIndex++).toString()), it.active / 1000.0)
gaugeActive.addMetric(listOf((coreIndex++).toString()), it.total / 1000.0)
}
mfs.addAll(listOf(gaugeTotal, gaugeActive))
}
//TODO does not work
private fun collectDeviceTemperatures(mfs : MutableList<MetricFamilySamples>){
val deviceTemperatures = metricEngine.getDeviceTemperatures()
val gauge = GaugeMetricFamily(
"android_system_temperature_celsius{where}` - ",
"Temperature on the device",
listOf("where")
)
deviceTemperatures.entries.forEach{
gauge.addMetric(listOf(it.key), it.value)
}
mfs.add(gauge)
}
private fun collectHasWiFiConnection(mfs : MutableList<MetricFamilySamples>){
metricEngine.getHasWiFiConnected()?.let {
val result : Double = if (it) {
@ -108,6 +72,22 @@ class AndroidCustomExporter(private val metricEngine: MetricsEngine) : Collector
}
}
private fun collectBatteryIsCharging(mfs: MutableList<MetricFamilySamples>){
val result = if (metricEngine.getBatteryIsCharging()){
1.0
}else{
0.0
}
val gauge = GaugeMetricFamily(
"android_battery_is_charging",
"Android battery state",
listOf(),
)
gauge.addMetric(listOf(), result)
mfs.add(gauge)
}
private fun collectHasCellularConnection(mfs : MutableList<MetricFamilySamples>){
metricEngine.getHasCellularConnected()?.let {
val result : Double = if (it) {

View File

@ -2,6 +2,7 @@
package com.birdthedeveloper.prometheus.android.exporter.worker
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
@ -192,7 +193,6 @@ class MetricsEngine(private val context: Context) : SensorEventListener {
val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { intFilter ->
context.registerReceiver(null, intFilter)
}
//val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
val batteryRatio: Float? = batteryStatus?.let { intent ->
val level: Int = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)
@ -204,6 +204,16 @@ 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)
}
val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
return status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL
}
fun getNumberOfCpuCores(): Int {
return Os.sysconf(OsConstants._SC_NPROCESSORS_CONF).toInt()
}
@ -212,24 +222,6 @@ class MetricsEngine(private val context: Context) : SensorEventListener {
return SystemClock.elapsedRealtime() / 1000.0
}
fun getCpuUsage() : Array<CpuUsageInfo> {
return hwPropertiesManager.cpuUsages
}
fun getDeviceTemperatures() : Map<String, Double> {
val result = mutableMapOf<String, Double>()
temperatureTypes.keys.forEach { type ->
val array = hwPropertiesManager.getDeviceTemperatures(
type,
HardwarePropertiesManager.TEMPERATURE_CURRENT
)
if(array.isNotEmpty()){
result[temperatureTypes[type]!!] = array[0].toDouble()
}
}
return result
}
fun getAndroidOsVersion(): String{
return Build.VERSION.RELEASE
}