21
21
import com .uber .cadence .common .RetryOptions ;
22
22
import com .uber .cadence .internal .common .Retryer ;
23
23
import com .uber .cadence .internal .logging .LoggerTag ;
24
+ import com .uber .cadence .internal .metrics .MetricsTag ;
24
25
import com .uber .cadence .internal .metrics .MetricsType ;
25
26
import com .uber .cadence .internal .worker .ActivityTaskHandler .Result ;
26
27
import com .uber .cadence .serviceclient .IWorkflowService ;
28
+ import com .uber .m3 .tally .Scope ;
27
29
import com .uber .m3 .tally .Stopwatch ;
28
30
import com .uber .m3 .util .Duration ;
31
+ import com .uber .m3 .util .ImmutableMap ;
29
32
import java .nio .charset .StandardCharsets ;
30
33
import java .util .Objects ;
31
34
import java .util .concurrent .CancellationException ;
@@ -157,26 +160,31 @@ private TaskHandlerImpl(ActivityTaskHandler handler) {
157
160
158
161
@ Override
159
162
public void handle (MeasurableActivityTask task ) throws Exception {
160
- options
161
- .getMetricsScope ()
163
+ Scope metricsScope =
164
+ options
165
+ .getMetricsScope ()
166
+ .tagged (
167
+ ImmutableMap .of (MetricsTag .ACTIVITY_TYPE , task .task .getActivityType ().getName ()));
168
+ metricsScope
162
169
.timer (MetricsType .TASK_LIST_QUEUE_LATENCY )
163
170
.record (
164
171
Duration .ofNanos (
165
172
task .task .getStartedTimestamp () - task .task .getScheduledTimestamp ()));
166
173
174
+ // The following tags are for logging.
167
175
MDC .put (LoggerTag .ACTIVITY_ID , task .task .getActivityId ());
168
176
MDC .put (LoggerTag .ACTIVITY_TYPE , task .task .getActivityType ().getName ());
169
177
MDC .put (LoggerTag .WORKFLOW_ID , task .task .getWorkflowExecution ().getWorkflowId ());
170
178
MDC .put (LoggerTag .RUN_ID , task .task .getWorkflowExecution ().getRunId ());
171
179
172
180
try {
173
- Stopwatch sw = options . getMetricsScope () .timer (MetricsType .ACTIVITY_EXEC_LATENCY ).start ();
181
+ Stopwatch sw = metricsScope .timer (MetricsType .ACTIVITY_EXEC_LATENCY ).start ();
174
182
ActivityTaskHandler .Result response =
175
- handler .handle (service , domain , task .task , options . getMetricsScope () );
183
+ handler .handle (service , domain , task .task , metricsScope );
176
184
sw .stop ();
177
185
178
- sw = options . getMetricsScope () .timer (MetricsType .ACTIVITY_RESP_LATENCY ).start ();
179
- sendReply (task .task , response );
186
+ sw = metricsScope .timer (MetricsType .ACTIVITY_RESP_LATENCY ).start ();
187
+ sendReply (task .task , response , metricsScope );
180
188
sw .stop ();
181
189
182
190
task .markDone ();
@@ -185,8 +193,8 @@ public void handle(MeasurableActivityTask task) throws Exception {
185
193
new RespondActivityTaskCanceledRequest ();
186
194
cancelledRequest .setDetails (
187
195
String .valueOf (e .getMessage ()).getBytes (StandardCharsets .UTF_8 ));
188
- Stopwatch sw = options . getMetricsScope () .timer (MetricsType .ACTIVITY_RESP_LATENCY ).start ();
189
- sendReply (task .task , new Result (null , null , cancelledRequest , null ));
196
+ Stopwatch sw = metricsScope .timer (MetricsType .ACTIVITY_RESP_LATENCY ).start ();
197
+ sendReply (task .task , new Result (null , null , cancelledRequest , null ), metricsScope );
190
198
sw .stop ();
191
199
} finally {
192
200
MDC .remove (LoggerTag .ACTIVITY_ID );
@@ -211,7 +219,8 @@ public Throwable wrapFailure(MeasurableActivityTask task, Throwable failure) {
211
219
failure );
212
220
}
213
221
214
- private void sendReply (PollForActivityTaskResponse task , ActivityTaskHandler .Result response )
222
+ private void sendReply (
223
+ PollForActivityTaskResponse task , ActivityTaskHandler .Result response , Scope metricsScope )
215
224
throws TException {
216
225
RetryOptions ro = response .getRequestRetryOptions ();
217
226
RespondActivityTaskCompletedRequest taskCompleted = response .getTaskCompleted ();
@@ -225,7 +234,7 @@ private void sendReply(PollForActivityTaskResponse task, ActivityTaskHandler.Res
225
234
taskCompleted .setTaskToken (task .getTaskToken ());
226
235
taskCompleted .setIdentity (options .getIdentity ());
227
236
Retryer .retry (ro , () -> service .RespondActivityTaskCompleted (taskCompleted ));
228
- options . getMetricsScope () .counter (MetricsType .ACTIVITY_TASK_COMPLETED_COUNTER ).inc (1 );
237
+ metricsScope .counter (MetricsType .ACTIVITY_TASK_COMPLETED_COUNTER ).inc (1 );
229
238
} else {
230
239
RespondActivityTaskFailedRequest taskFailed = response .getTaskFailed ();
231
240
if (taskFailed != null ) {
@@ -240,7 +249,7 @@ private void sendReply(PollForActivityTaskResponse task, ActivityTaskHandler.Res
240
249
taskFailed .setTaskToken (task .getTaskToken ());
241
250
taskFailed .setIdentity (options .getIdentity ());
242
251
Retryer .retry (ro , () -> service .RespondActivityTaskFailed (taskFailed ));
243
- options . getMetricsScope () .counter (MetricsType .ACTIVITY_TASK_FAILED_COUNTER ).inc (1 );
252
+ metricsScope .counter (MetricsType .ACTIVITY_TASK_FAILED_COUNTER ).inc (1 );
244
253
} else {
245
254
RespondActivityTaskCanceledRequest taskCancelled = response .getTaskCancelled ();
246
255
if (taskCancelled != null ) {
@@ -255,7 +264,7 @@ private void sendReply(PollForActivityTaskResponse task, ActivityTaskHandler.Res
255
264
EntityNotExistsError .class ,
256
265
DomainNotActiveError .class );
257
266
Retryer .retry (ro , () -> service .RespondActivityTaskCanceled (taskCancelled ));
258
- options . getMetricsScope () .counter (MetricsType .ACTIVITY_TASK_CANCELED_COUNTER ).inc (1 );
267
+ metricsScope .counter (MetricsType .ACTIVITY_TASK_CANCELED_COUNTER ).inc (1 );
259
268
}
260
269
}
261
270
}
0 commit comments