private MetricValue interpolate(MetricValue a, MetricValue b) { if ((a.getBoolValue() != null || a.getIntValue() != null || a.getFltValue() != null) && (b.getBoolValue() != null || b.getIntValue() != null || b.getFltValue() != null)) return MetricValue.fromDblValue(backWeight * a.value().get().doubleValue() + forwWeight * b.value().get().doubleValue()); if (a.getStrValue() != null && b.getStrValue() != null) return a; if (a.getHistValue() != null && b.getHistValue() != null) { return MetricValue.fromHistValue(Histogram.add( Histogram.multiply(a.getHistValue(), backWeight), Histogram.multiply(b.getHistValue(), forwWeight))); } // Mismatched types, return empty metric value. return MetricValue.EMPTY; } }
private static MetricValue strAsMetricValue(String str) { // Try to coerce elem to a boolean. if ("true".equals(str)) return MetricValue.TRUE; if ("false".equals(str)) return MetricValue.FALSE; // Try to coerce elem to an integral number. try { return MetricValue.fromIntValue(Long.parseLong(str)); } catch (NumberFormatException e) { // Ignore. } // Try to coerce elem to a floating point number. try { return MetricValue.fromDblValue(Double.parseDouble(str)); } catch (NumberFormatException e) { // Ignore. } // Give up and just yield a string. return MetricValue.fromStrValue(str); }
private static SelType sel_type_(MetricValue mv) { if (mv.getBoolValue() != null) return SelType.BOOLEAN; if (mv.getIntValue() != null) return SelType.INT; if (mv.getFltValue() != null) return SelType.FLOAT; if (mv.getStrValue() != null) return SelType.STRING; if (mv.getHistValue() != null) return SelType.HISTOGRAM; return SelType.NONE; }
final MetricValue m = tag_entry.getValue(); if (m.getBoolValue() != null) return SimpleMapEntry.create(key, m.getBoolValue()); if (m.getIntValue() != null) return SimpleMapEntry.create(key, m.getIntValue()); if (m.getFltValue() != null) return SimpleMapEntry.create(key, m.getFltValue()); if (m.getStrValue() != null) return SimpleMapEntry.create(key, m.getStrValue()); return SimpleMapEntry.create(key, null); }) value = mv.value().orElse(null); else if (mv.getBoolValue() != null) value = mv.getBoolValue(); else if (mv.getIntValue() != null) value = mv.getIntValue(); else if (mv.getFltValue() != null) value = mv.getFltValue(); else if (mv.getStrValue() != null) value = mv.getStrValue(); else value = null;
return Stream.of(SimpleMapEntry.create(MetricName.valueOf(attrPath), MetricValue.fromBoolean((Boolean)attrObj))); return Stream.of(SimpleMapEntry.create(MetricName.valueOf(attrPath), MetricValue.fromNumberValue((Number)attrObj))); return Stream.of(SimpleMapEntry.create(MetricName.valueOf(attrPath), MetricValue.fromStrValue((String)attrObj)));
/** * Construct a metric value from a Number implementation. * @param number A floating point or integral type. * @return a MetricValue holding the specified number. * @throws IllegalArgumentException if the derived type of Number is not recognized. */ public static MetricValue fromNumberValue(Number number) { if (number == null) { return EMPTY; } else if (number instanceof Float || number instanceof Double) { return fromDblValue(number.doubleValue()); } else if (number instanceof Byte || number instanceof Short || number instanceof Integer || number instanceof Long) { return fromIntValue(number.longValue()); } else { throw new IllegalArgumentException("Unrecognized number type: " + number.getClass()); } }
/** * Construct a new metric with a constant value. * @param name The name of the metric. * @param value The value of the metric. */ public SimpleMetric(MetricName name, String value) { this(name, MetricValue.fromStrValue(requireNonNull(value))); }
result.put(FAILED_COLLECTIONS_METRIC, MetricValue.fromIntValue(failed_collections)); result.put(GROUP_COUNT_METRIC, MetricValue.fromIntValue(ctx.getTSData().getCurrentCollection().getGroups(x -> true).size())); result.put(METRIC_COUNT_METRIC, MetricValue.fromIntValue(metric_count)); result.put(CONFIG_PRESENT_METRIC, MetricValue.fromBoolean(has_config)); result.put(SCRAPE_DURATION, opt_duration_to_metricvalue_(scrape_duration)); result.put(RULE_EVAL_DURATION, opt_duration_to_metricvalue_(rule_eval_duration)); result.put(PROCESSOR_DURATION, opt_duration_to_metricvalue_(processor_duration)); result.put(UPTIME_DURATION, duration_to_metricvalue_(uptime)); result.put(SCRAPE_COUNT, MetricValue.fromIntValue(++scrape_count_)); result.put(SCRAPE_INTERVAL, opt_duration_to_metricvalue_(last_scrape.map(prev -> new Duration(prev, now)))); result.put(SCRAPE_TS, MetricValue.fromIntValue(now.getMillis())); return result;
@Override protected MetricValue scalar_fallback_() { return MetricValue.fromIntValue(0); }
result = MetricValue.fromDblValue(right_fraction * left_value + left_fraction * right_value); } else if (left < collected.size()) { result = MetricValue.fromDblValue(collected.get(left)); } else { result = MetricValue.EMPTY;
@Override protected MetricValue unmap_(Number v) { return MetricValue.fromNumberValue(v); } @Override
}) .flatMap(opt -> opt.map(Stream::of).orElseGet(Stream::empty)) .filter(entry -> !entry.getValue().histogram().isPresent()) // Skip any histograms. .collect(Collectors.toList()); LOG.log(Level.FINER, "scalars = {0}", scalars); .orElseGet(Collections::<Tags, MetricValue>emptyMap) .entrySet().stream() .filter(entry -> !entry.getValue().histogram().isPresent()) // Skip any histograms. .map(tagged_value -> SimpleMapEntry.create(tagged_value.getKey(), SimpleMapEntry.create(value.getKey(), tagged_value.getValue()))); })
private Optional<MetricValue> apply_(MetricValue val) { return val.asBool() .map((Boolean x) -> !x) .map(MetricValue::fromBoolean); }
/** * Construct a new metric with a constant value. * @param name The name of the metric. * @param value The value of the metric. */ public SimpleMetric(MetricName name, Boolean value) { this(name, MetricValue.fromBoolean(requireNonNull(value))); }
return Stream.of(SimpleMapEntry.create(MetricName.valueOf(attrPath), MetricValue.fromBoolean((Boolean)attrObj))); return Stream.of(SimpleMapEntry.create(MetricName.valueOf(attrPath), MetricValue.fromNumberValue((Number)attrObj))); return Stream.of(SimpleMapEntry.create(MetricName.valueOf(attrPath), MetricValue.fromStrValue((String)attrObj)));
public Entry(@NonNull String tagName, @NonNull MetricValue tagValue) { if (tagName.isEmpty()) throw new IllegalArgumentException("tag with empty name"); if (!tagValue.isPresent()) throw new IllegalArgumentException("tag with absent metric value"); if (tagValue.histogram().isPresent()) throw new IllegalArgumentException("tag with histogram makes no sense"); this.tagName = tagName; this.tagValue = tagValue; } }
/** * Construct a metric value from a Number implementation. * @param number A floating point or integral type. * @return a MetricValue holding the specified number. * @throws IllegalArgumentException if the derived type of Number is not recognized. */ public static MetricValue fromNumberValue(Number number) { if (number == null) { return EMPTY; } else if (number instanceof Float || number instanceof Double) { return fromDblValue(number.doubleValue()); } else if (number instanceof Byte || number instanceof Short || number instanceof Integer || number instanceof Long) { return fromIntValue(number.longValue()); } else { throw new IllegalArgumentException("Unrecognized number type: " + number.getClass()); } }