Search in sources :

Example 1 with MicrometerClientStatsReporter

use of io.temporal.common.reporter.MicrometerClientStatsReporter in project samples-java by temporalio.

the class MetricsStarter 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("starterCustomTag1", "starterCustomTag1Value", "starterCustomTag2", "starterCustomTag2Value")).reporter(new MicrometerClientStatsReporter(registry)).reportEvery(com.uber.m3.util.Duration.ofSeconds(1));
    // Start the prometheus scrape endpoint for starter metrics
    HttpServer scrapeEndpoint = MetricsUtils.startPrometheusScrapeEndpoint(registry, 8081);
    // Stopping the starter 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);
    WorkflowOptions workflowOptions = WorkflowOptions.newBuilder().setWorkflowId("metricsWorkflow").setTaskQueue(MetricsWorker.DEFAULT_TASK_QUEUE_NAME).build();
    MetricsWorkflow workflow = client.newWorkflowStub(MetricsWorkflow.class, workflowOptions);
    String result = workflow.exec("hello metrics");
    System.out.println("Result: " + result);
    System.out.println("Starter metrics are available at http://localhost:8081/prometheus");
// We don't shut down the process here so metrics can be viewed.
}
Also used : PrometheusMeterRegistry(io.micrometer.prometheus.PrometheusMeterRegistry) MetricsWorkflow(io.temporal.samples.metrics.workflow.MetricsWorkflow) RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) MicrometerClientStatsReporter(io.temporal.common.reporter.MicrometerClientStatsReporter) Scope(com.uber.m3.tally.Scope) HttpServer(com.sun.net.httpserver.HttpServer) WorkflowClient(io.temporal.client.WorkflowClient) WorkflowOptions(io.temporal.client.WorkflowOptions) WorkflowServiceStubsOptions(io.temporal.serviceclient.WorkflowServiceStubsOptions) WorkflowServiceStubs(io.temporal.serviceclient.WorkflowServiceStubs)

Example 2 with MicrometerClientStatsReporter

use of io.temporal.common.reporter.MicrometerClientStatsReporter 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)2 RootScopeBuilder (com.uber.m3.tally.RootScopeBuilder)2 Scope (com.uber.m3.tally.Scope)2 PrometheusMeterRegistry (io.micrometer.prometheus.PrometheusMeterRegistry)2 WorkflowClient (io.temporal.client.WorkflowClient)2 MicrometerClientStatsReporter (io.temporal.common.reporter.MicrometerClientStatsReporter)2 WorkflowServiceStubs (io.temporal.serviceclient.WorkflowServiceStubs)2 WorkflowServiceStubsOptions (io.temporal.serviceclient.WorkflowServiceStubsOptions)2 WorkflowOptions (io.temporal.client.WorkflowOptions)1 MetricsActivitiesImpl (io.temporal.samples.metrics.activities.MetricsActivitiesImpl)1 MetricsWorkflow (io.temporal.samples.metrics.workflow.MetricsWorkflow)1 Worker (io.temporal.worker.Worker)1 WorkerFactory (io.temporal.worker.WorkerFactory)1