Search in sources :

Example 31 with Scope

use of com.uber.m3.tally.Scope in project sdk-java by temporalio.

the class DeterministicRunnerTest method workflowThreadsWillEvictCacheWhenMaxThreadCountIsHit.

@Test
public void workflowThreadsWillEvictCacheWhenMaxThreadCountIsHit() throws Throwable {
    // Arrange
    // Arrange
    Map<String, String> tags = new ImmutableMap.Builder<String, String>(2).put(MetricsTag.NAMESPACE, "namespace").put(MetricsTag.TASK_QUEUE, "stickyTaskQueue").build();
    StatsReporter reporter = mock(StatsReporter.class);
    Scope scope = new RootScopeBuilder().reporter(reporter).reportEvery(com.uber.m3.util.Duration.ofMillis(300)).tagged(tags);
    ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 3, 1, TimeUnit.SECONDS, new SynchronousQueue<>());
    AtomicReference<String> status = new AtomicReference<>();
    WorkflowExecutorCache cache = new WorkflowExecutorCache(3, scope);
    ReplayWorkflowContext replayWorkflowContext = mock(ReplayWorkflowContext.class);
    when(replayWorkflowContext.getMetricsScope()).thenReturn(scope);
    when(replayWorkflowContext.getWorkflowExecution()).thenReturn(WorkflowExecution.newBuilder().setWorkflowId("id1").setRunId("run1").build());
    when(replayWorkflowContext.getNamespace()).thenReturn("namespace");
    when(replayWorkflowContext.getWorkflowType()).thenReturn(WorkflowType.getDefaultInstance());
    DeterministicRunnerImpl d = new DeterministicRunnerImpl(threadPool, new SyncWorkflowContext(replayWorkflowContext, DataConverter.getDefaultInstance(), null, null, null), () -> {
        Promise<Void> thread = Async.procedure(() -> {
            status.set("started");
            WorkflowThread.await("doing something", () -> false);
            status.set("done");
        });
        thread.get();
    }, cache);
    WorkflowRunTaskHandler workflowRunTaskHandler = new DeterministicRunnerTestWorkflowRunTaskHandler(d);
    PollWorkflowTaskQueueResponse response = HistoryUtils.generateWorkflowTaskWithInitialHistory();
    cache.getOrCreate(response, new com.uber.m3.tally.NoopScope(), () -> workflowRunTaskHandler);
    cache.addToCache(response.getWorkflowExecution(), workflowRunTaskHandler);
    d.runUntilAllBlocked(DeterministicRunner.DEFAULT_DEADLOCK_DETECTION_TIMEOUT_MS);
    assertEquals(2, threadPool.getActiveCount());
    assertEquals(1, cache.size());
    DeterministicRunnerImpl d2 = new DeterministicRunnerImpl(threadPool, new SyncWorkflowContext(replayWorkflowContext, DataConverter.getDefaultInstance(), null, null, null), () -> {
        Promise<Void> thread = Async.procedure(() -> {
            status.set("started");
            WorkflowThread.await("doing something else", () -> false);
            status.set("done");
        });
        thread.get();
    }, cache);
    // Act: This should kick out threads consumed by 'd'
    d2.runUntilAllBlocked(DeterministicRunner.DEFAULT_DEADLOCK_DETECTION_TIMEOUT_MS);
    // Assert: Cache is evicted and only threads consumed by d2 remain.
    assertEquals(2, threadPool.getActiveCount());
    // cache was evicted
    assertEquals(0, cache.size());
    // Wait for reporter
    Thread.sleep(600);
    verify(reporter, atLeastOnce()).reportCounter(eq(MetricsType.STICKY_CACHE_THREAD_FORCED_EVICTION), eq(tags), anyLong());
}
Also used : RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) NoopScope(com.uber.m3.tally.NoopScope) StatsReporter(com.uber.m3.tally.StatsReporter) ImmutableMap(com.uber.m3.util.ImmutableMap) PollWorkflowTaskQueueResponse(io.temporal.api.workflowservice.v1.PollWorkflowTaskQueueResponse) NoopScope(com.uber.m3.tally.NoopScope) Scope(com.uber.m3.tally.Scope) CancellationScope(io.temporal.workflow.CancellationScope) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor)

Example 32 with Scope

use of com.uber.m3.tally.Scope in project sdk-java by temporalio.

the class ReplayWorkflowTaskHandler method handleWorkflowTask.

@Override
public WorkflowTaskHandler.Result handleWorkflowTask(PollWorkflowTaskQueueResponse workflowTask) throws Exception {
    String workflowType = workflowTask.getWorkflowType().getName();
    Scope metricsScope = options.getMetricsScope().tagged(ImmutableMap.of(MetricsTag.WORKFLOW_TYPE, workflowType));
    return handleWorkflowTaskWithQuery(workflowTask.toBuilder(), metricsScope);
}
Also used : Scope(com.uber.m3.tally.Scope)

Example 33 with Scope

use of com.uber.m3.tally.Scope in project sdk-java by temporalio.

the class MetricsTest method setUp.

public void setUp(WorkerFactoryOptions workerFactoryOptions) {
    Scope metricsScope;
    reporter = new TestStatsReporter();
    metricsScope = new RootScopeBuilder().reporter(reporter).reportEvery(com.uber.m3.util.Duration.ofMillis(10));
    TestEnvironmentOptions testOptions = TestEnvironmentOptions.newBuilder().setMetricsScope(metricsScope).setWorkflowClientOptions(WorkflowClientOptions.newBuilder().setNamespace(NAMESPACE).build()).setWorkerFactoryOptions(workerFactoryOptions).build();
    testEnvironment = TestWorkflowEnvironment.newInstance(testOptions);
}
Also used : TestEnvironmentOptions(io.temporal.testing.TestEnvironmentOptions) RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) Scope(com.uber.m3.tally.Scope) TestStatsReporter(io.temporal.common.reporter.TestStatsReporter)

Example 34 with Scope

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

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

Scope (com.uber.m3.tally.Scope)35 RootScopeBuilder (com.uber.m3.tally.RootScopeBuilder)15 Test (org.junit.Test)13 ImmutableMap (com.uber.m3.util.ImmutableMap)12 StatsReporter (com.uber.m3.tally.StatsReporter)11 NoopScope (com.uber.cadence.internal.metrics.NoopScope)9 Histogram (com.uber.m3.tally.Histogram)7 Timer (com.uber.m3.tally.Timer)7 PollForDecisionTaskResponse (com.uber.cadence.PollForDecisionTaskResponse)5 WorkflowOptions (com.uber.cadence.client.WorkflowOptions)5 Counter (com.uber.m3.tally.Counter)5 Gauge (com.uber.m3.tally.Gauge)5 Stopwatch (com.uber.m3.tally.Stopwatch)5 Buckets (com.uber.m3.tally.Buckets)4 NoopScope (com.uber.m3.tally.NoopScope)4 ReplayAwareScope (com.uber.cadence.internal.metrics.ReplayAwareScope)3 ValueBuckets (com.uber.m3.tally.ValueBuckets)3 PollWorkflowTaskQueueResponse (io.temporal.api.workflowservice.v1.PollWorkflowTaskQueueResponse)3 WorkflowServiceStubs (io.temporal.serviceclient.WorkflowServiceStubs)3 HttpServer (com.sun.net.httpserver.HttpServer)2