protected Sample convertMeasurementToSample(Measurement measurement) { String prometheusName = measurement.id().name().replace(".", "_"); List<String> labelNames = new ArrayList<>(); List<String> labelValues = new ArrayList<>(); for (Tag tag : measurement.id().tags()) { labelNames.add(tag.key()); labelValues.add(tag.value()); } return new Sample(prometheusName, labelNames, labelValues, measurement.value()); }
private void printNetLog(StringBuilder sb, MeasurementNode osNode) { MeasurementNode netNode = osNode.findChild(OsMeter.OS_TYPE_NET); if (netNode == null || netNode.getMeasurements().isEmpty()) { return; } appendLine(sb, " net:"); appendLine(sb, " send(Bps) recv(Bps) send(pps) recv(pps) interface"); StringBuilder tmpSb = new StringBuilder(); for (MeasurementNode interfaceNode : netNode.getChildren().values()) { double sendRate = interfaceNode.findChild(NetMeter.TAG_SEND.value()).summary(); double sendPacketsRate = interfaceNode.findChild(NetMeter.TAG_PACKETS_SEND.value()).summary(); double receiveRate = interfaceNode.findChild(NetMeter.TAG_RECEIVE.value()).summary(); double receivePacketsRate = interfaceNode.findChild(NetMeter.TAG_PACKETS_RECEIVE.value()).summary(); if (sendRate == 0 && receiveRate == 0 && receivePacketsRate == 0 && sendPacketsRate == 0) { continue; } appendLine(tmpSb, " %-12s %-12s %-12s %-12s %s", NetUtils.humanReadableBytes((long) sendRate), NetUtils.humanReadableBytes((long) receiveRate), NetUtils.humanReadableBytes((long) sendPacketsRate), NetUtils.humanReadableBytes((long) receivePacketsRate), interfaceNode.getName()); } if (tmpSb.length() != 0) { sb.append(tmpSb.toString()); } }
public void from(Iterable<Measurement> measurements, MeasurementGroupConfig groupConfig) { for (Measurement measurement : measurements) { Id id = measurement.id(); MeasurementNode node = addChild(id.name(), measurement); List<TagFinder> tagFinders = groupConfig.findTagFinders(id.name()); if (tagFinders == null) { continue; } for (TagFinder tagFinder : tagFinders) { Tag tag = tagFinder.find(id.tags()); if (tag == null) { if (tagFinder.skipOnNull()) { break; } throw new IllegalStateException( String.format("tag key \"%s\" not exist in %s", tagFinder.getTagKey(), measurement)); } node = node.addChild(tag.value(), measurement); } } } }
private String[] toStringArray(Tag[] ts, int length) { String[] strs = new String[length * 2]; for (int i = 0; i < length; ++i) { strs[2 * i] = ts[i].key(); strs[2 * i + 1] = ts[i].value(); } return strs; }
private void putTag(Map<String, String> tags, Tag tag) { if (tag != null) { tags.put(tag.key(), tag.value()); } }
@Override public boolean equals(Object obj) { if (obj == this) return true; if (!(obj instanceof Tag)) return false; Tag tag = (Tag) obj; return key.equals(tag.key()) && value.equals(tag.value()); }
JsonStringBuilder addField(String k, Tag tag) { if (tag != null) { addField(k, tag.value()); } return this; }
JsonStringBuilder addField(String k, Tag tag) { if (tag != null) { addField(k, tag.value()); } return this; }
@Override public boolean equals(Object obj) { if (obj == this) return true; if (!(obj instanceof Tag)) return false; Tag tag = (Tag) obj; return key.equals(tag.key()) && value.equals(tag.value()); }
/** Create a new instance. */ public MetricsRegistry(Clock clock, com.codahale.metrics.MetricRegistry impl) { this(clock, impl, id -> { Id normalized = Utils.normalize(id); StringBuilder buf = new StringBuilder(); buf.append(normalized.name()); for (Tag t : normalized.tags()) { buf.append('.').append(t.key()).append('-').append(t.value()); } return buf.toString(); }); }
/** Convert {@code id} to a map. */ static Map<String, String> toMap(Id id) { Map<String, String> tags = new HashMap<>(); for (Tag t : id.tags()) { tags.put(t.key(), t.value()); } tags.put("name", id.name()); return tags; }
/** * Convert an arbitrary implementation of {@link Tag} to BasicTag. When * used as part of an id this is needed to ensure correct behavior. Alternative * implementations will likely vary the hashCode/equals causing ids that should * be equivalent to not match as expected. */ static BasicTag convert(Tag t) { return (t instanceof BasicTag) ? (BasicTag) t : new BasicTag(t.key(), t.value()); }
private static int operation(Id id) { for (Tag t : id.tags()) { if ("statistic".equals(t.key())) { return operation(t.value()); } } LOGGER.warn("invalid statistic for {}, value will be dropped", id); return UNKNOWN; }
private static int operation(Id id) { for (Tag t : id.tags()) { if ("statistic".equals(t.key())) { return operation(t.value()); } } LOGGER.warn("invalid statistic for {}, value will be dropped", id); return UNKNOWN; }
static List<Measurement> measurements(com.netflix.spectator.api.Meter meter) { return stream(meter.measure().spliterator(), false) .map(m -> new Measurement( m.id().name(), stream(m.id().tags().spliterator(), false) .map(t -> Tag.of(t.key(), t.value())) .collect(Collectors.toList()), m.value()) ) .collect(Collectors.toList()); }
/** Converts a spectator id into a MonitorConfig that can be used by servo. */ MonitorConfig toMonitorConfig(Id id, Tag stat) { MonitorConfig.Builder builder = new MonitorConfig.Builder(id.name()); if (stat != null) { builder.withTag(stat.key(), stat.value()); } for (Tag t : id.tags()) { builder.withTag(t.key(), t.value()); } return builder.build(); }
private String get(Id id, String key) { for (Tag t : id.tags()) { if (key.equals(t.key())) { return t.value(); } } return null; }
@Override public Id withTags(Iterable<Tag> ts) { MonitorConfig.Builder builder = new MonitorConfig.Builder(config); for (Tag t : ts) { builder.withTag(t.key(), t.value()); } return new ServoId(builder.build()); }
private TagsValuePair newTagsValuePair(Measurement m) { Map<String, String> tags = new HashMap<>(); for (Tag t : m.id().tags()) { tags.put(t.key(), t.value()); } tags.put("name", m.id().name()); return new TagsValuePair(tags, m.value()); }
@Test public void testSingle() { ArrayTagSet ts = ArrayTagSet.create("k", "v"); for (Tag t : ts) { Assertions.assertEquals(t.key(), "k"); Assertions.assertEquals(t.value(), "v"); } }