Search in sources :

Example 1 with MetricsActivitiesImpl

use of io.temporal.samples.metrics.activities.MetricsActivitiesImpl in project samples-java by temporalio.

the class MetricsWorker method main.

public static void main(String[] args) {
    // Set up prometheus registry and stats reported
    PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
    // Set up a new scope, report every 1 second
    Scope scope = new RootScopeBuilder().tags(ImmutableMap.of("workerCustomTag1", "workerCustomTag1Value", "workerCustomTag2", "workerCustomTag2Value")).reporter(new MicrometerClientStatsReporter(registry)).reportEvery(com.uber.m3.util.Duration.ofSeconds(1));
    // Start the prometheus scrape endpoint
    HttpServer scrapeEndpoint = MetricsUtils.startPrometheusScrapeEndpoint(registry, 8080);
    // Stopping the worker will stop the http server that exposes the
    // scrape endpoint.
    Runtime.getRuntime().addShutdownHook(new Thread(() -> scrapeEndpoint.stop(1)));
    // Add metrics scope to workflow service stub options
    WorkflowServiceStubsOptions stubOptions = WorkflowServiceStubsOptions.newBuilder().setMetricsScope(scope).build();
    WorkflowServiceStubs service = WorkflowServiceStubs.newInstance(stubOptions);
    WorkflowClient client = WorkflowClient.newInstance(service);
    WorkerFactory factory = WorkerFactory.newInstance(client);
    Worker worker = factory.newWorker(DEFAULT_TASK_QUEUE_NAME);
    worker.registerWorkflowImplementationTypes(MetricsWorkflowImpl.class);
    worker.registerActivitiesImplementations(new MetricsActivitiesImpl());
    factory.start();
    System.out.println("Workers metrics are available at http://localhost:8080/prometheus");
}
Also used : PrometheusMeterRegistry(io.micrometer.prometheus.PrometheusMeterRegistry) RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) MicrometerClientStatsReporter(io.temporal.common.reporter.MicrometerClientStatsReporter) Scope(com.uber.m3.tally.Scope) MetricsActivitiesImpl(io.temporal.samples.metrics.activities.MetricsActivitiesImpl) HttpServer(com.sun.net.httpserver.HttpServer) WorkflowClient(io.temporal.client.WorkflowClient) Worker(io.temporal.worker.Worker) WorkerFactory(io.temporal.worker.WorkerFactory) WorkflowServiceStubsOptions(io.temporal.serviceclient.WorkflowServiceStubsOptions) WorkflowServiceStubs(io.temporal.serviceclient.WorkflowServiceStubs)

Aggregations

HttpServer (com.sun.net.httpserver.HttpServer)1 RootScopeBuilder (com.uber.m3.tally.RootScopeBuilder)1 Scope (com.uber.m3.tally.Scope)1 PrometheusMeterRegistry (io.micrometer.prometheus.PrometheusMeterRegistry)1 WorkflowClient (io.temporal.client.WorkflowClient)1 MicrometerClientStatsReporter (io.temporal.common.reporter.MicrometerClientStatsReporter)1 MetricsActivitiesImpl (io.temporal.samples.metrics.activities.MetricsActivitiesImpl)1 WorkflowServiceStubs (io.temporal.serviceclient.WorkflowServiceStubs)1 WorkflowServiceStubsOptions (io.temporal.serviceclient.WorkflowServiceStubsOptions)1 Worker (io.temporal.worker.Worker)1 WorkerFactory (io.temporal.worker.WorkerFactory)1