|
| 1 | +package datadog.trace.agent.test.assertions; |
| 2 | + |
| 3 | +import static datadog.trace.agent.test.assertions.Matchers.any; |
| 4 | +import static datadog.trace.agent.test.assertions.Matchers.is; |
| 5 | +import static datadog.trace.agent.test.assertions.Matchers.isNonNull; |
| 6 | +import static datadog.trace.api.DDTags.ERROR_MSG; |
| 7 | +import static datadog.trace.api.DDTags.ERROR_STACK; |
| 8 | +import static datadog.trace.api.DDTags.ERROR_TYPE; |
| 9 | +import static datadog.trace.api.DDTags.LANGUAGE_TAG_KEY; |
| 10 | +import static datadog.trace.api.DDTags.REQUIRED_CODE_ORIGIN_TAGS; |
| 11 | +import static datadog.trace.api.DDTags.RUNTIME_ID_TAG; |
| 12 | +import static datadog.trace.api.DDTags.THREAD_ID; |
| 13 | +import static datadog.trace.api.DDTags.THREAD_NAME; |
| 14 | +import static datadog.trace.common.sampling.RateByServiceTraceSampler.SAMPLING_AGENT_RATE; |
| 15 | +import static datadog.trace.common.writer.ddagent.TraceMapper.SAMPLING_PRIORITY_KEY; |
| 16 | + |
| 17 | +import datadog.trace.api.DDTags; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +public final class TagsMatcher { |
| 22 | + final Map<String, Matcher<?>> tagMatchers; |
| 23 | + |
| 24 | + private TagsMatcher(Map<String, Matcher<?>> tagMatchers) { |
| 25 | + this.tagMatchers = tagMatchers; |
| 26 | + } |
| 27 | + |
| 28 | + public static TagsMatcher defaultTags() { |
| 29 | + Map<String, Matcher<?>> tagMatchers = new HashMap<>(); |
| 30 | + tagMatchers.put(THREAD_NAME, isNonNull()); |
| 31 | + tagMatchers.put(THREAD_ID, isNonNull()); |
| 32 | + tagMatchers.put(RUNTIME_ID_TAG, any()); |
| 33 | + tagMatchers.put(LANGUAGE_TAG_KEY, any()); |
| 34 | + tagMatchers.put(SAMPLING_AGENT_RATE, any()); |
| 35 | + tagMatchers.put(SAMPLING_PRIORITY_KEY.toString(), any()); |
| 36 | + tagMatchers.put("_sample_rate", any()); |
| 37 | + tagMatchers.put(DDTags.PID_TAG, any()); |
| 38 | + tagMatchers.put(DDTags.SCHEMA_VERSION_TAG_KEY, any()); |
| 39 | + tagMatchers.put(DDTags.PROFILING_ENABLED, any()); |
| 40 | + tagMatchers.put(DDTags.PROFILING_CONTEXT_ENGINE, any()); |
| 41 | + tagMatchers.put(DDTags.BASE_SERVICE, any()); |
| 42 | + tagMatchers.put(DDTags.DSM_ENABLED, any()); |
| 43 | + tagMatchers.put(DDTags.DJM_ENABLED, any()); |
| 44 | + tagMatchers.put(DDTags.PARENT_ID, any()); |
| 45 | + tagMatchers.put(DDTags.SPAN_LINKS, any()); // this is checked by LinksAsserter |
| 46 | + |
| 47 | + for (String tagName : REQUIRED_CODE_ORIGIN_TAGS) { |
| 48 | + tagMatchers.put(tagName, any()); |
| 49 | + } |
| 50 | + // TODO Keep porting default tag logic |
| 51 | + // TODO Dev notes: |
| 52 | + // - it seems there is way too many logic there |
| 53 | + // - need to check if its related to tracing only |
| 54 | + |
| 55 | + return new TagsMatcher(tagMatchers); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Requires the following tag to match the given matcher. |
| 60 | + * |
| 61 | + * @param tagName The tag name to match. |
| 62 | + * @param matcher The matcher to apply to the tag value. |
| 63 | + * @return A tag matcher that requires the following tag to match the given matcher. |
| 64 | + */ |
| 65 | + public static TagsMatcher tag(String tagName, Matcher<?> matcher) { |
| 66 | + Map<String, Matcher<?>> tagMatchers = new HashMap<>(); |
| 67 | + tagMatchers.put(tagName, matcher); |
| 68 | + return new TagsMatcher(tagMatchers); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Requires the following tags to be present. |
| 73 | + * |
| 74 | + * @param tagNames The tag names to match. |
| 75 | + * @return A tag matcher that requires the following tags to be present. |
| 76 | + */ |
| 77 | + public static TagsMatcher includes(String... tagNames) { |
| 78 | + Map<String, Matcher<?>> tagMatchers = new HashMap<>(); |
| 79 | + for (String tagName : tagNames) { |
| 80 | + tagMatchers.put(tagName, any()); |
| 81 | + } |
| 82 | + return new TagsMatcher(tagMatchers); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Requires the error tags to match the given error. |
| 87 | + * |
| 88 | + * @param error The error to match. |
| 89 | + * @return A tag matcher that requires the error tags to match the given error. |
| 90 | + */ |
| 91 | + public static TagsMatcher error(Throwable error) { |
| 92 | + return error(error.getClass(), error.getMessage()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Requires the error tags to match the given error type. |
| 97 | + * |
| 98 | + * @param errorType The error type to match. |
| 99 | + * @return A tag matcher that requires the error tags to match the given error type. |
| 100 | + */ |
| 101 | + public static TagsMatcher error(Class<? extends Throwable> errorType) { |
| 102 | + return error(errorType, null); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Requires the error tags to match the given error type and message. |
| 107 | + * |
| 108 | + * @param errorType The error type to match. |
| 109 | + * @param message The error message to match. |
| 110 | + * @return A tag matcher that requires the error tags to match the given error type and message. |
| 111 | + */ |
| 112 | + public static TagsMatcher error(Class<? extends Throwable> errorType, String message) { |
| 113 | + Map<String, Matcher<?>> tagMatchers = new HashMap<>(); |
| 114 | + tagMatchers.put(ERROR_TYPE, Matchers.<String>validates(s -> testErrorType(errorType, s))); |
| 115 | + tagMatchers.put(ERROR_STACK, isNonNull()); |
| 116 | + if (message != null) { |
| 117 | + tagMatchers.put(ERROR_MSG, is(message)); |
| 118 | + } |
| 119 | + return new TagsMatcher(tagMatchers); |
| 120 | + } |
| 121 | + |
| 122 | + static boolean testErrorType(Class<? extends Throwable> errorType, String actual) { |
| 123 | + if (errorType.getName().equals(actual)) { |
| 124 | + return true; |
| 125 | + } |
| 126 | + try { |
| 127 | + // also accept type names which are subclasses of the given error type |
| 128 | + return errorType.isAssignableFrom( |
| 129 | + Class.forName(actual, false, TagsMatcher.class.getClassLoader())); |
| 130 | + } catch (Throwable ignore) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments