|
7 | 7 |
|
8 | 8 | import static io.opentelemetry.instrumentation.api.incubator.semconv.rpc.RpcCommonAttributesExtractor.RPC_METHOD_ORIGINAL; |
9 | 9 | import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; |
| 10 | +import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE; |
10 | 11 | import static org.assertj.core.api.Assertions.entry; |
11 | 12 |
|
12 | 13 | import io.opentelemetry.api.common.AttributeKey; |
@@ -60,6 +61,13 @@ public String getRpcMethod(Map<String, String> request) { |
60 | 61 | return service + "/" + method; |
61 | 62 | } |
62 | 63 |
|
| 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 | + |
63 | 71 | @Override |
64 | 72 | public boolean isPredefined(Map<String, String> stringStringMap) { |
65 | 73 | return predefined; |
@@ -139,4 +147,63 @@ private static void testExtractor( |
139 | 147 | extractor.onEnd(attributes, context, request, null, null); |
140 | 148 | assertThat(attributes.build()).containsOnly(expectedArray); |
141 | 149 | } |
| 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 | + } |
142 | 209 | } |
0 commit comments