001/*
002 * Copyright (C) 2020-2023 The Prometheus jmx_exporter Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package io.prometheus.jmx;
018
019import io.prometheus.client.Collector.Type;
020import java.util.List;
021
022/**
023 * MatchedRule is the result of matching a JMX bean against the rules present in the configuration
024 * file. As rules are matched using regular expressions, caching helps prevent having to match the
025 * same beans to the same list of regular expressions.
026 */
027public class MatchedRule {
028
029    final String name;
030    final String matchName;
031    final Type type;
032    final String help;
033    final List<String> labelNames;
034    final List<String> labelValues;
035    final Double value;
036    final double valueFactor;
037
038    private static final MatchedRule _unmatched = new MatchedRule();
039
040    private MatchedRule() {
041        this.name = null;
042        this.matchName = null;
043        this.type = null;
044        this.help = null;
045        this.labelNames = null;
046        this.labelValues = null;
047        this.value = null;
048        this.valueFactor = 1.0;
049    }
050
051    public MatchedRule(
052            final String name,
053            final String matchName,
054            final Type type,
055            final String help,
056            final List<String> labelNames,
057            final List<String> labelValues,
058            final Double value,
059            double valueFactor) {
060        this.name = name;
061        this.matchName = matchName;
062        this.type = type;
063        this.help = help;
064        this.labelNames = labelNames;
065        this.labelValues = labelValues;
066        this.value = value;
067        this.valueFactor = valueFactor;
068    }
069
070    /**
071     * An unmatched MatchedRule, used when no rule matching a JMX bean has been found in the
072     * configuration. Cached unmatched rules are still a cache hit, that will not produce any
073     * metric/value.
074     *
075     * @return the invalid rule
076     */
077    public static MatchedRule unmatched() {
078        return _unmatched;
079    }
080
081    public boolean isUnmatched() {
082        return this == _unmatched;
083    }
084
085    public boolean isMatched() {
086        return !isUnmatched();
087    }
088}