Search in sources :

Example 16 with RootScopeBuilder

use of com.uber.m3.tally.RootScopeBuilder 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 17 with RootScopeBuilder

use of com.uber.m3.tally.RootScopeBuilder 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

RootScopeBuilder (com.uber.m3.tally.RootScopeBuilder)17 Scope (com.uber.m3.tally.Scope)14 StatsReporter (com.uber.m3.tally.StatsReporter)11 NoopScope (com.uber.cadence.internal.metrics.NoopScope)9 ImmutableMap (com.uber.m3.util.ImmutableMap)9 WorkflowOptions (com.uber.cadence.client.WorkflowOptions)5 PollForDecisionTaskResponse (com.uber.cadence.PollForDecisionTaskResponse)4 Test (org.junit.Test)4 TestStatsReporter (io.temporal.common.reporter.TestStatsReporter)3 Before (org.junit.Before)3 HttpServer (com.sun.net.httpserver.HttpServer)2 DeciderCache (com.uber.cadence.internal.replay.DeciderCache)2 TestEnvironmentOptions (com.uber.cadence.testing.TestEnvironmentOptions)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 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2