Skip to content

Commit 5d51fbb

Browse files
committed
add getErrorType
1 parent ef9440e commit 5d51fbb

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/semconv/rpc/RpcAttributesExtractorTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import static io.opentelemetry.instrumentation.api.incubator.semconv.rpc.RpcCommonAttributesExtractor.RPC_METHOD_ORIGINAL;
99
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
10+
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;
1011
import static org.assertj.core.api.Assertions.entry;
1112

1213
import io.opentelemetry.api.common.AttributeKey;
@@ -60,6 +61,13 @@ public String getRpcMethod(Map<String, String> request) {
6061
return service + "/" + method;
6162
}
6263

64+
@Nullable
65+
@Override
66+
public String getErrorType(
67+
Map<String, String> request, @Nullable Void response, @Nullable Throwable error) {
68+
return request.get("errorType");
69+
}
70+
6371
@Override
6472
public boolean isPredefined(Map<String, String> stringStringMap) {
6573
return predefined;
@@ -139,4 +147,63 @@ private static void testExtractor(
139147
extractor.onEnd(attributes, context, request, null, null);
140148
assertThat(attributes.build()).containsOnly(expectedArray);
141149
}
150+
151+
@Test
152+
void shouldExtractErrorType_getter() {
153+
Map<String, String> request = new HashMap<>();
154+
request.put("service", "my.Service");
155+
request.put("method", "Method");
156+
request.put("errorType", "CANCELLED");
157+
158+
AttributesExtractor<Map<String, String>, Void> extractor =
159+
RpcServerAttributesExtractor.create(new TestGetter(false));
160+
161+
Context context = Context.root();
162+
AttributesBuilder attributes = Attributes.builder();
163+
extractor.onStart(attributes, context, request);
164+
extractor.onEnd(attributes, context, request, null, null);
165+
166+
if (SemconvStability.emitStableRpcSemconv()) {
167+
assertThat(attributes.build()).containsEntry(ERROR_TYPE, "CANCELLED");
168+
}
169+
}
170+
171+
@Test
172+
void shouldExtractErrorType_exceptionClassName() {
173+
Map<String, String> request = new HashMap<>();
174+
request.put("service", "my.Service");
175+
request.put("method", "Method");
176+
177+
AttributesExtractor<Map<String, String>, Void> extractor =
178+
RpcServerAttributesExtractor.create(new TestGetter(false));
179+
180+
Context context = Context.root();
181+
AttributesBuilder attributes = Attributes.builder();
182+
extractor.onStart(attributes, context, request);
183+
extractor.onEnd(attributes, context, request, null, new IllegalArgumentException());
184+
185+
if (SemconvStability.emitStableRpcSemconv()) {
186+
assertThat(attributes.build())
187+
.containsEntry(ERROR_TYPE, "java.lang.IllegalArgumentException");
188+
}
189+
}
190+
191+
@Test
192+
void shouldNotExtractErrorType_noError() {
193+
Map<String, String> request = new HashMap<>();
194+
request.put("service", "my.Service");
195+
request.put("method", "Method");
196+
197+
AttributesExtractor<Map<String, String>, Void> extractor =
198+
RpcServerAttributesExtractor.create(new TestGetter(false));
199+
200+
Context context = Context.root();
201+
AttributesBuilder attributes = Attributes.builder();
202+
extractor.onStart(attributes, context, request);
203+
extractor.onEnd(attributes, context, request, null, null);
204+
205+
if (SemconvStability.emitStableRpcSemconv()) {
206+
assertThat(attributes.build()).doesNotContainKey(ERROR_TYPE);
207+
}
208+
}
142209
}

0 commit comments

Comments
 (0)