Java Code Examples for com.codahale.metrics.ConsoleReporter#start()

The following examples show how to use com.codahale.metrics.ConsoleReporter#start() . 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: ConsoleReporterStarter.java    From fluo with Apache License 2.0 6 votes vote down vote up
@Override
public List<AutoCloseable> start(Params params) {
  SimpleConfiguration config =
      new FluoConfiguration(params.getConfiguration()).getReporterConfiguration("console");

  if (!config.getBoolean("enable", false)) {
    return Collections.emptyList();
  }

  TimeUnit rateUnit = TimeUnit.valueOf(config.getString("rateUnit", "seconds").toUpperCase());
  TimeUnit durationUnit =
      TimeUnit.valueOf(config.getString("durationUnit", "milliseconds").toUpperCase());
  PrintStream out = System.out;
  if (config.getString("target", "stdout").equals("stderr")) {
    out = System.err;
  }

  ConsoleReporter reporter = ConsoleReporter.forRegistry(params.getMetricRegistry())
      .convertDurationsTo(durationUnit).convertRatesTo(rateUnit).outputTo(out).build();
  reporter.start(config.getInt("frequency", 60), TimeUnit.SECONDS);

  log.info("Reporting metrics to console");

  return Collections.singletonList((AutoCloseable) reporter);
}
 
Example 2
Source File: SentryMetrics.java    From incubator-sentry with Apache License 2.0 6 votes vote down vote up
public synchronized void initReporting(Reporting reporting) {
  if(!reportingInitialized) {
    switch(reporting) {
      case CONSOLE:
        final ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(SentryMetricsServletContextListener.METRIC_REGISTRY)
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .build();
        consoleReporter.start(1, TimeUnit.SECONDS);
        break;
      case JMX:
        final JmxReporter jmxReporter = JmxReporter.forRegistry(SentryMetricsServletContextListener.METRIC_REGISTRY)
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .build();
        jmxReporter.start();
        break;
    }
  }
}
 
Example 3
Source File: DiagnosticMode.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
private void startLoggingMetrics(final File diagnosticFolder) {
    final File metricLog = new File(diagnosticFolder, FILE_NAME_METRIC_LOG);

    try {
        final PrintStream logStream = new PrintStream(metricLog, Charset.defaultCharset().name());
        final ConsoleReporter metricReporter = ConsoleReporter.forRegistry(metricRegistry)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .outputTo(logStream)
                .build();
        metricReporter.start(1, TimeUnit.SECONDS);

    } catch (final IOException e) {
        log.error("Not able to create metric.log, for {}", e.getCause());
    }
}
 
Example 4
Source File: GetStarted.java    From metrics-zabbix with Apache License 2.0 6 votes vote down vote up
public static void main(String args[]) throws IOException, InterruptedException {
	ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).convertRatesTo(TimeUnit.SECONDS)
			.convertDurationsTo(TimeUnit.MILLISECONDS).build();
	metrics.register("jvm.mem", new MemoryUsageGaugeSet());
	metrics.register("jvm.gc", new GarbageCollectorMetricSet());
	reporter.start(5, TimeUnit.SECONDS);

	String hostName = "192.168.66.29";
	ZabbixSender zabbixSender = new ZabbixSender("192.168.90.102", 10051);
	ZabbixReporter zabbixReporter = ZabbixReporter.forRegistry(metrics).hostName(hostName).prefix("test.")
			.build(zabbixSender);

	zabbixReporter.start(1, TimeUnit.SECONDS);

	TimeUnit.SECONDS.sleep(500);
}
 
Example 5
Source File: StandaloneExample.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    final MetricRegistry metrics = new MetricRegistry();

    final HadoopMetrics2Reporter metrics2Reporter = HadoopMetrics2Reporter.forRegistry(metrics).build(
            DefaultMetricsSystem.initialize("StandaloneTest"), // The application-level name
            "Test", // Component name
            "Test", // Component description
            "Test"); // Name for each metric record
    final ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metrics).build();

    MetricsSystem metrics2 = DefaultMetricsSystem.instance();
    // Writes to stdout without a filename configuration
    // Will be invoked every 10seconds by default
    FileSink sink = new FileSink();
    metrics2.register("filesink", "filesink", sink);
    sink.init(new SubsetConfiguration(null, null) {
        public String getString(String key) {
            if (key.equals("filename")) {
                return null;
            }
            return super.getString(key);
        }
    });

    // How often should the dropwizard reporter be invoked
    metrics2Reporter.start(500, TimeUnit.MILLISECONDS);
    // How often will the dropwziard metrics be logged to the console
    consoleReporter.start(2, TimeUnit.SECONDS);

    generateMetrics(metrics, 5000, 25, TimeUnit.MILLISECONDS, metrics2Reporter, 10);
}
 
