|
| 1 | +package datadog.trace.bootstrap.otel.common.export; |
| 2 | + |
| 3 | +import static datadog.trace.api.config.GeneralConfig.ENV; |
| 4 | +import static datadog.trace.api.config.GeneralConfig.SERVICE_NAME; |
| 5 | +import static datadog.trace.api.config.GeneralConfig.TAGS; |
| 6 | +import static datadog.trace.api.config.GeneralConfig.VERSION; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 8 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 9 | + |
| 10 | +import com.google.protobuf.CodedInputStream; |
| 11 | +import com.google.protobuf.WireFormat; |
| 12 | +import datadog.trace.api.Config; |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.LinkedHashMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Properties; |
| 17 | +import java.util.stream.Stream; |
| 18 | +import org.junit.jupiter.params.ParameterizedTest; |
| 19 | +import org.junit.jupiter.params.provider.Arguments; |
| 20 | +import org.junit.jupiter.params.provider.MethodSource; |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests for {@link OtlpResourceProto#buildResourceMessage}. |
| 24 | + * |
| 25 | + * <p>Each test creates a {@link Config} from a {@link Properties} instance, calls {@link |
| 26 | + * OtlpResourceProto#buildResourceMessage}, then extracts the byte array and verifies its content |
| 27 | + * against the OpenTelemetry protobuf encoding defined in {@code |
| 28 | + * opentelemetry/proto/resource/v1/resource.proto}. |
| 29 | + * |
| 30 | + * <p>Relevant proto field numbers: |
| 31 | + * |
| 32 | + * <pre> |
| 33 | + * Resource { repeated KeyValue attributes = 1; } |
| 34 | + * KeyValue { string key = 1; AnyValue value = 2; } |
| 35 | + * AnyValue { string string_value = 1; } |
| 36 | + * </pre> |
| 37 | + */ |
| 38 | +class OtlpResourceProtoTest { |
| 39 | + |
| 40 | + // ── test data ───────────────────────────────────────────────────────────── |
| 41 | + |
| 42 | + private static Properties props(String... keyValues) { |
| 43 | + Properties props = new Properties(); |
| 44 | + for (int i = 0; i < keyValues.length; i += 2) { |
| 45 | + props.setProperty(keyValues[i], keyValues[i + 1]); |
| 46 | + } |
| 47 | + return props; |
| 48 | + } |
| 49 | + |
| 50 | + private static Map<String, String> attrs(String... keyValues) { |
| 51 | + Map<String, String> map = new LinkedHashMap<>(); |
| 52 | + for (int i = 0; i < keyValues.length; i += 2) { |
| 53 | + map.put(keyValues[i], keyValues[i + 1]); |
| 54 | + } |
| 55 | + return map; |
| 56 | + } |
| 57 | + |
| 58 | + static Stream<Arguments> resourceMessageCases() { |
| 59 | + return Stream.of( |
| 60 | + // service not set: should use the auto-detected name |
| 61 | + Arguments.of( |
| 62 | + "service not set, no env, no version, no tags", |
| 63 | + props(), |
| 64 | + attrs("service.name", Config.get().getServiceName())), |
| 65 | + // custom service name |
| 66 | + Arguments.of( |
| 67 | + "custom service name, no env, no version, no tags", |
| 68 | + props(SERVICE_NAME, "my-service"), |
| 69 | + attrs("service.name", "my-service")), |
| 70 | + // env set to empty string: no deployment.environment.name written; |
| 71 | + Arguments.of( |
| 72 | + "env set to empty string", |
| 73 | + props(SERVICE_NAME, "my-service", ENV, ""), |
| 74 | + attrs("service.name", "my-service")), |
| 75 | + // env set to non-empty value: deployment.environment.name written; |
| 76 | + Arguments.of( |
| 77 | + "env set to non-empty value", |
| 78 | + props(SERVICE_NAME, "my-service", ENV, "prod"), |
| 79 | + attrs("service.name", "my-service", "deployment.environment.name", "prod")), |
| 80 | + // version set to empty string: no service.version written; |
| 81 | + Arguments.of( |
| 82 | + "version set to empty string", |
| 83 | + props(SERVICE_NAME, "my-service", VERSION, ""), |
| 84 | + attrs("service.name", "my-service")), |
| 85 | + // version set to non-empty value: service.version written; |
| 86 | + Arguments.of( |
| 87 | + "version set to non-empty value", |
| 88 | + props(SERVICE_NAME, "my-service", VERSION, "1.0.0"), |
| 89 | + attrs("service.name", "my-service", "service.version", "1.0.0")), |
| 90 | + // tags as comma-separated key:value pairs (no env or version) |
| 91 | + Arguments.of( |
| 92 | + "tags as comma-separated key:value pairs", |
| 93 | + props(SERVICE_NAME, "my-service", TAGS, "region:us-east,team:platform"), |
| 94 | + attrs( |
| 95 | + "service.name", "my-service", |
| 96 | + "region", "us-east", |
| 97 | + "team", "platform")), |
| 98 | + // all config values set together |
| 99 | + Arguments.of( |
| 100 | + "service, env, version, and tags all set", |
| 101 | + props( |
| 102 | + SERVICE_NAME, |
| 103 | + "my-service", |
| 104 | + ENV, |
| 105 | + "staging", |
| 106 | + VERSION, |
| 107 | + "2.0.0", |
| 108 | + TAGS, |
| 109 | + "region:eu-west," |
| 110 | + + "service:ignored-service," |
| 111 | + + "env:ignored-env," |
| 112 | + + "version:ignored-version," |
| 113 | + + "SERVICE:ignored-service," |
| 114 | + + "ENV:ignored-env," |
| 115 | + + "VERSION:ignored-version," |
| 116 | + + "service.name:ignored-service," |
| 117 | + + "deployment.environment.name:ignored-env," |
| 118 | + + "service.version:ignored-version," |
| 119 | + + "SERVICE.NAME:ignored-service," |
| 120 | + + "DEPLOYMENT.ENVIRONMENT.NAME:ignored-env," |
| 121 | + + "SERVICE.VERSION:ignored-version"), |
| 122 | + attrs( |
| 123 | + "service.name", "my-service", |
| 124 | + "deployment.environment.name", "staging", |
| 125 | + "service.version", "2.0.0", |
| 126 | + "region", "eu-west"))); |
| 127 | + } |
| 128 | + |
| 129 | + // ── test ───────────────────────────────────────────────────────────────── |
| 130 | + |
| 131 | + @ParameterizedTest(name = "{0}") |
| 132 | + @MethodSource("resourceMessageCases") |
| 133 | + void testBuildResourceMessage( |
| 134 | + String caseName, Properties properties, Map<String, String> expectedAttributes) |
| 135 | + throws IOException { |
| 136 | + Config config = Config.get(properties); |
| 137 | + byte[] bytes = OtlpResourceProto.buildResourceMessage(config); |
| 138 | + |
| 139 | + Map<String, String> actualAttributes = parseResourceAttributes(bytes); |
| 140 | + assertEquals(expectedAttributes, actualAttributes, "For case: " + caseName); |
| 141 | + } |
| 142 | + |
| 143 | + // ── parsing helpers ─────────────────────────────────────────────────────── |
| 144 | + |
| 145 | + /** |
| 146 | + * Parses the resource message bytes into an attribute map while validating the protobuf wire |
| 147 | + * format (field numbers and wire types) of every field read. |
| 148 | + * |
| 149 | + * <p>{@code buildResourceMessage} returns a length-prefixed message with an outer tag (field 1, |
| 150 | + * LEN wire type) followed by the Resource body size and body. Read the outer tag, then iterate |
| 151 | + * over all {@code Resource.attributes} (field 1, LEN wire type). Each attribute is a {@code |
| 152 | + * KeyValue} whose {@code value} is an {@code AnyValue} containing a {@code string_value}. |
| 153 | + */ |
| 154 | + private static Map<String, String> parseResourceAttributes(byte[] bytes) throws IOException { |
| 155 | + // Read the outer tag (field 1, LEN wire type) that wraps the Resource body |
| 156 | + CodedInputStream outer = CodedInputStream.newInstance(bytes); |
| 157 | + int outerTag = outer.readTag(); |
| 158 | + assertEquals(1, WireFormat.getTagFieldNumber(outerTag), "outer field is Resource (field 1)"); |
| 159 | + assertEquals(WireFormat.WIRETYPE_LENGTH_DELIMITED, WireFormat.getTagWireType(outerTag)); |
| 160 | + CodedInputStream resource = outer.readBytes().newCodedInput(); |
| 161 | + |
| 162 | + Map<String, String> attributes = new LinkedHashMap<>(); |
| 163 | + while (!resource.isAtEnd()) { |
| 164 | + // Each attribute is Resource.attributes (field 1, LEN wire type) |
| 165 | + int tag = resource.readTag(); |
| 166 | + assertEquals(1, WireFormat.getTagFieldNumber(tag), "Resource.attributes is field 1"); |
| 167 | + assertEquals(WireFormat.WIRETYPE_LENGTH_DELIMITED, WireFormat.getTagWireType(tag)); |
| 168 | + |
| 169 | + // Read the full KeyValue body |
| 170 | + CodedInputStream kv = resource.readBytes().newCodedInput(); |
| 171 | + |
| 172 | + String key = readKeyField(kv); |
| 173 | + CodedInputStream av = readAnyValueField(kv); |
| 174 | + |
| 175 | + // Read AnyValue.string_value (field 1, LEN) |
| 176 | + int avTag = av.readTag(); |
| 177 | + assertEquals(1, WireFormat.getTagFieldNumber(avTag), "AnyValue.string_value is field 1"); |
| 178 | + assertEquals(WireFormat.WIRETYPE_LENGTH_DELIMITED, WireFormat.getTagWireType(avTag)); |
| 179 | + String value = av.readString(); |
| 180 | + assertTrue(av.isAtEnd(), "no extra fields in AnyValue"); |
| 181 | + assertTrue(kv.isAtEnd(), "no extra fields in KeyValue"); |
| 182 | + |
| 183 | + attributes.put(key, value); |
| 184 | + } |
| 185 | + return attributes; |
| 186 | + } |
| 187 | + |
| 188 | + /** Reads the {@code KeyValue.key} field (field 1, LEN) and returns the string value. */ |
| 189 | + private static String readKeyField(CodedInputStream kv) throws IOException { |
| 190 | + int tag = kv.readTag(); |
| 191 | + assertEquals(1, WireFormat.getTagFieldNumber(tag), "KeyValue.key is field 1"); |
| 192 | + assertEquals(WireFormat.WIRETYPE_LENGTH_DELIMITED, WireFormat.getTagWireType(tag)); |
| 193 | + return kv.readString(); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Reads the {@code KeyValue.value} field (field 2, LEN) and returns a stream over the {@code |
| 198 | + * AnyValue} body. |
| 199 | + */ |
| 200 | + private static CodedInputStream readAnyValueField(CodedInputStream kv) throws IOException { |
| 201 | + int tag = kv.readTag(); |
| 202 | + assertEquals(2, WireFormat.getTagFieldNumber(tag), "KeyValue.value is field 2"); |
| 203 | + assertEquals(WireFormat.WIRETYPE_LENGTH_DELIMITED, WireFormat.getTagWireType(tag)); |
| 204 | + return kv.readBytes().newCodedInput(); |
| 205 | + } |
| 206 | +} |
0 commit comments