001/* 002 * Copyright (C) 2020-present 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.metrics.model.snapshots.PrometheusNaming; 020import java.util.List; 021import java.util.Objects; 022 023/** 024 * MatchedRule is the result of matching a JMX bean against the rules present in the configuration 025 * file. As rules are matched using regular expressions, caching helps prevent having to match the 026 * same beans to the same list of regular expressions. 027 */ 028public class MatchedRule { 029 030 final String name; 031 final String matchName; 032 final String type; 033 final String help; 034 final List<String> labelNames; 035 final List<String> labelValues; 036 final Double value; 037 final double valueFactor; 038 039 private static final MatchedRule _unmatched = new MatchedRule(); 040 041 private MatchedRule() { 042 this.name = null; 043 this.matchName = null; 044 this.type = null; 045 this.help = null; 046 this.labelNames = null; 047 this.labelValues = null; 048 this.value = null; 049 this.valueFactor = 1.0; 050 } 051 052 public MatchedRule( 053 final String name, 054 final String matchName, 055 final String type, 056 final String help, 057 final List<String> labelNames, 058 final List<String> labelValues, 059 final Double value, 060 double valueFactor) { 061 this.name = name; 062 this.matchName = matchName; 063 this.type = type; 064 this.help = help; 065 this.labelNames = labelNames; 066 this.labelValues = labelValues; 067 this.value = value; 068 this.valueFactor = valueFactor; 069 } 070 071 public MatchedRule withValue(double value) { 072 return new MatchedRule( 073 PrometheusNaming.sanitizeMetricName(this.name), 074 this.matchName, 075 this.type, 076 this.help, 077 this.labelNames, 078 this.labelValues, 079 value, 080 this.valueFactor); 081 } 082 083 /** 084 * An unmatched MatchedRule, used when no rule matching a JMX bean has been found in the 085 * configuration. Cached unmatched rules are still a cache hit, that will not produce any 086 * metric/value. 087 * 088 * @return the invalid rule 089 */ 090 public static MatchedRule unmatched() { 091 return _unmatched; 092 } 093 094 public boolean isUnmatched() { 095 return this == _unmatched; 096 } 097 098 public boolean isMatched() { 099 return !isUnmatched(); 100 } 101 102 @Override 103 public String toString() { 104 return "MatchedRule{" 105 + "name='" 106 + name 107 + '\'' 108 + ", matchName='" 109 + matchName 110 + '\'' 111 + ", type='" 112 + type 113 + '\'' 114 + ", help='" 115 + help 116 + '\'' 117 + ", labelNames=" 118 + labelNames 119 + ", labelValues=" 120 + labelValues 121 + ", value=" 122 + value 123 + ", valueFactor=" 124 + valueFactor 125 + '}'; 126 } 127 128 @Override 129 public boolean equals(Object o) { 130 if (this == o) return true; 131 if (o == null || getClass() != o.getClass()) return false; 132 MatchedRule that = (MatchedRule) o; 133 return Double.compare(valueFactor, that.valueFactor) == 0 134 && Objects.equals(name, that.name) 135 && Objects.equals(matchName, that.matchName) 136 && Objects.equals(type, that.type) 137 && Objects.equals(help, that.help) 138 && Objects.equals(labelNames, that.labelNames) 139 && Objects.equals(labelValues, that.labelValues) 140 && Objects.equals(value, that.value); 141 } 142 143 @Override 144 public int hashCode() { 145 return Objects.hash( 146 name, matchName, type, help, labelNames, labelValues, value, valueFactor); 147 } 148}