Example 6
Source File: StreamingMetrics.java    From kylin with Apache License 2.0 5 votes vote down vote up
public void start() {
    switch (METRICS_OPTION) {
    case "":
        logger.info("Skip streaming metricRegistry because it is empty.");
        break;
    // for test purpose
    case "console":
        logger.info("Use console to collect streaming metricRegistry.");
        ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry)
                .convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
        consoleReporter.start(STREAM_METRICS_INTERVAL, TimeUnit.SECONDS);
        break;
    case "csv":
        File metricsFolder = new File("stream_metrics_csv");
        if (!metricsFolder.exists()) {
            boolean res = metricsFolder.mkdirs();
            if (!res) {
                logger.error("Cannot create dir for stream_metrics_csv");
                break;
            }
        }
        logger.info("Collect streaming metricRegistry in csv format.");
        CsvReporter scvReporter = CsvReporter.forRegistry(metricRegistry).convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS).build(metricsFolder);
        scvReporter.start(STREAM_METRICS_INTERVAL, TimeUnit.SECONDS);
        break;
    case "jmx":
        final JmxReporter jmxReporter = JmxReporter.forRegistry(metricRegistry).build();
        jmxReporter.start();
        break;
    default:
        logger.info("Skip metricRegistry because the option {} is not identified.", METRICS_OPTION);
    }
}
 
Example 7
Source File: StandaloneExample.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    final MetricRegistry metrics = new MetricRegistry();

    final HadoopMetrics2Reporter metrics2Reporter = HadoopMetrics2Reporter.forRegistry(metrics).build(
            DefaultMetricsSystem.initialize("StandaloneTest"), // The application-level name
            "Test", // Component name
            "Test", // Component description
            "Test"); // Name for each metric record
    final ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metrics).build();

    MetricsSystem metrics2 = DefaultMetricsSystem.instance();
    // Writes to stdout without a filename configuration
    // Will be invoked every 10seconds by default
    FileSink sink = new FileSink();
    metrics2.register("filesink", "filesink", sink);
    sink.init(new SubsetConfiguration(null, null) {
        public String getString(String key) {
            if (key.equals("filename")) {
                return null;
            }
            return super.getString(key);
        }
    });

    // How often should the dropwizard reporter be invoked
    metrics2Reporter.start(500, TimeUnit.MILLISECONDS);
    // How often will the dropwziard metrics be logged to the console
    consoleReporter.start(2, TimeUnit.SECONDS);

    generateMetrics(metrics, 5000, 25, TimeUnit.MILLISECONDS, metrics2Reporter, 10);
}
 
Example 8
Source File: MemoryUsageGaugeSetTest.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Test
public void testJVMMetrics() throws InterruptedException {
    LOG.info("Starting testJVMMetrics");
    final MetricRegistry metrics = new MetricRegistry();
    ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
        .convertRatesTo(TimeUnit.SECONDS)
        .convertDurationsTo(TimeUnit.MILLISECONDS)
        .build();
    metrics.registerAll(new MemoryUsageGaugeSet());
    metrics.register("sample", (Gauge<Double>) () -> 0.1234);
    reporter.start(1, TimeUnit.SECONDS);
    reporter.close();
}
 
Example 9
Source File: SendToLocalInfluxDB.java    From dropwizard-metrics-influxdb with Apache License 2.0 5 votes vote down vote up
private static ConsoleReporter startConsoleReporter(MetricRegistry registry) throws Exception {
    final ConsoleReporter reporter = ConsoleReporter
        .forRegistry(registry)
        .convertRatesTo(TimeUnit.SECONDS)
        .convertDurationsTo(TimeUnit.MILLISECONDS)
        .build();
    reporter.start(1, TimeUnit.MINUTES);
    return reporter;
}
 
