private void openSslStatGauge(OpenSslSessionStats stats, String sslContextId, String statName, ToDoubleFunction<OpenSslSessionStats> value) { Id id = spectatorRegistry.createId("server.ssl.stats", "id", sslContextId, "stat", statName); spectatorRegistry.gauge(id, stats, value); LOG.debug("Registered spectator gauge - " + id.name()); }
private void openSslStatGauge(OpenSslSessionStats stats, String sslContextId, String statName, ToDoubleFunction<OpenSslSessionStats> value) { Id id = spectatorRegistry.createId("server.ssl.stats", "id", sslContextId, "stat", statName); spectatorRegistry.gauge(id, stats, value); LOG.debug("Registered spectator gauge - " + id.name()); }
private Id suffixBaseId(String suffix) { return registry.createId(this.baseId.name() + "." + suffix).withTags(this.baseId.tags()); }
protected String idToString(Id id, StringBuilder sb) { sb.setLength(0); sb.append(id.name()).append('('); sb.append(StreamSupport .stream(id .tags() .spliterator(), false) .map(Object::toString) .collect( Collectors.joining(","))); sb.append(')'); return sb.toString(); } }
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()); }
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); } } } }
/** 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; }
static Stream<Id> clientCallMetrics(Registry registry) { return registry .stream() .map(Meter::id) .filter(id -> "ipc.client.call".equals(id.name())); }
private static void validateIpcMeter(Registry registry, IpcMetric metric, Class<?> type) { final String name = metric.metricName(); registry.stream() .filter(m -> name.equals(m.id().name()) && !isPercentile(m.id())) .forEach(m -> { assertTrue(type.isAssignableFrom(m.getClass()), "[%s] has the wrong type, expected %s but found %s", m.id(), type.getSimpleName(), getName(m.getClass())); metric.validate(m.id()); }); }
/** * Implements the MeasurementFilter interface. */ @SuppressWarnings("PMD.JUnit4TestShouldUseTestAnnotation") @Override public boolean test(Measurement measurement) { IncludeExcludePatterns patterns = metricToPatterns(measurement.id().name()); return patterns != null && patterns.test(measurement); }
/** * Returns a new id with the tag list sorted by key and with no duplicate keys. */ public static Id normalize(Id id) { return (new DefaultId(id.name())).withTags(id.tags()).normalize(); }
@Test public void createIdName() { Id id = registry.createId("foo"); Assertions.assertEquals("foo", id.name()); Assertions.assertFalse(id.tags().iterator().hasNext()); }
@Test public void createIdNameAndTags() { Id id = registry.createId("foo", "a", "1", "b", "2"); Assertions.assertEquals("foo", id.name()); Map<String, String> tags = new HashMap<>(); tags.put("a", "1"); tags.put("b", "2"); for (Tag t : id.tags()) { Assertions.assertEquals(tags.remove(t.key()), t.value()); } Assertions.assertTrue(tags.isEmpty()); }
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()); }
/** 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(); }
@Test public void measure() { final ServoRegistry r = new ServoRegistry(clock); Gauge g = r.gauge(r.createId("foo")); g.set(1.0); Iterator<Measurement> ms = g.measure().iterator(); Assertions.assertTrue(ms.hasNext()); Measurement m = ms.next(); Assertions.assertFalse(ms.hasNext()); Assertions.assertEquals("foo", m.id().name()); Assertions.assertEquals(1.0, 1.0, 1e-12); }
@Test public void testTags() { ArrayTagSet ts = ArrayTagSet.create("k1", "v1"); Id id = new DefaultId("foo", ts); Assertions.assertEquals(id.name(), "foo"); Assertions.assertEquals(id.tags(), ts); }
@Test public void gaugeMeasure() { Gauge g = registry.gauge("foo"); g.set(42.0); int i = 0; for (Measurement m : g.measure()) { ++i; Assertions.assertEquals("foo", m.id().name()); Assertions.assertEquals(42.0, m.value(), 1e-12); } Assertions.assertEquals(1, i); }
@Test public void counterMeasure() { Counter c = registry.counter("foo"); c.increment(); int i = 0; for (Measurement m : c.measure()) { ++i; Assertions.assertEquals("foo", m.id().name()); Assertions.assertEquals(1.0, m.value(), 1e-12); } Assertions.assertEquals(1, i); }