| 
18 | 18 | import java.util.List;  | 
19 | 19 | import java.util.Map;  | 
20 | 20 | import java.util.Set;  | 
21 |  | -import java.util.stream.Collectors;  | 
22 | 21 | import javax.annotation.Nullable;  | 
23 | 22 | import javax.management.MBeanServerConnection;  | 
24 | 23 | import javax.management.MalformedObjectNameException;  | 
@@ -173,52 +172,71 @@ public MetricDef buildMetricDef() throws Exception {  | 
173 | 172 |       StateMapping stateMapping = getEffectiveStateMapping(m, this);  | 
174 | 173 | 
 
  | 
175 | 174 |       if (stateMapping.isEmpty()) {  | 
176 |  | -        MetricExtractor metricExtractor =  | 
 | 175 | +        metricExtractors.add(  | 
177 | 176 |             new MetricExtractor(  | 
178 |  | -                attrExtractor, metricInfo, attributeList.toArray(new MetricAttribute[0]));  | 
179 |  | -        metricExtractors.add(metricExtractor);  | 
 | 177 | +                attrExtractor, metricInfo, attributeList.toArray(new MetricAttribute[0])));  | 
180 | 178 |       } else {  | 
181 | 179 | 
 
  | 
182 | 180 |         // generate one metric extractor per state metric key  | 
183 | 181 |         // each metric extractor will have the state attribute replaced with a constant  | 
184 |  | -        for (String key : stateMapping.getStateKeys()) {  | 
185 |  | -          List<MetricAttribute> stateMetricAttributes =  | 
186 |  | -              attributeList.stream()  | 
187 |  | -                  .map(  | 
188 |  | -                      ma -> {  | 
189 |  | -                        if (!ma.isStateAttribute()) {  | 
190 |  | -                          return ma;  | 
191 |  | -                        } else {  | 
192 |  | -                          return new MetricAttribute(  | 
193 |  | -                              ma.getAttributeName(), MetricAttributeExtractor.fromConstant(key));  | 
194 |  | -                        }  | 
195 |  | -                      })  | 
196 |  | -                  .collect(Collectors.toList());  | 
197 |  | - | 
198 |  | -          BeanAttributeExtractor stateMetricExtractor =  | 
199 |  | -              new BeanAttributeExtractor(attrExtractor.getAttributeName()) {  | 
200 |  | - | 
201 |  | -                @Override  | 
202 |  | -                protected Number extractNumericalAttribute(  | 
203 |  | -                    MBeanServerConnection connection, ObjectName objectName) {  | 
204 |  | -                  String rawStateValue = attrExtractor.extractValue(connection, objectName);  | 
205 |  | -                  String mappedStateValue = stateMapping.getStateValue(rawStateValue);  | 
206 |  | -                  return key.equals(mappedStateValue) ? 1 : 0;  | 
207 |  | -                }  | 
208 |  | -              };  | 
209 |  | - | 
210 |  | -          metricExtractors.add(  | 
211 |  | -              new MetricExtractor(  | 
212 |  | -                  stateMetricExtractor,  | 
213 |  | -                  metricInfo,  | 
214 |  | -                  stateMetricAttributes.toArray(new MetricAttribute[0])));  | 
215 |  | -        }  | 
 | 182 | +        metricExtractors.addAll(  | 
 | 183 | +            createStateMappingExtractors(stateMapping, attributeList, attrExtractor, metricInfo));  | 
216 | 184 |       }  | 
217 | 185 |     }  | 
218 | 186 | 
 
  | 
219 | 187 |     return new MetricDef(group, metricExtractors.toArray(new MetricExtractor[0]));  | 
220 | 188 |   }  | 
221 | 189 | 
 
  | 
 | 190 | +  private static List<MetricExtractor> createStateMappingExtractors(  | 
 | 191 | +      StateMapping stateMapping,  | 
 | 192 | +      List<MetricAttribute> attributeList,  | 
 | 193 | +      BeanAttributeExtractor attrExtractor,  | 
 | 194 | +      MetricInfo metricInfo) {  | 
 | 195 | + | 
 | 196 | +    List<MetricExtractor> extractors = new ArrayList<>();  | 
 | 197 | +    for (String key : stateMapping.getStateKeys()) {  | 
 | 198 | +      List<MetricAttribute> stateMetricAttributes = new ArrayList<>();  | 
 | 199 | + | 
 | 200 | +      for (MetricAttribute ma : attributeList) {  | 
 | 201 | +        // replace state metric attribute with constant key value  | 
 | 202 | +        if (!ma.isStateAttribute()) {  | 
 | 203 | +          stateMetricAttributes.add(ma);  | 
 | 204 | +        } else {  | 
 | 205 | +          stateMetricAttributes.add(  | 
 | 206 | +              new MetricAttribute(  | 
 | 207 | +                  ma.getAttributeName(), MetricAttributeExtractor.fromConstant(key)));  | 
 | 208 | +        }  | 
 | 209 | +      }  | 
 | 210 | + | 
 | 211 | +      BeanAttributeExtractor stateMetricExtractor =  | 
 | 212 | +          new BeanAttributeExtractor(attrExtractor.getAttributeName()) {  | 
 | 213 | + | 
 | 214 | +            @Override  | 
 | 215 | +            protected Object getSampleValue(  | 
 | 216 | +                MBeanServerConnection connection, ObjectName objectName) {  | 
 | 217 | +              // metric actual type is sampled in the discovery process, so we have to  | 
 | 218 | +              // make this extractor as extracting integers.  | 
 | 219 | +              return 0;  | 
 | 220 | +            }  | 
 | 221 | + | 
 | 222 | +            @Override  | 
 | 223 | +            protected Number extractNumericalAttribute(  | 
 | 224 | +                MBeanServerConnection connection, ObjectName objectName) {  | 
 | 225 | +              String rawStateValue = attrExtractor.extractValue(connection, objectName);  | 
 | 226 | +              String mappedStateValue = stateMapping.getStateValue(rawStateValue);  | 
 | 227 | +              return key.equals(mappedStateValue) ? 1 : 0;  | 
 | 228 | +            }  | 
 | 229 | +          };  | 
 | 230 | + | 
 | 231 | +      extractors.add(  | 
 | 232 | +          new MetricExtractor(  | 
 | 233 | +              stateMetricExtractor,  | 
 | 234 | +              metricInfo,  | 
 | 235 | +              stateMetricAttributes.toArray(new MetricAttribute[0])));  | 
 | 236 | +    }  | 
 | 237 | +    return extractors;  | 
 | 238 | +  }  | 
 | 239 | + | 
222 | 240 |   private static List<MetricAttribute> combineMetricAttributes(  | 
223 | 241 |       List<MetricAttribute> ownAttributes, List<MetricAttribute> metricAttributes) {  | 
224 | 242 | 
 
  | 
 | 
0 commit comments