Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[metering] tag rule metric with human readable rule name in addition … #2194

Merged
merged 1 commit into from
Feb 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.automation.RuleRegistry;
import org.openhab.core.io.monitor.MeterRegistryProvider;
import org.openhab.core.io.monitor.internal.metrics.BundleStateMetric;
import org.openhab.core.io.monitor.internal.metrics.EventCountMetric;
Expand Down Expand Up @@ -57,13 +58,15 @@ public class DefaultMetricsRegistration implements ReadyService.ReadyTracker, Me
private final CompositeMeterRegistry registry = Metrics.globalRegistry;
private final ReadyService readyService;
private final ThingRegistry thingRegistry;
private final RuleRegistry ruleRegistry;

@Activate
public DefaultMetricsRegistration(BundleContext bundleContext, final @Reference ReadyService readyService,
final @Reference ThingRegistry thingRegistry) {
final @Reference ThingRegistry thingRegistry, final @Reference RuleRegistry ruleRegistry) {
this.bundleContext = bundleContext;
this.readyService = readyService;
this.thingRegistry = thingRegistry;
this.ruleRegistry = ruleRegistry;
}

@Activate
Expand All @@ -87,7 +90,7 @@ private void registerMeters() {
meters.add(new BundleStateMetric(bundleContext, tags));
meters.add(new ThingStateMetric(bundleContext, thingRegistry, tags));
meters.add(new EventCountMetric(bundleContext, tags));
meters.add(new RuleMetric(bundleContext, tags));
meters.add(new RuleMetric(bundleContext, tags, ruleRegistry));
meters.add(new ThreadPoolMetric(tags));

meters.forEach(m -> m.bindTo(registry));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Set;

import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.automation.Rule;
import org.openhab.core.automation.RuleRegistry;
import org.openhab.core.automation.RuleStatus;
import org.openhab.core.automation.events.RuleStatusInfoEvent;
import org.openhab.core.events.Event;
Expand Down Expand Up @@ -47,16 +49,19 @@ public class RuleMetric implements OpenhabCoreMeterBinder, EventSubscriber {
public static final String RULES_TOPIC_FILTER = "openhab/rules/*";
private final Logger logger = LoggerFactory.getLogger(RuleMetric.class);
private static final Tag CORE_RULE_METRIC_TAG = Tag.of("metric", "openhab.core.metric.rules");
private static final String RULE_TAG_NAME = "rule";
private static final String RULE_ID_TAG_NAME = "rule";
private static final String RULE_NAME_TAG_NAME = "rulename";
private @Nullable MeterRegistry meterRegistry;
private final Set<Tag> tags = new HashSet<>();
private ServiceRegistration<?> eventSubscriberRegistration;
private BundleContext bundleContext;
private RuleRegistry ruleRegistry;

public RuleMetric(BundleContext bundleContext, Collection<Tag> tags) {
public RuleMetric(BundleContext bundleContext, Collection<Tag> tags, RuleRegistry ruleRegistry) {
this.tags.addAll(tags);
this.tags.add(CORE_RULE_METRIC_TAG);
this.bundleContext = bundleContext;
this.ruleRegistry = ruleRegistry;
}

@Override
Expand Down Expand Up @@ -106,15 +111,27 @@ public void receive(Event event) {
return;
}
String topic = event.getTopic();
String rule = topic.substring(RULES_TOPIC_PREFIX.length(), topic.indexOf(RULES_TOPIC_SUFFIX));
String ruleId = topic.substring(RULES_TOPIC_PREFIX.length(), topic.indexOf(RULES_TOPIC_SUFFIX));
if (!event.getPayload().contains(RuleStatus.RUNNING.name())) {
logger.trace("Skipping rule status info with status other than RUNNING {}", event.getPayload());
return;
}

logger.debug("Rule {} RUNNING - updating metric.", rule);
logger.debug("Rule {} RUNNING - updating metric.", ruleId);
Set<Tag> tagsWithRule = new HashSet<>(tags);
tagsWithRule.add(Tag.of(RULE_TAG_NAME, rule));
tagsWithRule.add(Tag.of(RULE_ID_TAG_NAME, ruleId));
String ruleName = getRuleName(ruleId);
if (ruleName != null) {
tagsWithRule.add(Tag.of(RULE_NAME_TAG_NAME, ruleName));
}
meterRegistry.counter(METRIC_NAME, tagsWithRule).increment();
}

private String getRuleName(String ruleId) {
Rule rule = ruleRegistry.get(ruleId);
if (rule != null) {
return rule.getName();
}
return null;
}
}