Skip to content

Commit

Permalink
Fixing all metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanno - Felix Wagner committed Jul 26, 2018
1 parent e6b3b88 commit 3e243ca
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 124 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="com.kuguhome.openhab.prometheusmetrics.exposable.OpenHABBundleStateMetric">
<service>
<provide interface="com.kuguhome.openhab.prometheusmetrics.exposable.OpenHABBundleStateMetric"/>
<provide interface="com.kuguhome.openhab.prometheusmetrics.api.RESTExposable"/>
</service>
<implementation class="com.kuguhome.openhab.prometheusmetrics.exposable.OpenHABBundleStateMetric"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" activate="activate" deactivate="deactivate" name="com.kuguhome.openhab.prometheusmetrics.exposable.ThreadPoolMetric">
<service>
<provide interface="com.kuguhome.openhab.prometheusmetrics.exposable.ThreadPoolMetric"/>
<provide interface="com.kuguhome.openhab.prometheusmetrics.api.RESTExposable"/>
</service>
<reference cardinality="1..1" field="threadPoolManager" interface="com.kuguhome.openhab.prometheusmetrics.internal.KuguThreadPoolManager" name="threadPoolManager"/>
<implementation class="com.kuguhome.openhab.prometheusmetrics.exposable.ThreadPoolMetric"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="com.kuguhome.openhab.prometheusmetrics.internal.KuguThreadPoolManager">
<service>
<provide interface="com.kuguhome.openhab.prometheusmetrics.internal.KuguThreadPoolManager"/>
<provide interface="org.eclipse.smarthome.core.common.ThreadPoolManager"/>
</service>
<implementation class="com.kuguhome.openhab.prometheusmetrics.internal.KuguThreadPoolManager"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" activate="activate" deactivate="deactivate" name="com.kuguhome.openhab.prometheusmetrics.rest.PrometheusMetricsRESTResource">
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.3.0" activate="activate" deactivate="deactivate" name="com.kuguhome.openhab.prometheusmetrics.rest.PrometheusMetricsRESTResource">
<service>
<provide interface="org.eclipse.smarthome.io.rest.RESTResource"/>
<provide interface="com.kuguhome.openhab.prometheusmetrics.rest.PrometheusMetricsRESTResource"/>
</service>
<reference bind="setHttpService" interface="org.osgi.service.http.HttpService" name="HttpService" unbind="unsetHttpService"/>
<reference bind="setInboxCountMetric" cardinality="1..1" interface="com.kuguhome.openhab.prometheusmetrics.exposable.InboxCountMetric" name="InboxCountMetric" policy="dynamic"/>
<reference bind="setMetricManager" interface="com.kuguhome.openhab.prometheusmetrics.api.DefaultMetricManager" name="MetricManager"/>
<reference bind="setOpenHABThingStateMetric" cardinality="1..1" interface="com.kuguhome.openhab.prometheusmetrics.exposable.OpenHABThingStateMetric" name="OpenHABThingStateMetric" policy="dynamic"/>
<reference bind="setSmarthomeEventCountMetric" cardinality="1..1" interface="com.kuguhome.openhab.prometheusmetrics.api.RESTExposable" name="SmarthomeEventCountMetric" policy="dynamic"/>
<reference cardinality="1..1" field="httpService" interface="org.osgi.service.http.HttpService" name="httpService"/>
<reference cardinality="1..1" field="inboxCountMetric" interface="com.kuguhome.openhab.prometheusmetrics.exposable.InboxCountMetric" name="inboxCountMetric" policy="dynamic"/>
<reference cardinality="1..1" field="metricManager" interface="com.kuguhome.openhab.prometheusmetrics.api.DefaultMetricManager" name="metricManager"/>
<reference cardinality="1..1" field="openHABBundleStateMetric" interface="com.kuguhome.openhab.prometheusmetrics.exposable.OpenHABBundleStateMetric" name="openHABBundleStateMetric" policy="dynamic"/>
<reference cardinality="1..1" field="openHABThingStateMetric" interface="com.kuguhome.openhab.prometheusmetrics.exposable.OpenHABThingStateMetric" name="openHABThingStateMetric" policy="dynamic"/>
<reference cardinality="1..1" field="smarthomeEventCountMetric" interface="com.kuguhome.openhab.prometheusmetrics.exposable.SmarthomeEventCountMetric" name="smarthomeEventCountMetric" policy="dynamic"/>
<reference cardinality="1..1" field="threadPoolMetric" interface="com.kuguhome.openhab.prometheusmetrics.exposable.ThreadPoolMetric" name="threadPoolMetric" policy="dynamic"/>
<implementation class="com.kuguhome.openhab.prometheusmetrics.rest.PrometheusMetricsRESTResource"/>
</scr:component>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<String, Double>() {
* {
* put("test_label", Math.random());
* }
* });
*/

metricManager.getExposables().parallelStream().filter(Objects::nonNull).forEach(RESTExposable::expose);

final StringWriter writer = new StringWriter();
Expand All @@ -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);
Expand All @@ -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;
* }
*/
}

0 comments on commit 3e243ca

Please sign in to comment.