Skip to content

Commit 08e6e0c

Browse files
committed
tests
1 parent d3f9dbe commit 08e6e0c

File tree

3 files changed

+123
-11
lines changed

3 files changed

+123
-11
lines changed

api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributeType.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ public enum ExtendedAttributeType {
3131
@Deprecated
3232
EXTENDED_ATTRIBUTES,
3333
/**
34-
* Simple attributes ({@link AttributeType#STRING}, {@link AttributeType#LONG}, {@link
35-
* AttributeType#DOUBLE}, {@link AttributeType#BOOLEAN}, {@link AttributeType#STRING_ARRAY},
36-
* {@link AttributeType#LONG_ARRAY}, {@link AttributeType#DOUBLE_ARRAY}, {@link
37-
* AttributeType#BOOLEAN_ARRAY}) SHOULD be used whenever possible. Instrumentations SHOULD assume
38-
* that backends do not index individual properties of complex attributes, that querying or
39-
* aggregating on such properties is inefficient and complicated, and that reporting complex
40-
* attributes carries higher performance overhead.
34+
* Simple attributes ({@link ExtendedAttributeType#STRING}, {@link ExtendedAttributeType#LONG},
35+
* {@link ExtendedAttributeType#DOUBLE}, {@link ExtendedAttributeType#BOOLEAN}, {@link
36+
* ExtendedAttributeType#STRING_ARRAY}, {@link ExtendedAttributeType#LONG_ARRAY}, {@link
37+
* ExtendedAttributeType#DOUBLE_ARRAY}, {@link ExtendedAttributeType#BOOLEAN_ARRAY}) SHOULD be
38+
* used whenever possible. Instrumentations SHOULD assume that backends do not index individual
39+
* properties of complex attributes, that querying or aggregating on such properties is
40+
* inefficient and complicated, and that reporting complex attributes carries higher performance
41+
* overhead.
4142
*/
4243
VALUE;
4344
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributes.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ default <T> T get(AttributeKey<T> key) {
6363
/**
6464
* Returns the value for the given {@link ExtendedAttributeKey}, or {@code null} if not found.
6565
*
66-
* <p>Note: this method will automatically return the corresponding {@link Value} instance when
67-
* passed a key of type {@link ExtendedAttributeType#VALUE} and a simple attribute is found. This
68-
* is the inverse of {@link AttributesBuilder#put(ExtendedAttributeKey, Object)} when the key is
69-
* {@link ExtendedAttributeType#VALUE}.
66+
* <p>Note: this method will automatically return the corresponding {@link
67+
* io.opentelemetry.api.common.Value} instance when passed a key of type {@link
68+
* ExtendedAttributeType#VALUE} and a simple attribute is found. This is the inverse of {@link
69+
* ExtendedAttributesBuilder#put(ExtendedAttributeKey, Object)} when the key is {@link
70+
* ExtendedAttributeType#VALUE}.
7071
*
7172
* <ul>
7273
* <li>If {@code put(ExtendedAttributeKey.stringKey("key"), "a")} was called, then {@code

api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributesTest.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.stringKey;
1616
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.valueKey;
1717
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.assertj.core.api.Assertions.entry;
1819

1920
import com.google.common.collect.ImmutableMap;
2021
import io.opentelemetry.api.common.AttributeKey;
@@ -390,66 +391,175 @@ private static ExtendedAttributeType getType(Object value) {
390391

391392
@Test
392393
void complexValueStoredAsString() {
394+
// When putting a VALUE attribute with a string Value, it should be stored as STRING type
393395
ExtendedAttributes attributes =
394396
ExtendedAttributes.builder().put(valueKey("key"), Value.of("test")).build();
397+
398+
// Should be stored as STRING type internally
395399
assertThat(attributes.get(stringKey("key"))).isEqualTo("test");
400+
assertThat(attributes.get(valueKey("key"))).isEqualTo(Value.of("test"));
401+
402+
// forEach should show STRING type
403+
Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>();
404+
attributes.forEach(entriesSeen::put);
405+
assertThat(entriesSeen).containsExactly(entry(stringKey("key"), "test"));
406+
407+
// asMap should show STRING type
408+
assertThat(attributes.asMap()).containsExactly(entry(stringKey("key"), "test"));
396409
}
397410

398411
@Test
399412
void complexValueStoredAsLong() {
413+
// When putting a VALUE attribute with a long Value, it should be stored as LONG type
400414
ExtendedAttributes attributes =
401415
ExtendedAttributes.builder().put(valueKey("key"), Value.of(123L)).build();
416+
417+
// Should be stored as LONG type internally
402418
assertThat(attributes.get(longKey("key"))).isEqualTo(123L);
419+
assertThat(attributes.get(valueKey("key"))).isEqualTo(Value.of(123L));
420+
421+
// forEach should show LONG type
422+
Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>();
423+
attributes.forEach(entriesSeen::put);
424+
assertThat(entriesSeen).containsExactly(entry(longKey("key"), 123L));
425+
426+
// asMap should show LONG type
427+
assertThat(attributes.asMap()).containsExactly(entry(longKey("key"), 123L));
403428
}
404429

405430
@Test
406431
void complexValueStoredAsDouble() {
432+
// When putting a VALUE attribute with a double Value, it should be stored as DOUBLE type
407433
ExtendedAttributes attributes =
408434
ExtendedAttributes.builder().put(valueKey("key"), Value.of(1.23)).build();
435+
436+
// Should be stored as DOUBLE type internally
409437
assertThat(attributes.get(doubleKey("key"))).isEqualTo(1.23);
438+
assertThat(attributes.get(valueKey("key"))).isEqualTo(Value.of(1.23));
439+
440+
// forEach should show DOUBLE type
441+
Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>();
442+
attributes.forEach(entriesSeen::put);
443+
assertThat(entriesSeen).containsExactly(entry(doubleKey("key"), 1.23));
444+
445+
// asMap should show DOUBLE type
446+
assertThat(attributes.asMap()).containsExactly(entry(doubleKey("key"), 1.23));
410447
}
411448

412449
@Test
413450
void complexValueStoredAsBoolean() {
451+
// When putting a VALUE attribute with a boolean Value, it should be stored as BOOLEAN type
414452
ExtendedAttributes attributes =
415453
ExtendedAttributes.builder().put(valueKey("key"), Value.of(true)).build();
454+
455+
// Should be stored as BOOLEAN type internally
416456
assertThat(attributes.get(booleanKey("key"))).isEqualTo(true);
457+
assertThat(attributes.get(valueKey("key"))).isEqualTo(Value.of(true));
458+
459+
// forEach should show BOOLEAN type
460+
Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>();
461+
attributes.forEach(entriesSeen::put);
462+
assertThat(entriesSeen).containsExactly(entry(booleanKey("key"), true));
463+
464+
// asMap should show BOOLEAN type
465+
assertThat(attributes.asMap()).containsExactly(entry(booleanKey("key"), true));
417466
}
418467

419468
@Test
420469
void complexValueStoredAsStringArray() {
470+
// When putting a VALUE attribute with a homogeneous string array, it should be stored as
471+
// STRING_ARRAY type
421472
ExtendedAttributes attributes =
422473
ExtendedAttributes.builder()
423474
.put(valueKey("key"), Value.of(Arrays.asList(Value.of("a"), Value.of("b"))))
424475
.build();
476+
477+
// Should be stored as STRING_ARRAY type internally
425478
assertThat(attributes.get(stringArrayKey("key"))).containsExactly("a", "b");
479+
assertThat(attributes.get(valueKey("key")))
480+
.isEqualTo(Value.of(Arrays.asList(Value.of("a"), Value.of("b"))));
481+
482+
// forEach should show STRING_ARRAY type
483+
Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>();
484+
attributes.forEach(entriesSeen::put);
485+
assertThat(entriesSeen).containsExactly(entry(stringArrayKey("key"), Arrays.asList("a", "b")));
486+
487+
// asMap should show STRING_ARRAY type
488+
assertThat(attributes.asMap())
489+
.containsExactly(entry(stringArrayKey("key"), Arrays.asList("a", "b")));
426490
}
427491

428492
@Test
429493
void complexValueStoredAsLongArray() {
494+
// When putting a VALUE attribute with a homogeneous long array, it should be stored as
495+
// LONG_ARRAY type
430496
ExtendedAttributes attributes =
431497
ExtendedAttributes.builder()
432498
.put(valueKey("key"), Value.of(Arrays.asList(Value.of(1L), Value.of(2L))))
433499
.build();
500+
501+
// Should be stored as LONG_ARRAY type internally
434502
assertThat(attributes.get(longArrayKey("key"))).containsExactly(1L, 2L);
503+
assertThat(attributes.get(valueKey("key")))
504+
.isEqualTo(Value.of(Arrays.asList(Value.of(1L), Value.of(2L))));
505+
506+
// forEach should show LONG_ARRAY type
507+
Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>();
508+
attributes.forEach(entriesSeen::put);
509+
assertThat(entriesSeen).containsExactly(entry(longArrayKey("key"), Arrays.asList(1L, 2L)));
510+
511+
// asMap should show LONG_ARRAY type
512+
assertThat(attributes.asMap())
513+
.containsExactly(entry(longArrayKey("key"), Arrays.asList(1L, 2L)));
435514
}
436515

437516
@Test
438517
void complexValueStoredAsDoubleArray() {
518+
// When putting a VALUE attribute with a homogeneous double array, it should be stored as
519+
// DOUBLE_ARRAY type
439520
ExtendedAttributes attributes =
440521
ExtendedAttributes.builder()
441522
.put(valueKey("key"), Value.of(Arrays.asList(Value.of(1.1), Value.of(2.2))))
442523
.build();
524+
525+
// Should be stored as DOUBLE_ARRAY type internally
443526
assertThat(attributes.get(doubleArrayKey("key"))).containsExactly(1.1, 2.2);
527+
assertThat(attributes.get(valueKey("key")))
528+
.isEqualTo(Value.of(Arrays.asList(Value.of(1.1), Value.of(2.2))));
529+
530+
// forEach should show DOUBLE_ARRAY type
531+
Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>();
532+
attributes.forEach(entriesSeen::put);
533+
assertThat(entriesSeen).containsExactly(entry(doubleArrayKey("key"), Arrays.asList(1.1, 2.2)));
534+
535+
// asMap should show DOUBLE_ARRAY type
536+
assertThat(attributes.asMap())
537+
.containsExactly(entry(doubleArrayKey("key"), Arrays.asList(1.1, 2.2)));
444538
}
445539

446540
@Test
447541
void complexValueStoredAsBooleanArray() {
542+
// When putting a VALUE attribute with a homogeneous boolean array, it should be stored as
543+
// BOOLEAN_ARRAY type
448544
ExtendedAttributes attributes =
449545
ExtendedAttributes.builder()
450546
.put(valueKey("key"), Value.of(Arrays.asList(Value.of(true), Value.of(false))))
451547
.build();
548+
549+
// Should be stored as BOOLEAN_ARRAY type internally
452550
assertThat(attributes.get(booleanArrayKey("key"))).containsExactly(true, false);
551+
assertThat(attributes.get(valueKey("key")))
552+
.isEqualTo(Value.of(Arrays.asList(Value.of(true), Value.of(false))));
553+
554+
// forEach should show BOOLEAN_ARRAY type
555+
Map<ExtendedAttributeKey<?>, Object> entriesSeen = new HashMap<>();
556+
attributes.forEach(entriesSeen::put);
557+
assertThat(entriesSeen)
558+
.containsExactly(entry(booleanArrayKey("key"), Arrays.asList(true, false)));
559+
560+
// asMap should show BOOLEAN_ARRAY type
561+
assertThat(attributes.asMap())
562+
.containsExactly(entry(booleanArrayKey("key"), Arrays.asList(true, false)));
453563
}
454564

455565
@Test

0 commit comments

Comments
 (0)