Java源码示例:io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics

示例1
private void configureMetrics(ServerBuilder sb, PrometheusMeterRegistry registry) {
    sb.meterRegistry(registry);
    sb.service(METRICS_PATH, new PrometheusExpositionService(registry.getPrometheusRegistry()));
    sb.decorator(MetricCollectingService.newDecorator(MeterIdPrefixFunction.ofDefault("api")));

    // Bind system metrics.
    new FileDescriptorMetrics().bindTo(registry);
    new ProcessorMetrics().bindTo(registry);
    new ClassLoaderMetrics().bindTo(registry);
    new UptimeMetrics().bindTo(registry);
    new DiskSpaceMetrics(cfg.dataDir()).bindTo(registry);
    new JvmGcMetrics().bindTo(registry);
    new JvmMemoryMetrics().bindTo(registry);
    new JvmThreadMetrics().bindTo(registry);

    // Bind global thread pool metrics.
    ExecutorServiceMetrics.monitor(registry, ForkJoinPool.commonPool(), "commonPool");
}
 
示例2
public static void main(String[] args) {
    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    ScheduledExecutorService es = Executors.newSingleThreadScheduledExecutor();
    new ExecutorServiceMetrics(es, "executor.sample", emptyList()).bindTo(registry);

    es.scheduleWithFixedDelay(() -> Mono.delay(Duration.ofMillis(20)).block(), 0,
            10, TimeUnit.MILLISECONDS);

    while (true) {
    }
}
 
示例3
@Override
public ScheduledExecutorService wrap(
    ScheduledExecutorService executor, String name, String... tags) {

  monitorThreadPoolExecutor(executor, name, tags);
  return ExecutorServiceMetrics.monitor(meterRegistry, executor, name, Tags.of(tags));
}
 
示例4
@Override
public CronExecutorService wrap(CronExecutorService executor, String name, String... tags) {
  monitorThreadPoolExecutor(executor, name, tags);
  new io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics(
          executor, name, Tags.of(tags))
      .bindTo(meterRegistry);

  return new TimedCronExecutorService(meterRegistry, executor, name, Tags.of(tags));
}
 
示例5
private void doStart() throws Exception {
    boolean success = false;
    ExecutorService repositoryWorker = null;
    ScheduledExecutorService purgeWorker = null;
    ProjectManager pm = null;
    CommandExecutor executor = null;
    PrometheusMeterRegistry meterRegistry = null;
    Server server = null;
    SessionManager sessionManager = null;
    try {
        meterRegistry = PrometheusMeterRegistries.newRegistry();

        logger.info("Starting the Central Dogma ..");
        final ThreadPoolExecutor repositoryWorkerImpl = new ThreadPoolExecutor(
                cfg.numRepositoryWorkers(), cfg.numRepositoryWorkers(),
                60, TimeUnit.SECONDS, new LinkedTransferQueue<>(),
                new DefaultThreadFactory("repository-worker", true));
        repositoryWorkerImpl.allowCoreThreadTimeOut(true);
        repositoryWorker = ExecutorServiceMetrics.monitor(meterRegistry, repositoryWorkerImpl,
                                                          "repositoryWorker");

        logger.info("Starting the project manager: {}", cfg.dataDir());

        purgeWorker = Executors.newSingleThreadScheduledExecutor(
                new DefaultThreadFactory("purge-worker", true));

        pm = new DefaultProjectManager(cfg.dataDir(), repositoryWorker, purgeWorker,
                                       meterRegistry, cfg.repositoryCacheSpec());

        logger.info("Started the project manager: {}", pm);

        logger.info("Current settings:\n{}", cfg);

        sessionManager = initializeSessionManager();

        logger.info("Starting the command executor ..");
        executor = startCommandExecutor(pm, repositoryWorker, purgeWorker,
                                        meterRegistry, sessionManager);
        if (executor.isWritable()) {
            logger.info("Started the command executor.");

            initializeInternalProject(executor);

            // Migrate tokens and create metadata files if it does not exist.
            MigrationUtil.migrate(pm, executor);
        }

        logger.info("Starting the RPC server.");
        server = startServer(pm, executor, meterRegistry, sessionManager);
        logger.info("Started the RPC server at: {}", server.activePorts());
        logger.info("Started the Central Dogma successfully.");
        success = true;
    } finally {
        if (success) {
            this.repositoryWorker = repositoryWorker;
            this.purgeWorker = purgeWorker;
            this.pm = pm;
            this.executor = executor;
            this.meterRegistry = meterRegistry;
            this.server = server;
            this.sessionManager = sessionManager;
        } else {
            doStop(server, executor, pm, repositoryWorker, purgeWorker, sessionManager);
        }
    }
}
 
示例6
@Bean
public MeterBinder executorServiceMetrics(final ExecutorService executor) {
    return new ExecutorServiceMetrics(executor, "http-example", singleton(Tag.of("clientId", "example")));
}
 
示例7
@Override
public ExecutorService wrap(ExecutorService executor, String name, String... tags) {
  monitorThreadPoolExecutor(executor, name, tags);
  return ExecutorServiceMetrics.monitor(meterRegistry, executor, name, Tags.of(tags));
}
 
示例8
@Override
public synchronized ScheduledExecutorService apply(Scheduler scheduler, ScheduledExecutorService service) {
	//this is equivalent to `toString`, a detailed name like `parallel("foo", 3)`
	String schedulerName = Scannable
			.from(scheduler)
			.scanOrDefault(Attr.NAME, scheduler.getClass().getName());

	//we hope that each NAME is unique enough, but we'll differentiate by Scheduler
	String schedulerId =
			seenSchedulers.computeIfAbsent(scheduler, s -> {
				int schedulerDifferentiator = this.schedulerDifferentiator
						.computeIfAbsent(schedulerName, k -> new AtomicInteger(0))
						.getAndIncrement();

				return (schedulerDifferentiator == 0) ? schedulerName
						: schedulerName + "#" + schedulerDifferentiator;
			});

	//we now want an executorId unique to a given scheduler
	String executorId = schedulerId + "-" +
			executorDifferentiator.computeIfAbsent(scheduler, key -> new AtomicInteger(0))
			                      .getAndIncrement();

	Tags tags = Tags.of(TAG_SCHEDULER_ID, schedulerId);

	/*
	Design note: we assume that a given Scheduler won't apply the decorator twice to the
	same ExecutorService. Even though, it would simply create an extraneous meter for
	that ExecutorService, which we think is not that bad (compared to paying the price
	upfront of also tracking executors instances to deduplicate). The main goal is to
	detect Scheduler instances that have already started decorating their executors,
	in order to avoid consider two calls in a row as duplicates (yet still being able
	to distinguish between two instances with the same name and configuration).
	 */

	class MetricsRemovingScheduledExecutorService extends DelegatingScheduledExecutorService {

		MetricsRemovingScheduledExecutorService() {
			super(ExecutorServiceMetrics.monitor(globalRegistry, service, executorId, tags));
		}

		@Override
		public List<Runnable> shutdownNow() {
			removeMetrics();
			return super.shutdownNow();
		}

		@Override
		public void shutdown() {
			removeMetrics();
			super.shutdown();
		}

		void removeMetrics() {
			Search.in(globalRegistry)
			      .tag("name", executorId)
			      .meters()
			      .forEach(globalRegistry::remove);
		}
	}
	return new MetricsRemovingScheduledExecutorService();
}