Java Code Examples for com.netflix.spectator.api.Clock#SYSTEM

The following examples show how to use com.netflix.spectator.api.Clock#SYSTEM . 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: SparkSink.java    From spectator with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new instance. Spark looks for a constructor with all three parameters, so the
 * {@code SecurityManager} needs to be in the signature even though it isn't used.
 */
@SuppressWarnings("PMD.UnusedFormalParameter")
public SparkSink(
    Properties properties,
    MetricRegistry registry,
    org.apache.spark.SecurityManager manager) throws MalformedURLException {
  final Config config = loadConfig();
  statelessRegistry = new StatelessRegistry(
      Clock.SYSTEM, new SpectatorConfig(config.getConfig("spectator.spark.stateless")));
  reporter = SpectatorReporter.forRegistry(registry)
      .withSpectatorRegistry(statelessRegistry)
      .withNameFunction(SparkNameFunction.fromConfig(config, statelessRegistry))
      .withValueFunction(SparkValueFunction.fromConfig(config))
      .withGaugeCounters(Pattern.compile(config.getString("spectator.spark.gauge-counters")))
      .build();
  pollPeriod = getPeriod(properties);
  pollUnit = getUnit(properties);

  // If there is a need to collect application metrics from jobs running on Spark, then
  // this should be enabled. The apps can report to the global registry and it will get
  // picked up by the Spark integration.
  if (shouldAddToGlobal(properties)) {
    Spectator.globalRegistry().add(statelessRegistry);
  }
}
 
Example 2
Source File: AtlasRegistryTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void shutdownWithoutStarting() {
  AtlasRegistry r = new AtlasRegistry(
      Clock.SYSTEM,
      k -> k.equals("atlas.enabled") ? "true" : null);
  r.close();
}
 
Example 3
Source File: PolledMeterTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void monitorValueNull() {
  Registry r = new DefaultRegistry(Clock.SYSTEM, p -> null);
  Id id = r.createId("test");

  PolledMeter.using(r).withId(id).<Collection<?>>monitorValue(null, Collection::size);
  PolledMeter.update(r);

  Assertions.assertEquals(Double.NaN, r.gauge(id).value());
}
 
Example 4
Source File: PolledMeterTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void monitorValueNullPropagate() {
  RegistryConfig config = s -> s.equals("propagateWarnings") ? "true" : null;
  Registry r = new DefaultRegistry(Clock.SYSTEM, config);
  Id id = r.createId("test");

  IllegalArgumentException e = Assertions.assertThrows(
      IllegalArgumentException.class,
      () -> PolledMeter.using(r)
          .withId(id)
          .<Collection<?>>monitorValue(null, Collection::size)
  );
  Assertions.assertTrue(e.getMessage().startsWith("obj is null"));
}
 
Example 5
Source File: PolledMeterTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void monitorMonotonicCounterNull() {
  Registry r = new DefaultRegistry(Clock.SYSTEM, p -> null);
  Id id = r.createId("test");

  AtomicLong v = new AtomicLong(42);
  PolledMeter.using(r).withId(id).<AtomicLong>monitorMonotonicCounter(null, n -> v.get());
  PolledMeter.update(r);

  Assertions.assertEquals(0, r.counter(id).count());
}
 
Example 6
Source File: PolledMeterTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@Test
public void monitorMonotonicCounterNullPropagate() {
  RegistryConfig config = s -> s.equals("propagateWarnings") ? "true" : null;
  Registry r = new DefaultRegistry(Clock.SYSTEM, config);
  Id id = r.createId("test");

  AtomicLong v = new AtomicLong(42);
  IllegalArgumentException e = Assertions.assertThrows(
      IllegalArgumentException.class,
      () -> PolledMeter.using(r)
          .withId(id)
          .<AtomicLong>monitorValue(null, n -> v.get())
  );
  Assertions.assertTrue(e.getMessage().startsWith("obj is null"));
}
 
Example 7
Source File: GlobalRegistry.java    From servicecomb-java-chassis with Apache License 2.0 4 votes vote down vote up
public GlobalRegistry() {
  this(Clock.SYSTEM);
}
 
Example 8
Source File: MetricsRegistry.java    From spectator with Apache License 2.0 4 votes vote down vote up
/** Create a new instance. */
public MetricsRegistry() {
  this(Clock.SYSTEM, new com.codahale.metrics.MetricRegistry());
}
 
Example 9
Source File: PercentileDistributionSummaryTest.java    From spectator with Apache License 2.0 4 votes vote down vote up
private Registry newRegistry() {
  return new DefaultRegistry(Clock.SYSTEM, k -> null);
}
 
Example 10
Source File: PercentileTimerTest.java    From spectator with Apache License 2.0 4 votes vote down vote up
private Registry newRegistry() {
  return new DefaultRegistry(Clock.SYSTEM, k -> null);
}
 
Example 11
Source File: Agent.java    From spectator with Apache License 2.0 4 votes vote down vote up
/** Entry point for the agent. */
public static void premain(String arg, Instrumentation instrumentation) throws Exception {
  // Setup logging
  final List<Object> resources = parseResourceList(arg);
  Config config = loadConfig(resources);
  LOGGER.debug("loaded configuration: {}", config.root().render());
  createDependencyProperties(config);

  // Setup Registry
  AtlasRegistry registry = new AtlasRegistry(Clock.SYSTEM, new AgentAtlasConfig(config));

  // Add to global registry for http stats and GC logger
  Spectator.globalRegistry().add(registry);

  // Enable GC logger
  GcLogger gcLogger = new GcLogger();
  if (config.getBoolean("collection.gc")) {
    gcLogger.start(null);
  }

  // Enable JVM data collection
  if (config.getBoolean("collection.jvm")) {
    Jmx.registerStandardMXBeans(registry);
  }

  // Enable JMX query collection
  if (config.getBoolean("collection.jmx")) {
    ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(r -> {
      Thread t = new Thread(r, "spectator-agent-jmx");
      t.setDaemon(true);
      return t;
    });

    // Poll the JMX data at least once per step interval unless it is less than a second
    // to avoid to much overhead
    Duration step = Duration.parse(config.getString("atlas.step"));
    long delay = Math.max(1000L, step.toMillis() / 2L);

    // Keep track of last time the configs have been loaded
    final AtomicLong lastUpdated = new AtomicLong(System.currentTimeMillis());
    final JmxPoller poller = new JmxPoller(registry);
    poller.updateConfigs(config.getConfigList("jmx.mappings"));

    exec.scheduleWithFixedDelay(() -> {
      try {
        List<File> updatedConfigs = findUpdatedConfigs(resources, lastUpdated.get());
        if (!updatedConfigs.isEmpty()) {
          LOGGER.info("detected updated config files: {}", updatedConfigs);
          lastUpdated.set(System.currentTimeMillis());
          Config cfg = loadConfig(resources);
          poller.updateConfigs(cfg.getConfigList("jmx.mappings"));
        }
      } catch (Exception e) {
        LOGGER.warn("failed to update jmx config mappings, using previous config", e);
      }
      poller.poll();
    }, delay, delay, TimeUnit.MILLISECONDS);
  }

  // Start collection for the registry
  registry.start();

  // Shutdown registry
  Runtime.getRuntime().addShutdownHook(new Thread(registry::stop, "spectator-agent-shutdown"));
}