Add Prometheus Service Discovery for online devices (#3788)

This commit is contained in:
Aleš Komárek 2022-09-14 05:22:59 +02:00 committed by GitHub
parent b3cca5dcb6
commit a5e3cd1a42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -644,6 +644,33 @@ def _ping_func(filename, address):
return filename, rc == 0 return filename, rc == 0
class PrometheusServiceDiscoveryHandler(BaseHandler):
@authenticated
def get(self):
entries = _list_dashboard_entries()
self.set_header("content-type", "application/json")
sd = []
for entry in entries:
if entry.web_port is None:
continue
labels = {
"__meta_name": entry.name,
"__meta_esp_platform": entry.target_platform,
"__meta_esphome_version": entry.storage.esphome_version,
}
for integration in entry.storage.loaded_integrations:
labels[f"__meta_integration_{integration}"] = "true"
sd.append(
{
"targets": [
f"{entry.address}:{entry.web_port}",
],
"labels": labels,
}
)
self.write(json.dumps(sd))
class MDNSStatusThread(threading.Thread): class MDNSStatusThread(threading.Thread):
def run(self): def run(self):
global IMPORT_RESULT global IMPORT_RESULT
@ -993,6 +1020,7 @@ def make_app(debug=get_bool_env(ENV_DEV)):
(f"{rel}import", ImportRequestHandler), (f"{rel}import", ImportRequestHandler),
(f"{rel}secret_keys", SecretKeysRequestHandler), (f"{rel}secret_keys", SecretKeysRequestHandler),
(f"{rel}rename", EsphomeRenameHandler), (f"{rel}rename", EsphomeRenameHandler),
(f"{rel}prometheus-sd", PrometheusServiceDiscoveryHandler),
], ],
**app_settings, **app_settings,
) )