org.eclipse.microprofile.metrics.Metadata Java Examples
The following examples show how to use
org.eclipse.microprofile.metrics.Metadata.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MetricTypeMismatchTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
@Test public void wrongTypeInMetadata() { Metadata metadata1 = Metadata.builder() .withName("metric1") .withType(MetricType.COUNTER) .build(); try { registry.meter(metadata1); fail("Must not be able to register a metric if the type in its metadata is different than the what we specified by using a particular registration method."); } catch (Exception e) { assertThat(e, instanceOf(IllegalArgumentException.class)); assertThat(e.getMessage(), containsString("Attempting to register a meter, but the passed metadata contains type=counter")); assertEquals(0, registry.getMetrics().size()); } }
Example #2
Source File: OpenMetricsExporterTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
@Test public void exportGauges() { OpenMetricsExporter exporter = new OpenMetricsExporter(); MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Metadata metadata = Metadata .builder() .withType(MetricType.GAUGE) .withName("mygauge") .withDescription("awesome") .build(); Tag blueTag = new Tag("color", "blue"); registry.register(metadata, (Gauge<Long>) () -> 42L, blueTag); Tag greenTag = new Tag("color", "green"); registry.register(metadata, (Gauge<Long>) () -> 26L, greenTag); String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "mygauge").toString(); System.out.println(result); assertHasTypeLineExactlyOnce(result, "application_mygauge", "gauge"); assertHasHelpLineExactlyOnce(result, "application_mygauge", "awesome"); assertHasValueLineExactlyOnce(result, "application_mygauge", "42.0", blueTag); assertHasValueLineExactlyOnce(result, "application_mygauge", "26.0", greenTag); }
Example #3
Source File: JsonExporterTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
@Test public void testGauges() { JsonExporter exporter = new JsonExporter(); MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Gauge<Long> gaugeWithoutTags = () -> 1L; Gauge<Long> gaugeRed = () -> 2L; Gauge<Long> gaugeBlue = () -> 3L; final Metadata metadata = new MetadataBuilder() .withType(MetricType.GAUGE) .withName("mygauge") .build(); registry.register(metadata, gaugeWithoutTags); registry.register(metadata, gaugeRed, new Tag("color", "red")); registry.register(metadata, gaugeBlue, new Tag("color", "blue"), new Tag("foo", "bar")); String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "mygauge").toString(); System.out.println(result); JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject(); assertEquals(1, json.getInt("mygauge")); assertEquals(2, json.getInt("mygauge;color=red")); assertEquals(3, json.getInt("mygauge;color=blue;foo=bar")); }
Example #4
Source File: JsonMetadataExporter.java From smallrye-metrics with Apache License 2.0 | 6 votes |
private JsonObject metricJSON(Metadata metadata, List<List<String>> tagSets) { JsonObjectBuilder obj = JsonProviderHolder.get().createObjectBuilder(); obj.add("unit", metadata.getUnit()); if (metadata.getType() != null) { obj.add("type", metadata.getType()); } metadata.description().ifPresent(s -> obj.add("description", s)); if (metadata.getDisplayName() != null) { obj.add("displayName", metadata.getDisplayName()); } // append known sets of tags JsonArrayBuilder tagsArray = JsonProviderHolder.get().createArrayBuilder(); tagSets.forEach(tagSet -> { JsonArrayBuilder innerArrayBuilder = JsonProviderHolder.get().createArrayBuilder(); tagSet.forEach(innerArrayBuilder::add); tagsArray.add(innerArrayBuilder); }); obj.add("tags", tagsArray); return obj.build(); }
Example #5
Source File: MetricTypeMismatchTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
@Test public void metricsWithDifferentType() { Metadata metadata1 = Metadata.builder().withName("metric1") .withDescription("description1").build(); Metadata metadata2 = Metadata.builder().withName("metric1") .withDescription("description2").build(); registry.histogram(metadata1); try { registry.meter(metadata2); fail("Must not be able to register if a metric with different type is registered under the same name"); } catch (Exception e) { assertThat(e, instanceOf(IllegalStateException.class)); assertEquals(1, registry.getMetrics().size()); } }
Example #6
Source File: OpenMetricsExporterTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
@Test public void testNameOverride() { OpenMetricsExporter exporter = new OpenMetricsExporter(); MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Metadata metadata = new ExtendedMetadata("voltage1", "Voltage", "Measures your electric potential", MetricType.GAUGE, "volts", null, false, Optional.of(true), false, "baz"); Tag tag = new Tag("a", "b"); registry.register(metadata, (Gauge<Long>) () -> 3L, tag); String result = exporter.exportOneScope(MetricRegistry.Type.APPLICATION).toString(); System.out.println(result); assertHasHelpLineExactlyOnce(result, "application_baz", "Measures your electric potential"); assertHasTypeLineExactlyOnce(result, "application_baz", "gauge"); assertHasValueLineExactlyOnce(result, "application_baz", "3.0", tag); }
Example #7
Source File: OpenMetricsExporterTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
@Test public void testSkippingOfScope() { OpenMetricsExporter exporter = new OpenMetricsExporter(); MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Metadata metadata = new ExtendedMetadata("foo", "foo", "FooDescription", MetricType.COUNTER, "volts", null, false, Optional.of(true), true, null); Tag tag = new Tag("a", "b"); registry.counter(metadata, tag); String result = exporter.exportOneScope(MetricRegistry.Type.APPLICATION).toString(); System.out.println(result); assertHasHelpLineExactlyOnce(result, "foo_total", "FooDescription"); assertHasTypeLineExactlyOnce(result, "foo_total", "counter"); assertHasValueLineExactlyOnce(result, "foo_total_volts", "0.0", tag); }
Example #8
Source File: JsonExporter.java From smallrye-metrics with Apache License 2.0 | 6 votes |
private Map<String, JsonValue> exportMetricsForMap(Map<MetricID, Metric> metricMap, Map<String, Metadata> metadataMap) { Map<String, JsonValue> result = new HashMap<>(); // split into groups by metric name Map<String, Map<MetricID, Metric>> metricsGroupedByName = metricMap.entrySet().stream() .collect(Collectors.groupingBy( entry -> entry.getKey().getName(), Collectors.mapping(e -> e, Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)))); // and then for each group, perform the export metricsGroupedByName.entrySet().stream() .map(entry -> exportMetricsByName(entry.getValue(), metadataMap.get(entry.getKey()))) .forEach(map -> { map.forEach(result::put); }); return result; }
Example #9
Source File: SmallRyeMetricsRecorder.java From quarkus with Apache License 2.0 | 6 votes |
public void registerMetricFromProducer(String beanId, MetricType metricType, String metricName, String[] tags, String description, String displayName, String unit) { ArcContainer container = Arc.container(); InjectableBean<Object> injectableBean = container.bean(beanId); BeanManager beanManager = container.beanManager(); Metric reference = (Metric) beanManager.getReference(injectableBean, Metric.class, beanManager.createCreationalContext(injectableBean)); MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Metadata metadata = Metadata.builder() .withType(metricType) .withName(metricName) .withDescription(description) .withDisplayName(displayName) .withUnit(unit) .notReusable() .build(); registry.register(metadata, reference, TagsUtils.parseTagsAsArray(tags)); }
Example #10
Source File: SmallRyeMetricsRecorder.java From quarkus with Apache License 2.0 | 6 votes |
private void runtimeMetrics(MetricRegistry registry) { RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); Metadata meta = Metadata.builder() .withName(JVM_UPTIME) .withType(MetricType.GAUGE) .withUnit(MetricUnits.MILLISECONDS) .withDisplayName("JVM Uptime") .withDescription("Displays the time from the start of the Java virtual machine in milliseconds.") .build(); registry.register(meta, new Gauge() { @Override public Number getValue() { return runtimeMXBean.getUptime(); } }); }
Example #11
Source File: MetricRegistryTest.java From microprofile-metrics with Apache License 2.0 | 6 votes |
@Test @InSequence(4) public void useExistingMetaDataTest() { String displayName = "displayCounterFoo"; String metricName = "counterFoo"; //first to register a "complex" metadata metrics.counter(Metadata.builder().withName(metricName).withDisplayName(displayName).withType(MetricType.COUNTER).build()); Tag purpleTag = new Tag("colour","purple"); //creates with a simple/template metadata or uses an existing one. metrics.counter(metricName, purpleTag); //check both counters have been registered assertExists(Counter.class, new MetricID(metricName)); assertExists(Counter.class, new MetricID(metricName, purpleTag)); //check that the "original" metadata wasn't replaced by the empty default metadata Assert.assertEquals(metrics.getMetadata(metricName).getDisplayName(), displayName); }
Example #12
Source File: MetricAppBean.java From microprofile-metrics with Apache License 2.0 | 6 votes |
public void histogramMe() { Metadata metadata = Metadata.builder().withName("metricTest.test1.histogram") .withType(MetricType.HISTOGRAM).withUnit(MetricUnits.BYTES).build(); Histogram histogram = metrics.histogram(metadata); // Go both ways to minimize error due to decay for (int i = 0; i < 500; i++) { histogram.update(i); histogram.update(999 - i); } Metadata metadata2 = Metadata.builder().withName("metricTest.test1.histogram2") .withType(MetricType.HISTOGRAM).withUnit(MetricUnits.NONE).build(); Histogram histogram2 = metrics.histogram(metadata2); histogram2.update(1); }
Example #13
Source File: OpenMetricsExporter.java From smallrye-metrics with Apache License 2.0 | 6 votes |
private void addTags(StringBuilder sb, Map<String, String> tags, MetricRegistry.Type scope, Metadata metadata) { if (tags == null || tags.isEmpty()) { // always add the microprofile_scope even if there are no other tags if (writeScopeInTag(metadata)) { sb.append("{microprofile_scope=\"" + scope.getName().toLowerCase() + "\"}"); } return; } else { Iterator<Map.Entry<String, String>> iter = tags.entrySet().iterator(); sb.append("{"); while (iter.hasNext()) { Map.Entry<String, String> tag = iter.next(); sb.append(tag.getKey()).append("=\"").append(quoteValue(tag.getValue())).append("\""); if (iter.hasNext()) { sb.append(","); } } // append the microprofile_scope after other tags if (writeScopeInTag(metadata)) { sb.append(",microprofile_scope=\"" + scope.getName().toLowerCase() + "\""); } sb.append("}"); } }
Example #14
Source File: ExportersMetricScalingTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
/** * Given a Meter, * check that the statistics from OpenMetrics will be presented as per_second. */ @Test public void meter_openMetrics() throws InterruptedException { MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Metadata metadata = Metadata.builder() .withName("meter1") .withType(MetricType.METERED) .build(); Meter metric = registry.meter(metadata); metric.mark(10); TimeUnit.SECONDS.sleep(1); OpenMetricsExporter exporter = new OpenMetricsExporter(); String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("meter1")).toString(); Assert.assertThat(exported, containsString("application_meter1_total 10.0")); double ratePerSecond = Double.parseDouble(Arrays.stream(exported.split("\\n")) .filter(line -> line.contains("application_meter1_rate_per_second")) .filter(line -> !line.contains("TYPE") && !line.contains("HELP")) .findFirst() .get() .split(" ")[1]); Assert.assertTrue("Rate per second should be between 1 and 10 but is " + ratePerSecond, ratePerSecond > 1 && ratePerSecond < 10); }
Example #15
Source File: OpenMetricsExporter.java From smallrye-metrics with Apache License 2.0 | 6 votes |
private void writeTypeLine(StringBuilder sb, MetricRegistry.Type scope, String key, Metadata md, String suffix, String typeOverride) { if (!alreadyExportedNames.get().contains(md.getName())) { sb.append("# TYPE "); getNameWithScopeAndSuffix(sb, scope, key, suffix, md); if (typeOverride != null) { sb.append(typeOverride); } else if (md.getTypeRaw().equals(MetricType.TIMER)) { sb.append(SUMMARY); } else if (md.getTypeRaw().equals(MetricType.METERED)) { sb.append(COUNTER); } else { sb.append(md.getType()); } sb.append(LF); } }
Example #16
Source File: ExportersMetricScalingTest.java From smallrye-metrics with Apache License 2.0 | 6 votes |
/** * Given a Histogram with unit=MINUTES, * check that the statistics from OpenMetricsExporter will be presented in SECONDS. */ @Test public void histogram_openMetrics() { MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Metadata metadata = Metadata.builder() .withName("histogram1") .withType(MetricType.HISTOGRAM) .withUnit(MetricUnits.MINUTES) .build(); Histogram metric = registry.histogram(metadata); metric.update(30); metric.update(40); metric.update(50); OpenMetricsExporter exporter = new OpenMetricsExporter(); String exported = exporter.exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("histogram1")).toString(); Assert.assertThat(exported, containsString("application_histogram1_min_seconds 1800.0")); Assert.assertThat(exported, containsString("application_histogram1_max_seconds 3000.0")); Assert.assertThat(exported, containsString("application_histogram1_mean_seconds 2400.0")); Assert.assertThat(exported, containsString("application_histogram1_seconds{quantile=\"0.5\"} 2400.0")); }
Example #17
Source File: OpenMetricsExporter.java From smallrye-metrics with Apache License 2.0 | 5 votes |
private void writeSnapshotBasics(StringBuilder sb, MetricRegistry.Type scope, Metadata md, Snapshot snapshot, String unit, boolean performScaling, Map<String, String> tags) { writeTypeAndValue(sb, scope, "_min" + unit, snapshot.getMin(), GAUGE, md, performScaling, tags); writeTypeAndValue(sb, scope, "_max" + unit, snapshot.getMax(), GAUGE, md, performScaling, tags); writeTypeAndValue(sb, scope, "_mean" + unit, snapshot.getMean(), GAUGE, md, performScaling, tags); writeTypeAndValue(sb, scope, "_stddev" + unit, snapshot.getStdDev(), GAUGE, md, performScaling, tags); }
Example #18
Source File: MetricsRegistryImpl.java From smallrye-metrics with Apache License 2.0 | 5 votes |
private Metadata sanitizeMetadata(Metadata metadata, MetricType metricType) { // if the metadata does not specify a type, we add it here // if the metadata specifies a type, we check that it's the correct one // (for example, someone might have called registry.counter(metadata) where metadata.type="gauge") if (metadata.getTypeRaw() == null || metadata.getTypeRaw() == MetricType.INVALID) { return Metadata.builder(metadata).withType(metricType).build(); } else { if (metadata.getTypeRaw() != metricType) { throw SmallRyeMetricsMessages.msg.typeMismatch(metricType, metadata.getTypeRaw()); } else { return metadata; } } }
Example #19
Source File: MetadataHolder.java From quarkus with Apache License 2.0 | 5 votes |
public Metadata toMetadata() { final MetadataBuilder builder = Metadata.builder() .withName(name); if (description != null) { builder.withDescription(description); } if (displayName != null) { builder.withDisplayName(displayName); } return builder.withType(metricType) .withUnit(unit) .build(); }
Example #20
Source File: OpenMetricsExporter.java From smallrye-metrics with Apache License 2.0 | 5 votes |
private void writeMeterRateValues(StringBuilder sb, MetricRegistry.Type scope, Metered metric, Metadata md, Map<String, String> tags) { writeTypeAndValue(sb, scope, "_rate_per_second", metric.getMeanRate(), GAUGE, md, false, tags); writeTypeAndValue(sb, scope, "_one_min_rate_per_second", metric.getOneMinuteRate(), GAUGE, md, false, tags); writeTypeAndValue(sb, scope, "_five_min_rate_per_second", metric.getFiveMinuteRate(), GAUGE, md, false, tags); writeTypeAndValue(sb, scope, "_fifteen_min_rate_per_second", metric.getFifteenMinuteRate(), GAUGE, md, false, tags); }
Example #21
Source File: OpenMetricsExporterTest.java From smallrye-metrics with Apache License 2.0 | 5 votes |
@Test public void exportConcurrentGauges() { OpenMetricsExporter exporter = new OpenMetricsExporter(); MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Metadata metadata = Metadata .builder() .withType(MetricType.CONCURRENT_GAUGE) .withName("myconcurrentgauge") .withDescription("awesome") .withUnit("dollars") // this should get ignored and should not be reflected in the output .build(); Tag blueTag = new Tag("color", "blue"); ConcurrentGauge blueCGauge = registry.concurrentGauge(metadata, blueTag); Tag greenTag = new Tag("color", "green"); ConcurrentGauge greenCGauge = registry.concurrentGauge(metadata, greenTag); blueCGauge.inc(); blueCGauge.inc(); greenCGauge.inc(); String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "myconcurrentgauge").toString(); System.out.println(result); assertHasTypeLineExactlyOnce(result, "application_myconcurrentgauge_current", "gauge"); assertHasTypeLineExactlyOnce(result, "application_myconcurrentgauge_min", "gauge"); assertHasTypeLineExactlyOnce(result, "application_myconcurrentgauge_max", "gauge"); assertHasHelpLineExactlyOnce(result, "application_myconcurrentgauge_current", "awesome"); assertHasValueLineExactlyOnce(result, "application_myconcurrentgauge_current", "2.0", blueTag); assertHasValueLineExactlyOnce(result, "application_myconcurrentgauge_current", "1.0", greenTag); }
Example #22
Source File: OpenMetricsExporter.java From smallrye-metrics with Apache License 2.0 | 5 votes |
private void writeHelpLine(final StringBuilder sb, MetricRegistry.Type scope, String key, Metadata md, String suffix) { // Only write this line if we actually have a description in metadata Optional<String> description = md.description(); if (writeHelpLine && description.filter(s -> !s.isEmpty()).isPresent() && !alreadyExportedNames.get().contains(md.getName())) { sb.append("# HELP "); getNameWithScopeAndSuffix(sb, scope, key, suffix, md); sb.append(quoteHelpText(description.get())); sb.append(LF); } }
Example #23
Source File: RegistrationCornerCasesTest.java From smallrye-metrics with Apache License 2.0 | 5 votes |
@Test public void ambiguousClassButTypeIsProvidedDuringRegistrationWithTags() { Metadata metadata = Metadata.builder() .withType(MetricType.COUNTER) .withName("test") .build(); // the Ambiguous class can be a counter as well as gauge, but we specified the type in the metadata // so it should be registered as a counter registry.register(metadata, new Ambiguous(), new Tag("a", "b")); Assert.assertEquals(MetricType.COUNTER, registry.getMetadata("test").getTypeRaw()); Assert.assertEquals(666L, registry.getCounter(new MetricID("test", new Tag("a", "b"))).getCount()); }
Example #24
Source File: MetricRegistryTest.java From microprofile-metrics with Apache License 2.0 | 5 votes |
/** * if there is a mismatch because the type specified in the `Metadata` is different than the one * implied by the method name, an exception must be thrown */ @Test(expected = Exception.class) @InSequence(7) public void conflictingMetadataTest() { Metadata metadata = Metadata.builder().withName("metric1").withType(MetricType.COUNTER).build(); metrics.meter(metadata); }
Example #25
Source File: OpenMetricsExporter.java From smallrye-metrics with Apache License 2.0 | 5 votes |
private void createSimpleValueLine(StringBuilder sb, MetricRegistry.Type scope, String key, Metadata md, Metric metric, String suffix, Map<String, String> tags) { // value line fillBaseName(sb, scope, key, suffix, md); // append the base unit only in case that the key wasn't overridden if (getOpenMetricsKeyOverride(md) == null) { String unit = OpenMetricsUnit.getBaseUnitAsOpenMetricsString(md.unit()); if (!unit.equals(NONE)) { sb.append(USCORE).append(unit); } } addTags(sb, tags, scope, md); double valIn; if (md.getTypeRaw().equals(MetricType.GAUGE)) { Number value1 = (Number) ((Gauge) metric).getValue(); if (value1 != null) { valIn = value1.doubleValue(); } else { valIn = Double.NaN; } } else { valIn = (double) ((Counter) metric).getCount(); } Double value = OpenMetricsUnit.scaleToBase(md.unit().orElse(NONE), valIn); sb.append(SPACE).append(value).append(LF); }
Example #26
Source File: MetadataMismatchTest.java From smallrye-metrics with Apache License 2.0 | 5 votes |
@Test public void metricsWithDifferentMetadata() { Metadata metadata1 = Metadata.builder().withName("myhistogram").withDescription("description1").build(); Metadata metadata2 = Metadata.builder().withName("myhistogram").withDescription("description2").build(); registry.histogram(metadata1); try { registry.histogram(metadata2); fail("Shouldn't be able to re-register a metric with different metadata"); } catch (Exception e) { assertThat(e, instanceOf(IllegalStateException.class)); assertEquals(1, registry.getMetrics().size()); } }
Example #27
Source File: HibernateOrmProcessor.java From quarkus with Apache License 2.0 | 5 votes |
private MetricBuildItem createMetricBuildItem(String metricName, String description, String metric, boolean metricsEnabled) { return new MetricBuildItem(Metadata.builder() .withName(metricName) .withDescription(description) .withType(MetricType.COUNTER) .build(), new HibernateCounter("default", metric), metricsEnabled, "hibernate-orm"); }
Example #28
Source File: RegistrationCornerCasesTest.java From smallrye-metrics with Apache License 2.0 | 5 votes |
@Test public void ambiguousClassButTypeIsProvidedDuringRegistration() { Metadata metadata = Metadata.builder() .withType(MetricType.COUNTER) .withName("test") .build(); // the Ambiguous class can be a counter as well as gauge, but we specified the type in the metadata // so it should be registered as a counter registry.register(metadata, new Ambiguous()); Assert.assertEquals(MetricType.COUNTER, registry.getMetadata("test").getTypeRaw()); Assert.assertEquals(666L, registry.getCounter(new MetricID("test")).getCount()); }
Example #29
Source File: OpenMetricsExporterTest.java From smallrye-metrics with Apache License 2.0 | 5 votes |
/** * OpenMetrics exporter should only emit a HELP line if a description exists and is not empty */ @Test public void testEmptyDescription() { OpenMetricsExporter exporter = new OpenMetricsExporter(); MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION); Metadata metadata = Metadata.builder() .withName("counter_with_empty_description") .withDescription("").build(); registry.counter(metadata); String export = exporter .exportOneMetric(MetricRegistry.Type.APPLICATION, new MetricID("counter_with_empty_description")).toString(); assertThat(export, not(containsString("HELP"))); }
Example #30
Source File: ReusabilityByDefaultTest.java From smallrye-metrics with Apache License 2.0 | 5 votes |
@Test public void testMeter() { Metadata metadata = Metadata.builder().withName("mymeter").build(); registry.meter(metadata).mark(100); registry.meter(metadata).mark(100); assertEquals(200, registry.meter("mymeter").getCount()); }