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());
}
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);
}
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);
}
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.
}
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");
}
Aggregations