Example 10
Source File: MetricsSetup.java    From quarks with Apache License 2.0 5 votes vote down vote up
/**
 * Starts the metric {@code ConsoleReporter} polling every second.
 */
public MetricsSetup startConsoleReporter() {
    ConsoleReporter reporter = ConsoleReporter.forRegistry(registry()).convertRatesTo(ratesUnit)
            .convertDurationsTo(durationsUnit).build();
    reporter.start(1, TimeUnit.SECONDS);
    return this;
}
 
Example 11
Source File: MetricsReporting.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static Consumer<RatisMetricRegistry> consoleReporter(TimeDuration rate) {
  return ratisMetricRegistry -> {
    ConsoleReporter reporter = ConsoleReporter.forRegistry(ratisMetricRegistry.getDropWizardMetricRegistry())
        .convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
    reporter.start(rate.getDuration(), rate.getUnit());
    ratisMetricRegistry.setConsoleReporter(reporter);
  };
}
 
Example 12
Source File: JbootConsoleReporter.java    From jboot with Apache License 2.0 5 votes vote down vote up
@Override
public void report(MetricRegistry metricRegistry) {
    final ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry)
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .build();
    reporter.start(1, TimeUnit.MINUTES);
}
 
Example 13
Source File: StreamingMetrics.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public void start() {
    switch (METRICS_OPTION) {
    case "":
        logger.info("Skip streaming metricRegistry because it is empty.");
        break;
    // for test purpose
    case "console":
        logger.info("Use console to collect streaming metricRegistry.");
        ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry)
                .convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
        consoleReporter.start(STREAM_METRICS_INTERVAL, TimeUnit.SECONDS);
        break;
    case "csv":
        File metricsFolder = new File("stream_metrics_csv");
        if (!metricsFolder.exists()) {
            boolean res = metricsFolder.mkdirs();
            if (!res) {
                logger.error("Cannot create dir for stream_metrics_csv");
                break;
            }
        }
        logger.info("Collect streaming metricRegistry in csv format.");
        CsvReporter scvReporter = CsvReporter.forRegistry(metricRegistry).convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS).build(metricsFolder);
        scvReporter.start(STREAM_METRICS_INTERVAL, TimeUnit.SECONDS);
        break;
    case "jmx":
        final JmxReporter jmxReporter = JmxReporter.forRegistry(metricRegistry).build();
        jmxReporter.start();
        break;
    default:
        logger.info("Skip metricRegistry because the option {} is not identified.", METRICS_OPTION);
    }
}
 
Example 14
Source File: AtomicRangeGroup.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
public AtomicRangeGroup(String dataPath, String groupId, PeerId serverId, long minSlot, long maxSlot,
                        NodeOptions nodeOptions, RpcServer rpcServer) throws IOException {
    // Init file path
    FileUtils.forceMkdir(new File(dataPath));
    this.minSlot = minSlot;
    this.maxSlot = maxSlot;

    // Init statemachine
    this.fsm = new AtomicStateMachine();

    // Set statemachine to bootstrap options
    nodeOptions.setFsm(this.fsm);
    nodeOptions.setEnableMetrics(true);
    nodeOptions.getRaftOptions().setReplicatorPipeline(true);
    nodeOptions.getRaftOptions().setSync(true);
    nodeOptions.getRaftOptions().setReadOnlyOptions(ReadOnlyOption.ReadOnlySafe);

    // Set the data path
    // Log, required
    nodeOptions.setLogUri(dataPath + File.separator + "log");
    // Metadata, required
    nodeOptions.setRaftMetaUri(dataPath + File.separator + "raft_meta");
    // Snapshot, not required, but recommend
    nodeOptions.setSnapshotUri(dataPath + File.separator + "snapshot");
    // Init raft group service framework
    this.raftGroupService = new RaftGroupService(groupId, serverId, nodeOptions, rpcServer);
    // Startup node
    this.node = this.raftGroupService.start();

    final ConsoleReporter reporter = ConsoleReporter.forRegistry(node.getNodeMetrics().getMetricRegistry())
        .convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
    reporter.start(60, TimeUnit.SECONDS);

}
 
