Skip to content

Commit 9508e88

Browse files
committed
Add logging of workflow and activity run failures
1 parent e9962f7 commit 9508e88

10 files changed

+65
-18
lines changed

temporal-opentracing/src/main/java/io/temporal/opentracing/OpenTracingOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
package io.temporal.opentracing;
2121

22-
import javax.annotation.Nonnull;
2322
import java.util.HashMap;
2423
import java.util.Map;
24+
import javax.annotation.Nonnull;
2525

2626
public class OpenTracingOptions {
2727
private final Map<SpanOperationType, String> customSpanOperationNamePrefixes;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.opentracing;
21+
22+
public class StandardLogNames {
23+
public static final String FAILURE_MESSAGE = "failureMessage";
24+
public static final String FAILURE_CAUSE = "failureCause";
25+
}

temporal-opentracing/src/main/java/io/temporal/opentracing/StandardTags.java renamed to temporal-opentracing/src/main/java/io/temporal/opentracing/StandardTagNames.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020
package io.temporal.opentracing;
2121

22-
public class StandardTags {
22+
public class StandardTagNames {
2323
public static final String WORKFLOW_ID = "workflowId";
2424
public static final String RUN_ID = "runId";
2525
public static final String PARENT_WORKFLOW_ID = "parentWorkflowId";
2626
public static final String PARENT_RUN_ID = "parentRunId";
27+
public static final String FAILED = "failed";
2728
}

temporal-opentracing/src/main/java/io/temporal/opentracing/internal/OpenTracingActivityInboundCallsInterceptor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public ActivityOutput execute(ActivityInput input) {
7171
.start();
7272
try (Scope scope = tracer.scopeManager().activate(activityRunSpan)) {
7373
return super.execute(input);
74+
} catch (Throwable t) {
75+
OpenTracingSpanUtils.logFail(activityRunSpan, t);
76+
throw t;
7477
} finally {
7578
activityRunSpan.finish();
7679
}

temporal-opentracing/src/main/java/io/temporal/opentracing/internal/OpenTracingContextAccessor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
import io.temporal.api.common.v1.Payload;
2828
import io.temporal.common.converter.DataConverter;
2929
import io.temporal.common.interceptors.Header;
30-
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
31-
32-
import javax.annotation.Nonnull;
3330
import java.lang.reflect.Type;
3431
import java.util.HashMap;
3532
import java.util.Iterator;
@@ -39,7 +36,7 @@
3936
public class OpenTracingContextAccessor {
4037
private static final String TRACER_HEADER_KEY = "_tracer-data";
4138
private static final Type HASH_MAP_STRING_STRING_TYPE =
42-
TypeToken.getParameterized(HashMap.class, String.class, String.class).getType();
39+
TypeToken.getParameterized(HashMap.class, String.class, String.class).getType();
4340

4441
public static void writeSpanContextToHeader(
4542
SpanContext spanContext, Header header, Tracer tracer) {
@@ -61,7 +58,8 @@ public static SpanContext readSpanContextFromHeader(Header header, Tracer tracer
6158
return null;
6259
}
6360
Map<String, String> serializedSpanContext =
64-
DataConverter.getDefaultInstance().fromPayload(payload, HashMap.class, HASH_MAP_STRING_STRING_TYPE);
61+
DataConverter.getDefaultInstance()
62+
.fromPayload(payload, HashMap.class, HASH_MAP_STRING_STRING_TYPE);
6563
return deserializeSpanContextFromMap(serializedSpanContext, tracer);
6664
}
6765

temporal-opentracing/src/main/java/io/temporal/opentracing/internal/OpenTracingSpanUtils.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
import io.opentracing.Span;
2525
import io.opentracing.SpanContext;
2626
import io.opentracing.Tracer;
27-
import io.temporal.opentracing.StandardTags;
27+
import io.temporal.opentracing.StandardLogNames;
28+
import io.temporal.opentracing.StandardTagNames;
2829
import java.util.HashMap;
2930
import java.util.Map;
3031
import java.util.concurrent.TimeUnit;
@@ -36,7 +37,7 @@ public static Tracer.SpanBuilder createWorkflowStartSpan(
3637
Map<String, String> tags =
3738
new HashMap<String, String>() {
3839
{
39-
put(StandardTags.WORKFLOW_ID, workflowId);
40+
put(StandardTagNames.WORKFLOW_ID, workflowId);
4041
}
4142
};
4243
return createSpan(tracer, startTimeMs, operationName, tags, null, References.FOLLOWS_FROM);
@@ -52,9 +53,9 @@ public static Tracer.SpanBuilder createChildWorkflowStartSpan(
5253
Map<String, String> tags =
5354
new HashMap<String, String>() {
5455
{
55-
put(StandardTags.WORKFLOW_ID, workflowId);
56-
put(StandardTags.PARENT_WORKFLOW_ID, parentWorkflowId);
57-
put(StandardTags.PARENT_RUN_ID, parentRunId);
56+
put(StandardTagNames.WORKFLOW_ID, workflowId);
57+
put(StandardTagNames.PARENT_WORKFLOW_ID, parentWorkflowId);
58+
put(StandardTagNames.PARENT_RUN_ID, parentRunId);
5859
}
5960
};
6061
return createSpan(tracer, startTimeMs, operationName, tags, null, References.FOLLOWS_FROM);
@@ -70,8 +71,8 @@ public static Tracer.SpanBuilder createWorkflowRunSpan(
7071
Map<String, String> tags =
7172
new HashMap<String, String>() {
7273
{
73-
put(StandardTags.WORKFLOW_ID, workflowId);
74-
put(StandardTags.RUN_ID, runId);
74+
put(StandardTagNames.WORKFLOW_ID, workflowId);
75+
put(StandardTagNames.RUN_ID, runId);
7576
}
7677
};
7778
return createSpan(
@@ -88,8 +89,8 @@ public static Tracer.SpanBuilder createActivityStartSpan(
8889
Map<String, String> tags =
8990
new HashMap<String, String>() {
9091
{
91-
put(StandardTags.WORKFLOW_ID, workflowId);
92-
put(StandardTags.RUN_ID, runId);
92+
put(StandardTagNames.WORKFLOW_ID, workflowId);
93+
put(StandardTagNames.RUN_ID, runId);
9394
}
9495
};
9596
return createSpan(tracer, startTimeMs, operationName, tags, null, References.CHILD_OF);
@@ -105,14 +106,27 @@ public static Tracer.SpanBuilder createActivityRunSpan(
105106
Map<String, String> tags =
106107
new HashMap<String, String>() {
107108
{
108-
put(StandardTags.WORKFLOW_ID, workflowId);
109-
put(StandardTags.RUN_ID, runId);
109+
put(StandardTagNames.WORKFLOW_ID, workflowId);
110+
put(StandardTagNames.RUN_ID, runId);
110111
}
111112
};
112113
return createSpan(
113114
tracer, startTimeMs, operationName, tags, activityStartSpanContext, References.CHILD_OF);
114115
}
115116

117+
public static void logFail(Span toSpan, Throwable failReason) {
118+
toSpan.setTag(StandardTagNames.FAILED, true);
119+
Map<String, Object> logPayload =
120+
new HashMap<String, Object>() {
121+
{
122+
put(StandardLogNames.FAILURE_MESSAGE, failReason.getMessage());
123+
put(StandardLogNames.FAILURE_CAUSE, failReason);
124+
}
125+
};
126+
127+
toSpan.log(System.currentTimeMillis(), logPayload);
128+
}
129+
116130
private static Tracer.SpanBuilder createSpan(
117131
Tracer tracer,
118132
long startTimeMs,

temporal-opentracing/src/main/java/io/temporal/opentracing/internal/OpenTracingWorkflowInboundCallsInterceptor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public WorkflowOutput execute(WorkflowInput input) {
6464
.start();
6565
try (Scope scope = tracer.scopeManager().activate(workflowRunSpan)) {
6666
return super.execute(input);
67+
} catch (Throwable t) {
68+
OpenTracingSpanUtils.logFail(workflowRunSpan, t);
69+
throw t;
6770
} finally {
6871
workflowRunSpan.finish();
6972
}

temporal-opentracing/src/test/java/io/temporal/opentracing/OpenTracingActivityFailureTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public void testActivityFailureSpanStructure() {
165165
MockSpan activityFailRunSpan = activityRunSpans.get(0);
166166
assertEquals(activityStartSpan.context().spanId(), activityFailRunSpan.parentId());
167167
assertEquals("RunActivity-Activity1", activityFailRunSpan.operationName());
168+
assertEquals(true, activityFailRunSpan.tags().get(StandardTagNames.FAILED));
168169

169170
MockSpan activitySuccessfulRunSpan = activityRunSpans.get(1);
170171
assertEquals(activityStartSpan.context().spanId(), activitySuccessfulRunSpan.parentId());

temporal-opentracing/src/test/java/io/temporal/opentracing/OpenTracingWorkflowReplayTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ public void testWorkflowReplaySpanStructure() {
186186
MockSpan workflowFirstRunSpan = workflowRunSpans.get(0);
187187
assertEquals(workflowStartSpan.context().spanId(), workflowFirstRunSpan.parentId());
188188
assertEquals("RunWorkflow-TestWorkflow", workflowFirstRunSpan.operationName());
189+
assertEquals(true, workflowFirstRunSpan.tags().get(StandardTagNames.FAILED));
189190

190191
List<MockSpan> workflowFirstRunChildren = spansHelper.getByParentSpan(workflowFirstRunSpan);
191192
assertEquals(1, workflowFirstRunChildren.size());

temporal-opentracing/src/test/java/io/temporal/opentracing/OpenTracingWorkflowRetryTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public void testWorkflowReplaySpanStructure() {
187187
MockSpan workflowFirstRunSpan = workflowRunSpans.get(0);
188188
assertEquals(workflowStartSpan.context().spanId(), workflowFirstRunSpan.parentId());
189189
assertEquals("RunWorkflow-TestWorkflow", workflowFirstRunSpan.operationName());
190+
assertEquals(true, workflowFirstRunSpan.tags().get(StandardTagNames.FAILED));
190191

191192
List<MockSpan> workflowFirstRunChildren = spansHelper.getByParentSpan(workflowFirstRunSpan);
192193
assertEquals(1, workflowFirstRunChildren.size());

0 commit comments

Comments
 (0)