From 6e36ad54fcd4e2254fcc144ede424a259144e301 Mon Sep 17 00:00:00 2001 From: Michal Date: Thu, 24 Oct 2024 15:03:46 +0100 Subject: [PATCH] Add Labels to Metrics with Device Names --- Program.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Program.cs b/Program.cs index 1dfc4e8..92612e9 100644 --- a/Program.cs +++ b/Program.cs @@ -27,6 +27,7 @@ namespace PrometheusExporterEdenic private readonly string _orgid; private readonly string _apimainurl = "https://api.edenic.io/api/v1"; private string _devicename; + public string DeviceName => _devicename; public string DeviceId { get; private set; } public int ph { get; private set; } public int temperature { get; private set; } @@ -201,9 +202,18 @@ namespace PrometheusExporterEdenic }); }); - var ph = Metrics.CreateGauge("edenic_ph", "Edenic ph"); - var temperature = Metrics.CreateGauge("edenic_temperature", "Edenic temperature"); - var ec = Metrics.CreateGauge("edenic_ec", "Edenic EC"); + var phGauge = Metrics.CreateGauge("edenic_ph", "Edenic ph", new GaugeConfiguration + { + LabelNames = new[] { "device_name" } + }); + var temperatureGauge = Metrics.CreateGauge("edenic_temperature", "Edenic temperature", new GaugeConfiguration + { + LabelNames = new[] { "device_name" } + }); + var ecGauge = Metrics.CreateGauge("edenic_ec", "Edenic EC", new GaugeConfiguration + { + LabelNames = new[] { "device_name" } + }); string EDENIC_API_TOKEN = Environment.GetEnvironmentVariable("EDENIC_API_TOKEN"); string EDENIC_ORG_ID = Environment.GetEnvironmentVariable("EDENIC_ORG_ID"); @@ -217,9 +227,9 @@ namespace PrometheusExporterEdenic while (true) { await client.GetTelemetry(); - ph.Set(client.ph / 100.0); // Convert back to float - temperature.Set(client.temperature / 100.0); // Convert back to float - ec.Set(client.ec / 100.0); // Convert back to float + phGauge.WithLabels(client.DeviceName).Set(client.ph / 100.0); + temperatureGauge.WithLabels(client.DeviceName).Set(client.temperature / 100.0); + ecGauge.WithLabels(client.DeviceName).Set(client.ec / 100.0); await Task.Delay(60000); } });