Example 15
Source File: KafkaReporterSample.java    From metrics-kafka with Apache License 2.0 4 votes vote down vote up
public static void main(String args[]) throws IOException,
		InterruptedException {
	ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
			.convertRatesTo(TimeUnit.SECONDS)
			.convertDurationsTo(TimeUnit.MILLISECONDS).build();
	metrics.register("jvm.mem", new MemoryUsageGaugeSet());
	metrics.register("jvm.gc", new GarbageCollectorMetricSet());

	final Histogram responseSizes = metrics.histogram("response-sizes");
	final com.codahale.metrics.Timer metricsTimer = metrics
			.timer("test-timer");

	timer.schedule(new TimerTask() {
		int i = 100;

		@Override
		public void run() {
			Context context = metricsTimer.time();
			try {
				TimeUnit.MILLISECONDS.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			responseSizes.update(i++);
			context.stop();
		}

	}, 1000, 1000);

	reporter.start(5, TimeUnit.SECONDS);

	String hostName = "localhost";
	String topic = "test-kafka-reporter";
	Properties props = new Properties();
	props.put("metadata.broker.list", "127.0.0.1:9092");
	props.put("serializer.class", "kafka.serializer.StringEncoder");
	props.put("partitioner.class", "kafka.producer.DefaultPartitioner");
	props.put("request.required.acks", "1");

	String prefix = "test.";
	ProducerConfig config = new ProducerConfig(props);
	KafkaReporter kafkaReporter = KafkaReporter.forRegistry(metrics)
			.config(config).topic(topic).hostName(hostName).prefix(prefix).build();

	kafkaReporter.start(1, TimeUnit.SECONDS);

	TimeUnit.SECONDS.sleep(500);
}
 
Example 16
Source File: MetricsConfiguration.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
@Bean(destroyMethod = "stop")
public ConsoleReporter consoleReporter() {
    ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry).build();
    reporter.start(1, TimeUnit.DAYS);
    return reporter;
}
 
Example 17
Source File: ServingServer.java    From FATE-Serving with Apache License 2.0 4 votes vote down vote up
private void start(String[] args) throws Exception {
    this.initialize();
    applicationContext = SpringApplication.run(SpringConfig.class, args);
    ApplicationHolder.applicationContext = applicationContext;
    int port = Integer.parseInt(Configuration.getProperty(Dict.PROPERTY_SERVER_PORT));
    //TODO: Server custom configuration

    int processors = Runtime.getRuntime().availableProcessors();

    Integer corePoolSize = Configuration.getPropertyInt("serving.core.pool.size", processors);
    Integer maxPoolSize = Configuration.getPropertyInt("serving.max.pool.size", processors * 2);
    Integer aliveTime = Configuration.getPropertyInt("serving.pool.alive.time", 1000);
    Integer queueSize = Configuration.getPropertyInt("serving.pool.queue.size", 10);
    Executor executor = new ThreadPoolExecutor(corePoolSize, maxPoolSize, aliveTime.longValue(), TimeUnit.MILLISECONDS,
            new SynchronousQueue(), new NamedThreadFactory("ServingServer", true));

    FateServerBuilder serverBuilder = (FateServerBuilder) ServerBuilder.forPort(port);
    serverBuilder.keepAliveTime(100,TimeUnit.MILLISECONDS);
    serverBuilder.executor(executor);
    //new ServiceOverloadProtectionHandle()
    serverBuilder.addService(ServerInterceptors.intercept(applicationContext.getBean(InferenceService.class), new ServiceExceptionHandler(), new ServiceOverloadProtectionHandle()), InferenceService.class);
    serverBuilder.addService(ServerInterceptors.intercept(applicationContext.getBean(ModelService.class), new ServiceExceptionHandler(), new ServiceOverloadProtectionHandle()), ModelService.class);
    serverBuilder.addService(ServerInterceptors.intercept(applicationContext.getBean(ProxyService.class), new ServiceExceptionHandler(), new ServiceOverloadProtectionHandle()), ProxyService.class);
    server = serverBuilder.build();
    logger.info("server started listening on port: {}, use configuration: {}", port, this.confPath);
    server.start();
    String userRegisterString = Configuration.getProperty(Dict.USE_REGISTER,"true");
    useRegister = Boolean.valueOf(userRegisterString);
    if(useRegister) {
        logger.info("serving-server is using register center");
    }
    else{
        logger.warn("serving-server not use register center");
    }
    if (useRegister) {
        ZookeeperRegistry zookeeperRegistry = applicationContext.getBean(ZookeeperRegistry.class);
        zookeeperRegistry.subProject(Dict.PROPERTY_PROXY_ADDRESS);
        zookeeperRegistry.subProject(Dict.PROPERTY_FLOW_ADDRESS);

        BaseModel.routerService = applicationContext.getBean(RouterService.class);
        FateServer.serviceSets.forEach(servie -> {
            try {
                String serviceName = servie.serviceName();
                String weightKey = serviceName + ".weight";
                HashMap properties = Configuration.getProperties();
                if (properties.get(weightKey) != null) {
                    int weight = Integer.valueOf(properties.get(weightKey).toString());
                    if (weight > 0) {
                        zookeeperRegistry.getServieWeightMap().put(weightKey, weight);
                    }
                }
            } catch (Throwable e) {
                logger.error("parse interface weight error", e);
            }

        });

        zookeeperRegistry.register(FateServer.serviceSets);

    }

    ModelService modelService = applicationContext.getBean(ModelService.class);
    modelService.restore();

    ConsoleReporter reporter = applicationContext.getBean(ConsoleReporter.class);
    reporter.start(1, TimeUnit.MINUTES);

    JmxReporter jmxReporter = applicationContext.getBean(JmxReporter.class);
    jmxReporter.start();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            logger.info("*** shutting down gRPC server since JVM is shutting down");
            ServingServer.this.stop();
            logger.info("*** server shut down");
        }
    });
}
 
