diff --git a/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.exposable.OpenHABBundleStateMetric.xml b/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.exposable.OpenHABBundleStateMetric.xml new file mode 100644 index 0000000..0e93b5a --- /dev/null +++ b/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.exposable.OpenHABBundleStateMetric.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.exposable.ThreadPoolMetric.xml b/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.exposable.ThreadPoolMetric.xml new file mode 100644 index 0000000..58ac268 --- /dev/null +++ b/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.exposable.ThreadPoolMetric.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.internal.KuguThreadPoolManager.xml b/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.internal.KuguThreadPoolManager.xml new file mode 100644 index 0000000..aa97165 --- /dev/null +++ b/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.internal.KuguThreadPoolManager.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.rest.PrometheusMetricsRESTResource.xml b/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.rest.PrometheusMetricsRESTResource.xml index 9914367..fc17f06 100644 --- a/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.rest.PrometheusMetricsRESTResource.xml +++ b/com.kuguhome.openhab.prometheusmetrics/OSGI-INF/com.kuguhome.openhab.prometheusmetrics.rest.PrometheusMetricsRESTResource.xml @@ -1,13 +1,15 @@ - + - - - - - + + + + + + + \ No newline at end of file diff --git a/com.kuguhome.openhab.prometheusmetrics/src/main/java/com/kuguhome/openhab/prometheusmetrics/exposable/ThreadPoolMetric.java b/com.kuguhome.openhab.prometheusmetrics/src/main/java/com/kuguhome/openhab/prometheusmetrics/exposable/ThreadPoolMetric.java index b5f453b..d9121b4 100644 --- a/com.kuguhome.openhab.prometheusmetrics/src/main/java/com/kuguhome/openhab/prometheusmetrics/exposable/ThreadPoolMetric.java +++ b/com.kuguhome.openhab.prometheusmetrics/src/main/java/com/kuguhome/openhab/prometheusmetrics/exposable/ThreadPoolMetric.java @@ -8,7 +8,6 @@ import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.ReferenceCardinality; -import org.osgi.service.component.annotations.ReferencePolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -62,6 +61,7 @@ public class ThreadPoolMetric implements RESTExposable { "the amount of time that threads in excess of the regular pool size may remain idle before being terminated") .labelNames("pool").register(restRegistry); + @Reference(cardinality = ReferenceCardinality.MANDATORY) protected KuguThreadPoolManager threadPoolManager; @Override @@ -128,13 +128,4 @@ protected void deactivate() { logger.info(ThreadPoolMetric.class.getName() + " deactivated."); } - public void unsetThreadPoolManager() { - this.threadPoolManager = null; - } - - @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC) - public void setThreadPoolManager(KuguThreadPoolManager threadPoolManager) { - this.threadPoolManager = threadPoolManager; - } - } diff --git a/com.kuguhome.openhab.prometheusmetrics/src/main/java/com/kuguhome/openhab/prometheusmetrics/rest/PrometheusMetricsRESTResource.java b/com.kuguhome.openhab.prometheusmetrics/src/main/java/com/kuguhome/openhab/prometheusmetrics/rest/PrometheusMetricsRESTResource.java index b79692d..d2ebae6 100644 --- a/com.kuguhome.openhab.prometheusmetrics/src/main/java/com/kuguhome/openhab/prometheusmetrics/rest/PrometheusMetricsRESTResource.java +++ b/com.kuguhome.openhab.prometheusmetrics/src/main/java/com/kuguhome/openhab/prometheusmetrics/rest/PrometheusMetricsRESTResource.java @@ -35,11 +35,13 @@ import org.slf4j.LoggerFactory; import com.kuguhome.openhab.prometheusmetrics.api.DefaultMetricManager; -import com.kuguhome.openhab.prometheusmetrics.api.MetricManager; import com.kuguhome.openhab.prometheusmetrics.api.RESTExposable; import com.kuguhome.openhab.prometheusmetrics.exposable.InboxCountMetric; import com.kuguhome.openhab.prometheusmetrics.exposable.JVMMetric; +import com.kuguhome.openhab.prometheusmetrics.exposable.OpenHABBundleStateMetric; import com.kuguhome.openhab.prometheusmetrics.exposable.OpenHABThingStateMetric; +import com.kuguhome.openhab.prometheusmetrics.exposable.SmarthomeEventCountMetric; +import com.kuguhome.openhab.prometheusmetrics.exposable.ThreadPoolMetric; import io.prometheus.client.CollectorRegistry; import io.prometheus.client.exporter.common.TextFormat; @@ -61,25 +63,10 @@ public class PrometheusMetricsRESTResource implements RESTResource { private final Logger logger = LoggerFactory.getLogger(PrometheusMetricsRESTResource.class); - public static final String METRICS_ALIAS = "/metrics"; - /* - * public static final String COUNTER_NAME = "logback_appender_total"; - * - * private final static Counter logCounter = Counter - * .build("openhab_logmessages_total", "Logback log statements at various log levels").labelNames("level") - * .register(CollectorRegistry.defaultRegistry); - * private final static Gauge logErrorCounter = Gauge - * .build("openhab_logmessages_error", "Logback log statements at various log levels").labelNames("type") - * .register(CollectorRegistry.defaultRegistry); - * - * public static final Counter.Child TRACE_LABEL = logCounter.labels("trace"); - * public static final Counter.Child DEBUG_LABEL = logCounter.labels("debug"); - * public static final Counter.Child INFO_LABEL = logCounter.labels("info"); - * public static final Counter.Child WARN_LABEL = logCounter.labels("warn"); - * public static final Counter.Child ERROR_LABEL = logCounter.labels("error"); - */ public static final String PATH_HABMETRICS = "metrics"; + public static final String METRICS_ALIAS = "/" + PATH_HABMETRICS; + @Reference protected HttpService httpService; @GET @@ -91,13 +78,7 @@ public class PrometheusMetricsRESTResource implements RESTResource { @ApiResponse(code = 404, message = "Unknown page") }) public Response getThingsMetricsPrometheus(@Context HttpServletRequest request, @Context HttpServletResponse response) throws Exception { - /* - * simpleMetric.set("simple_metric", 0, new HashMap() { - * { - * put("test_label", Math.random()); - * } - * }); - */ + metricManager.getExposables().parallelStream().filter(Objects::nonNull).forEach(RESTExposable::expose); final StringWriter writer = new StringWriter(); @@ -111,14 +92,11 @@ protected void activate() { JVMMetric.initialize(); - // metricManager.registerMetric(openHABBundleStateMetric); - // metricManager.registerMetric(threadPoolMetric); + metricManager.registerMetric(openHABBundleStateMetric); + metricManager.registerMetric(threadPoolMetric); metricManager.registerMetric(smarthomeEventCountMetric); metricManager.registerMetric(openHABThingStateMetric); metricManager.registerMetric(inboxCountMetric); - // metricManager.registerMetric(null); - // metricManager.registerMetric(simpleMetric); - // metricManager.registerMetric(new ToDoMetric()); try { httpService.registerResources(METRICS_ALIAS, "web", null); @@ -135,88 +113,21 @@ protected void deactivate() { } @Reference - protected void setHttpService(HttpService httpService) { - this.httpService = httpService; - } - - protected void unsetHttpService(HttpService httpService) { - this.httpService = null; - } - - protected MetricManager metricManager; - - protected RESTExposable inboxCountMetric; - // protected RESTExposable openHABBundleStateMetric; - protected RESTExposable openHABThingStateMetric; - protected RESTExposable smarthomeEventCountMetric; - // protected RESTExposable threadPoolMetric; - // protected MetricSettable simpleMetric; - - public void unsetMetricManager() { - this.metricManager = null; - } - - @Reference - public void setMetricManager(DefaultMetricManager metricManager) { - this.metricManager = metricManager; - } + protected DefaultMetricManager metricManager; - public void unsetInboxCountMetric() { - this.inboxCountMetric = null; - } + @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC) + volatile protected InboxCountMetric inboxCountMetric; - @Reference(cardinality = ReferenceCardinality.MANDATORY, name = "InboxCountMetric", policy = ReferencePolicy.DYNAMIC) - public void setInboxCountMetric(InboxCountMetric inboxCountMetric) { - this.inboxCountMetric = inboxCountMetric; - } + @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC) + volatile protected OpenHABBundleStateMetric openHABBundleStateMetric; - /* - * public void unsetOpenHABBundleStateMetric() { - * this.openHABBundleStateMetric = null; - * } - * - * @Reference(cardinality = ReferenceCardinality.MANDATORY, name = "OpenHABBundleStateMetric", policy = - * ReferencePolicy.DYNAMIC) - * public void setOpenHABBundleStateMetric(OpenHABBundleStateMetric openHABBundleStateMetric) { - * this.openHABBundleStateMetric = openHABBundleStateMetric; - * } - */ - public void unsetOpenHABThingStateMetric() { - this.openHABThingStateMetric = null; - } + @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC) + volatile protected OpenHABThingStateMetric openHABThingStateMetric; - @Reference(cardinality = ReferenceCardinality.MANDATORY, name = "OpenHABThingStateMetric", policy = ReferencePolicy.DYNAMIC) - public void setOpenHABThingStateMetric(OpenHABThingStateMetric openHABThingStateMetric) { - this.openHABThingStateMetric = openHABThingStateMetric; - } + @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC) + volatile protected SmarthomeEventCountMetric smarthomeEventCountMetric; - public void unsetSmarthomeEventCountMetric() { - this.smarthomeEventCountMetric = null; - } + @Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC) + volatile protected ThreadPoolMetric threadPoolMetric; - @Reference(cardinality = ReferenceCardinality.MANDATORY, name = "SmarthomeEventCountMetric", policy = ReferencePolicy.DYNAMIC) - public void setSmarthomeEventCountMetric(RESTExposable smarthomeEventCountMetric) { - this.smarthomeEventCountMetric = smarthomeEventCountMetric; - } - /* - * public void unsetSimpleMetric() { - * this.simpleMetric = null; - * } - * - * @Reference(cardinality = ReferenceCardinality.MANDATORY, name = "SimpleMetric", policy = ReferencePolicy.DYNAMIC) - * public void setSimpleMetric(MetricSettable simpleMetric) { - * this.simpleMetric = simpleMetric; - * } - */ - /* - * public void unsetThreadPoolMetric() { - * this.threadPoolMetric = null; - * } - * - * @Reference(cardinality = ReferenceCardinality.MANDATORY, name = "ThreadPoolMetric", policy = - * ReferencePolicy.DYNAMIC) - * public void setThreadPoolMetric(RESTExposable threadPoolMetric) { - * this.threadPoolMetric = threadPoolMetric; - * } - */ }