Java Code Examples for com.spotify.metrics.core.MetricId#build()

The following examples show how to use com.spotify.metrics.core.MetricId#build() . 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: FastForwardAgent.java    From ffwd with Apache License 2.0 6 votes vote down vote up
private static Statistics setupStatistics() throws IOException {
    final String key = System.getProperty("ffwd.key", "ffwd-java");

    final SemanticMetricRegistry registry = new SemanticMetricRegistry();
    final SemanticCoreStatistics statistics = new SemanticCoreStatistics(registry);

    final MetricId gauges = MetricId.build();

    registry.register(gauges, new ThreadStatesMetricSet());
    registry.register(gauges, new GarbageCollectorMetricSet());
    registry.register(gauges, new MemoryUsageGaugeSet());

    final MetricId metric = MetricId.build(key);

    final FastForwardReporter ffwd = FastForwardReporter
        .forRegistry(registry)
        .schedule(TimeUnit.SECONDS, 30)
        .prefix(metric)
        .build();

    ffwd.start();

    return new Statistics(ffwd, statistics);
}
 
Example 2
Source File: DynamicMetricExample.java    From semantic-metrics with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws Exception {
    final MetricId base = MetricId.build("current-thing");

    final FastForwardReporter reporter = FastForwardReporter
        .forRegistry(registry)
        .prefix(APP_PREFIX)
        .schedule(TimeUnit.SECONDS, 5)
        .build();

    reporter.start();

    final String[] things = {
        "shoe", "bucket", "chair"
    };

    for (int i = 0; i < things.length; i++) {
        final String thing = things[i];
        Thread.sleep(1000);
        final Meter meter =
            registry.meter(base.tagged("thing", thing).tagged("index", Integer.toString(i)));
        meter.mark(10 * i);
    }

    System.out.println("Sending dynamic metrics...");
    System.in.read();

    reporter.stop();
}
 
Example 3
Source File: FastForwardReporterTest.java    From semantic-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAddExtractedTags() throws Exception {
    final Map<String, String> tags = ImmutableMap.of("FFWD_TAG_foo", "bar");

    final Supplier<Map<String, String>> environmentSupplier =
        Suppliers.ofInstance(tags);

    reporter = FastForwardReporter
        .forRegistry(registry)
        .schedule(TimeUnit.MILLISECONDS, REPORTING_PERIOD)
        .fastForward(fastForward)
        .tagExtractor(new EnvironmentTagExtractor(environmentSupplier))
        .executorService(executorService)
        .build();

    ArgumentCaptor<Metric> argument = ArgumentCaptor.forClass(Metric.class);

    MetricId name = MetricId.build("thename");

    final com.codahale.metrics.Counter counter = registry.counter(name);
    counter.inc(1);
    reporter.start();

    executorService.tick(REPORTING_PERIOD + REPORTING_PERIOD / 3, TimeUnit.MILLISECONDS);

    verify(fastForward, atLeastOnce()).send(argument.capture());

    final ImmutableMap<String, String> expected =
        ImmutableMap.of("metric_type", "counter", "foo", "bar");

    assertEquals(expected, argument.getValue().getAttributes());
}
 
Example 4
Source File: GarbageCollectorMetricSet.java    From semantic-metrics with Apache License 2.0 5 votes vote down vote up
@Override
public Map<MetricId, Metric> getMetrics() {
    final Map<MetricId, Metric> gauges = new HashMap<MetricId, Metric>();
    final MetricId base = MetricId.build();

    for (final GarbageCollectorMXBean m : garbageCollectors) {
        final MetricId gc = base.tagged("gc", m.getName());

        final MetricId collectionCount =
            gc.tagged("what", "jvm-gc-collections", "unit", "collection/s");
        final MetricId collectionTime =
            gc.tagged("what", "jvm-gc-collection-time", "unit", "ms/s");

        gauges.put(collectionCount, new DerivedLongGauge() {
            @Override
            public Long getNext() {
                return m.getCollectionCount();
            }
        });

        gauges.put(collectionTime, new DerivedLongGauge() {
            @Override
            public Long getNext() {
                return m.getCollectionTime();
            }
        });
    }

    return Collections.unmodifiableMap(gauges);
}
 
Example 5
Source File: SemanticStatisticsModule.java    From heroic with Apache License 2.0 4 votes vote down vote up
@Provides
@SemanticStatisticsScope
public LifeCycle life(
    AsyncFramework async, SemanticMetricRegistry registry, ServiceInfo info,
    LifeCycleRegistry lifeCycleRegistry
) {
    final MetricId gauges = MetricId.build();

    registry.register(gauges, new ThreadStatesMetricSet());
    registry.register(gauges, new GarbageCollectorMetricSet());
    registry.register(gauges, new MemoryUsageGaugeSet());
    registry.register(gauges, CpuGaugeSet.create());

    final MetricId metric =
        MetricId.build("heroic").tagged("service", "heroic", "heroic_id", info.getId());

    final FastForwardReporter reporter;

    try {
        reporter = FastForwardReporter
            .forRegistry(registry)
            .schedule(TimeUnit.SECONDS, 30)
            .prefix(metric)
            .build();
    } catch (IOException e) {
        throw new RuntimeException("Failed to configure reporter", e);
    }

    return () -> {
        final LifeCycleRegistry scope = lifeCycleRegistry.scoped("ffwd-reporter");

        scope.start(() -> async.call(() -> {
            reporter.start();
            return null;
        }));

        scope.stop(() -> async.call(() -> {
            reporter.stop();
            return null;
        }));
    };
}