Example 18
Source File: MergeSort.java    From newts with Apache License 2.0 4 votes vote down vote up
public void execute(String... args) throws IOException {
    

    CmdLineParser parser = createCmdLineParser();
    try {
            parser.parseArgument(args);
    } catch (CmdLineException e) {
        // handling of wrong arguments
        System.err.println(e.getMessage());
        parser.printUsage(System.err);
        return;
    }
    
    final MetricRegistry metrics = new MetricRegistry();
    ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
            .outputTo(System.err)
            .convertRatesTo(SECONDS)
            .convertDurationsTo(MILLISECONDS)
            .build();

    reporter.start(10, SECONDS);


    Meter linesMeter = metrics.meter("lines");
    Meter filesMeter = metrics.meter("files");
    Meter dirsMeter = metrics.meter("dirs");
    Meter batchMeter = metrics.meter("batches");
    Path root = m_source.toPath();
    
    if (m_targetDir == null) {
        m_targetDir = Files.createTempDir();
        System.err.println("Working Directory: " + m_targetDir);
    }
    
    LOG.debug("Scanning {} for GSOD data files...", root);

    FluentIterable<KeyedIterable<Path, Path>> dirs = FileIterable.groupFilesByDir(root);

    for(KeyedIterable<Path, Path> filesInDir : dirs) {
        Path subdir = root.relativize(filesInDir.getKey());
        String dirName = subdir.getFileName().toString();
        
        System.err.println("Sorted dir: " + subdir);
        FluentIterable<Iterable<String>> contentIterables = filesInDir
                .transform(this.<Path>meter(filesMeter))
                .transform(lines("YEARMODA"))
                ;
        FluentIterable<List<Iterable<String>>> batches = FluentIterable.from(Iterables.partition(contentIterables, m_mergeCount));
        FluentIterable<Iterable<GSODLine>> sortedBatches = batches.transform(lift2GsodLines()).transform(mergeSorter());

        Path sortedDir = m_targetDir.toPath().resolve(subdir);
        sortedDir.toFile().mkdirs();
        
        int count = 1;
        for(Iterable<GSODLine> batch : sortedBatches) {
            Path sortedFile = sortedDir.resolve(dirName+"-batch-"+(count++)+".gz");
            System.err.println("Creating " + sortedFile);
            try (PrintStream out = open(sortedFile)) {
                out.println(HDR);
                for(GSODLine line : batch) {
                    out.println(line);
                    linesMeter.mark();
                }
            }
            batchMeter.mark();
        }
        
        dirsMeter.mark();
        
    }
    
    
}