Java源码示例:org.eclipse.microprofile.metrics.MetadataBuilder

示例1
@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"));
}
 
示例2
@Test
public void testSimpleTimers() {
    JsonExporter exporter = new JsonExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = new MetadataBuilder()
            .withUnit(MetricUnits.SECONDS)
            .withName("mysimpletimer")
            .build();

    SimpleTimer timerWithoutTags = registry.simpleTimer(metadata);
    SimpleTimer timerRed = registry.simpleTimer(metadata, new Tag("color", "red"));
    SimpleTimer timerBlue = registry.simpleTimer(metadata, new Tag("color", "blue"), new Tag("foo", "bar"));

    timerWithoutTags.update(Duration.ofSeconds(1));
    timerRed.update(Duration.ofSeconds(2));
    timerBlue.update(Duration.ofSeconds(3));
    timerBlue.update(Duration.ofSeconds(4));

    String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "mysimpletimer").toString();
    JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject();

    JsonObject mytimerObject = json.getJsonObject("mysimpletimer");

    assertEquals(1.0, mytimerObject.getJsonNumber("count").doubleValue(), 1e-10);
    assertEquals(1.0, mytimerObject.getJsonNumber("count;color=red").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("count;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("elapsedTime").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("elapsedTime;color=red").doubleValue(), 1e-10);
    assertEquals(7.0, mytimerObject.getJsonNumber("elapsedTime;color=blue;foo=bar").doubleValue(), 1e-10);
}
 
示例3
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();
}
 
示例4
@Test
public void testTimers() {
    JsonExporter exporter = new JsonExporter();
    MetricRegistry registry = MetricRegistries.get(MetricRegistry.Type.APPLICATION);
    Metadata metadata = new MetadataBuilder()
            .withUnit(MetricUnits.SECONDS)
            .withName("mytimer")
            .build();

    Timer timerWithoutTags = registry.timer(metadata);
    Timer timerRed = registry.timer(metadata, new Tag("color", "red"));
    Timer timerBlue = registry.timer(metadata, new Tag("color", "blue"), new Tag("foo", "bar"));

    timerWithoutTags.update(Duration.ofSeconds(1));
    timerRed.update(Duration.ofSeconds(2));
    timerBlue.update(Duration.ofSeconds(3));

    String result = exporter.exportMetricsByName(MetricRegistry.Type.APPLICATION, "mytimer").toString();
    JsonObject json = Json.createReader(new StringReader(result)).read().asJsonObject();

    JsonObject mytimerObject = json.getJsonObject("mytimer");

    assertEquals(1.0, mytimerObject.getJsonNumber("count").doubleValue(), 1e-10);
    assertEquals(1.0, mytimerObject.getJsonNumber("count;color=red").doubleValue(), 1e-10);
    assertEquals(1.0, mytimerObject.getJsonNumber("count;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("elapsedTime").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("elapsedTime;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("elapsedTime;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p50").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p50;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p50;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p75").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p75;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p75;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p95").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p95;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p95;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p98").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p98;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p98;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p99").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p99;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p99;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("p999").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("p999;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("p999;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("min").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("min;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("min;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("mean").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("mean;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("mean;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(1.0, mytimerObject.getJsonNumber("max").doubleValue(), 1e-10);
    assertEquals(2.0, mytimerObject.getJsonNumber("max;color=red").doubleValue(), 1e-10);
    assertEquals(3.0, mytimerObject.getJsonNumber("max;color=blue;foo=bar").doubleValue(), 1e-10);

    assertEquals(0.0, mytimerObject.getJsonNumber("stddev").doubleValue(), 1e-10);
    assertEquals(0.0, mytimerObject.getJsonNumber("stddev;color=red").doubleValue(), 1e-10);
    assertEquals(0.0, mytimerObject.getJsonNumber("stddev;color=blue;foo=bar").doubleValue(), 1e-10);
}