From 53d396e86a3af14713e9ecfb05426c6696b71d7e Mon Sep 17 00:00:00 2001 From: jwatson Date: Wed, 2 Sep 2020 16:35:45 -0700 Subject: [PATCH 01/20] Add a generic type for the key on the ImmutableKeyValuePairs. This necessitated changing the `get` to `getValue` to avoid clashing with the java.util.Map interface. Also introduced convenience sub-interfaces for consuming labels and attributes, so consumers don't have to worry about the types if they don't want to. --- .../io/opentelemetry/common/Attributes.java | 5 ++ .../exporters/zipkin/ZipkinSpanExporter.java | 7 +- .../opentracingshim/testbed/TestUtils.java | 2 +- .../nestedcallbacks/NestedCallbacksTest.java | 2 +- .../sdk/resources/ResourceProvidersTest.java | 2 +- .../sdk/resources/ResourceTest.java | 6 +- .../sdk/trace/AttributesMap.java | 2 +- .../trace/RecordEventsReadableSpanTest.java | 51 +++++++------- .../sdk/trace/SpanBuilderSdkTest.java | 66 +++++++++---------- .../extensions/resources/OsResourceTest.java | 4 +- .../resources/ProcessResourceTest.java | 6 +- .../extensions/trace/testbed/TestUtils.java | 2 +- .../nestedcallbacks/NestedCallbacksTest.java | 2 +- 13 files changed, 82 insertions(+), 75 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/common/Attributes.java b/api/src/main/java/io/opentelemetry/common/Attributes.java index a179358a1de..0d258895d77 100644 --- a/api/src/main/java/io/opentelemetry/common/Attributes.java +++ b/api/src/main/java/io/opentelemetry/common/Attributes.java @@ -50,6 +50,11 @@ abstract static class ArrayBackedAttributes extends Attributes { public Builder toBuilder() { return new Builder(new ArrayList<>(data())); } + + @Override + public AttributeValue getValue(String key) { + return get(key); + } } /** Returns a {@link Attributes} instance with no attributes. */ diff --git a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java index 37ab7f0aaca..3b02879b43e 100644 --- a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java +++ b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java @@ -156,14 +156,14 @@ public void consume(String key, AttributeValue value) { }); Status status = spanData.getStatus(); // for GRPC spans, include status code & description. - if (status != null && spanAttributes.get(SemanticAttributes.RPC_SERVICE.key()) != null) { + if (status != null && spanAttributes.getValue(SemanticAttributes.RPC_SERVICE.key()) != null) { spanBuilder.putTag(GRPC_STATUS_CODE, status.getCanonicalCode().toString()); if (status.getDescription() != null) { spanBuilder.putTag(GRPC_STATUS_DESCRIPTION, status.getDescription()); } } // add the error tag, if it isn't already in the source span. - if (status != null && !status.isOk() && spanAttributes.get(STATUS_ERROR) == null) { + if (status != null && !status.isOk() && spanAttributes.getValue(STATUS_ERROR) == null) { spanBuilder.putTag(STATUS_ERROR, status.getCanonicalCode().toString()); } @@ -189,7 +189,8 @@ private static Endpoint chooseEndpoint(SpanData spanData, Endpoint localEndpoint ReadableAttributes resourceAttributes = spanData.getResource().getAttributes(); // use the service.name from the Resource, if it's been set. - AttributeValue serviceNameValue = resourceAttributes.get(ResourceAttributes.SERVICE_NAME.key()); + AttributeValue serviceNameValue = + resourceAttributes.getValue(ResourceAttributes.SERVICE_NAME.key()); if (serviceNameValue == null) { return localEndpoint; } diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java index 241448c829b..9fe84653b35 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java @@ -45,7 +45,7 @@ public static List getByAttr( return getByCondition( spans, spanData -> { - AttributeValue attrValue = spanData.getAttributes().get(key); + AttributeValue attrValue = spanData.getAttributes().getValue(key); if (attrValue == null) { return false; } diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java index bf020549902..5b74a30c424 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -64,7 +64,7 @@ void test() { assertEquals(3, attrs.size()); for (int i = 1; i <= 3; i++) { assertEquals( - Integer.toString(i), spans.get(0).getAttributes().get("key" + i).getStringValue()); + Integer.toString(i), spans.get(0).getAttributes().getValue("key" + i).getStringValue()); } assertNull(tracer.scopeManager().activeSpan()); diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java index fe95e21dc38..22836d0ee41 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java @@ -27,7 +27,7 @@ class ResourceProvidersTest { void default_resource_includes_attributes_from_providers() { Resource resource = Resource.getDefault(); - AttributeValue providerAttribute = resource.getAttributes().get("providerAttribute"); + AttributeValue providerAttribute = resource.getAttributes().getValue("providerAttribute"); assertThat(providerAttribute).isNotNull(); assertThat(providerAttribute.getLongValue()).isEqualTo(42); } diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java index 825784de60f..01018fadae1 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java @@ -242,10 +242,10 @@ void testMergeResources_Resource2_Null() { void testSdkTelemetryResources() { Resource resource = Resource.getTelemetrySdk(); ReadableAttributes attributes = resource.getAttributes(); - assertThat(attributes.get("telemetry.sdk.name")) + assertThat(attributes.getValue("telemetry.sdk.name")) .isEqualTo(AttributeValue.stringAttributeValue("opentelemetry")); - assertThat(attributes.get("telemetry.sdk.language")) + assertThat(attributes.getValue("telemetry.sdk.language")) .isEqualTo(AttributeValue.stringAttributeValue("java")); - assertThat(attributes.get("telemetry.sdk.version").getStringValue()).isNotNull(); + assertThat(attributes.getValue("telemetry.sdk.version").getStringValue()).isNotNull(); } } diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java index d3425629f9d..df9695cbf74 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java @@ -73,7 +73,7 @@ public void forEach(KeyValueConsumer consumer) { @Nullable @Override - public AttributeValue get(String key) { + public AttributeValue getValue(String key) { return data.get(key); } diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java index 94742f3ab73..f3559d36f33 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java @@ -265,7 +265,7 @@ void toSpanData_SpanDataDoesNotChangeWhenModifyingSpan() { // Assert that the snapshot does not reflect the modified state, but the state of the time when // toSpanData was called. assertThat(spanData.getAttributes().size()).isEqualTo(attributes.size()); - assertThat(spanData.getAttributes().get("anotherKey")).isNull(); + assertThat(spanData.getAttributes().getValue("anotherKey")).isNull(); assertThat(spanData.getHasEnded()).isFalse(); assertThat(spanData.getEndEpochNanos()).isEqualTo(0); assertThat(spanData.getName()).isEqualTo(SPAN_NAME); @@ -275,7 +275,7 @@ void toSpanData_SpanDataDoesNotChangeWhenModifyingSpan() { // state. spanData = span.toSpanData(); assertThat(spanData.getAttributes().size()).isEqualTo(attributes.size() + 1); - assertThat(spanData.getAttributes().get("anotherKey")) + assertThat(spanData.getAttributes().getValue("anotherKey")) .isEqualTo(AttributeValue.stringAttributeValue("anotherValue")); assertThat(spanData.getHasEnded()).isTrue(); assertThat(spanData.getEndEpochNanos()).isGreaterThan(0); @@ -405,27 +405,27 @@ void setAttribute() { } SpanData spanData = span.toSpanData(); assertThat(spanData.getAttributes().size()).isEqualTo(14); - assertThat(spanData.getAttributes().get("StringKey")).isNotNull(); - assertThat(spanData.getAttributes().get("EmptyStringKey")).isNotNull(); - assertThat(spanData.getAttributes().get("EmptyStringAttributeValue")).isNotNull(); - assertThat(spanData.getAttributes().get("LongKey")).isNotNull(); - assertThat(spanData.getAttributes().get("DoubleKey")).isNotNull(); - assertThat(spanData.getAttributes().get("BooleanKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayStringKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayLongKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayDoubleKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayBooleanKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayWithNullLongKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayWithNullStringKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayWithNullDoubleKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayWithNullBooleanKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayStringKey").getStringArrayValue().size()) + assertThat(spanData.getAttributes().getValue("StringKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("EmptyStringKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("EmptyStringAttributeValue")).isNotNull(); + assertThat(spanData.getAttributes().getValue("LongKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("DoubleKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("BooleanKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("ArrayStringKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("ArrayLongKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("ArrayDoubleKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("ArrayBooleanKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("ArrayWithNullLongKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("ArrayWithNullStringKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("ArrayWithNullDoubleKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("ArrayWithNullBooleanKey")).isNotNull(); + assertThat(spanData.getAttributes().getValue("ArrayStringKey").getStringArrayValue().size()) .isEqualTo(4); - assertThat(spanData.getAttributes().get("ArrayLongKey").getLongArrayValue().size()) + assertThat(spanData.getAttributes().getValue("ArrayLongKey").getLongArrayValue().size()) .isEqualTo(5); - assertThat(spanData.getAttributes().get("ArrayDoubleKey").getDoubleArrayValue().size()) + assertThat(spanData.getAttributes().getValue("ArrayDoubleKey").getDoubleArrayValue().size()) .isEqualTo(5); - assertThat(spanData.getAttributes().get("ArrayBooleanKey").getBooleanArrayValue().size()) + assertThat(spanData.getAttributes().getValue("ArrayBooleanKey").getBooleanArrayValue().size()) .isEqualTo(4); } @@ -606,13 +606,13 @@ void droppingAndAddingAttributes() { for (int i = 0; i < maxNumberOfAttributes / 2; i++) { int val = i + maxNumberOfAttributes * 3 / 2; AttributeValue expectedValue = AttributeValue.longAttributeValue(val); - assertThat(spanData.getAttributes().get("MyStringAttributeKey" + i)) + assertThat(spanData.getAttributes().getValue("MyStringAttributeKey" + i)) .isEqualTo(expectedValue); } // Test that we have the newest re-added initial entries. for (int i = maxNumberOfAttributes / 2; i < maxNumberOfAttributes; i++) { AttributeValue expectedValue = AttributeValue.longAttributeValue(i); - assertThat(spanData.getAttributes().get("MyStringAttributeKey" + i)) + assertThat(spanData.getAttributes().getValue("MyStringAttributeKey" + i)) .isEqualTo(expectedValue); } } finally { @@ -698,7 +698,7 @@ void recordException_noMessage() { List events = span.toSpanData().getEvents(); assertThat(events).hasSize(1); Event event = events.get(0); - assertThat(event.getAttributes().get("exception.message")).isNull(); + assertThat(event.getAttributes().getValue("exception.message")).isNull(); } private static class InnerClassException extends Exception {} @@ -713,7 +713,7 @@ void recordException_innerClassException() { List events = span.toSpanData().getEvents(); assertThat(events).hasSize(1); Event event = events.get(0); - assertThat(event.getAttributes().get("exception.type")) + assertThat(event.getAttributes().getValue("exception.type")) .isEqualTo( stringAttributeValue( "io.opentelemetry.sdk.trace.RecordEventsReadableSpanTest.InnerClassException")); @@ -880,7 +880,8 @@ private void verifySpanData( // verify equality manually, since the implementations don't all equals with each other. ReadableAttributes spanDataAttributes = spanData.getAttributes(); assertThat(spanDataAttributes.size()).isEqualTo(attributes.size()); - spanDataAttributes.forEach((key, value) -> assertThat(attributes.get(key)).isEqualTo(value)); + spanDataAttributes.forEach( + (key, value) -> assertThat(attributes.getValue(key)).isEqualTo(value)); } @Test diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index ed0e243d014..5b8ca8c8738 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -218,11 +218,11 @@ void setAttribute() { SpanData spanData = span.toSpanData(); ReadableAttributes attrs = spanData.getAttributes(); assertThat(attrs.size()).isEqualTo(5); - assertThat(attrs.get("string")).isEqualTo(AttributeValue.stringAttributeValue("value")); - assertThat(attrs.get("long")).isEqualTo(AttributeValue.longAttributeValue(12345L)); - assertThat(attrs.get("double")).isEqualTo(AttributeValue.doubleAttributeValue(0.12345)); - assertThat(attrs.get("boolean")).isEqualTo(AttributeValue.booleanAttributeValue(true)); - assertThat(attrs.get("stringAttribute")) + assertThat(attrs.getValue("string")).isEqualTo(AttributeValue.stringAttributeValue("value")); + assertThat(attrs.getValue("long")).isEqualTo(AttributeValue.longAttributeValue(12345L)); + assertThat(attrs.getValue("double")).isEqualTo(AttributeValue.doubleAttributeValue(0.12345)); + assertThat(attrs.getValue("boolean")).isEqualTo(AttributeValue.booleanAttributeValue(true)); + assertThat(attrs.getValue("stringAttribute")) .isEqualTo(AttributeValue.stringAttributeValue("attrvalue")); assertThat(spanData.getTotalAttributeCount()).isEqualTo(5); } finally { @@ -243,11 +243,11 @@ void setAttribute_afterEnd() { try { ReadableAttributes attrs = span.toSpanData().getAttributes(); assertThat(attrs.size()).isEqualTo(5); - assertThat(attrs.get("string")).isEqualTo(AttributeValue.stringAttributeValue("value")); - assertThat(attrs.get("long")).isEqualTo(AttributeValue.longAttributeValue(12345L)); - assertThat(attrs.get("double")).isEqualTo(AttributeValue.doubleAttributeValue(.12345)); - assertThat(attrs.get("boolean")).isEqualTo(AttributeValue.booleanAttributeValue(true)); - assertThat(attrs.get("stringAttribute")) + assertThat(attrs.getValue("string")).isEqualTo(AttributeValue.stringAttributeValue("value")); + assertThat(attrs.getValue("long")).isEqualTo(AttributeValue.longAttributeValue(12345L)); + assertThat(attrs.getValue("double")).isEqualTo(AttributeValue.doubleAttributeValue(.12345)); + assertThat(attrs.getValue("boolean")).isEqualTo(AttributeValue.booleanAttributeValue(true)); + assertThat(attrs.getValue("stringAttribute")) .isEqualTo(AttributeValue.stringAttributeValue("attrvalue")); } finally { span.end(); @@ -261,11 +261,11 @@ void setAttribute_afterEnd() { ReadableAttributes attrs = span.toSpanData().getAttributes(); assertThat(attrs.size()).isEqualTo(5); - assertThat(attrs.get("string2")).isNull(); - assertThat(attrs.get("long2")).isNull(); - assertThat(attrs.get("double2")).isNull(); - assertThat(attrs.get("boolean2")).isNull(); - assertThat(attrs.get("stringAttribute2")).isNull(); + assertThat(attrs.getValue("string2")).isNull(); + assertThat(attrs.getValue("long2")).isNull(); + assertThat(attrs.getValue("double2")).isNull(); + assertThat(attrs.getValue("boolean2")).isNull(); + assertThat(attrs.getValue("stringAttribute2")).isNull(); } @Test @@ -313,18 +313,18 @@ void setAttribute_NoEffectAfterStartSpan() { ReadableAttributes beforeAttributes = span.toSpanData().getAttributes(); assertThat(beforeAttributes.size()).isEqualTo(2); - assertThat(beforeAttributes.get("key1")) + assertThat(beforeAttributes.getValue("key1")) .isEqualTo(AttributeValue.stringAttributeValue("value1")); - assertThat(beforeAttributes.get("key2")) + assertThat(beforeAttributes.getValue("key2")) .isEqualTo(AttributeValue.stringAttributeValue("value2")); spanBuilder.setAttribute("key3", "value3"); ReadableAttributes afterAttributes = span.toSpanData().getAttributes(); assertThat(afterAttributes.size()).isEqualTo(2); - assertThat(afterAttributes.get("key1")) + assertThat(afterAttributes.getValue("key1")) .isEqualTo(AttributeValue.stringAttributeValue("value1")); - assertThat(afterAttributes.get("key2")) + assertThat(afterAttributes.getValue("key2")) .isEqualTo(AttributeValue.stringAttributeValue("value2")); } @@ -406,7 +406,7 @@ void droppingAttributes() { ReadableAttributes attrs = span.toSpanData().getAttributes(); assertThat(attrs.size()).isEqualTo(maxNumberOfAttrs); for (int i = 0; i < maxNumberOfAttrs; i++) { - assertThat(attrs.get("key" + i)).isEqualTo(AttributeValue.longAttributeValue(i)); + assertThat(attrs.getValue("key" + i)).isEqualTo(AttributeValue.longAttributeValue(i)); } } finally { span.end(); @@ -448,25 +448,25 @@ public void tooLargeAttributeValuesAreTruncated() { try { ReadableAttributes attrs = span.toSpanData().getAttributes(); - assertThat(attrs.get("builderStringNull")).isEqualTo(null); - assertThat(attrs.get("builderStringSmall")) + assertThat(attrs.getValue("builderStringNull")).isEqualTo(null); + assertThat(attrs.getValue("builderStringSmall")) .isEqualTo(AttributeValue.stringAttributeValue("small")); - assertThat(attrs.get("builderStringLarge")) + assertThat(attrs.getValue("builderStringLarge")) .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.get("builderLong")).isEqualTo(AttributeValue.longAttributeValue(42L)); - assertThat(attrs.get("builderStringLargeValue")) + assertThat(attrs.getValue("builderLong")).isEqualTo(AttributeValue.longAttributeValue(42L)); + assertThat(attrs.getValue("builderStringLargeValue")) .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.get("builderStringArray")) + assertThat(attrs.getValue("builderStringArray")) .isEqualTo(AttributeValue.arrayAttributeValue("small", null, "very large")); - assertThat(attrs.get("spanStringSmall")) + assertThat(attrs.getValue("spanStringSmall")) .isEqualTo(AttributeValue.stringAttributeValue("small")); - assertThat(attrs.get("spanStringLarge")) + assertThat(attrs.getValue("spanStringLarge")) .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.get("spanLong")).isEqualTo(AttributeValue.longAttributeValue(42L)); - assertThat(attrs.get("spanStringLarge")) + assertThat(attrs.getValue("spanLong")).isEqualTo(AttributeValue.longAttributeValue(42L)); + assertThat(attrs.getValue("spanStringLarge")) .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.get("spanStringArray")) + assertThat(attrs.getValue("spanStringArray")) .isEqualTo(AttributeValue.arrayAttributeValue("small", null, "very large")); } finally { span.end(); @@ -487,7 +487,7 @@ void addAttributes_OnlyViaSampler() { RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); try { assertThat(span.toSpanData().getAttributes().size()).isEqualTo(1); - assertThat(span.toSpanData().getAttributes().get(Samplers.SAMPLING_PROBABILITY.key())) + assertThat(span.toSpanData().getAttributes().getValue(Samplers.SAMPLING_PROBABILITY.key())) .isEqualTo(AttributeValue.doubleAttributeValue(1)); } finally { span.end(); @@ -582,7 +582,7 @@ public String getDescription() { .startSpan(); try { assertThat(span.getContext().isSampled()).isTrue(); - assertThat(span.toSpanData().getAttributes().get(samplerAttributeName)).isNotNull(); + assertThat(span.toSpanData().getAttributes().getValue(samplerAttributeName)).isNotNull(); } finally { span.end(); } diff --git a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java index cd8cf07e9b4..75fb394a220 100644 --- a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java +++ b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java @@ -62,7 +62,7 @@ void windows() { @Test void inDefault() { ReadableAttributes attributes = Resource.getDefault().getAttributes(); - assertThat(attributes.get(ResourceAttributes.OS_NAME.key())).isNotNull(); - assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION.key())).isNotNull(); + assertThat(attributes.getValue(ResourceAttributes.OS_NAME.key())).isNotNull(); + assertThat(attributes.getValue(ResourceAttributes.OS_DESCRIPTION.key())).isNotNull(); } } diff --git a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java index 43946e139f9..7836661d428 100644 --- a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java +++ b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java @@ -44,8 +44,8 @@ void normal() { @Test void inDefault() { ReadableAttributes attributes = Resource.getDefault().getAttributes(); - assertThat(attributes.get(ResourceAttributes.PROCESS_PID.key())).isNotNull(); - assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH.key())).isNotNull(); - assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE.key())).isNotNull(); + assertThat(attributes.getValue(ResourceAttributes.PROCESS_PID.key())).isNotNull(); + assertThat(attributes.getValue(ResourceAttributes.PROCESS_EXECUTABLE_PATH.key())).isNotNull(); + assertThat(attributes.getValue(ResourceAttributes.PROCESS_COMMAND_LINE.key())).isNotNull(); } } diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java index b585debb839..d473a7ddcbc 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java @@ -44,7 +44,7 @@ public static List getByAttr( return getByCondition( spans, span -> { - AttributeValue attrValue = span.getAttributes().get(key); + AttributeValue attrValue = span.getAttributes().getValue(key); if (attrValue == null) { return false; } diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java index 21242718578..b6f29bfa052 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -61,7 +61,7 @@ void test() { ReadableAttributes attrs = spans.get(0).getAttributes(); assertThat(attrs.size()).isEqualTo(3); for (int i = 1; i <= 3; i++) { - assertThat(attrs.get("key" + i).getStringValue()).isEqualTo(Integer.toString(i)); + assertThat(attrs.getValue("key" + i).getStringValue()).isEqualTo(Integer.toString(i)); } assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); From 03db6d1f81c986dd7f63db5fc5a37cdce641e862 Mon Sep 17 00:00:00 2001 From: jwatson Date: Tue, 8 Sep 2020 07:58:44 -0700 Subject: [PATCH 02/20] Make AttributesMap not extend map, and restore the get method name for Attributes. --- .../io/opentelemetry/common/Attributes.java | 5 -- .../exporters/zipkin/ZipkinSpanExporter.java | 7 +- .../opentracingshim/testbed/TestUtils.java | 2 +- .../nestedcallbacks/NestedCallbacksTest.java | 2 +- .../sdk/resources/ResourceProvidersTest.java | 2 +- .../sdk/resources/ResourceTest.java | 6 +- .../sdk/trace/AttributesMap.java | 2 +- .../trace/RecordEventsReadableSpanTest.java | 51 +++++++------- .../sdk/trace/SpanBuilderSdkTest.java | 66 +++++++++---------- .../extensions/resources/OsResourceTest.java | 4 +- .../resources/ProcessResourceTest.java | 6 +- .../extensions/trace/testbed/TestUtils.java | 2 +- .../nestedcallbacks/NestedCallbacksTest.java | 2 +- 13 files changed, 75 insertions(+), 82 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/common/Attributes.java b/api/src/main/java/io/opentelemetry/common/Attributes.java index 0d258895d77..a179358a1de 100644 --- a/api/src/main/java/io/opentelemetry/common/Attributes.java +++ b/api/src/main/java/io/opentelemetry/common/Attributes.java @@ -50,11 +50,6 @@ abstract static class ArrayBackedAttributes extends Attributes { public Builder toBuilder() { return new Builder(new ArrayList<>(data())); } - - @Override - public AttributeValue getValue(String key) { - return get(key); - } } /** Returns a {@link Attributes} instance with no attributes. */ diff --git a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java index 3b02879b43e..37ab7f0aaca 100644 --- a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java +++ b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java @@ -156,14 +156,14 @@ public void consume(String key, AttributeValue value) { }); Status status = spanData.getStatus(); // for GRPC spans, include status code & description. - if (status != null && spanAttributes.getValue(SemanticAttributes.RPC_SERVICE.key()) != null) { + if (status != null && spanAttributes.get(SemanticAttributes.RPC_SERVICE.key()) != null) { spanBuilder.putTag(GRPC_STATUS_CODE, status.getCanonicalCode().toString()); if (status.getDescription() != null) { spanBuilder.putTag(GRPC_STATUS_DESCRIPTION, status.getDescription()); } } // add the error tag, if it isn't already in the source span. - if (status != null && !status.isOk() && spanAttributes.getValue(STATUS_ERROR) == null) { + if (status != null && !status.isOk() && spanAttributes.get(STATUS_ERROR) == null) { spanBuilder.putTag(STATUS_ERROR, status.getCanonicalCode().toString()); } @@ -189,8 +189,7 @@ private static Endpoint chooseEndpoint(SpanData spanData, Endpoint localEndpoint ReadableAttributes resourceAttributes = spanData.getResource().getAttributes(); // use the service.name from the Resource, if it's been set. - AttributeValue serviceNameValue = - resourceAttributes.getValue(ResourceAttributes.SERVICE_NAME.key()); + AttributeValue serviceNameValue = resourceAttributes.get(ResourceAttributes.SERVICE_NAME.key()); if (serviceNameValue == null) { return localEndpoint; } diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java index 9fe84653b35..241448c829b 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java @@ -45,7 +45,7 @@ public static List getByAttr( return getByCondition( spans, spanData -> { - AttributeValue attrValue = spanData.getAttributes().getValue(key); + AttributeValue attrValue = spanData.getAttributes().get(key); if (attrValue == null) { return false; } diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java index 5b74a30c424..bf020549902 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -64,7 +64,7 @@ void test() { assertEquals(3, attrs.size()); for (int i = 1; i <= 3; i++) { assertEquals( - Integer.toString(i), spans.get(0).getAttributes().getValue("key" + i).getStringValue()); + Integer.toString(i), spans.get(0).getAttributes().get("key" + i).getStringValue()); } assertNull(tracer.scopeManager().activeSpan()); diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java index 22836d0ee41..fe95e21dc38 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java @@ -27,7 +27,7 @@ class ResourceProvidersTest { void default_resource_includes_attributes_from_providers() { Resource resource = Resource.getDefault(); - AttributeValue providerAttribute = resource.getAttributes().getValue("providerAttribute"); + AttributeValue providerAttribute = resource.getAttributes().get("providerAttribute"); assertThat(providerAttribute).isNotNull(); assertThat(providerAttribute.getLongValue()).isEqualTo(42); } diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java index 01018fadae1..825784de60f 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java @@ -242,10 +242,10 @@ void testMergeResources_Resource2_Null() { void testSdkTelemetryResources() { Resource resource = Resource.getTelemetrySdk(); ReadableAttributes attributes = resource.getAttributes(); - assertThat(attributes.getValue("telemetry.sdk.name")) + assertThat(attributes.get("telemetry.sdk.name")) .isEqualTo(AttributeValue.stringAttributeValue("opentelemetry")); - assertThat(attributes.getValue("telemetry.sdk.language")) + assertThat(attributes.get("telemetry.sdk.language")) .isEqualTo(AttributeValue.stringAttributeValue("java")); - assertThat(attributes.getValue("telemetry.sdk.version").getStringValue()).isNotNull(); + assertThat(attributes.get("telemetry.sdk.version").getStringValue()).isNotNull(); } } diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java index df9695cbf74..d3425629f9d 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java @@ -73,7 +73,7 @@ public void forEach(KeyValueConsumer consumer) { @Nullable @Override - public AttributeValue getValue(String key) { + public AttributeValue get(String key) { return data.get(key); } diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java index f3559d36f33..94742f3ab73 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java @@ -265,7 +265,7 @@ void toSpanData_SpanDataDoesNotChangeWhenModifyingSpan() { // Assert that the snapshot does not reflect the modified state, but the state of the time when // toSpanData was called. assertThat(spanData.getAttributes().size()).isEqualTo(attributes.size()); - assertThat(spanData.getAttributes().getValue("anotherKey")).isNull(); + assertThat(spanData.getAttributes().get("anotherKey")).isNull(); assertThat(spanData.getHasEnded()).isFalse(); assertThat(spanData.getEndEpochNanos()).isEqualTo(0); assertThat(spanData.getName()).isEqualTo(SPAN_NAME); @@ -275,7 +275,7 @@ void toSpanData_SpanDataDoesNotChangeWhenModifyingSpan() { // state. spanData = span.toSpanData(); assertThat(spanData.getAttributes().size()).isEqualTo(attributes.size() + 1); - assertThat(spanData.getAttributes().getValue("anotherKey")) + assertThat(spanData.getAttributes().get("anotherKey")) .isEqualTo(AttributeValue.stringAttributeValue("anotherValue")); assertThat(spanData.getHasEnded()).isTrue(); assertThat(spanData.getEndEpochNanos()).isGreaterThan(0); @@ -405,27 +405,27 @@ void setAttribute() { } SpanData spanData = span.toSpanData(); assertThat(spanData.getAttributes().size()).isEqualTo(14); - assertThat(spanData.getAttributes().getValue("StringKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("EmptyStringKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("EmptyStringAttributeValue")).isNotNull(); - assertThat(spanData.getAttributes().getValue("LongKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("DoubleKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("BooleanKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("ArrayStringKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("ArrayLongKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("ArrayDoubleKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("ArrayBooleanKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("ArrayWithNullLongKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("ArrayWithNullStringKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("ArrayWithNullDoubleKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("ArrayWithNullBooleanKey")).isNotNull(); - assertThat(spanData.getAttributes().getValue("ArrayStringKey").getStringArrayValue().size()) + assertThat(spanData.getAttributes().get("StringKey")).isNotNull(); + assertThat(spanData.getAttributes().get("EmptyStringKey")).isNotNull(); + assertThat(spanData.getAttributes().get("EmptyStringAttributeValue")).isNotNull(); + assertThat(spanData.getAttributes().get("LongKey")).isNotNull(); + assertThat(spanData.getAttributes().get("DoubleKey")).isNotNull(); + assertThat(spanData.getAttributes().get("BooleanKey")).isNotNull(); + assertThat(spanData.getAttributes().get("ArrayStringKey")).isNotNull(); + assertThat(spanData.getAttributes().get("ArrayLongKey")).isNotNull(); + assertThat(spanData.getAttributes().get("ArrayDoubleKey")).isNotNull(); + assertThat(spanData.getAttributes().get("ArrayBooleanKey")).isNotNull(); + assertThat(spanData.getAttributes().get("ArrayWithNullLongKey")).isNotNull(); + assertThat(spanData.getAttributes().get("ArrayWithNullStringKey")).isNotNull(); + assertThat(spanData.getAttributes().get("ArrayWithNullDoubleKey")).isNotNull(); + assertThat(spanData.getAttributes().get("ArrayWithNullBooleanKey")).isNotNull(); + assertThat(spanData.getAttributes().get("ArrayStringKey").getStringArrayValue().size()) .isEqualTo(4); - assertThat(spanData.getAttributes().getValue("ArrayLongKey").getLongArrayValue().size()) + assertThat(spanData.getAttributes().get("ArrayLongKey").getLongArrayValue().size()) .isEqualTo(5); - assertThat(spanData.getAttributes().getValue("ArrayDoubleKey").getDoubleArrayValue().size()) + assertThat(spanData.getAttributes().get("ArrayDoubleKey").getDoubleArrayValue().size()) .isEqualTo(5); - assertThat(spanData.getAttributes().getValue("ArrayBooleanKey").getBooleanArrayValue().size()) + assertThat(spanData.getAttributes().get("ArrayBooleanKey").getBooleanArrayValue().size()) .isEqualTo(4); } @@ -606,13 +606,13 @@ void droppingAndAddingAttributes() { for (int i = 0; i < maxNumberOfAttributes / 2; i++) { int val = i + maxNumberOfAttributes * 3 / 2; AttributeValue expectedValue = AttributeValue.longAttributeValue(val); - assertThat(spanData.getAttributes().getValue("MyStringAttributeKey" + i)) + assertThat(spanData.getAttributes().get("MyStringAttributeKey" + i)) .isEqualTo(expectedValue); } // Test that we have the newest re-added initial entries. for (int i = maxNumberOfAttributes / 2; i < maxNumberOfAttributes; i++) { AttributeValue expectedValue = AttributeValue.longAttributeValue(i); - assertThat(spanData.getAttributes().getValue("MyStringAttributeKey" + i)) + assertThat(spanData.getAttributes().get("MyStringAttributeKey" + i)) .isEqualTo(expectedValue); } } finally { @@ -698,7 +698,7 @@ void recordException_noMessage() { List events = span.toSpanData().getEvents(); assertThat(events).hasSize(1); Event event = events.get(0); - assertThat(event.getAttributes().getValue("exception.message")).isNull(); + assertThat(event.getAttributes().get("exception.message")).isNull(); } private static class InnerClassException extends Exception {} @@ -713,7 +713,7 @@ void recordException_innerClassException() { List events = span.toSpanData().getEvents(); assertThat(events).hasSize(1); Event event = events.get(0); - assertThat(event.getAttributes().getValue("exception.type")) + assertThat(event.getAttributes().get("exception.type")) .isEqualTo( stringAttributeValue( "io.opentelemetry.sdk.trace.RecordEventsReadableSpanTest.InnerClassException")); @@ -880,8 +880,7 @@ private void verifySpanData( // verify equality manually, since the implementations don't all equals with each other. ReadableAttributes spanDataAttributes = spanData.getAttributes(); assertThat(spanDataAttributes.size()).isEqualTo(attributes.size()); - spanDataAttributes.forEach( - (key, value) -> assertThat(attributes.getValue(key)).isEqualTo(value)); + spanDataAttributes.forEach((key, value) -> assertThat(attributes.get(key)).isEqualTo(value)); } @Test diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index 5b8ca8c8738..ed0e243d014 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -218,11 +218,11 @@ void setAttribute() { SpanData spanData = span.toSpanData(); ReadableAttributes attrs = spanData.getAttributes(); assertThat(attrs.size()).isEqualTo(5); - assertThat(attrs.getValue("string")).isEqualTo(AttributeValue.stringAttributeValue("value")); - assertThat(attrs.getValue("long")).isEqualTo(AttributeValue.longAttributeValue(12345L)); - assertThat(attrs.getValue("double")).isEqualTo(AttributeValue.doubleAttributeValue(0.12345)); - assertThat(attrs.getValue("boolean")).isEqualTo(AttributeValue.booleanAttributeValue(true)); - assertThat(attrs.getValue("stringAttribute")) + assertThat(attrs.get("string")).isEqualTo(AttributeValue.stringAttributeValue("value")); + assertThat(attrs.get("long")).isEqualTo(AttributeValue.longAttributeValue(12345L)); + assertThat(attrs.get("double")).isEqualTo(AttributeValue.doubleAttributeValue(0.12345)); + assertThat(attrs.get("boolean")).isEqualTo(AttributeValue.booleanAttributeValue(true)); + assertThat(attrs.get("stringAttribute")) .isEqualTo(AttributeValue.stringAttributeValue("attrvalue")); assertThat(spanData.getTotalAttributeCount()).isEqualTo(5); } finally { @@ -243,11 +243,11 @@ void setAttribute_afterEnd() { try { ReadableAttributes attrs = span.toSpanData().getAttributes(); assertThat(attrs.size()).isEqualTo(5); - assertThat(attrs.getValue("string")).isEqualTo(AttributeValue.stringAttributeValue("value")); - assertThat(attrs.getValue("long")).isEqualTo(AttributeValue.longAttributeValue(12345L)); - assertThat(attrs.getValue("double")).isEqualTo(AttributeValue.doubleAttributeValue(.12345)); - assertThat(attrs.getValue("boolean")).isEqualTo(AttributeValue.booleanAttributeValue(true)); - assertThat(attrs.getValue("stringAttribute")) + assertThat(attrs.get("string")).isEqualTo(AttributeValue.stringAttributeValue("value")); + assertThat(attrs.get("long")).isEqualTo(AttributeValue.longAttributeValue(12345L)); + assertThat(attrs.get("double")).isEqualTo(AttributeValue.doubleAttributeValue(.12345)); + assertThat(attrs.get("boolean")).isEqualTo(AttributeValue.booleanAttributeValue(true)); + assertThat(attrs.get("stringAttribute")) .isEqualTo(AttributeValue.stringAttributeValue("attrvalue")); } finally { span.end(); @@ -261,11 +261,11 @@ void setAttribute_afterEnd() { ReadableAttributes attrs = span.toSpanData().getAttributes(); assertThat(attrs.size()).isEqualTo(5); - assertThat(attrs.getValue("string2")).isNull(); - assertThat(attrs.getValue("long2")).isNull(); - assertThat(attrs.getValue("double2")).isNull(); - assertThat(attrs.getValue("boolean2")).isNull(); - assertThat(attrs.getValue("stringAttribute2")).isNull(); + assertThat(attrs.get("string2")).isNull(); + assertThat(attrs.get("long2")).isNull(); + assertThat(attrs.get("double2")).isNull(); + assertThat(attrs.get("boolean2")).isNull(); + assertThat(attrs.get("stringAttribute2")).isNull(); } @Test @@ -313,18 +313,18 @@ void setAttribute_NoEffectAfterStartSpan() { ReadableAttributes beforeAttributes = span.toSpanData().getAttributes(); assertThat(beforeAttributes.size()).isEqualTo(2); - assertThat(beforeAttributes.getValue("key1")) + assertThat(beforeAttributes.get("key1")) .isEqualTo(AttributeValue.stringAttributeValue("value1")); - assertThat(beforeAttributes.getValue("key2")) + assertThat(beforeAttributes.get("key2")) .isEqualTo(AttributeValue.stringAttributeValue("value2")); spanBuilder.setAttribute("key3", "value3"); ReadableAttributes afterAttributes = span.toSpanData().getAttributes(); assertThat(afterAttributes.size()).isEqualTo(2); - assertThat(afterAttributes.getValue("key1")) + assertThat(afterAttributes.get("key1")) .isEqualTo(AttributeValue.stringAttributeValue("value1")); - assertThat(afterAttributes.getValue("key2")) + assertThat(afterAttributes.get("key2")) .isEqualTo(AttributeValue.stringAttributeValue("value2")); } @@ -406,7 +406,7 @@ void droppingAttributes() { ReadableAttributes attrs = span.toSpanData().getAttributes(); assertThat(attrs.size()).isEqualTo(maxNumberOfAttrs); for (int i = 0; i < maxNumberOfAttrs; i++) { - assertThat(attrs.getValue("key" + i)).isEqualTo(AttributeValue.longAttributeValue(i)); + assertThat(attrs.get("key" + i)).isEqualTo(AttributeValue.longAttributeValue(i)); } } finally { span.end(); @@ -448,25 +448,25 @@ public void tooLargeAttributeValuesAreTruncated() { try { ReadableAttributes attrs = span.toSpanData().getAttributes(); - assertThat(attrs.getValue("builderStringNull")).isEqualTo(null); - assertThat(attrs.getValue("builderStringSmall")) + assertThat(attrs.get("builderStringNull")).isEqualTo(null); + assertThat(attrs.get("builderStringSmall")) .isEqualTo(AttributeValue.stringAttributeValue("small")); - assertThat(attrs.getValue("builderStringLarge")) + assertThat(attrs.get("builderStringLarge")) .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.getValue("builderLong")).isEqualTo(AttributeValue.longAttributeValue(42L)); - assertThat(attrs.getValue("builderStringLargeValue")) + assertThat(attrs.get("builderLong")).isEqualTo(AttributeValue.longAttributeValue(42L)); + assertThat(attrs.get("builderStringLargeValue")) .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.getValue("builderStringArray")) + assertThat(attrs.get("builderStringArray")) .isEqualTo(AttributeValue.arrayAttributeValue("small", null, "very large")); - assertThat(attrs.getValue("spanStringSmall")) + assertThat(attrs.get("spanStringSmall")) .isEqualTo(AttributeValue.stringAttributeValue("small")); - assertThat(attrs.getValue("spanStringLarge")) + assertThat(attrs.get("spanStringLarge")) .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.getValue("spanLong")).isEqualTo(AttributeValue.longAttributeValue(42L)); - assertThat(attrs.getValue("spanStringLarge")) + assertThat(attrs.get("spanLong")).isEqualTo(AttributeValue.longAttributeValue(42L)); + assertThat(attrs.get("spanStringLarge")) .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.getValue("spanStringArray")) + assertThat(attrs.get("spanStringArray")) .isEqualTo(AttributeValue.arrayAttributeValue("small", null, "very large")); } finally { span.end(); @@ -487,7 +487,7 @@ void addAttributes_OnlyViaSampler() { RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); try { assertThat(span.toSpanData().getAttributes().size()).isEqualTo(1); - assertThat(span.toSpanData().getAttributes().getValue(Samplers.SAMPLING_PROBABILITY.key())) + assertThat(span.toSpanData().getAttributes().get(Samplers.SAMPLING_PROBABILITY.key())) .isEqualTo(AttributeValue.doubleAttributeValue(1)); } finally { span.end(); @@ -582,7 +582,7 @@ public String getDescription() { .startSpan(); try { assertThat(span.getContext().isSampled()).isTrue(); - assertThat(span.toSpanData().getAttributes().getValue(samplerAttributeName)).isNotNull(); + assertThat(span.toSpanData().getAttributes().get(samplerAttributeName)).isNotNull(); } finally { span.end(); } diff --git a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java index 75fb394a220..cd8cf07e9b4 100644 --- a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java +++ b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java @@ -62,7 +62,7 @@ void windows() { @Test void inDefault() { ReadableAttributes attributes = Resource.getDefault().getAttributes(); - assertThat(attributes.getValue(ResourceAttributes.OS_NAME.key())).isNotNull(); - assertThat(attributes.getValue(ResourceAttributes.OS_DESCRIPTION.key())).isNotNull(); + assertThat(attributes.get(ResourceAttributes.OS_NAME.key())).isNotNull(); + assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION.key())).isNotNull(); } } diff --git a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java index 7836661d428..43946e139f9 100644 --- a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java +++ b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java @@ -44,8 +44,8 @@ void normal() { @Test void inDefault() { ReadableAttributes attributes = Resource.getDefault().getAttributes(); - assertThat(attributes.getValue(ResourceAttributes.PROCESS_PID.key())).isNotNull(); - assertThat(attributes.getValue(ResourceAttributes.PROCESS_EXECUTABLE_PATH.key())).isNotNull(); - assertThat(attributes.getValue(ResourceAttributes.PROCESS_COMMAND_LINE.key())).isNotNull(); + assertThat(attributes.get(ResourceAttributes.PROCESS_PID.key())).isNotNull(); + assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH.key())).isNotNull(); + assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE.key())).isNotNull(); } } diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java index d473a7ddcbc..b585debb839 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java @@ -44,7 +44,7 @@ public static List getByAttr( return getByCondition( spans, span -> { - AttributeValue attrValue = span.getAttributes().getValue(key); + AttributeValue attrValue = span.getAttributes().get(key); if (attrValue == null) { return false; } diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java index b6f29bfa052..21242718578 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -61,7 +61,7 @@ void test() { ReadableAttributes attrs = spans.get(0).getAttributes(); assertThat(attrs.size()).isEqualTo(3); for (int i = 1; i <= 3; i++) { - assertThat(attrs.getValue("key" + i).getStringValue()).isEqualTo(Integer.toString(i)); + assertThat(attrs.get("key" + i).getStringValue()).isEqualTo(Integer.toString(i)); } assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); From ef6ba9662b6ffdb327a1e1f1b333b7971008e33a Mon Sep 17 00:00:00 2001 From: jwatson Date: Tue, 8 Sep 2020 10:40:28 -0700 Subject: [PATCH 03/20] key class and implementation --- .../common/AttributeKeyImpl.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java new file mode 100644 index 00000000000..be0ce922641 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java @@ -0,0 +1,80 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.common; + +import java.util.List; + +class AttributeKeyImpl implements AttributeKey { + private final String key; + + AttributeKeyImpl(String key) { + this.key = key; + } + + @Override + public String get() { + return key; + } + + static class StringKey extends AttributeKeyImpl { + public StringKey(String key) { + super(key); + } + } + + static class BooleanKey extends AttributeKeyImpl { + BooleanKey(String key) { + super(key); + } + } + + static class LongKey extends AttributeKeyImpl { + LongKey(String key) { + super(key); + } + } + + static class DoubleKey extends AttributeKeyImpl { + DoubleKey(String key) { + super(key); + } + } + + static class StringArrayKey extends AttributeKeyImpl> { + public StringArrayKey(String key) { + super(key); + } + } + + static class BooleanArrayKey extends AttributeKeyImpl> { + BooleanArrayKey(String key) { + super(key); + } + } + + static class LongArrayKey extends AttributeKeyImpl> { + LongArrayKey(String key) { + super(key); + } + } + + static class DoubleArrayKey extends AttributeKeyImpl> { + DoubleArrayKey(String key) { + super(key); + } + } +} From e72fdb05cbb14104b6b8c2ad0d0836a0573f0ab9 Mon Sep 17 00:00:00 2001 From: jwatson Date: Tue, 8 Sep 2020 10:41:55 -0700 Subject: [PATCH 04/20] key class and implementation --- .../io/opentelemetry/common/AttributeKey.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 api/src/main/java/io/opentelemetry/common/AttributeKey.java diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKey.java b/api/src/main/java/io/opentelemetry/common/AttributeKey.java new file mode 100644 index 00000000000..36541ece092 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/common/AttributeKey.java @@ -0,0 +1,28 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.common; + +/** + * This interface provides a handle for setting the values of {@link Attributes}. The type of value + * that can be set with an implementation of this key is denoted by the type parameter. + * + * @param The type of value that can be set with the key. + */ +public interface AttributeKey { + /** Returns the underlying String representation of the key. */ + String get(); +} From 2b6a07235eab40e9f15de3427277974e00865491 Mon Sep 17 00:00:00 2001 From: jwatson Date: Wed, 9 Sep 2020 11:44:50 -0700 Subject: [PATCH 05/20] The code compiles --- .../common/AttributeConsumer.java | 29 ++- .../io/opentelemetry/common/AttributeKey.java | 3 + .../common/AttributeKeyImpl.java | 130 ++++++++-- .../io/opentelemetry/common/Attributes.java | 196 ++++++++++----- .../common/ImmutableKeyValuePairs.java | 2 +- .../common/RawAttributeConsumer.java | 21 ++ .../common/ReadableAttributes.java | 16 +- .../opentelemetry/internal/StringUtils.java | 22 +- .../io/opentelemetry/trace/DefaultSpan.java | 4 +- .../io/opentelemetry/trace/DefaultTracer.java | 4 +- .../java/io/opentelemetry/trace/Span.java | 6 +- .../attributes/BooleanAttributeSetter.java | 89 ------- .../attributes/DoubleAttributeSetter.java | 89 ------- .../trace/attributes/LongAttributeSetter.java | 89 ------- .../trace/attributes/SemanticAttributes.java | 195 ++++++--------- .../attributes/StringAttributeSetter.java | 90 ------- .../opentelemetry/common/AttributesTest.java | 165 ++++++------ .../opentelemetry/trace/DefaultSpanTest.java | 36 +-- .../opentelemetry/trace/SpanBuilderTest.java | 4 +- .../BooleanAttributeSetterTest.java | 36 --- .../attributes/DoubleAttributeSetterTest.java | 36 --- .../attributes/LongAttributeSetterTest.java | 36 --- .../attributes/SemanticAttributesTest.java | 114 --------- .../attributes/StringAttributeSetterTest.java | 36 --- .../exporters/jaeger/Adapter.java | 60 ++--- .../exporters/jaeger/AdapterTest.java | 70 +++--- .../logging/LoggingMetricExporterTest.java | 5 +- .../logging/LoggingSpanExporterTest.java | 4 +- .../exporters/otlp/CommonAdapter.java | 148 +++++++---- .../exporters/otlp/ResourceAdapter.java | 10 +- .../exporters/otlp/SpanAdapter.java | 23 +- .../exporters/otlp/CommonAdapterTest.java | 29 ++- .../exporters/otlp/MetricAdapterTest.java | 5 +- .../exporters/otlp/ResourceAdapterTest.java | 21 +- .../exporters/otlp/SpanAdapterTest.java | 12 +- .../prometheus/MetricAdapterTest.java | 4 +- .../prometheus/PrometheusCollectorTest.java | 6 +- .../exporters/zipkin/ZipkinSpanExporter.java | 37 ++- .../zipkin/ZipkinSpanExporterTest.java | 60 ++--- .../extensions/trace/MessageEvent.java | 37 +-- .../opentracingshim/SpanBuilderShim.java | 35 ++- .../opentracingshim/SpanShim.java | 16 +- .../opentracingshim/testbed/TestUtils.java | 44 +--- .../nestedcallbacks/NestedCallbacksTest.java | 4 +- .../PromisePropagationTest.java | 4 +- .../sdk/trace/SpanPipelineBenchmark.java | 13 +- .../opentelemetry/sdk/resources/Resource.java | 120 ++++++++- .../sdk/resources/ResourceAttributes.java | 102 +++----- .../resources/EnvAutodetectResourceTest.java | 16 +- .../sdk/resources/ResourceProvidersTest.java | 6 +- .../sdk/resources/ResourceTest.java | 143 ++++------- .../sdk/resources/TestResourceProvider.java | 5 +- .../sdk/metrics/BatchRecorderSdkTest.java | 5 +- .../sdk/metrics/DoubleCounterSdkTest.java | 5 +- .../sdk/metrics/DoubleSumObserverSdkTest.java | 5 +- .../metrics/DoubleUpDownCounterSdkTest.java | 5 +- .../DoubleUpDownSumObserverSdkTest.java | 5 +- .../metrics/DoubleValueObserverSdkTest.java | 5 +- .../metrics/DoubleValueRecorderSdkTest.java | 5 +- .../sdk/metrics/LongCounterSdkTest.java | 5 +- .../sdk/metrics/LongSumObserverSdkTest.java | 5 +- .../sdk/metrics/LongUpDownCounterSdkTest.java | 5 +- .../metrics/LongUpDownSumObserverSdkTest.java | 5 +- .../sdk/metrics/LongValueObserverSdkTest.java | 5 +- .../sdk/metrics/LongValueRecorderSdkTest.java | 5 +- .../sdk/metrics/MeterSdkTest.java | 5 +- .../sdk/trace/AttributesMap.java | 66 ++++- .../sdk/trace/RecordEventsReadableSpan.java | 38 +-- .../io/opentelemetry/sdk/trace/Samplers.java | 9 +- .../sdk/trace/SpanBuilderSdk.java | 30 ++- .../trace/RecordEventsReadableSpanTest.java | 236 +++++++++--------- .../opentelemetry/sdk/trace/SamplersTest.java | 15 +- .../sdk/trace/SpanBuilderSdkTest.java | 223 ++++++++--------- .../io/opentelemetry/sdk/trace/TestUtils.java | 13 +- .../sdk/trace/TimedEventTest.java | 8 +- .../sdk/trace/TracerSdkTest.java | 3 +- .../trace/aws/resource/BeanstalkResource.java | 6 +- .../trace/aws/resource/Ec2Resource.java | 16 +- .../trace/aws/resource/EcsResource.java | 4 +- .../aws/resource/BeanstalkResourceTest.java | 10 +- .../trace/aws/resource/Ec2ResourceTest.java | 35 +-- .../trace/aws/resource/EcsResourceTest.java | 12 +- .../jaeger/sampler/RateLimitingSampler.java | 12 +- .../sampler/RateLimitingSamplerTest.java | 7 +- .../sdk/extensions/resources/OsResource.java | 4 +- .../extensions/resources/ProcessResource.java | 7 +- .../extensions/resources/OsResourceTest.java | 22 +- .../resources/ProcessResourceTest.java | 17 +- .../extensions/trace/testbed/TestUtils.java | 31 +-- .../nestedcallbacks/NestedCallbacksTest.java | 3 +- .../PromisePropagationTest.java | 4 +- .../extensions/zpages/TracezZPageHandler.java | 37 +-- 92 files changed, 1502 insertions(+), 1923 deletions(-) create mode 100644 api/src/main/java/io/opentelemetry/common/RawAttributeConsumer.java delete mode 100644 api/src/main/java/io/opentelemetry/trace/attributes/BooleanAttributeSetter.java delete mode 100644 api/src/main/java/io/opentelemetry/trace/attributes/DoubleAttributeSetter.java delete mode 100644 api/src/main/java/io/opentelemetry/trace/attributes/LongAttributeSetter.java delete mode 100644 api/src/main/java/io/opentelemetry/trace/attributes/StringAttributeSetter.java delete mode 100644 api/src/test/java/io/opentelemetry/trace/attributes/BooleanAttributeSetterTest.java delete mode 100644 api/src/test/java/io/opentelemetry/trace/attributes/DoubleAttributeSetterTest.java delete mode 100644 api/src/test/java/io/opentelemetry/trace/attributes/LongAttributeSetterTest.java delete mode 100644 api/src/test/java/io/opentelemetry/trace/attributes/SemanticAttributesTest.java delete mode 100644 api/src/test/java/io/opentelemetry/trace/attributes/StringAttributeSetterTest.java diff --git a/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java b/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java index 8196ef2261e..5f0b2598261 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java @@ -16,10 +16,35 @@ package io.opentelemetry.common; +import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; +import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.LongKey; +import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; +import java.util.List; + /** * Convenience interface for consuming {@link ReadableAttributes}. * * @since 0.9.0 */ -public interface AttributeConsumer - extends ReadableKeyValuePairs.KeyValueConsumer {} +public interface AttributeConsumer { + void consume(StringKey key, String value); + + void consume(BooleanKey key, boolean value); + + void consume(DoubleKey key, double value); + + void consume(LongKey key, long value); + + void consume(StringArrayKey key, List value); + + void consume(BooleanArrayKey key, List value); + + void consume(DoubleArrayKey key, List value); + + void consume(LongArrayKey key, List value); +} diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKey.java b/api/src/main/java/io/opentelemetry/common/AttributeKey.java index 36541ece092..ea7d91f9f2c 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeKey.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeKey.java @@ -25,4 +25,7 @@ public interface AttributeKey { /** Returns the underlying String representation of the key. */ String get(); + + /** Returns the type of attribute for this key. Useful for building switch statements. */ + AttributeValue.Type getType(); } diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java index be0ce922641..760cffce9d4 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java @@ -18,7 +18,7 @@ import java.util.List; -class AttributeKeyImpl implements AttributeKey { +public abstract class AttributeKeyImpl implements AttributeKey { private final String key; AttributeKeyImpl(String key) { @@ -30,51 +30,147 @@ public String get() { return key; } - static class StringKey extends AttributeKeyImpl { - public StringKey(String key) { + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof AttributeKeyImpl)) { + return false; + } + + AttributeKeyImpl that = (AttributeKeyImpl) o; + + return key != null ? key.equals(that.key) : that.key == null; + } + + @Override + public int hashCode() { + return key != null ? key.hashCode() : 0; + } + + @Override + public String toString() { + return "AttributeKeyImpl{" + "key='" + key + '\'' + '}'; + } + + public static StringKey stringKey(String key) { + return new StringKey(key); + } + + public static class StringKey extends AttributeKeyImpl { + private StringKey(String key) { super(key); } + + @Override + public AttributeValue.Type getType() { + return AttributeValue.Type.STRING; + } } - static class BooleanKey extends AttributeKeyImpl { - BooleanKey(String key) { + public static BooleanKey booleanKey(String key) { + return new BooleanKey(key); + } + + public static class BooleanKey extends AttributeKeyImpl { + private BooleanKey(String key) { super(key); } + + @Override + public AttributeValue.Type getType() { + return AttributeValue.Type.BOOLEAN; + } + } + + public static LongKey longKey(String key) { + return new LongKey(key); } - static class LongKey extends AttributeKeyImpl { - LongKey(String key) { + public static class LongKey extends AttributeKeyImpl { + private LongKey(String key) { super(key); } + + @Override + public AttributeValue.Type getType() { + return AttributeValue.Type.LONG; + } + } + + public static DoubleKey doubleKey(String key) { + return new DoubleKey(key); } - static class DoubleKey extends AttributeKeyImpl { - DoubleKey(String key) { + public static class DoubleKey extends AttributeKeyImpl { + private DoubleKey(String key) { super(key); } + + @Override + public AttributeValue.Type getType() { + return AttributeValue.Type.DOUBLE; + } } - static class StringArrayKey extends AttributeKeyImpl> { - public StringArrayKey(String key) { + public static StringArrayKey stringArrayKey(String key) { + return new StringArrayKey(key); + } + + public static class StringArrayKey extends AttributeKeyImpl> { + private StringArrayKey(String key) { super(key); } + + @Override + public AttributeValue.Type getType() { + return AttributeValue.Type.STRING_ARRAY; + } + } + + public static BooleanArrayKey booleanArrayKey(String key) { + return new BooleanArrayKey(key); } - static class BooleanArrayKey extends AttributeKeyImpl> { - BooleanArrayKey(String key) { + public static class BooleanArrayKey extends AttributeKeyImpl> { + private BooleanArrayKey(String key) { super(key); } + + @Override + public AttributeValue.Type getType() { + return AttributeValue.Type.BOOLEAN_ARRAY; + } } - static class LongArrayKey extends AttributeKeyImpl> { - LongArrayKey(String key) { + public static LongArrayKey longArrayKey(String key) { + return new LongArrayKey(key); + } + + public static class LongArrayKey extends AttributeKeyImpl> { + private LongArrayKey(String key) { super(key); } + + @Override + public AttributeValue.Type getType() { + return AttributeValue.Type.LONG_ARRAY; + } + } + + public static DoubleArrayKey doubleArrayKey(String key) { + return new DoubleArrayKey(key); } - static class DoubleArrayKey extends AttributeKeyImpl> { - DoubleArrayKey(String key) { + public static class DoubleArrayKey extends AttributeKeyImpl> { + private DoubleArrayKey(String key) { super(key); } + + @Override + public AttributeValue.Type getType() { + return AttributeValue.Type.DOUBLE_ARRAY; + } } } diff --git a/api/src/main/java/io/opentelemetry/common/Attributes.java b/api/src/main/java/io/opentelemetry/common/Attributes.java index a179358a1de..dad3d04f8f1 100644 --- a/api/src/main/java/io/opentelemetry/common/Attributes.java +++ b/api/src/main/java/io/opentelemetry/common/Attributes.java @@ -16,14 +16,26 @@ package io.opentelemetry.common; -import static io.opentelemetry.common.AttributeValue.arrayAttributeValue; -import static io.opentelemetry.common.AttributeValue.booleanAttributeValue; -import static io.opentelemetry.common.AttributeValue.doubleAttributeValue; -import static io.opentelemetry.common.AttributeValue.longAttributeValue; -import static io.opentelemetry.common.AttributeValue.stringAttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import com.google.auto.value.AutoValue; +import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; +import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.LongKey; +import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; import java.util.ArrayList; +import java.util.Arrays; import java.util.Iterator; import java.util.List; import javax.annotation.concurrent.Immutable; @@ -33,8 +45,9 @@ * *

The keys are {@link String}s and the values are {@link AttributeValue} instances. */ +@SuppressWarnings("rawtypes") @Immutable -public abstract class Attributes extends ImmutableKeyValuePairs +public abstract class Attributes extends ImmutableKeyValuePairs implements ReadableAttributes { private static final Attributes EMPTY = Attributes.newBuilder().build(); @@ -52,13 +65,55 @@ public Builder toBuilder() { } } + @SuppressWarnings("unchecked") + @Override + public T get(AttributeKey key) { + return (T) super.get(key); + } + + @SuppressWarnings("unchecked") + @Override + public void forEachRaw(RawAttributeConsumer consumer) { + List data = data(); + for (int i = 0; i < data.size(); i += 2) { + consumer.consume((AttributeKey) data.get(i), data.get(i + 1)); + } + } + + @SuppressWarnings("unchecked") + @Override + public void forEach(AttributeConsumer consumer) { + List data = data(); + for (int i = 0; i < data.size(); i += 2) { + Object key = data.get(i); + Object value = data.get(i + 1); + if (key instanceof StringKey) { + consumer.consume((StringKey) key, (String) value); + } else if (key instanceof BooleanKey) { + consumer.consume((BooleanKey) key, (boolean) value); + } else if (key instanceof LongKey) { + consumer.consume((LongKey) key, (long) value); + } else if (key instanceof DoubleKey) { + consumer.consume((DoubleKey) key, (double) value); + } else if (key instanceof StringArrayKey) { + consumer.consume((StringArrayKey) key, (List) value); + } else if (key instanceof BooleanArrayKey) { + consumer.consume((BooleanArrayKey) key, (List) value); + } else if (key instanceof LongArrayKey) { + consumer.consume((LongArrayKey) key, (List) value); + } else if (key instanceof DoubleArrayKey) { + consumer.consume((DoubleArrayKey) key, (List) value); + } + } + } + /** Returns a {@link Attributes} instance with no attributes. */ public static Attributes empty() { return EMPTY; } /** Returns a {@link Attributes} instance with a single key-value pair. */ - public static Attributes of(String key, AttributeValue value) { + public static Attributes of(AttributeKey key, T value) { return sortAndFilterToAttributes(key, value); } @@ -66,8 +121,8 @@ public static Attributes of(String key, AttributeValue value) { * Returns a {@link Attributes} instance with two key-value pairs. Order of the keys is not * preserved. Duplicate keys will be removed. */ - public static Attributes of( - String key1, AttributeValue value1, String key2, AttributeValue value2) { + public static Attributes of( + AttributeKey key1, T value1, AttributeKey key2, U value2) { return sortAndFilterToAttributes(key1, value1, key2, value2); } @@ -75,13 +130,13 @@ public static Attributes of( * Returns a {@link Attributes} instance with three key-value pairs. Order of the keys is not * preserved. Duplicate keys will be removed. */ - public static Attributes of( - String key1, - AttributeValue value1, - String key2, - AttributeValue value2, - String key3, - AttributeValue value3) { + public static Attributes of( + AttributeKey key1, + T value1, + AttributeKey key2, + U value2, + AttributeKey key3, + V value3) { return sortAndFilterToAttributes(key1, value1, key2, value2, key3, value3); } @@ -89,15 +144,15 @@ public static Attributes of( * Returns a {@link Attributes} instance with four key-value pairs. Order of the keys is not * preserved. Duplicate keys will be removed. */ - public static Attributes of( - String key1, - AttributeValue value1, - String key2, - AttributeValue value2, - String key3, - AttributeValue value3, - String key4, - AttributeValue value4) { + public static Attributes of( + AttributeKey key1, + T value1, + AttributeKey key2, + U value2, + AttributeKey key3, + V value3, + AttributeKey key4, + W value4) { return sortAndFilterToAttributes(key1, value1, key2, value2, key3, value3, key4, value4); } @@ -105,17 +160,17 @@ public static Attributes of( * Returns a {@link Attributes} instance with five key-value pairs. Order of the keys is not * preserved. Duplicate keys will be removed. */ - public static Attributes of( - String key1, - AttributeValue value1, - String key2, - AttributeValue value2, - String key3, - AttributeValue value3, - String key4, - AttributeValue value4, - String key5, - AttributeValue value5) { + public static Attributes of( + AttributeKey key1, + T value1, + AttributeKey key2, + U value2, + AttributeKey key3, + V value3, + AttributeKey key4, + W value4, + AttributeKey key5, + X value5) { return sortAndFilterToAttributes( key1, value1, key2, value2, @@ -139,7 +194,42 @@ public static Builder newBuilder(ReadableAttributes attributes) { attributes.forEach( new AttributeConsumer() { @Override - public void consume(String key, AttributeValue value) { + public void consume(StringKey key, String value) { + builder.setAttribute(key, value); + } + + @Override + public void consume(BooleanKey key, boolean value) { + builder.setAttribute(key, value); + } + + @Override + public void consume(DoubleKey key, double value) { + builder.setAttribute(key, value); + } + + @Override + public void consume(LongKey key, long value) { + builder.setAttribute(key, value); + } + + @Override + public void consume(StringArrayKey key, List value) { + builder.setAttribute(key, value); + } + + @Override + public void consume(BooleanArrayKey key, List value) { + builder.setAttribute(key, value); + } + + @Override + public void consume(DoubleArrayKey key, List value) { + builder.setAttribute(key, value); + } + + @Override + public void consume(LongArrayKey key, List value) { builder.setAttribute(key, value); } }); @@ -174,15 +264,15 @@ public Attributes build() { * * @return this Builder */ - public Builder setAttribute(String key, AttributeValue value) { - if (key == null || key.length() == 0) { + public Builder setAttribute(AttributeKey key, T value) { + if (key == null || key.get().length() == 0) { return this; } - if (value == null || value.isNull()) { + if (value == null) { // Remove key/value pairs Iterator itr = data.iterator(); while (itr.hasNext()) { - String k = (String) itr.next(); + AttributeKey k = (AttributeKey) itr.next(); if (key.equals(k)) { // delete key and value itr.remove(); @@ -206,8 +296,7 @@ public Builder setAttribute(String key, AttributeValue value) { * @return this Builder */ public Builder setAttribute(String key, String value) { - AttributeValue v = stringAttributeValue(value); - return setAttribute(key, v); + return setAttribute(stringKey(key), value); } /** @@ -216,8 +305,7 @@ public Builder setAttribute(String key, String value) { * @return this Builder */ public Builder setAttribute(String key, long value) { - AttributeValue v = longAttributeValue(value); - return setAttribute(key, v); + return setAttribute(longKey(key), value); } /** @@ -226,8 +314,7 @@ public Builder setAttribute(String key, long value) { * @return this Builder */ public Builder setAttribute(String key, double value) { - AttributeValue v = doubleAttributeValue(value); - return setAttribute(key, v); + return setAttribute(doubleKey(key), value); } /** @@ -236,8 +323,7 @@ public Builder setAttribute(String key, double value) { * @return this Builder */ public Builder setAttribute(String key, boolean value) { - AttributeValue v = booleanAttributeValue(value); - return setAttribute(key, v); + return setAttribute(booleanKey(key), value); } /** @@ -246,8 +332,7 @@ public Builder setAttribute(String key, boolean value) { * @return this Builder */ public Builder setAttribute(String key, String... value) { - AttributeValue v = arrayAttributeValue(value); - return setAttribute(key, v); + return setAttribute(stringArrayKey(key), Arrays.asList(value)); } /** @@ -256,8 +341,7 @@ public Builder setAttribute(String key, String... value) { * @return this Builder */ public Builder setAttribute(String key, Long... value) { - AttributeValue v = arrayAttributeValue(value); - return setAttribute(key, v); + return setAttribute(longArrayKey(key), Arrays.asList(value)); } /** @@ -266,8 +350,7 @@ public Builder setAttribute(String key, Long... value) { * @return this Builder */ public Builder setAttribute(String key, Double... value) { - AttributeValue v = arrayAttributeValue(value); - return setAttribute(key, v); + return setAttribute(doubleArrayKey(key), Arrays.asList(value)); } /** @@ -276,8 +359,7 @@ public Builder setAttribute(String key, Double... value) { * @return this Builder */ public Builder setAttribute(String key, Boolean... value) { - AttributeValue v = arrayAttributeValue(value); - return setAttribute(key, v); + return setAttribute(booleanArrayKey(key), Arrays.asList(value)); } } } diff --git a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java index 15047f5f20c..6dc7fce713d 100644 --- a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java +++ b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java @@ -61,7 +61,7 @@ public void forEach(KeyValueConsumer consumer) { @Nullable @SuppressWarnings("unchecked") - public V get(String key) { + public V get(K key) { for (int i = 0; i < data().size(); i += 2) { if (key.equals(data().get(i))) { return (V) data().get(i + 1); diff --git a/api/src/main/java/io/opentelemetry/common/RawAttributeConsumer.java b/api/src/main/java/io/opentelemetry/common/RawAttributeConsumer.java new file mode 100644 index 00000000000..135f6b83217 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/common/RawAttributeConsumer.java @@ -0,0 +1,21 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.common; + +public interface RawAttributeConsumer { + void consume(AttributeKey key, T value); +} diff --git a/api/src/main/java/io/opentelemetry/common/ReadableAttributes.java b/api/src/main/java/io/opentelemetry/common/ReadableAttributes.java index 59f48b79876..d14fcb43867 100644 --- a/api/src/main/java/io/opentelemetry/common/ReadableAttributes.java +++ b/api/src/main/java/io/opentelemetry/common/ReadableAttributes.java @@ -21,6 +21,18 @@ * *

See {@link Attributes} for the public API implementation. */ -public interface ReadableAttributes extends ReadableKeyValuePairs { - AttributeValue get(String key); +public interface ReadableAttributes { + T get(AttributeKey key); + + /** The number of attributes contained in this. */ + int size(); + + /** Whether there are any attributes contained in this. */ + boolean isEmpty(); + + /** Iterates over all the key-value pairs of attributes contained by this instance. */ + void forEach(AttributeConsumer consumer); + + /** Iterates over all the key-value pairs of attributes contained by this instance. */ + void forEachRaw(RawAttributeConsumer consumer); } diff --git a/api/src/main/java/io/opentelemetry/internal/StringUtils.java b/api/src/main/java/io/opentelemetry/internal/StringUtils.java index a4947c422f9..18a63594c5a 100644 --- a/api/src/main/java/io/opentelemetry/internal/StringUtils.java +++ b/api/src/main/java/io/opentelemetry/internal/StringUtils.java @@ -16,7 +16,8 @@ package io.opentelemetry.internal; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; +import io.opentelemetry.common.AttributeKeyImpl; import java.util.List; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; @@ -71,17 +72,18 @@ public static boolean isValidMetricName(String metricName) { * * @throws IllegalArgumentException if limit is zero or negative */ - public static AttributeValue truncateToSize(AttributeValue value, int limit) { + @SuppressWarnings("unchecked") + public static T truncateToSize(AttributeKey key, T value, int limit) { Utils.checkArgument(limit > 0, "attribute value limit must be positive, got %d", limit); if (value == null - || (value.getType() != AttributeValue.Type.STRING - && value.getType() != AttributeValue.Type.STRING_ARRAY)) { + || (!(key instanceof AttributeKeyImpl.StringKey) + && !(key instanceof AttributeKeyImpl.StringArrayKey))) { return value; } - if (value.getType() == AttributeValue.Type.STRING_ARRAY) { - List strings = value.getStringArrayValue(); + if (key instanceof AttributeKeyImpl.StringArrayKey) { + List strings = (List) value; if (strings.isEmpty()) { return value; } @@ -92,14 +94,10 @@ public static AttributeValue truncateToSize(AttributeValue value, int limit) { newStrings[i] = truncateToSize(string, limit); } - return AttributeValue.arrayAttributeValue(newStrings); + return (T) newStrings; } - String string = value.getStringValue(); - // Don't allocate new AttributeValue if not needed - return (string == null || string.length() <= limit) - ? value - : AttributeValue.stringAttributeValue(string.substring(0, limit)); + return (T) truncateToSize((String) value, limit); } @Nullable diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java index 23a3a24faa5..63ae327b35d 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultSpan.java @@ -16,7 +16,7 @@ package io.opentelemetry.trace; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; import javax.annotation.concurrent.Immutable; @@ -73,7 +73,7 @@ public void setAttribute(String key, double value) {} public void setAttribute(String key, boolean value) {} @Override - public void setAttribute(String key, AttributeValue value) {} + public void setAttribute(AttributeKey key, T value) {} @Override public void addEvent(String name) {} diff --git a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java index 97f12c0de16..5a237c63e6b 100644 --- a/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java +++ b/api/src/main/java/io/opentelemetry/trace/DefaultTracer.java @@ -17,7 +17,7 @@ package io.opentelemetry.trace; import io.grpc.Context; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.context.Scope; import io.opentelemetry.internal.Utils; @@ -146,7 +146,7 @@ public NoopSpanBuilder setAttribute(String key, boolean value) { } @Override - public NoopSpanBuilder setAttribute(String key, AttributeValue value) { + public NoopSpanBuilder setAttribute(AttributeKey key, T value) { Utils.checkNotNull(key, "key"); Utils.checkNotNull(value, "value"); return this; diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index ae775248119..7e8fabcfc81 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -17,7 +17,7 @@ package io.opentelemetry.trace; import io.grpc.Context; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; @@ -132,7 +132,7 @@ enum Kind { * @param value the value for this attribute. * @since 0.1.0 */ - void setAttribute(String key, AttributeValue value); + void setAttribute(AttributeKey key, T value); /** * Adds an event to the {@link Span}. The timestamp of the {@link Event} will be the current time. @@ -556,7 +556,7 @@ interface Builder { * @throws NullPointerException if {@code value} is {@code null}. * @since 0.3.0 */ - Builder setAttribute(String key, AttributeValue value); + Builder setAttribute(AttributeKey key, T value); /** * Sets the {@link Span.Kind} for the newly created {@code Span}. If not called, the diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/BooleanAttributeSetter.java b/api/src/main/java/io/opentelemetry/trace/attributes/BooleanAttributeSetter.java deleted file mode 100644 index c8405b5a907..00000000000 --- a/api/src/main/java/io/opentelemetry/trace/attributes/BooleanAttributeSetter.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace.attributes; - -import io.opentelemetry.common.Attributes; -import io.opentelemetry.trace.Span; -import javax.annotation.concurrent.Immutable; - -/** Defines the behavior for a span attribute with boolean values. */ -@Immutable -public final class BooleanAttributeSetter { - - /** - * Returns a new attribute setter. - * - * @param attributeKey the attribute name - * @return the setter object - */ - public static BooleanAttributeSetter create(String attributeKey) { - return new BooleanAttributeSetter(attributeKey); - } - - private final String attributeKey; - - private BooleanAttributeSetter(String attributeKey) { - if (attributeKey == null || attributeKey.length() == 0) { - throw new IllegalArgumentException("attributeKey cannot be empty"); - } - this.attributeKey = attributeKey; - } - - /** - * Returns the attribute name. - * - * @return the attribute map key - */ - public String key() { - return attributeKey; - } - - /** - * Sets the attribute on the provided span. - * - * @param span the span to add the attribute to - * @param value the value for this attribute - */ - public void set(Span span, boolean value) { - span.setAttribute(key(), value); - } - - /** - * Sets the attribute on the provided span builder. - * - * @param spanBuilder the span builder to add the attribute to - * @param value the value for this attribute - */ - public void set(Span.Builder spanBuilder, boolean value) { - spanBuilder.setAttribute(key(), value); - } - - /** - * Sets the attribute on the provided {@link Attributes.Builder}. - * - * @param attributesBuilder the attributes builder to add the attribute to - * @param value the value for this attribute - */ - public void set(Attributes.Builder attributesBuilder, boolean value) { - attributesBuilder.setAttribute(key(), value); - } - - @Override - public String toString() { - return key(); - } -} diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/DoubleAttributeSetter.java b/api/src/main/java/io/opentelemetry/trace/attributes/DoubleAttributeSetter.java deleted file mode 100644 index dd45e106f51..00000000000 --- a/api/src/main/java/io/opentelemetry/trace/attributes/DoubleAttributeSetter.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace.attributes; - -import io.opentelemetry.common.Attributes; -import io.opentelemetry.trace.Span; -import javax.annotation.concurrent.Immutable; - -/** Defines the behavior for a span attribute with double values. */ -@Immutable -public final class DoubleAttributeSetter { - - /** - * Returns a new attribute setter. - * - * @param attributeKey the attribute name - * @return the setter object - */ - public static DoubleAttributeSetter create(String attributeKey) { - return new DoubleAttributeSetter(attributeKey); - } - - private final String attributeKey; - - private DoubleAttributeSetter(String attributeKey) { - if (attributeKey == null || attributeKey.length() == 0) { - throw new IllegalArgumentException("attributeKey cannot be empty"); - } - this.attributeKey = attributeKey; - } - - /** - * Returns the attribute name. - * - * @return the attribute map key - */ - public String key() { - return attributeKey; - } - - /** - * Sets the attribute on the provided span. - * - * @param span the span to add the attribute to - * @param value the value for this attribute - */ - public void set(Span span, double value) { - span.setAttribute(key(), value); - } - - /** - * Sets the attribute on the provided span builder. - * - * @param spanBuilder the span builder to add the attribute to - * @param value the value for this attribute - */ - public void set(Span.Builder spanBuilder, double value) { - spanBuilder.setAttribute(key(), value); - } - - /** - * Sets the attribute on the provided {@link Attributes.Builder}. - * - * @param attributesBuilder the attributes builder to add the attribute to - * @param value the value for this attribute - */ - public void set(Attributes.Builder attributesBuilder, double value) { - attributesBuilder.setAttribute(key(), value); - } - - @Override - public String toString() { - return key(); - } -} diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/LongAttributeSetter.java b/api/src/main/java/io/opentelemetry/trace/attributes/LongAttributeSetter.java deleted file mode 100644 index 4a0b0019771..00000000000 --- a/api/src/main/java/io/opentelemetry/trace/attributes/LongAttributeSetter.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace.attributes; - -import io.opentelemetry.common.Attributes; -import io.opentelemetry.trace.Span; -import javax.annotation.concurrent.Immutable; - -/** Defines the behavior for a span attribute with long values. */ -@Immutable -public final class LongAttributeSetter { - - /** - * Returns a new attribute setter. - * - * @param attributeKey the attribute name - * @return the setter object - */ - public static LongAttributeSetter create(String attributeKey) { - return new LongAttributeSetter(attributeKey); - } - - private final String attributeKey; - - private LongAttributeSetter(String attributeKey) { - if (attributeKey == null || attributeKey.length() == 0) { - throw new IllegalArgumentException("attributeKey cannot be empty"); - } - this.attributeKey = attributeKey; - } - - /** - * Returns the attribute name. - * - * @return the attribute map key - */ - public String key() { - return attributeKey; - } - - /** - * Sets the attribute on the provided span. - * - * @param span the span to add the attribute to - * @param value the value for this attribute - */ - public void set(Span span, long value) { - span.setAttribute(key(), value); - } - - /** - * Sets the attribute on the provided span builder. - * - * @param spanBuilder the span builder to add the attribute to - * @param value the value for this attribute - */ - public void set(Span.Builder spanBuilder, long value) { - spanBuilder.setAttribute(key(), value); - } - - /** - * Sets the attribute on the provided {@link Attributes.Builder}. - * - * @param attributesBuilder the attributes builder to add the attribute to - * @param value the value for this attribute - */ - public void set(Attributes.Builder attributesBuilder, long value) { - attributesBuilder.setAttribute(key(), value); - } - - @Override - public String toString() { - return key(); - } -} diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java b/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java index 737e8c6c594..d4b0cb72742 100644 --- a/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java +++ b/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java @@ -16,6 +16,14 @@ package io.opentelemetry.trace.attributes; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; + +import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; +import io.opentelemetry.common.AttributeKeyImpl.LongKey; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; + /** * Defines constants for all attribute names defined in the OpenTelemetry Semantic Conventions * specifications. @@ -27,129 +35,104 @@ public final class SemanticAttributes { /** Transport protocol used. */ - public static final StringAttributeSetter NET_TRANSPORT = - StringAttributeSetter.create("net.transport"); + public static final StringKey NET_TRANSPORT = stringKey("net.transport"); /** Remote address of the peer (dotted decimal for IPv4 or RFC5952 for IPv6). */ - public static final StringAttributeSetter NET_PEER_IP = - StringAttributeSetter.create("net.peer.ip"); + public static final StringKey NET_PEER_IP = stringKey("net.peer.ip"); /** Remote port number as an integer. E.g., 80. */ - public static final LongAttributeSetter NET_PEER_PORT = - LongAttributeSetter.create("net.peer.port"); + public static final LongKey NET_PEER_PORT = longKey("net.peer.port"); /** Remote hostname or similar. */ - public static final StringAttributeSetter NET_PEER_NAME = - StringAttributeSetter.create("net.peer.name"); + public static final StringKey NET_PEER_NAME = stringKey("net.peer.name"); /** Like net.peer.ip but for the host IP. Useful in case of a multi-IP host. */ - public static final StringAttributeSetter NET_HOST_IP = - StringAttributeSetter.create("net.host.ip"); + public static final StringKey NET_HOST_IP = stringKey("net.host.ip"); /** Like net.peer.port but for the host port. */ - public static final LongAttributeSetter NET_HOST_PORT = - LongAttributeSetter.create("net.host.port"); + public static final LongKey NET_HOST_PORT = longKey("net.host.port"); /** Local hostname or similar. */ - public static final StringAttributeSetter NET_HOST_NAME = - StringAttributeSetter.create("net.host.name"); + public static final StringKey NET_HOST_NAME = stringKey("net.host.name"); /** Logical name of a remote service. */ - public static final StringAttributeSetter PEER_SERVICE = - StringAttributeSetter.create("peer.service"); + public static final StringKey PEER_SERVICE = stringKey("peer.service"); /** * Username or client_id extracted from the access token or Authorization header in the inbound * request from outside the system. */ - public static final StringAttributeSetter ENDUSER_ID = StringAttributeSetter.create("enduser.id"); + public static final StringKey ENDUSER_ID = stringKey("enduser.id"); /** * Actual/assumed role the client is making the request under extracted from token or application * security context. */ - public static final StringAttributeSetter ENDUSER_ROLE = - StringAttributeSetter.create("enduser.role"); + public static final StringKey ENDUSER_ROLE = stringKey("enduser.role"); /** * Scopes or granted authorities the client currently possesses extracted from token or * application security context. The value would come from the scope associated with an OAuth 2.0 * Access Token or an attribute value in a SAML 2.0 Assertion. */ - public static final StringAttributeSetter ENDUSER_SCOPE = - StringAttributeSetter.create("enduser.scope"); + public static final StringKey ENDUSER_SCOPE = stringKey("enduser.scope"); /** HTTP request method. E.g. "GET". */ - public static final StringAttributeSetter HTTP_METHOD = - StringAttributeSetter.create("http.method"); + public static final StringKey HTTP_METHOD = stringKey("http.method"); /** Full HTTP request URL in the form scheme://host[:port]/path?query[#fragment]. */ - public static final StringAttributeSetter HTTP_URL = StringAttributeSetter.create("http.url"); + public static final StringKey HTTP_URL = stringKey("http.url"); /** The full request target as passed in a HTTP request line or equivalent. */ - public static final StringAttributeSetter HTTP_TARGET = - StringAttributeSetter.create("http.target"); + public static final StringKey HTTP_TARGET = stringKey("http.target"); /** The value of the HTTP host header. */ - public static final StringAttributeSetter HTTP_HOST = StringAttributeSetter.create("http.host"); + public static final StringKey HTTP_HOST = stringKey("http.host"); /** The URI scheme identifying the used protocol: "http" or "https". */ - public static final StringAttributeSetter HTTP_SCHEME = - StringAttributeSetter.create("http.scheme"); + public static final StringKey HTTP_SCHEME = stringKey("http.scheme"); /** HTTP response status code. E.g. 200 (integer) If and only if one was received/sent. */ - public static final LongAttributeSetter HTTP_STATUS_CODE = - LongAttributeSetter.create("http.status_code"); + public static final LongKey HTTP_STATUS_CODE = longKey("http.status_code"); /** HTTP reason phrase. E.g. "OK" */ - public static final StringAttributeSetter HTTP_STATUS_TEXT = - StringAttributeSetter.create("http.status_text"); + public static final StringKey HTTP_STATUS_TEXT = stringKey("http.status_text"); /** Kind of HTTP protocol used: "1.0", "1.1", "2", "SPDY" or "QUIC". */ - public static final StringAttributeSetter HTTP_FLAVOR = - StringAttributeSetter.create("http.flavor"); + public static final StringKey HTTP_FLAVOR = stringKey("http.flavor"); /** Value of the HTTP "User-Agent" header sent by the client. */ - public static final StringAttributeSetter HTTP_USER_AGENT = - StringAttributeSetter.create("http.user_agent"); + public static final StringKey HTTP_USER_AGENT = stringKey("http.user_agent"); /** The primary server name of the matched virtual host. Usually obtained via configuration. */ - public static final StringAttributeSetter HTTP_SERVER_NAME = - StringAttributeSetter.create("http.server_name"); + public static final StringKey HTTP_SERVER_NAME = stringKey("http.server_name"); /** The matched route (path template). */ - public static final StringAttributeSetter HTTP_ROUTE = StringAttributeSetter.create("http.route"); + public static final StringKey HTTP_ROUTE = stringKey("http.route"); /** The IP address of the original client behind all proxies, if known. */ - public static final StringAttributeSetter HTTP_CLIENT_IP = - StringAttributeSetter.create("http.client_ip"); + public static final StringKey HTTP_CLIENT_IP = stringKey("http.client_ip"); /** * The size of the request payload body, in bytes. For payloads using transport encoding, this is * the compressed size. */ - public static final LongAttributeSetter HTTP_REQUEST_CONTENT_LENGTH = - LongAttributeSetter.create("http.request_content_length"); + public static final LongKey HTTP_REQUEST_CONTENT_LENGTH = longKey("http.request_content_length"); /** * The size of the uncompressed request payload body, in bytes. Only set for requests that use * transport encoding. */ - public static final LongAttributeSetter HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED = - LongAttributeSetter.create("http.request_content_length_uncompressed"); + public static final LongKey HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED = + longKey("http.request_content_length_uncompressed"); /** * The size of the response payload body, in bytes. For payloads using transport encoding, this is * the compressed size. */ - public static final LongAttributeSetter HTTP_RESPONSE_CONTENT_LENGTH = - LongAttributeSetter.create("http.response_content_length"); + public static final LongKey HTTP_RESPONSE_CONTENT_LENGTH = + longKey("http.response_content_length"); /** * The size of the uncompressed response payload body, in bytes. Only set for responses that use * transport encoding. */ - public static final LongAttributeSetter HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED = - LongAttributeSetter.create("http.response_content_length_uncompressed"); + public static final LongKey HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED = + longKey("http.response_content_length_uncompressed"); /** A string identifying the remoting system, e.g., "grpc", "java_rmi" or "wcf". */ - public static final StringAttributeSetter RPC_SYSTEM = StringAttributeSetter.create("rpc.system"); + public static final StringKey RPC_SYSTEM = stringKey("rpc.system"); /** The full name of the service being called, including its package name, if applicable. */ - public static final StringAttributeSetter RPC_SERVICE = - StringAttributeSetter.create("rpc.service"); + public static final StringKey RPC_SERVICE = stringKey("rpc.service"); /* The name of the method being called, must be equal to the $method part in the span name */ - public static final StringAttributeSetter RPC_METHOD = StringAttributeSetter.create("rpc.method"); + public static final StringKey RPC_METHOD = stringKey("rpc.method"); /** The name of a gRPC span event to populate for each message sent / received. */ public static final String GRPC_MESSAGE_EVENT_NAME = "message"; /** gRPC span event attribute with value "SENT" or "RECEIVED". */ - public static final StringAttributeSetter GRPC_MESSAGE_TYPE = - StringAttributeSetter.create("message.type"); + public static final StringKey GRPC_MESSAGE_TYPE = stringKey("message.type"); /** gRPC span event attribute starting from 1 for each of sent messages and received messages. */ - public static final LongAttributeSetter GRPC_MESSAGE_ID = - LongAttributeSetter.create("message.id"); + public static final LongKey GRPC_MESSAGE_ID = longKey("message.id"); /** gRPC span event attribute for compressed size of a message. */ - public static final LongAttributeSetter GRPC_MESSAGE_COMPRESSED_SIZE = - LongAttributeSetter.create("message.compressed_size"); + public static final LongKey GRPC_MESSAGE_COMPRESSED_SIZE = longKey("message.compressed_size"); /** gRPC span event attribute for uncompressed size of a message. */ - public static final LongAttributeSetter GRPC_MESSAGE_UNCOMPRESSED_SIZE = - LongAttributeSetter.create("message.uncompressed_size"); + public static final LongKey GRPC_MESSAGE_UNCOMPRESSED_SIZE = longKey("message.uncompressed_size"); /** * An identifier for the database management system (DBMS) product being used. @@ -158,138 +141,118 @@ public final class SemanticAttributes { * href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/database.md#notes-and-well-known-identifiers-for-dbsystem">A * list of well-known identifiers */ - public static final StringAttributeSetter DB_SYSTEM = StringAttributeSetter.create("db.system"); + public static final StringKey DB_SYSTEM = stringKey("db.system"); /** Database name. */ - public static final StringAttributeSetter DB_NAME = StringAttributeSetter.create("db.name"); + public static final StringKey DB_NAME = stringKey("db.name"); /** * The connection string used to connect to the database. It's recommended to remove embedded * credentials. This will replace db.url. */ - public static final StringAttributeSetter DB_CONNECTION_STRING = - StringAttributeSetter.create("db.connection_string"); + public static final StringKey DB_CONNECTION_STRING = stringKey("db.connection_string"); /** Database statement for the given database type. */ - public static final StringAttributeSetter DB_STATEMENT = - StringAttributeSetter.create("db.statement"); + public static final StringKey DB_STATEMENT = stringKey("db.statement"); /** Database operation that is being executed. */ - public static final StringAttributeSetter DB_OPERATION = - StringAttributeSetter.create("db.operation"); + public static final StringKey DB_OPERATION = stringKey("db.operation"); /** Username for accessing database. */ - public static final StringAttributeSetter DB_USER = StringAttributeSetter.create("db.user"); + public static final StringKey DB_USER = stringKey("db.user"); /** * For db.system == mssql, the instance name connecting to. This name is used to determine the * port of a named instance. When set, {@link #NET_PEER_PORT} is not required, but recommended * when connecting to a non-standard port. */ - public static final StringAttributeSetter MSSQL_SQL_SERVER = - StringAttributeSetter.create("db.mssql.instance_name"); + public static final StringKey MSSQL_SQL_SERVER = stringKey("db.mssql.instance_name"); /** * For JDBC clients, the fully-qualified class name of the Java Database Connectivity (JDBC) * driver used to connect, e.g. "org.postgresql.Driver" or * "com.microsoft.sqlserver.jdbc.SQLServerDriver". */ - public static final StringAttributeSetter JDBC_DRIVER_CLASSNAME = - StringAttributeSetter.create("db.jdbc.driver_classname"); + public static final StringKey JDBC_DRIVER_CLASSNAME = stringKey("db.jdbc.driver_classname"); /** * For db.system == cassandra, the name of the keyspace being accessed. To be used instead of the * generic db.name attribute. */ - public static final StringAttributeSetter CASSANDRA_KEYSPACE = - StringAttributeSetter.create("db.cassandra.keyspace"); + public static final StringKey CASSANDRA_KEYSPACE = stringKey("db.cassandra.keyspace"); /** * For db.system == hbase, the namespace being accessed. To be used instead of the generic db.name * attribute. */ - public static final StringAttributeSetter HBASE_NAMESPACE = - StringAttributeSetter.create("db.hbase.namespace"); + public static final StringKey HBASE_NAMESPACE = stringKey("db.hbase.namespace"); /** * For db.system == redis, the index of the database being accessed as used in the SELECT command, * provided as an integer. To be used instead of the generic db.name attribute. */ - public static final StringAttributeSetter REDIS_DATABASE_INDEX = - StringAttributeSetter.create("db.redis.database_index"); + public static final StringKey REDIS_DATABASE_INDEX = stringKey("db.redis.database_index"); /** * For db.system == mongodb, the collection being accessed within the database stated in db.name */ - public static final StringAttributeSetter MONGODB_COLLECTION = - StringAttributeSetter.create("db.mongodb.collection"); + public static final StringKey MONGODB_COLLECTION = stringKey("db.mongodb.collection"); /** A string identifying the messaging system such as kafka, rabbitmq or activemq. */ - public static final StringAttributeSetter MESSAGING_SYSTEM = - StringAttributeSetter.create("messaging.system"); + public static final StringKey MESSAGING_SYSTEM = stringKey("messaging.system"); /** * The message destination name, e.g. MyQueue or MyTopic. This might be equal to the span name but * is required nevertheless */ - public static final StringAttributeSetter MESSAGING_DESTINATION = - StringAttributeSetter.create("messaging.destination"); + public static final StringKey MESSAGING_DESTINATION = stringKey("messaging.destination"); /** The kind of message destination. Either queue or topic. */ - public static final StringAttributeSetter MESSAGING_DESTINATION_KIND = - StringAttributeSetter.create("messaging.destination_kind"); + public static final StringKey MESSAGING_DESTINATION_KIND = + stringKey("messaging.destination_kind"); /** A boolean that is true if the message destination is temporary. */ - public static final BooleanAttributeSetter MESSAGING_TEMP_DESTINATION = - BooleanAttributeSetter.create("messaging.temp_destination"); + public static final BooleanKey MESSAGING_TEMP_DESTINATION = + booleanKey("messaging.temp_destination"); /** The name of the transport protocol such as AMQP or MQTT. */ - public static final StringAttributeSetter MESSAGING_PROTOCOL = - StringAttributeSetter.create("messaging.protocol"); + public static final StringKey MESSAGING_PROTOCOL = stringKey("messaging.protocol"); /** The version of the transport protocol such as 0.9.1. */ - public static final StringAttributeSetter MESSAGING_PROTOCOL_VERSION = - StringAttributeSetter.create("messaging.protocol_version"); + public static final StringKey MESSAGING_PROTOCOL_VERSION = + stringKey("messaging.protocol_version"); /** * Connection string such as tibjmsnaming://localhost:7222 or * https://queue.amazonaws.com/80398EXAMPLE/MyQueue */ - public static final StringAttributeSetter MESSAGING_URL = - StringAttributeSetter.create("messaging.url"); + public static final StringKey MESSAGING_URL = stringKey("messaging.url"); /** * A value used by the messaging system as an identifier for the message, represented as a string. */ - public static final StringAttributeSetter MESSAGING_MESSAGE_ID = - StringAttributeSetter.create("messaging.message_id"); + public static final StringKey MESSAGING_MESSAGE_ID = stringKey("messaging.message_id"); /** * A value identifying the conversation to which the message belongs, represented as a string. * Sometimes called "Correlation ID". */ - public static final StringAttributeSetter MESSAGING_CONVERSATION_ID = - StringAttributeSetter.create("messaging.conversation_id"); + public static final StringKey MESSAGING_CONVERSATION_ID = stringKey("messaging.conversation_id"); /** * The (uncompressed) size of the message payload in bytes. Also use this attribute if it is * unknown whether the compressed or uncompressed payload size is reported. */ - public static final LongAttributeSetter MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES = - LongAttributeSetter.create("messaging.message_payload_size_bytes"); + public static final LongKey MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES = + longKey("messaging.message_payload_size_bytes"); /** The compressed size of the message payload in bytes. */ - public static final LongAttributeSetter MESSAGING_MESSAGE_PAYLOAD_COMPRESSED_SIZE_BYTES = - LongAttributeSetter.create("messaging.message_payload_compressed_size_bytes"); + public static final LongKey MESSAGING_MESSAGE_PAYLOAD_COMPRESSED_SIZE_BYTES = + longKey("messaging.message_payload_compressed_size_bytes"); /** * A string identifying which part and kind of message consumption this span describes: either * "receive" or "process". If the operation is "send", this attribute must not be set: the * operation can be inferred from the span kind in that case. */ - public static final StringAttributeSetter MESSAGING_OPERATION = - StringAttributeSetter.create("messaging.operation"); + public static final StringKey MESSAGING_OPERATION = stringKey("messaging.operation"); /** The name of an {@link io.opentelemetry.trace.Event} describing an exception. */ public static final String EXCEPTION_EVENT_NAME = "exception"; /** The type of the exception, i.e., it's fully qualified name. */ - public static final StringAttributeSetter EXCEPTION_TYPE = - StringAttributeSetter.create("exception.type"); + public static final StringKey EXCEPTION_TYPE = stringKey("exception.type"); /** The exception message. */ - public static final StringAttributeSetter EXCEPTION_MESSAGE = - StringAttributeSetter.create("exception.message"); + public static final StringKey EXCEPTION_MESSAGE = stringKey("exception.message"); /** * A string representing the stacktrace of an exception, as produced by {@link * Throwable#printStackTrace()}. */ - public static final StringAttributeSetter EXCEPTION_STACKTRACE = - StringAttributeSetter.create("exception.stacktrace"); + public static final StringKey EXCEPTION_STACKTRACE = stringKey("exception.stacktrace"); /** Id of the thread that has started a span, as produced by {@link Thread#getId()}. */ - public static final LongAttributeSetter THREAD_ID = LongAttributeSetter.create("thread.id"); + public static final LongKey THREAD_ID = longKey("thread.id"); /** Name of the thread that has started a span, as produced by {@link Thread#getName()}. */ - public static final StringAttributeSetter THREAD_NAME = - StringAttributeSetter.create("thread.name"); + public static final StringKey THREAD_NAME = stringKey("thread.name"); /** Type of the trigger on which the function is executed. */ public static final StringAttributeSetter FAAS_TRIGGER = diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/StringAttributeSetter.java b/api/src/main/java/io/opentelemetry/trace/attributes/StringAttributeSetter.java deleted file mode 100644 index cb79a58cd42..00000000000 --- a/api/src/main/java/io/opentelemetry/trace/attributes/StringAttributeSetter.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace.attributes; - -import io.opentelemetry.common.Attributes; -import io.opentelemetry.trace.Span; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -/** Defines the behavior for a span attribute with string values. */ -@Immutable -public final class StringAttributeSetter { - - /** - * Returns a new attribute setter. - * - * @param attributeKey the attribute name - * @return the setter object - */ - public static StringAttributeSetter create(String attributeKey) { - return new StringAttributeSetter(attributeKey); - } - - private final String attributeKey; - - private StringAttributeSetter(String attributeKey) { - if (attributeKey == null || attributeKey.length() == 0) { - throw new IllegalArgumentException("attributeKey cannot be empty"); - } - this.attributeKey = attributeKey; - } - - /** - * Returns the attribute name. - * - * @return the attribute map key - */ - public String key() { - return attributeKey; - } - - /** - * Sets the attribute on the provided span. - * - * @param span the span to add the attribute to - * @param value the value for this attribute - */ - public void set(Span span, @Nullable String value) { - span.setAttribute(key(), value); - } - - /** - * Sets the attribute on the provided span builder. - * - * @param spanBuilder the span builder to add the attribute to - * @param value the value for this attribute - */ - public void set(Span.Builder spanBuilder, @Nullable String value) { - spanBuilder.setAttribute(key(), value); - } - - /** - * Sets the attribute on the provided {@link Attributes.Builder}. - * - * @param attributesBuilder the attributes builder to add the attribute to - * @param value the value for this attribute - */ - public void set(Attributes.Builder attributesBuilder, @Nullable String value) { - attributesBuilder.setAttribute(key(), value); - } - - @Override - public String toString() { - return key(); - } -} diff --git a/api/src/test/java/io/opentelemetry/common/AttributesTest.java b/api/src/test/java/io/opentelemetry/common/AttributesTest.java index a0ab498fc4b..1bb80c91427 100644 --- a/api/src/test/java/io/opentelemetry/common/AttributesTest.java +++ b/api/src/test/java/io/opentelemetry/common/AttributesTest.java @@ -16,45 +16,46 @@ package io.opentelemetry.common; -import static io.opentelemetry.common.AttributeValue.arrayAttributeValue; -import static io.opentelemetry.common.AttributeValue.booleanAttributeValue; -import static io.opentelemetry.common.AttributeValue.doubleAttributeValue; -import static io.opentelemetry.common.AttributeValue.longAttributeValue; -import static io.opentelemetry.common.AttributeValue.stringAttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import org.junit.jupiter.api.Test; /** Unit tests for {@link Attributes}s. */ +@SuppressWarnings("rawtypes") class AttributesTest { @Test void forEach() { - final Map entriesSeen = new HashMap<>(); + final Map entriesSeen = new HashMap<>(); - Attributes attributes = - Attributes.of( - "key1", stringAttributeValue("value1"), - "key2", AttributeValue.longAttributeValue(333)); + Attributes attributes = Attributes.of(stringKey("key1"), "value1", longKey("key2"), 333L); attributes.forEach(entriesSeen::put); assertThat(entriesSeen) - .containsExactly( - entry("key1", stringAttributeValue("value1")), entry("key2", longAttributeValue(333))); + .containsExactly(entry(stringKey("key1"), "value1"), entry(stringKey("key2"), 333L)); } @Test void forEach_singleAttribute() { - final Map entriesSeen = new HashMap<>(); + final Map entriesSeen = new HashMap<>(); - Attributes attributes = Attributes.of("key", stringAttributeValue("value")); + Attributes attributes = Attributes.of(stringKey("key"), "value"); attributes.forEach(entriesSeen::put); - assertThat(entriesSeen).containsExactly(entry("key", stringAttributeValue("value"))); + assertThat(entriesSeen).containsExactly(entry(stringKey("key"), "value")); } @Test @@ -69,34 +70,34 @@ void forEach_empty() { void orderIndependentEquality() { Attributes one = Attributes.of( - "key1", stringAttributeValue("value1"), - "key2", stringAttributeValue("value2")); + stringKey("key1"), "value1", + stringKey("key2"), "value2"); Attributes two = Attributes.of( - "key2", stringAttributeValue("value2"), - "key1", stringAttributeValue("value1")); + stringKey("key2"), "value2", + stringKey("key1"), "value1"); assertThat(one).isEqualTo(two); Attributes three = Attributes.of( - "key1", stringAttributeValue("value1"), - "key2", stringAttributeValue("value2"), - "", stringAttributeValue("empty"), - "key3", stringAttributeValue("value3"), - "key4", stringAttributeValue("value4")); + stringKey("key1"), "value1", + stringKey("key2"), "value2", + stringKey(""), "empty", + stringKey("key3"), "value3", + stringKey("key4"), "value4"); Attributes four = Attributes.of( null, - stringAttributeValue("null"), - "key2", - stringAttributeValue("value2"), - "key1", - stringAttributeValue("value1"), - "key4", - stringAttributeValue("value4"), - "key3", - stringAttributeValue("value3")); + "null", + stringKey("key2"), + "value2", + stringKey("key1"), + "value1", + stringKey("key4"), + "value4", + stringKey("key3"), + "value3"); assertThat(three).isEqualTo(four); } @@ -105,17 +106,16 @@ void orderIndependentEquality() { void deduplication() { Attributes one = Attributes.of( - "key1", stringAttributeValue("value1"), - "key1", stringAttributeValue("valueX")); - Attributes two = Attributes.of("key1", stringAttributeValue("value1")); + stringKey("key1"), "value1", + stringKey("key1"), "valueX"); + Attributes two = Attributes.of(stringKey("key1"), "value1"); assertThat(one).isEqualTo(two); } @Test void emptyAndNullKey() { - Attributes noAttributes = - Attributes.of("", stringAttributeValue("empty"), null, stringAttributeValue("null")); + Attributes noAttributes = Attributes.of(stringKey(""), "empty", null, "null"); assertThat(noAttributes.size()).isEqualTo(0); } @@ -133,10 +133,14 @@ void builder() { Attributes wantAttributes = Attributes.of( - "string", stringAttributeValue("value1"), - "long", longAttributeValue(100), - "double", doubleAttributeValue(33.44), - "boolean", booleanAttributeValue(false)); + stringKey("string"), + "value1", + longKey("long"), + 100L, + doubleKey("double"), + 33.44, + booleanKey("boolean"), + false); assertThat(attributes).isEqualTo(wantAttributes); Attributes.Builder newAttributes = Attributes.newBuilder(attributes); @@ -144,11 +148,16 @@ void builder() { assertThat(newAttributes.build()) .isEqualTo( Attributes.of( - "string", stringAttributeValue("value1"), - "long", longAttributeValue(100), - "double", doubleAttributeValue(33.44), - "boolean", booleanAttributeValue(false), - "newKey", stringAttributeValue("newValue"))); + stringKey("string"), + "value1", + longKey("long"), + 100L, + doubleKey("double"), + 33.44, + booleanKey("boolean"), + false, + stringKey("newKey"), + "newValue")); // Original not mutated. assertThat(attributes).isEqualTo(wantAttributes); } @@ -162,53 +171,53 @@ void builder_arrayTypes() { .setAttribute("double", 33.44, -44.33) .setAttribute("boolean", false, true) .setAttribute("boolean", "duplicateShouldBeRemoved") - .setAttribute("boolean", stringAttributeValue("dropped")) + .setAttribute(stringKey("boolean"), "dropped") .build(); assertThat(attributes) .isEqualTo( Attributes.of( - "string", arrayAttributeValue("value1", "value2"), - "long", arrayAttributeValue(100L, 200L), - "double", arrayAttributeValue(33.44, -44.33), - "boolean", arrayAttributeValue(false, true))); + stringArrayKey("string"), Arrays.asList("value1", "value2"), + longArrayKey("long"), Arrays.asList(100L, 200L), + doubleArrayKey("double"), Arrays.asList(33.44, -44.33), + booleanArrayKey("boolean"), Arrays.asList(false, true))); } @Test void get_Null() { - assertThat(Attributes.empty().get("foo")).isNull(); - assertThat(Attributes.of("key", stringAttributeValue("value")).get("foo")).isNull(); + assertThat(Attributes.empty().get(stringKey("foo"))).isNull(); + assertThat(Attributes.of(stringKey("key"), "value").get(stringKey("foo"))).isNull(); } @Test void get() { - assertThat(Attributes.of("key", stringAttributeValue("value")).get("key")) - .isEqualTo(stringAttributeValue("value")); - assertThat(Attributes.of("key", stringAttributeValue("value")).get("value")).isNull(); + assertThat(Attributes.of(stringKey("key"), "value").get(stringKey("key"))).isEqualTo("value"); + assertThat(Attributes.of(stringKey("key"), "value").get(stringKey("value"))).isNull(); Attributes threeElements = Attributes.of( - "string", stringAttributeValue("value"), - "boolean", booleanAttributeValue(true), - "long", longAttributeValue(1L)); - assertThat(threeElements.get("boolean")).isEqualTo(booleanAttributeValue(true)); - assertThat(threeElements.get("string")).isEqualTo(stringAttributeValue("value")); - assertThat(threeElements.get("long")).isEqualTo(longAttributeValue(1L)); + stringKey("string"), "value", booleanKey("boolean"), true, longKey("long"), 1L); + assertThat(threeElements.get(booleanKey("boolean"))).isEqualTo(true); + assertThat(threeElements.get(stringKey("string"))).isEqualTo("value"); + assertThat(threeElements.get(longKey("long"))).isEqualTo(1L); Attributes twoElements = - Attributes.of( - "string", stringAttributeValue("value"), - "boolean", booleanAttributeValue(true)); - assertThat(twoElements.get("boolean")).isEqualTo(booleanAttributeValue(true)); - assertThat(twoElements.get("string")).isEqualTo(stringAttributeValue("value")); + Attributes.of(stringKey("string"), "value", booleanKey("boolean"), true); + assertThat(twoElements.get(booleanKey("boolean"))).isEqualTo(true); + assertThat(twoElements.get(stringKey("string"))).isEqualTo("value"); Attributes fourElements = Attributes.of( - "string", stringAttributeValue("value"), - "boolean", booleanAttributeValue(true), - "long", longAttributeValue(1L), - "array", arrayAttributeValue("one", "two", "three")); - assertThat(fourElements.get("array")).isEqualTo(arrayAttributeValue("one", "two", "three")); - assertThat(threeElements.get("boolean")).isEqualTo(booleanAttributeValue(true)); - assertThat(threeElements.get("string")).isEqualTo(stringAttributeValue("value")); - assertThat(threeElements.get("long")).isEqualTo(longAttributeValue(1L)); + stringKey("string"), + "value", + booleanKey("boolean"), + true, + longKey("long"), + 1L, + stringArrayKey("array"), + Arrays.asList("one", "two", "three")); + assertThat(fourElements.get(stringArrayKey("array"))) + .isEqualTo(Arrays.asList("one", "two", "three")); + assertThat(threeElements.get(booleanKey("boolean"))).isEqualTo(true); + assertThat(threeElements.get(stringKey("string"))).isEqualTo("value"); + assertThat(threeElements.get(longKey("long"))).isEqualTo(1L); } @Test @@ -236,7 +245,7 @@ void toBuilder() { @Test void deleteByNull() { Attributes.Builder attributes = Attributes.newBuilder(); - attributes.setAttribute("attrValue", AttributeValue.stringAttributeValue("attrValue")); + attributes.setAttribute(stringKey("attrValue"), "attrValue"); attributes.setAttribute("string", "string"); attributes.setAttribute("long", 10); attributes.setAttribute("double", 1.0); @@ -246,7 +255,7 @@ void deleteByNull() { attributes.setAttribute("arrayDouble", new Double[] {1.0}); attributes.setAttribute("arrayBool", new Boolean[] {true}); assertThat(attributes.build().size()).isEqualTo(9); - attributes.setAttribute("attrValue", (AttributeValue) null); + attributes.setAttribute(stringKey("attrValue"), null); attributes.setAttribute("string", (String) null); attributes.setAttribute("arrayString", (String[]) null); attributes.setAttribute("arrayLong", (Long[]) null); diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java index f24a1874154..fac846d06ce 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java @@ -16,9 +16,15 @@ package io.opentelemetry.trace; +import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import org.junit.jupiter.api.Test; @@ -35,26 +41,20 @@ void hasInvalidContextAndDefaultSpanOptions() { @Test void doNotCrash() { Span span = DefaultSpan.getInvalid(); - span.setAttribute( - "MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue")); - span.setAttribute("MyBooleanAttributeKey", AttributeValue.booleanAttributeValue(true)); - span.setAttribute("MyLongAttributeKey", AttributeValue.longAttributeValue(123)); - span.setAttribute("NullString", (String) null); + span.setAttribute(stringKey("MyStringAttributeKey"), "MyStringAttributeValue"); + span.setAttribute(booleanKey("MyBooleanAttributeKey"), true); + span.setAttribute(longKey("MyLongAttributeKey"), 123L); + span.setAttribute("NullString", null); span.setAttribute("EmptyString", ""); - span.setAttribute("NullArrayString", AttributeValue.arrayAttributeValue((String[]) null)); - span.setAttribute("NullArrayBoolean", AttributeValue.arrayAttributeValue((Boolean[]) null)); - span.setAttribute("NullArrayLong", AttributeValue.arrayAttributeValue((Long[]) null)); - span.setAttribute("NullArrayDouble", AttributeValue.arrayAttributeValue((Double[]) null)); - span.setAttribute(null, (String) null); + span.setAttribute(stringArrayKey("NullArrayString"), null); + span.setAttribute(booleanArrayKey("NullArrayBoolean"), null); + span.setAttribute(longArrayKey("NullArrayLong"), null); + span.setAttribute(doubleArrayKey("NullArrayDouble"), null); + span.setAttribute((String) null, null); span.addEvent("event"); span.addEvent("event", 0); - span.addEvent( - "event", - Attributes.of("MyBooleanAttributeKey", AttributeValue.booleanAttributeValue(true))); - span.addEvent( - "event", - Attributes.of("MyBooleanAttributeKey", AttributeValue.booleanAttributeValue(true)), - 0); + span.addEvent("event", Attributes.of(booleanKey("MyBooleanAttributeKey"), true)); + span.addEvent("event", Attributes.of(booleanKey("MyBooleanAttributeKey"), true), 0); span.addEvent(new TestEvent()); span.addEvent(new TestEvent(), 0); span.addEvent((Event) null); diff --git a/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java b/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java index 4efa732bd6e..da855d7a918 100644 --- a/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java +++ b/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.trace; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.trace.Span.Kind; import org.junit.jupiter.api.Test; @@ -55,7 +55,7 @@ public Attributes getAttributes() { spanBuilder.setAttribute("key", 12345L); spanBuilder.setAttribute("key", .12345); spanBuilder.setAttribute("key", true); - spanBuilder.setAttribute("key", AttributeValue.stringAttributeValue("value")); + spanBuilder.setAttribute(stringKey("key"), "value"); spanBuilder.setStartTimestamp(12345L); assertThat(spanBuilder.startSpan()).isInstanceOf(DefaultSpan.class); } diff --git a/api/src/test/java/io/opentelemetry/trace/attributes/BooleanAttributeSetterTest.java b/api/src/test/java/io/opentelemetry/trace/attributes/BooleanAttributeSetterTest.java deleted file mode 100644 index d79f128a703..00000000000 --- a/api/src/test/java/io/opentelemetry/trace/attributes/BooleanAttributeSetterTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace.attributes; - -import static io.opentelemetry.common.AttributeValue.booleanAttributeValue; -import static org.assertj.core.api.Assertions.assertThat; - -import io.opentelemetry.common.Attributes; -import org.junit.jupiter.api.Test; - -class BooleanAttributeSetterTest { - - @Test - void attributesBuilder() { - BooleanAttributeSetter setter = BooleanAttributeSetter.create("there?"); - assertThat(setter.key()).isEqualTo("there?"); - assertThat(setter.toString()).isEqualTo("there?"); - Attributes.Builder attributes = Attributes.newBuilder(); - setter.set(attributes, true); - assertThat(attributes.build().get("there?")).isEqualTo(booleanAttributeValue(true)); - } -} diff --git a/api/src/test/java/io/opentelemetry/trace/attributes/DoubleAttributeSetterTest.java b/api/src/test/java/io/opentelemetry/trace/attributes/DoubleAttributeSetterTest.java deleted file mode 100644 index 84c1234943d..00000000000 --- a/api/src/test/java/io/opentelemetry/trace/attributes/DoubleAttributeSetterTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace.attributes; - -import static io.opentelemetry.common.AttributeValue.doubleAttributeValue; -import static org.assertj.core.api.Assertions.assertThat; - -import io.opentelemetry.common.Attributes; -import org.junit.jupiter.api.Test; - -class DoubleAttributeSetterTest { - - @Test - void attributesBuilder() { - DoubleAttributeSetter setter = DoubleAttributeSetter.create("how much?"); - assertThat(setter.key()).isEqualTo("how much?"); - assertThat(setter.toString()).isEqualTo("how much?"); - Attributes.Builder attributes = Attributes.newBuilder(); - setter.set(attributes, 10.0); - assertThat(attributes.build().get("how much?")).isEqualTo(doubleAttributeValue(10.0)); - } -} diff --git a/api/src/test/java/io/opentelemetry/trace/attributes/LongAttributeSetterTest.java b/api/src/test/java/io/opentelemetry/trace/attributes/LongAttributeSetterTest.java deleted file mode 100644 index bdc631d181b..00000000000 --- a/api/src/test/java/io/opentelemetry/trace/attributes/LongAttributeSetterTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace.attributes; - -import static io.opentelemetry.common.AttributeValue.longAttributeValue; -import static org.assertj.core.api.Assertions.assertThat; - -import io.opentelemetry.common.Attributes; -import org.junit.jupiter.api.Test; - -class LongAttributeSetterTest { - - @Test - void attributesBuilder() { - LongAttributeSetter setter = LongAttributeSetter.create("how much?"); - assertThat(setter.key()).isEqualTo("how much?"); - assertThat(setter.toString()).isEqualTo("how much?"); - Attributes.Builder attributes = Attributes.newBuilder(); - setter.set(attributes, 10); - assertThat(attributes.build().get("how much?")).isEqualTo(longAttributeValue(10)); - } -} diff --git a/api/src/test/java/io/opentelemetry/trace/attributes/SemanticAttributesTest.java b/api/src/test/java/io/opentelemetry/trace/attributes/SemanticAttributesTest.java deleted file mode 100644 index e2d02f7a60c..00000000000 --- a/api/src/test/java/io/opentelemetry/trace/attributes/SemanticAttributesTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace.attributes; - -import static org.assertj.core.api.Assertions.assertThat; - -import io.opentelemetry.trace.Span; -import java.lang.reflect.Field; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mockito; - -/** Unit tests for {@link SemanticAttributes}. */ -class SemanticAttributesTest { - - private Span span; - private Span.Builder builder; - - @BeforeEach - void setUp() { - span = Mockito.mock(Span.class); - builder = Mockito.mock(Span.Builder.class); - } - - @Test - void shouldEnableSetAttributeOnSpan() throws IllegalAccessException { - Field[] fields = SemanticAttributes.class.getFields(); - for (Field field : fields) { - Object attribute = field.get(null); - if (attribute instanceof StringAttributeSetter) { - setAndVerify((StringAttributeSetter) attribute, span, "TestValue"); - setAndVerify((StringAttributeSetter) attribute, builder, "TestValue"); - setAndVerify((StringAttributeSetter) attribute, span, null); - setAndVerify((StringAttributeSetter) attribute, builder, null); - } else if (attribute instanceof LongAttributeSetter) { - setAndVerify((LongAttributeSetter) attribute, span, 42L); - setAndVerify((LongAttributeSetter) attribute, builder, 42L); - } else if (attribute instanceof DoubleAttributeSetter) { - setAndVerify((DoubleAttributeSetter) attribute, span, 3.14); - setAndVerify((DoubleAttributeSetter) attribute, builder, 3.14); - } else if (attribute instanceof BooleanAttributeSetter) { - setAndVerify((BooleanAttributeSetter) attribute, span, true); - setAndVerify((BooleanAttributeSetter) attribute, builder, true); - } - } - } - - private static void setAndVerify(StringAttributeSetter setter, Span span, String value) { - setter.set(span, value); - Mockito.verify(span).setAttribute(setter.key(), value); - } - - private static void setAndVerify( - StringAttributeSetter setter, Span.Builder spanBuilder, String value) { - setter.set(spanBuilder, value); - Mockito.verify(spanBuilder).setAttribute(setter.key(), value); - } - - private static void setAndVerify(LongAttributeSetter setter, Span span, long value) { - setter.set(span, value); - Mockito.verify(span).setAttribute(setter.key(), value); - } - - private static void setAndVerify( - LongAttributeSetter setter, Span.Builder spanBuilder, long value) { - setter.set(spanBuilder, value); - Mockito.verify(spanBuilder).setAttribute(setter.key(), value); - } - - private static void setAndVerify(DoubleAttributeSetter setter, Span span, double value) { - setter.set(span, value); - Mockito.verify(span).setAttribute(setter.key(), value); - } - - private static void setAndVerify( - DoubleAttributeSetter setter, Span.Builder spanBuilder, double value) { - setter.set(spanBuilder, value); - Mockito.verify(spanBuilder).setAttribute(setter.key(), value); - } - - private static void setAndVerify(BooleanAttributeSetter setter, Span span, boolean value) { - setter.set(span, value); - Mockito.verify(span).setAttribute(setter.key(), value); - } - - private static void setAndVerify( - BooleanAttributeSetter setter, Span.Builder spanBuilder, boolean value) { - setter.set(spanBuilder, value); - Mockito.verify(spanBuilder).setAttribute(setter.key(), value); - } - - @Test - void shouldCreateAllSetterTypes() { - assertThat(BooleanAttributeSetter.create("attr.one")) - .isInstanceOf(BooleanAttributeSetter.class); - assertThat(DoubleAttributeSetter.create("attr.two")).isInstanceOf(DoubleAttributeSetter.class); - assertThat(LongAttributeSetter.create("attr.three")).isInstanceOf(LongAttributeSetter.class); - assertThat(StringAttributeSetter.create("attr.four")).isInstanceOf(StringAttributeSetter.class); - } -} diff --git a/api/src/test/java/io/opentelemetry/trace/attributes/StringAttributeSetterTest.java b/api/src/test/java/io/opentelemetry/trace/attributes/StringAttributeSetterTest.java deleted file mode 100644 index 86ef9b863f0..00000000000 --- a/api/src/test/java/io/opentelemetry/trace/attributes/StringAttributeSetterTest.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.trace.attributes; - -import static io.opentelemetry.common.AttributeValue.stringAttributeValue; -import static org.assertj.core.api.Assertions.assertThat; - -import io.opentelemetry.common.Attributes; -import org.junit.jupiter.api.Test; - -class StringAttributeSetterTest { - - @Test - void attributesBuilder() { - StringAttributeSetter setter = StringAttributeSetter.create("hello?"); - assertThat(setter.key()).isEqualTo("hello?"); - assertThat(setter.toString()).isEqualTo("hello?"); - Attributes.Builder attributes = Attributes.newBuilder(); - setter.set(attributes, "world"); - assertThat(attributes.build().get("hello?")).isEqualTo(stringAttributeValue("world")); - } -} diff --git a/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java b/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java index 9efff151337..2a951cf9ce8 100644 --- a/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java +++ b/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java @@ -16,12 +16,16 @@ package io.opentelemetry.exporters.jaeger; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; + import com.google.common.annotations.VisibleForTesting; import com.google.gson.Gson; import com.google.protobuf.Timestamp; import com.google.protobuf.util.Timestamps; -import io.opentelemetry.common.AttributeConsumer; +import io.opentelemetry.common.AttributeKey; +import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.exporters.jaeger.proto.api_v2.Model; import io.opentelemetry.sdk.extensions.otproto.TraceProtoUtils; @@ -33,13 +37,12 @@ import java.util.Collection; import java.util.List; import java.util.Locale; -import java.util.Map; import javax.annotation.concurrent.ThreadSafe; /** Adapts OpenTelemetry objects to Jaeger objects. */ @ThreadSafe final class Adapter { - static final String KEY_ERROR = "error"; + static final BooleanKey KEY_ERROR = booleanKey("error"); static final String KEY_LOG_MESSAGE = "message"; static final String KEY_SPAN_KIND = "span.kind"; static final String KEY_SPAN_STATUS_MESSAGE = "span.status.message"; @@ -131,7 +134,7 @@ static Model.Span toJaeger(SpanData span) { } if (!span.getStatus().isOk()) { - target.addTags(toKeyValue(KEY_ERROR, AttributeValue.booleanAttributeValue(true))); + target.addTags(toKeyValue(KEY_ERROR, true)); } return target.build(); @@ -177,31 +180,15 @@ static Model.Log toJaegerLog(Event event) { * * @param attributes the span attributes * @return a collection of Jaeger key values - * @see #toKeyValue(String, AttributeValue) - */ - @VisibleForTesting - static Collection toKeyValues(Map attributes) { - List tags = new ArrayList<>(attributes.size()); - for (Map.Entry entry : attributes.entrySet()) { - tags.add(toKeyValue(entry.getKey(), entry.getValue())); - } - return tags; - } - - /** - * Converts a map of attributes into a collection of Jaeger's {@link Model.KeyValue}. - * - * @param attributes the span attributes - * @return a collection of Jaeger key values - * @see #toKeyValue(String, AttributeValue) + * @see #toKeyValue */ @VisibleForTesting static Collection toKeyValues(ReadableAttributes attributes) { final List tags = new ArrayList<>(attributes.size()); - attributes.forEach( - new AttributeConsumer() { + attributes.forEachRaw( + new RawAttributeConsumer() { @Override - public void consume(String key, AttributeValue value) { + public void consume(AttributeKey key, T value) { tags.add(toKeyValue(key, value)); } }); @@ -216,41 +203,32 @@ public void consume(String key, AttributeValue value) { * @return a Jaeger key value */ @VisibleForTesting - static Model.KeyValue toKeyValue(String key, AttributeValue value) { + static Model.KeyValue toKeyValue(AttributeKey key, T value) { Model.KeyValue.Builder builder = Model.KeyValue.newBuilder(); - builder.setKey(key); + builder.setKey(key.get()); - switch (value.getType()) { + switch (key.getType()) { case STRING: - builder.setVStr(value.getStringValue()); + builder.setVStr((String) value); builder.setVType(Model.ValueType.STRING); break; case LONG: - builder.setVInt64(value.getLongValue()); + builder.setVInt64((long) value); builder.setVType(Model.ValueType.INT64); break; case BOOLEAN: - builder.setVBool(value.getBooleanValue()); + builder.setVBool((boolean) value); builder.setVType(Model.ValueType.BOOL); break; case DOUBLE: - builder.setVFloat64(value.getDoubleValue()); + builder.setVFloat64((double) value); builder.setVType(Model.ValueType.FLOAT64); break; case STRING_ARRAY: - builder.setVStr(new Gson().toJson(value.getStringArrayValue())); - builder.setVType(Model.ValueType.STRING); - break; case LONG_ARRAY: - builder.setVStr(new Gson().toJson(value.getLongArrayValue())); - builder.setVType(Model.ValueType.STRING); - break; case BOOLEAN_ARRAY: - builder.setVStr(new Gson().toJson(value.getBooleanArrayValue())); - builder.setVType(Model.ValueType.STRING); - break; case DOUBLE_ARRAY: - builder.setVStr(new Gson().toJson(value.getDoubleArrayValue())); + builder.setVStr(new Gson().toJson(value)); builder.setVType(Model.ValueType.STRING); break; } diff --git a/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java b/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java index 809c0e59f7e..5fe1f253c0b 100644 --- a/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java +++ b/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java @@ -16,13 +16,20 @@ package io.opentelemetry.exporters.jaeger; +import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import com.google.protobuf.util.Durations; import com.google.protobuf.util.Timestamps; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.exporters.jaeger.proto.api_v2.Model; import io.opentelemetry.sdk.extensions.otproto.TraceProtoUtils; @@ -37,6 +44,7 @@ import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceState; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -142,41 +150,21 @@ void testJaegerLog() { assertEquals("bar", keyValue.getVStr()); } - @Test - void testKeyValues() { - // prepare - AttributeValue valueB = AttributeValue.booleanAttributeValue(true); - - // test - Collection keyValues = - Adapter.toKeyValues(Collections.singletonMap("valueB", valueB)); - - // verify - // the actual content is checked in some other test - assertEquals(1, keyValues.size()); - } - @Test void testKeyValue() { - // prepare - AttributeValue valueB = AttributeValue.booleanAttributeValue(true); - AttributeValue valueD = AttributeValue.doubleAttributeValue(1.); - AttributeValue valueI = AttributeValue.longAttributeValue(2); - AttributeValue valueS = AttributeValue.stringAttributeValue("foobar"); - AttributeValue valueArrayB = AttributeValue.arrayAttributeValue(true, false); - AttributeValue valueArrayD = AttributeValue.arrayAttributeValue(1.2345, 6.789); - AttributeValue valueArrayI = AttributeValue.arrayAttributeValue(12345L, 67890L); - AttributeValue valueArrayS = AttributeValue.arrayAttributeValue("foobar", "barfoo"); - // test - Model.KeyValue kvB = Adapter.toKeyValue("valueB", valueB); - Model.KeyValue kvD = Adapter.toKeyValue("valueD", valueD); - Model.KeyValue kvI = Adapter.toKeyValue("valueI", valueI); - Model.KeyValue kvS = Adapter.toKeyValue("valueS", valueS); - Model.KeyValue kvArrayB = Adapter.toKeyValue("valueArrayB", valueArrayB); - Model.KeyValue kvArrayD = Adapter.toKeyValue("valueArrayD", valueArrayD); - Model.KeyValue kvArrayI = Adapter.toKeyValue("valueArrayI", valueArrayI); - Model.KeyValue kvArrayS = Adapter.toKeyValue("valueArrayS", valueArrayS); + Model.KeyValue kvB = Adapter.toKeyValue(booleanKey("valueB"), true); + Model.KeyValue kvD = Adapter.toKeyValue(doubleKey("valueD"), 1.); + Model.KeyValue kvI = Adapter.toKeyValue(longKey("valueI"), 2L); + Model.KeyValue kvS = Adapter.toKeyValue(stringKey("valueS"), "foobar"); + Model.KeyValue kvArrayB = + Adapter.toKeyValue(booleanArrayKey("valueArrayB"), Arrays.asList(true, false)); + Model.KeyValue kvArrayD = + Adapter.toKeyValue(doubleArrayKey("valueArrayD"), Arrays.asList(1.2345, 6.789)); + Model.KeyValue kvArrayI = + Adapter.toKeyValue(longArrayKey("valueArrayI"), Arrays.asList(12345L, 67890L)); + Model.KeyValue kvArrayS = + Adapter.toKeyValue(stringArrayKey("valueArrayS"), Arrays.asList("foobar", "barfoo")); // verify assertTrue(kvB.getVBool()); @@ -254,10 +242,10 @@ void testStatusNotOk() { void testSpanError() { Attributes attributes = Attributes.of( - "error.type", - AttributeValue.stringAttributeValue(this.getClass().getName()), - "error.message", - AttributeValue.stringAttributeValue("server error")); + stringKey("error.type"), + this.getClass().getName(), + stringKey("error.message"), + "server error"); long startMs = System.currentTimeMillis(); long endMs = startMs + 900; SpanData span = @@ -286,14 +274,12 @@ void testSpanError() { private static EventImpl getTimedEvent() { long epochNanos = TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis()); - AttributeValue valueS = AttributeValue.stringAttributeValue("bar"); - Attributes attributes = Attributes.of("foo", valueS); + Attributes attributes = Attributes.of(stringKey("foo"), "bar"); return EventImpl.create(epochNanos, "the log message", attributes); } private static SpanData getSpanData(long startMs, long endMs) { - AttributeValue valueB = AttributeValue.booleanAttributeValue(true); - Attributes attributes = Attributes.of("valueB", valueB); + Attributes attributes = Attributes.of(booleanKey("valueB"), true); Link link = Link.create(createSpanContext(LINK_TRACE_ID, LINK_SPAN_ID), attributes); @@ -305,7 +291,7 @@ private static SpanData getSpanData(long startMs, long endMs) { .setName("GET /api/endpoint") .setStartEpochNanos(TimeUnit.MILLISECONDS.toNanos(startMs)) .setEndEpochNanos(TimeUnit.MILLISECONDS.toNanos(endMs)) - .setAttributes(Attributes.of("valueB", valueB)) + .setAttributes(Attributes.of(booleanKey("valueB"), true)) .setEvents(Collections.singletonList(getTimedEvent())) .setTotalRecordedEvents(1) .setLinks(Collections.singletonList(link)) diff --git a/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingMetricExporterTest.java b/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingMetricExporterTest.java index 1dcd2dc7bf1..3f2cc539e2a 100644 --- a/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingMetricExporterTest.java +++ b/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingMetricExporterTest.java @@ -16,9 +16,9 @@ package io.opentelemetry.exporters.logging; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -58,8 +58,7 @@ void tearDown() { void testExport() { long nowEpochNanos = System.currentTimeMillis() * 1000 * 1000; - Resource resource = - Resource.create(Attributes.of("host", AttributeValue.stringAttributeValue("localhost"))); + Resource resource = Resource.create(Attributes.of(stringKey("host"), "localhost")); InstrumentationLibraryInfo instrumentationLibraryInfo = InstrumentationLibraryInfo.create("manualInstrumentation", "1.0"); exporter.export( diff --git a/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java b/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java index aff43c84334..c2cbc14de3d 100644 --- a/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java +++ b/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.exporters.logging; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.trace.TestSpanData; @@ -72,7 +72,7 @@ void returnCode() { EventImpl.create( epochNanos + 500, "somethingHappenedHere", - Attributes.of("important", AttributeValue.booleanAttributeValue(true))))) + Attributes.of(booleanKey("important"), true)))) .setTotalRecordedEvents(1) .setTotalRecordedLinks(0) .build(); diff --git a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/CommonAdapter.java b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/CommonAdapter.java index 9aa92b7dc0c..552ee817485 100644 --- a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/CommonAdapter.java +++ b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/CommonAdapter.java @@ -16,90 +16,146 @@ package io.opentelemetry.exporters.otlp; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; +import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; +import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.LongKey; +import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.proto.common.v1.AnyValue; import io.opentelemetry.proto.common.v1.ArrayValue; import io.opentelemetry.proto.common.v1.InstrumentationLibrary; import io.opentelemetry.proto.common.v1.KeyValue; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; +import java.util.List; final class CommonAdapter { - static KeyValue toProtoAttribute(String key, AttributeValue attributeValue) { - KeyValue.Builder builder = KeyValue.newBuilder().setKey(key); - switch (attributeValue.getType()) { + @SuppressWarnings("unchecked") + public static KeyValue toProtoAttribute(AttributeKey key, T value) { + KeyValue.Builder builder = KeyValue.newBuilder().setKey(key.get()); + switch (key.getType()) { case STRING: - return builder - .setValue(AnyValue.newBuilder().setStringValue(attributeValue.getStringValue()).build()) - .build(); + return makeStringKeyValue((StringKey) key, (String) value); case BOOLEAN: - return builder - .setValue(AnyValue.newBuilder().setBoolValue(attributeValue.getBooleanValue()).build()) - .build(); + return makeBooleanKeyValue((BooleanKey) key, (boolean) value); case LONG: - return builder - .setValue(AnyValue.newBuilder().setIntValue(attributeValue.getLongValue()).build()) - .build(); + return makeLongKeyValue((LongKey) key, (Long) value); case DOUBLE: - return builder - .setValue(AnyValue.newBuilder().setDoubleValue(attributeValue.getDoubleValue()).build()) - .build(); + return makeDoubleKeyValue((DoubleKey) key, (Double) value); case BOOLEAN_ARRAY: - return builder - .setValue( - AnyValue.newBuilder() - .setArrayValue(makeBooleanArrayAnyValue(attributeValue)) - .build()) - .build(); + return makeBooleanArrayKeyValue((BooleanArrayKey) key, (List) value); case LONG_ARRAY: - return builder - .setValue( - AnyValue.newBuilder().setArrayValue(makeLongArrayAnyValue(attributeValue)).build()) - .build(); + return makeLongArrayKeyValue((LongArrayKey) key, (List) value); case DOUBLE_ARRAY: - return builder - .setValue( - AnyValue.newBuilder() - .setArrayValue(makeDoubleArrayAnyValue(attributeValue)) - .build()) - .build(); + return makeDoubleArrayKeyValue((DoubleArrayKey) key, (List) value); case STRING_ARRAY: - return builder - .setValue( - AnyValue.newBuilder() - .setArrayValue(makeStringArrayAnyValue(attributeValue)) - .build()) - .build(); + return makeStringArrayKeyValue((StringArrayKey) key, (List) value); } return builder.setValue(AnyValue.getDefaultInstance()).build(); } - private static ArrayValue makeDoubleArrayAnyValue(AttributeValue attributeValue) { + private static KeyValue makeLongArrayKeyValue(LongArrayKey key, List value) { + KeyValue.Builder keyValueBuilder = + KeyValue.newBuilder() + .setKey(key.get()) + .setValue(AnyValue.newBuilder().setArrayValue(makeLongArrayAnyValue(value)).build()); + + return keyValueBuilder.build(); + } + + private static KeyValue makeDoubleArrayKeyValue(DoubleArrayKey key, List value) { + KeyValue.Builder keyValueBuilder = + KeyValue.newBuilder() + .setKey(key.get()) + .setValue(AnyValue.newBuilder().setArrayValue(makeDoubleArrayAnyValue(value)).build()); + + return keyValueBuilder.build(); + } + + private static KeyValue makeBooleanArrayKeyValue(BooleanArrayKey key, List value) { + KeyValue.Builder keyValueBuilder = + KeyValue.newBuilder() + .setKey(key.get()) + .setValue(AnyValue.newBuilder().setArrayValue(makeBooleanArrayAnyValue(value)).build()); + + return keyValueBuilder.build(); + } + + private static KeyValue makeStringArrayKeyValue(StringArrayKey key, List value) { + KeyValue.Builder keyValueBuilder = + KeyValue.newBuilder() + .setKey(key.get()) + .setValue(AnyValue.newBuilder().setArrayValue(makeStringArrayAnyValue(value)).build()); + + return keyValueBuilder.build(); + } + + private static KeyValue makeLongKeyValue(LongKey key, long value) { + KeyValue.Builder keyValueBuilder = + KeyValue.newBuilder() + .setKey(key.get()) + .setValue(AnyValue.newBuilder().setIntValue(value).build()); + + return keyValueBuilder.build(); + } + + private static KeyValue makeDoubleKeyValue(DoubleKey key, double value) { + KeyValue.Builder keyValueBuilder = + KeyValue.newBuilder() + .setKey(key.get()) + .setValue(AnyValue.newBuilder().setDoubleValue(value).build()); + + return keyValueBuilder.build(); + } + + private static KeyValue makeBooleanKeyValue(BooleanKey key, boolean value) { + KeyValue.Builder keyValueBuilder = + KeyValue.newBuilder() + .setKey(key.get()) + .setValue(AnyValue.newBuilder().setBoolValue(value).build()); + + return keyValueBuilder.build(); + } + + private static KeyValue makeStringKeyValue(StringKey key, String value) { + KeyValue.Builder keyValueBuilder = + KeyValue.newBuilder() + .setKey(key.get()) + .setValue(AnyValue.newBuilder().setStringValue(value).build()); + + return keyValueBuilder.build(); + } + + private static ArrayValue makeDoubleArrayAnyValue(List doubleArrayValue) { ArrayValue.Builder builder = ArrayValue.newBuilder(); - for (Double doubleValue : attributeValue.getDoubleArrayValue()) { + for (Double doubleValue : doubleArrayValue) { builder.addValues(AnyValue.newBuilder().setDoubleValue(doubleValue).build()); } return builder.build(); } - private static ArrayValue makeLongArrayAnyValue(AttributeValue attributeValue) { + private static ArrayValue makeLongArrayAnyValue(List longArrayValue) { ArrayValue.Builder builder = ArrayValue.newBuilder(); - for (Long intValue : attributeValue.getLongArrayValue()) { + for (Long intValue : longArrayValue) { builder.addValues(AnyValue.newBuilder().setIntValue(intValue).build()); } return builder.build(); } - private static ArrayValue makeStringArrayAnyValue(AttributeValue attributeValue) { + private static ArrayValue makeStringArrayAnyValue(List stringArrayValue) { ArrayValue.Builder builder = ArrayValue.newBuilder(); - for (String string : attributeValue.getStringArrayValue()) { + for (String string : stringArrayValue) { builder.addValues(AnyValue.newBuilder().setStringValue(string).build()); } return builder.build(); } - private static ArrayValue makeBooleanArrayAnyValue(AttributeValue attributeValue) { + private static ArrayValue makeBooleanArrayAnyValue(List booleanArrayValue) { ArrayValue.Builder builder = ArrayValue.newBuilder(); - for (Boolean bool : attributeValue.getBooleanArrayValue()) { + for (Boolean bool : booleanArrayValue) { builder.addValues(AnyValue.newBuilder().setBoolValue(bool).build()); } return builder.build(); diff --git a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/ResourceAdapter.java b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/ResourceAdapter.java index fb232b21410..e049d973654 100644 --- a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/ResourceAdapter.java +++ b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/ResourceAdapter.java @@ -16,8 +16,8 @@ package io.opentelemetry.exporters.otlp; -import io.opentelemetry.common.AttributeConsumer; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; +import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.proto.resource.v1.Resource; final class ResourceAdapter { @@ -25,10 +25,10 @@ static Resource toProtoResource(io.opentelemetry.sdk.resources.Resource resource final Resource.Builder builder = Resource.newBuilder(); resource .getAttributes() - .forEach( - new AttributeConsumer() { + .forEachRaw( + new RawAttributeConsumer() { @Override - public void consume(String key, AttributeValue value) { + public void consume(AttributeKey key, T value) { builder.addAttributes(CommonAdapter.toProtoAttribute(key, value)); } }); diff --git a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java index 994f497fdd3..6b4f05201d2 100644 --- a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java +++ b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java @@ -22,9 +22,9 @@ import static io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_PRODUCER; import static io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_SERVER; -import io.opentelemetry.common.AttributeConsumer; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; +import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans; import io.opentelemetry.proto.trace.v1.ResourceSpans; import io.opentelemetry.proto.trace.v1.Span; @@ -107,10 +107,10 @@ static Span toProtoSpan(SpanData spanData) { builder.setEndTimeUnixNano(spanData.getEndEpochNanos()); spanData .getAttributes() - .forEach( - new AttributeConsumer() { + .forEachRaw( + new RawAttributeConsumer() { @Override - public void consume(String key, AttributeValue value) { + public void consume(AttributeKey key, T value) { builder.addAttributes(CommonAdapter.toProtoAttribute(key, value)); } }); @@ -150,10 +150,10 @@ static Span.Event toProtoSpanEvent(Event event) { builder.setTimeUnixNano(event.getEpochNanos()); event .getAttributes() - .forEach( - new AttributeConsumer() { + .forEachRaw( + new RawAttributeConsumer() { @Override - public void consume(String key, AttributeValue value) { + public void consume(AttributeKey key, T value) { builder.addAttributes(CommonAdapter.toProtoAttribute(key, value)); } }); @@ -168,13 +168,14 @@ static Span.Link toProtoSpanLink(Link link) { builder.setSpanId(TraceProtoUtils.toProtoSpanId(link.getContext().getSpanIdAsHexString())); // TODO: Set TraceState; Attributes attributes = link.getAttributes(); - attributes.forEach( - new AttributeConsumer() { + attributes.forEachRaw( + new RawAttributeConsumer() { @Override - public void consume(String key, AttributeValue value) { + public void consume(AttributeKey key, T value) { builder.addAttributes(CommonAdapter.toProtoAttribute(key, value)); } }); + builder.setDroppedAttributesCount(link.getTotalAttributeCount() - attributes.size()); return builder.build(); } diff --git a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/CommonAdapterTest.java b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/CommonAdapterTest.java index 119633710ea..570838046d8 100644 --- a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/CommonAdapterTest.java +++ b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/CommonAdapterTest.java @@ -16,21 +16,29 @@ package io.opentelemetry.exporters.otlp; +import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.proto.common.v1.AnyValue; import io.opentelemetry.proto.common.v1.ArrayValue; import io.opentelemetry.proto.common.v1.InstrumentationLibrary; import io.opentelemetry.proto.common.v1.KeyValue; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; +import java.util.Arrays; import org.junit.jupiter.api.Test; /** Unit tests for {@link CommonAdapter}. */ class CommonAdapterTest { @Test void toProtoAttribute_Bool() { - assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.booleanAttributeValue(true))) + assertThat(CommonAdapter.toProtoAttribute(booleanKey("key"), true)) .isEqualTo( KeyValue.newBuilder() .setKey("key") @@ -40,8 +48,7 @@ void toProtoAttribute_Bool() { @Test void toProtoAttribute_BoolArray() { - assertThat( - CommonAdapter.toProtoAttribute("key", AttributeValue.arrayAttributeValue(true, false))) + assertThat(CommonAdapter.toProtoAttribute(booleanArrayKey("key"), Arrays.asList(true, false))) .isEqualTo( KeyValue.newBuilder() .setKey("key") @@ -58,7 +65,7 @@ void toProtoAttribute_BoolArray() { @Test void toProtoAttribute_String() { - assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.stringAttributeValue("string"))) + assertThat(CommonAdapter.toProtoAttribute(stringKey("key"), "string")) .isEqualTo( KeyValue.newBuilder() .setKey("key") @@ -70,7 +77,7 @@ void toProtoAttribute_String() { void toProtoAttribute_StringArray() { assertThat( CommonAdapter.toProtoAttribute( - "key", AttributeValue.arrayAttributeValue("string1", "string2"))) + stringArrayKey("key"), Arrays.asList("string1", "string2"))) .isEqualTo( KeyValue.newBuilder() .setKey("key") @@ -87,7 +94,7 @@ void toProtoAttribute_StringArray() { @Test void toProtoAttribute_Int() { - assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.longAttributeValue(100))) + assertThat(CommonAdapter.toProtoAttribute(longKey("key"), 100L)) .isEqualTo( KeyValue.newBuilder() .setKey("key") @@ -97,8 +104,7 @@ void toProtoAttribute_Int() { @Test void toProtoAttribute_IntArray() { - assertThat( - CommonAdapter.toProtoAttribute("key", AttributeValue.arrayAttributeValue(100L, 200L))) + assertThat(CommonAdapter.toProtoAttribute(longArrayKey("key"), Arrays.asList(100L, 200L))) .isEqualTo( KeyValue.newBuilder() .setKey("key") @@ -115,7 +121,7 @@ void toProtoAttribute_IntArray() { @Test void toProtoAttribute_Double() { - assertThat(CommonAdapter.toProtoAttribute("key", AttributeValue.doubleAttributeValue(100.3))) + assertThat(CommonAdapter.toProtoAttribute(doubleKey("key"), 100.3d)) .isEqualTo( KeyValue.newBuilder() .setKey("key") @@ -125,8 +131,7 @@ void toProtoAttribute_Double() { @Test void toProtoAttribute_DoubleArray() { - assertThat( - CommonAdapter.toProtoAttribute("key", AttributeValue.arrayAttributeValue(100.3, 200.5))) + assertThat(CommonAdapter.toProtoAttribute(doubleArrayKey("key"), Arrays.asList(100.3, 200.5))) .isEqualTo( KeyValue.newBuilder() .setKey("key") diff --git a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/MetricAdapterTest.java b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/MetricAdapterTest.java index 304e98d74ad..05ac5d7870b 100644 --- a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/MetricAdapterTest.java +++ b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/MetricAdapterTest.java @@ -16,12 +16,12 @@ package io.opentelemetry.exporters.otlp; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static io.opentelemetry.proto.metrics.v1.AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import com.google.common.collect.ImmutableList; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.proto.common.v1.AnyValue; @@ -337,8 +337,7 @@ void toProtoResourceMetrics() { Descriptor descriptor = Descriptor.create( "name", "description", "1", Descriptor.Type.MONOTONIC_DOUBLE, Labels.of("k", "v")); - Resource resource = - Resource.create(Attributes.of("ka", AttributeValue.stringAttributeValue("va"))); + Resource resource = Resource.create(Attributes.of(stringKey("ka"), "va")); io.opentelemetry.proto.resource.v1.Resource resourceProto = io.opentelemetry.proto.resource.v1.Resource.newBuilder() .addAllAttributes( diff --git a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/ResourceAdapterTest.java b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/ResourceAdapterTest.java index 0117216e072..d45c66e81f2 100644 --- a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/ResourceAdapterTest.java +++ b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/ResourceAdapterTest.java @@ -16,9 +16,12 @@ package io.opentelemetry.exporters.otlp; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.proto.common.v1.AnyValue; import io.opentelemetry.proto.common.v1.KeyValue; @@ -33,14 +36,14 @@ void toProtoResource() { ResourceAdapter.toProtoResource( Resource.create( Attributes.of( - "key_bool", - AttributeValue.booleanAttributeValue(true), - "key_string", - AttributeValue.stringAttributeValue("string"), - "key_int", - AttributeValue.longAttributeValue(100), - "key_double", - AttributeValue.doubleAttributeValue(100.3)))) + booleanKey("key_bool"), + true, + stringKey("key_string"), + "string", + longKey("key_int"), + 100L, + doubleKey("key_double"), + 100.3))) .getAttributesList()) .containsExactlyInAnyOrder( KeyValue.newBuilder() diff --git a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java index 06f0bd68387..f46b4bb5d76 100644 --- a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java +++ b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java @@ -16,6 +16,8 @@ package io.opentelemetry.exporters.otlp; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_CLIENT; import static io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_CONSUMER; import static io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_INTERNAL; @@ -41,7 +43,6 @@ import static org.assertj.core.api.Assertions.assertThat; import com.google.protobuf.ByteString; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.proto.common.v1.AnyValue; import io.opentelemetry.proto.common.v1.KeyValue; @@ -84,7 +85,7 @@ void toProtoSpan() { .setKind(Kind.SERVER) .setStartEpochNanos(12345) .setEndEpochNanos(12349) - .setAttributes(Attributes.of("key", AttributeValue.booleanAttributeValue(true))) + .setAttributes(Attributes.of(booleanKey("key"), true)) .setTotalAttributeCount(2) .setEvents( Collections.singletonList( @@ -267,7 +268,7 @@ void toProtoSpanEvent_WithAttributes() { EventImpl.create( 12345, "test_with_attributes", - Attributes.of("key_string", AttributeValue.stringAttributeValue("string")), + Attributes.of(stringKey("key_string"), "string"), 5))) .isEqualTo( Span.Event.newBuilder() @@ -296,10 +297,7 @@ void toProtoSpanLink_WithoutAttributes() { void toProtoSpanLink_WithAttributes() { assertThat( SpanAdapter.toProtoSpanLink( - Link.create( - SPAN_CONTEXT, - Attributes.of("key_string", AttributeValue.stringAttributeValue("string")), - 5))) + Link.create(SPAN_CONTEXT, Attributes.of(stringKey("key_string"), "string"), 5))) .isEqualTo( Span.Link.newBuilder() .setTraceId(ByteString.copyFrom(TRACE_ID_BYTES)) diff --git a/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/MetricAdapterTest.java b/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/MetricAdapterTest.java index d549997fe5f..082d4b4eb34 100644 --- a/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/MetricAdapterTest.java +++ b/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/MetricAdapterTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.exporters.prometheus; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import com.google.common.collect.ImmutableList; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -222,7 +222,7 @@ void toMetricFamilySamples() { MetricData metricData = MetricData.create( descriptor, - Resource.create(Attributes.of("kr", AttributeValue.stringAttributeValue("vr"))), + Resource.create(Attributes.of(stringKey("kr"), "vr")), InstrumentationLibraryInfo.create("full", "version"), Collections.singletonList( MetricData.DoublePoint.create(123, 456, Labels.of("kp", "vp"), 5))); diff --git a/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/PrometheusCollectorTest.java b/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/PrometheusCollectorTest.java index 0cce4256ac8..394e59bebd4 100644 --- a/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/PrometheusCollectorTest.java +++ b/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/PrometheusCollectorTest.java @@ -16,11 +16,11 @@ package io.opentelemetry.exporters.prometheus; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; import com.google.common.collect.ImmutableList; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -73,7 +73,7 @@ private static ImmutableList generateTestData() { "1", Descriptor.Type.MONOTONIC_LONG, Labels.of("kc", "vc")), - Resource.create(Attributes.of("kr", AttributeValue.stringAttributeValue("vr"))), + Resource.create(Attributes.of(stringKey("kr"), "vr")), InstrumentationLibraryInfo.create("grpc", "version"), Collections.singletonList( MetricData.LongPoint.create(123, 456, Labels.of("kp", "vp"), 5))), @@ -84,7 +84,7 @@ private static ImmutableList generateTestData() { "1", Descriptor.Type.MONOTONIC_DOUBLE, Labels.of("kc", "vc")), - Resource.create(Attributes.of("kr", AttributeValue.stringAttributeValue("vr"))), + Resource.create(Attributes.of(stringKey("kr"), "vr")), InstrumentationLibraryInfo.create("http", "version"), Collections.singletonList( MetricData.DoublePoint.create(123, 456, Labels.of("kp", "vp"), 3.5)))); diff --git a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java index 37ab7f0aaca..d96105adbdd 100644 --- a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java +++ b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java @@ -16,10 +16,13 @@ package io.opentelemetry.exporters.zipkin; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static java.util.concurrent.TimeUnit.NANOSECONDS; -import io.opentelemetry.common.AttributeConsumer; +import io.opentelemetry.common.AttributeKey; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -85,7 +88,7 @@ public final class ZipkinSpanExporter implements SpanExporter { // Note: these 3 fields are non-private for testing static final String GRPC_STATUS_CODE = "grpc.status_code"; static final String GRPC_STATUS_DESCRIPTION = "grpc.status_description"; - static final String STATUS_ERROR = "error"; + static final StringKey STATUS_ERROR = stringKey("error"); static final String KEY_INSTRUMENTATION_LIBRARY_NAME = "otel.instrumentation_library.name"; static final String KEY_INSTRUMENTATION_LIBRARY_VERSION = "otel.instrumentation_library.version"; @@ -147,16 +150,16 @@ static Span generateSpan(SpanData spanData, Endpoint localEndpoint) { } ReadableAttributes spanAttributes = spanData.getAttributes(); - spanAttributes.forEach( - new AttributeConsumer() { + spanAttributes.forEachRaw( + new RawAttributeConsumer() { @Override - public void consume(String key, AttributeValue value) { - spanBuilder.putTag(key, attributeValueToString(value)); + public void consume(AttributeKey key, T value) { + spanBuilder.putTag(key.get(), attributeValueToString(key, value)); } }); Status status = spanData.getStatus(); // for GRPC spans, include status code & description. - if (status != null && spanAttributes.get(SemanticAttributes.RPC_SERVICE.key()) != null) { + if (status != null && spanAttributes.get(SemanticAttributes.RPC_SERVICE) != null) { spanBuilder.putTag(GRPC_STATUS_CODE, status.getCanonicalCode().toString()); if (status.getDescription() != null) { spanBuilder.putTag(GRPC_STATUS_DESCRIPTION, status.getDescription()); @@ -164,7 +167,7 @@ public void consume(String key, AttributeValue value) { } // add the error tag, if it isn't already in the source span. if (status != null && !status.isOk() && spanAttributes.get(STATUS_ERROR) == null) { - spanBuilder.putTag(STATUS_ERROR, status.getCanonicalCode().toString()); + spanBuilder.putTag(STATUS_ERROR.get(), status.getCanonicalCode().toString()); } InstrumentationLibraryInfo instrumentationLibraryInfo = @@ -189,11 +192,11 @@ private static Endpoint chooseEndpoint(SpanData spanData, Endpoint localEndpoint ReadableAttributes resourceAttributes = spanData.getResource().getAttributes(); // use the service.name from the Resource, if it's been set. - AttributeValue serviceNameValue = resourceAttributes.get(ResourceAttributes.SERVICE_NAME.key()); + String serviceNameValue = resourceAttributes.get(ResourceAttributes.SERVICE_NAME); if (serviceNameValue == null) { return localEndpoint; } - return Endpoint.newBuilder().serviceName(serviceNameValue.getStringValue()).build(); + return Endpoint.newBuilder().serviceName(serviceNameValue).build(); } @Nullable @@ -223,25 +226,19 @@ private static long toEpochMicros(long epochNanos) { return NANOSECONDS.toMicros(epochNanos); } - private static String attributeValueToString(AttributeValue attributeValue) { - AttributeValue.Type type = attributeValue.getType(); + private static String attributeValueToString(AttributeKey key, T attributeValue) { + AttributeValue.Type type = key.getType(); switch (type) { case STRING: - return attributeValue.getStringValue(); case BOOLEAN: - return String.valueOf(attributeValue.getBooleanValue()); case LONG: - return String.valueOf(attributeValue.getLongValue()); case DOUBLE: - return String.valueOf(attributeValue.getDoubleValue()); + return String.valueOf(attributeValue); case STRING_ARRAY: - return commaSeparated(attributeValue.getStringArrayValue()); case BOOLEAN_ARRAY: - return commaSeparated(attributeValue.getBooleanArrayValue()); case LONG_ARRAY: - return commaSeparated(attributeValue.getLongArrayValue()); case DOUBLE_ARRAY: - return commaSeparated(attributeValue.getDoubleArrayValue()); + return commaSeparated((List) attributeValue); } throw new IllegalStateException("Unknown attribute type: " + type); } diff --git a/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java b/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java index dab9ab9601a..7ff8ac293f1 100644 --- a/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java +++ b/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java @@ -16,7 +16,14 @@ package io.opentelemetry.exporters.zipkin; -import static io.opentelemetry.common.AttributeValue.stringAttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doAnswer; @@ -24,7 +31,6 @@ import static org.mockito.Mockito.when; import com.google.common.collect.ImmutableList; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -39,6 +45,7 @@ import io.opentelemetry.trace.Status; import io.opentelemetry.trace.attributes.SemanticAttributes; import java.io.IOException; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -124,10 +131,7 @@ void generateSpan_ProducerKind() { @Test void generateSpan_ResourceServiceNameMapping() { final Resource resource = - Resource.create( - Attributes.of( - ResourceAttributes.SERVICE_NAME.key(), - stringAttributeValue("super-zipkin-service"))); + Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, "super-zipkin-service")); SpanData data = buildStandardSpan().setResource(resource).build(); Endpoint expectedEndpoint = Endpoint.newBuilder().serviceName("super-zipkin-service").build(); @@ -140,14 +144,14 @@ void generateSpan_ResourceServiceNameMapping() { void generateSpan_WithAttributes() { Attributes attributes = Attributes.newBuilder() - .setAttribute("string", stringAttributeValue("string value")) - .setAttribute("boolean", AttributeValue.booleanAttributeValue(false)) - .setAttribute("long", AttributeValue.longAttributeValue(9999L)) - .setAttribute("double", AttributeValue.doubleAttributeValue(222.333)) - .setAttribute("booleanArray", AttributeValue.arrayAttributeValue(true, false)) - .setAttribute("stringArray", AttributeValue.arrayAttributeValue("Hello")) - .setAttribute("doubleArray", AttributeValue.arrayAttributeValue(32.33d, -98.3d)) - .setAttribute("longArray", AttributeValue.arrayAttributeValue(33L, 999L)) + .setAttribute(stringKey("string"), "string value") + .setAttribute(booleanKey("boolean"), false) + .setAttribute(longKey("long"), 9999L) + .setAttribute(doubleKey("double"), 222.333d) + .setAttribute(booleanArrayKey("booleanArray"), Arrays.asList(true, false)) + .setAttribute(stringArrayKey("stringArray"), Collections.singletonList("Hello")) + .setAttribute(doubleArrayKey("doubleArray"), Arrays.asList(32.33d, -98.3d)) + .setAttribute(longArrayKey("longArray"), Arrays.asList(33L, 999L)) .build(); SpanData data = buildStandardSpan().setAttributes(attributes).setKind(Kind.CLIENT).build(); @@ -188,12 +192,12 @@ void generateSpan_WithInstrumentationLibraryInfo() { void generateSpan_AlreadyHasHttpStatusInfo() { Attributes attributeMap = Attributes.of( - SemanticAttributes.HTTP_STATUS_CODE.key(), - AttributeValue.longAttributeValue(404), - SemanticAttributes.HTTP_STATUS_TEXT.key(), - stringAttributeValue("NOT FOUND"), - "error", - stringAttributeValue("A user provided error")); + SemanticAttributes.HTTP_STATUS_CODE, + 404L, + SemanticAttributes.HTTP_STATUS_TEXT, + "NOT FOUND", + stringKey("error"), + "A user provided error"); SpanData data = buildStandardSpan() .setAttributes(attributeMap) @@ -206,17 +210,15 @@ void generateSpan_AlreadyHasHttpStatusInfo() { buildZipkinSpan(Span.Kind.CLIENT) .toBuilder() .clearTags() - .putTag(SemanticAttributes.HTTP_STATUS_CODE.key(), "404") - .putTag(SemanticAttributes.HTTP_STATUS_TEXT.key(), "NOT FOUND") + .putTag(SemanticAttributes.HTTP_STATUS_CODE.get(), "404") + .putTag(SemanticAttributes.HTTP_STATUS_TEXT.get(), "NOT FOUND") .putTag("error", "A user provided error") .build()); } @Test void generateSpan_WithRpcErrorStatus() { - Attributes attributeMap = - Attributes.of( - SemanticAttributes.RPC_SERVICE.key(), stringAttributeValue("my service name")); + Attributes attributeMap = Attributes.of(SemanticAttributes.RPC_SERVICE, "my service name"); String errorMessage = "timeout"; @@ -231,14 +233,14 @@ void generateSpan_WithRpcErrorStatus() { buildZipkinSpan(Span.Kind.SERVER) .toBuilder() .putTag(ZipkinSpanExporter.GRPC_STATUS_DESCRIPTION, errorMessage) - .putTag(SemanticAttributes.RPC_SERVICE.key(), "my service name") + .putTag(SemanticAttributes.RPC_SERVICE.get(), "my service name") .putTag(ZipkinSpanExporter.GRPC_STATUS_CODE, "DEADLINE_EXCEEDED") - .putTag(ZipkinSpanExporter.STATUS_ERROR, "DEADLINE_EXCEEDED") + .putTag(ZipkinSpanExporter.STATUS_ERROR.get(), "DEADLINE_EXCEEDED") .build()); } @Test - void testExport() throws IOException { + void testExport() { ZipkinSpanExporter zipkinSpanExporter = new ZipkinSpanExporter(mockEncoder, mockSender, "tweetiebird"); @@ -261,7 +263,7 @@ void testExport() throws IOException { } @Test - void testExport_failed() throws IOException { + void testExport_failed() { ZipkinSpanExporter zipkinSpanExporter = new ZipkinSpanExporter(mockEncoder, mockSender, "tweetiebird"); diff --git a/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/MessageEvent.java b/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/MessageEvent.java index acf168d49e0..707ab4ff0af 100644 --- a/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/MessageEvent.java +++ b/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/MessageEvent.java @@ -16,7 +16,11 @@ package io.opentelemetry.extensions.trace; -import io.opentelemetry.common.AttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; + +import io.opentelemetry.common.AttributeKeyImpl.LongKey; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.trace.Event; import javax.annotation.concurrent.Immutable; @@ -35,10 +39,10 @@ public final class MessageEvent implements Event { private static final String EVENT_NAME = "message"; - private static final String TYPE = "message.type"; - private static final String ID = "message.id"; - private static final String COMPRESSED_SIZE = "message.compressed_size"; - private static final String UNCOMPRESSED_SIZE = "message.uncompressed_size"; + private static final StringKey TYPE = stringKey("message.type"); + private static final LongKey ID = longKey("message.id"); + private static final LongKey COMPRESSED_SIZE = longKey("message.compressed_size"); + private static final LongKey UNCOMPRESSED_SIZE = longKey("message.uncompressed_size"); /** * Available types for a {@code MessageEvent}. @@ -60,12 +64,6 @@ public enum Type { RECEIVED, } - private static final AttributeValue sentAttributeValue = - AttributeValue.stringAttributeValue(Type.SENT.name()); - private static final AttributeValue receivedAttributeValue = - AttributeValue.stringAttributeValue(Type.RECEIVED.name()); - private static final AttributeValue zeroAttributeValue = AttributeValue.longAttributeValue(0); - private final Attributes attributes; /** @@ -85,19 +83,10 @@ public static MessageEvent create( Type type, long messageId, long uncompressedSize, long compressedSize) { Attributes.Builder attributeBuilder = Attributes.newBuilder(); attributeBuilder.setAttribute( - TYPE, type == Type.SENT ? sentAttributeValue : receivedAttributeValue); - attributeBuilder.setAttribute( - ID, messageId == 0 ? zeroAttributeValue : AttributeValue.longAttributeValue(messageId)); - attributeBuilder.setAttribute( - UNCOMPRESSED_SIZE, - uncompressedSize == 0 - ? zeroAttributeValue - : AttributeValue.longAttributeValue(uncompressedSize)); - attributeBuilder.setAttribute( - COMPRESSED_SIZE, - compressedSize == 0 - ? zeroAttributeValue - : AttributeValue.longAttributeValue(compressedSize)); + TYPE, type == Type.SENT ? Type.SENT.name() : Type.RECEIVED.name()); + attributeBuilder.setAttribute(ID, messageId); + attributeBuilder.setAttribute(UNCOMPRESSED_SIZE, uncompressedSize); + attributeBuilder.setAttribute(COMPRESSED_SIZE, compressedSize); return new MessageEvent(attributeBuilder.build()); } diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java index 13344c7ced4..9c61cdcdad4 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java @@ -16,7 +16,12 @@ package io.opentelemetry.opentracingshim; -import io.opentelemetry.common.AttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; + +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.correlationcontext.CorrelationContext; import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.Status; @@ -38,8 +43,11 @@ final class SpanBuilderShim extends BaseShimObject implements SpanBuilder { private boolean ignoreActiveSpan; private final List parentLinks = new ArrayList<>(); - private final List spanBuilderAttributeKeys = new ArrayList<>(); - private final List spanBuilderAttributeValues = new ArrayList<>(); + + @SuppressWarnings("rawtypes") + private final List spanBuilderAttributeKeys = new ArrayList<>(); + + private final List spanBuilderAttributeValues = new ArrayList<>(); private Kind spanKind; private boolean error; @@ -118,8 +126,8 @@ public SpanBuilder withTag(String key, String value) { } else if (Tags.ERROR.getKey().equals(key)) { error = Boolean.parseBoolean(value); } else { - this.spanBuilderAttributeKeys.add(key); - this.spanBuilderAttributeValues.add(AttributeValue.stringAttributeValue(value)); + this.spanBuilderAttributeKeys.add(stringKey(key)); + this.spanBuilderAttributeValues.add(value); } return this; @@ -130,8 +138,8 @@ public SpanBuilder withTag(String key, boolean value) { if (Tags.ERROR.getKey().equals(key)) { error = value; } else { - this.spanBuilderAttributeKeys.add(key); - this.spanBuilderAttributeValues.add(AttributeValue.booleanAttributeValue(value)); + this.spanBuilderAttributeKeys.add(booleanKey(key)); + this.spanBuilderAttributeValues.add(value); } return this; } @@ -143,11 +151,11 @@ public SpanBuilder withTag(String key, Number value) { || value instanceof Long || value instanceof Short || value instanceof Byte) { - this.spanBuilderAttributeKeys.add(key); - this.spanBuilderAttributeValues.add(AttributeValue.longAttributeValue(value.longValue())); + this.spanBuilderAttributeKeys.add(longKey(key)); + this.spanBuilderAttributeValues.add(value.longValue()); } else if (value instanceof Float || value instanceof Double) { - this.spanBuilderAttributeKeys.add(key); - this.spanBuilderAttributeValues.add(AttributeValue.doubleAttributeValue(value.doubleValue())); + this.spanBuilderAttributeKeys.add(doubleKey(key)); + this.spanBuilderAttributeValues.add(value.doubleValue()); } else { throw new IllegalArgumentException("Number type not supported"); } @@ -175,6 +183,7 @@ public SpanBuilder withStartTimestamp(long microseconds) { throw new UnsupportedOperationException(); } + @SuppressWarnings({"rawtypes", "unchecked"}) @Override public Span start() { CorrelationContext distContext = null; @@ -202,8 +211,8 @@ public Span start() { io.opentelemetry.trace.Span span = builder.startSpan(); for (int i = 0; i < this.spanBuilderAttributeKeys.size(); i++) { - String key = this.spanBuilderAttributeKeys.get(i); - AttributeValue value = this.spanBuilderAttributeValues.get(i); + AttributeKey key = this.spanBuilderAttributeKeys.get(i); + Object value = this.spanBuilderAttributeValues.get(i); span.setAttribute(key, value); } if (error) { diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java index 090fceeb261..3078a131e43 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java @@ -16,7 +16,11 @@ package io.opentelemetry.opentracingshim; -import io.opentelemetry.common.AttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; + import io.opentelemetry.common.Attributes; import io.opentelemetry.trace.Status; import io.opentracing.Span; @@ -203,15 +207,13 @@ static Attributes convertToAttributes(Map fields) { || value instanceof Short || value instanceof Integer || value instanceof Long) { - attributesBuilder.setAttribute( - key, AttributeValue.longAttributeValue(((Number) value).longValue())); + attributesBuilder.setAttribute(longKey(key), ((Number) value).longValue()); } else if (value instanceof Float || value instanceof Double) { - attributesBuilder.setAttribute( - key, AttributeValue.doubleAttributeValue(((Number) value).doubleValue())); + attributesBuilder.setAttribute(doubleKey(key), ((Number) value).doubleValue()); } else if (value instanceof Boolean) { - attributesBuilder.setAttribute(key, AttributeValue.booleanAttributeValue((Boolean) value)); + attributesBuilder.setAttribute(booleanKey(key), (Boolean) value); } else { - attributesBuilder.setAttribute(key, AttributeValue.stringAttributeValue(value.toString())); + attributesBuilder.setAttribute(stringKey(key), value.toString()); } } diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java index 241448c829b..f379e407081 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/TestUtils.java @@ -19,7 +19,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.exporters.inmemory.InMemorySpanExporter; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span.Kind; @@ -40,54 +40,20 @@ public static Callable finishedSpansSize(final InMemorySpanExporter exp } /** Returns a {@code List} with the {@code Span} matching the specified attribute. */ - public static List getByAttr( - List spans, final String key, final Object value) { + public static List getByAttr( + List spans, final AttributeKey key, final T value) { return getByCondition( spans, spanData -> { - AttributeValue attrValue = spanData.getAttributes().get(key); + T attrValue = spanData.getAttributes().get(key); if (attrValue == null) { return false; } - switch (attrValue.getType()) { - case STRING: - return value.equals(attrValue.getStringValue()); - case LONG: - return value.equals(attrValue.getLongValue()); - case BOOLEAN: - return value.equals(attrValue.getBooleanValue()); - case DOUBLE: - return value.equals(attrValue.getDoubleValue()); - case STRING_ARRAY: - return value.equals(attrValue.getStringArrayValue()); - case LONG_ARRAY: - return value.equals(attrValue.getLongArrayValue()); - case BOOLEAN_ARRAY: - return value.equals(attrValue.getBooleanArrayValue()); - case DOUBLE_ARRAY: - return value.equals(attrValue.getDoubleArrayValue()); - } - - return false; + return value.equals(attrValue); }); } - /** - * Returns one {@code Span} instance matching the specified attribute. In case of more than one - * instance being matched, an {@code IllegalArgumentException} will be thrown. - */ - @Nullable - public static SpanData getOneByAttr(List spans, String key, Object value) { - List found = getByAttr(spans, key, value); - if (found.size() > 1) { - throw new IllegalArgumentException( - "there is more than one span with tag '" + key + "' and value '" + value + "'"); - } - - return found.isEmpty() ? null : found.get(0); - } - /** Returns a {@code List} with the {@code Span} matching the specified kind. */ public static List getByKind(List spans, final Kind kind) { return getByCondition(spans, span -> span.getKind() == kind); diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java index bf020549902..754ff6218ec 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -16,6 +16,7 @@ package io.opentelemetry.opentracingshim.testbed.nestedcallbacks; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static io.opentelemetry.opentracingshim.testbed.TestUtils.finishedSpansSize; import static org.awaitility.Awaitility.await; import static org.hamcrest.core.IsEqual.equalTo; @@ -63,8 +64,7 @@ void test() { ReadableAttributes attrs = spans.get(0).getAttributes(); assertEquals(3, attrs.size()); for (int i = 1; i <= 3; i++) { - assertEquals( - Integer.toString(i), spans.get(0).getAttributes().get("key" + i).getStringValue()); + assertEquals(Integer.toString(i), spans.get(0).getAttributes().get(stringKey("key" + i))); } assertNull(tracer.scopeManager().activeSpan()); diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java index 5908e3e86e5..8bc0f605358 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java @@ -16,9 +16,11 @@ package io.opentelemetry.opentracingshim.testbed.promisepropagation; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static io.opentelemetry.opentracingshim.testbed.TestUtils.getByAttr; import static org.assertj.core.api.Assertions.assertThat; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.opentracingshim.TraceShim; import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk; @@ -108,7 +110,7 @@ void testPromiseCallback() { List finished = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(finished.size()).isEqualTo(4); - String component = Tags.COMPONENT.getKey(); + StringKey component = stringKey(Tags.COMPONENT.getKey()); List spanExamplePromise = getByAttr(finished, component, "example-promises"); assertThat(spanExamplePromise).hasSize(1); assertThat(spanExamplePromise.get(0).getParentSpanId()).isEqualTo(SpanId.getInvalid()); diff --git a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java index 72e55e60f0e..a60cad5ec49 100644 --- a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java +++ b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java @@ -16,9 +16,10 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeValue.booleanAttributeValue; -import static io.opentelemetry.common.AttributeValue.stringAttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import io.opentelemetry.common.AttributeKeyImpl; import io.opentelemetry.common.Attributes; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.common.CompletableResultCode; @@ -47,6 +48,8 @@ @State(Scope.Benchmark) public class SpanPipelineBenchmark { + private static final AttributeKeyImpl.StringKey LINK_ATTRIBUTE_KEY = stringKey("linkAttr"); + private static final AttributeKeyImpl.BooleanKey FINALIZED_KEY = booleanKey("finalized"); private final TracerSdk tracerSdk = OpenTelemetrySdk.getTracerProvider().get("benchmarkTracer"); @Setup(Level.Trial) @@ -74,7 +77,7 @@ private void doWork() { .setAttribute("key", "value") .addLink(new TestLink()) .startSpan(); - span.addEvent("started", Attributes.of("operation", stringAttributeValue("some_work"))); + span.addEvent("started", Attributes.of(stringKey("operation"), "some_work")); span.setAttribute("longAttribute", 33L); span.setAttribute("stringAttribute", "test_value"); span.setAttribute("doubleAttribute", 4844.44d); @@ -112,7 +115,7 @@ public SpanContext getContext() { @Override public Attributes getAttributes() { - return Attributes.of("linkAttr", stringAttributeValue("linkValue")); + return Attributes.of(LINK_ATTRIBUTE_KEY, "linkValue"); } } @@ -124,7 +127,7 @@ public String getName() { @Override public Attributes getAttributes() { - return Attributes.of("finalized", booleanAttributeValue(true)); + return Attributes.of(FINALIZED_KEY, true); } } } diff --git a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java index 324858e756b..039ffd41929 100644 --- a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java +++ b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java @@ -16,14 +16,25 @@ package io.opentelemetry.sdk.resources; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; + import com.google.auto.value.AutoValue; import com.google.auto.value.extension.memoized.Memoized; import io.opentelemetry.common.AttributeConsumer; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; +import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; +import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.LongKey; +import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.internal.Utils; +import java.util.List; import java.util.Objects; import java.util.Properties; import java.util.ServiceLoader; @@ -45,13 +56,24 @@ public abstract class Resource { private static final String ERROR_MESSAGE_INVALID_VALUE = " should be a ASCII string with a length not exceed " + MAX_LENGTH + " characters."; private static final Resource EMPTY = create(Attributes.empty()); - private static final Resource TELEMETRY_SDK = - create( - Attributes.newBuilder() - .setAttribute("telemetry.sdk.name", "opentelemetry") - .setAttribute("telemetry.sdk.language", "java") - .setAttribute("telemetry.sdk.version", readVersion()) - .build()); + + // todo: move to ResourceAttributes + private static final StringKey SDK_NAME = stringKey("telemetry.sdk.name"); + private static final StringKey SDK_LANGUAGE = stringKey("telemetry.sdk.language"); + private static final StringKey SDK_VERSION = stringKey("telemetry.sdk.version"); + + private static final Resource TELEMETRY_SDK; + + static { + TELEMETRY_SDK = + create( + Attributes.newBuilder() + .setAttribute(SDK_NAME, "opentelemetry") + .setAttribute(SDK_LANGUAGE, "java") + .setAttribute(SDK_VERSION, readVersion()) + .build()); + } + private static final Resource DEFAULT = new EnvAutodetectResource.Builder() .readEnvironmentVariables() @@ -170,7 +192,42 @@ private Merger(Attributes.Builder attrBuilder) { } @Override - public void consume(String key, AttributeValue value) { + public void consume(StringKey key, String value) { + attrBuilder.setAttribute(key, value); + } + + @Override + public void consume(BooleanKey key, boolean value) { + attrBuilder.setAttribute(key, value); + } + + @Override + public void consume(DoubleKey key, double value) { + attrBuilder.setAttribute(key, value); + } + + @Override + public void consume(LongKey key, long value) { + attrBuilder.setAttribute(key, value); + } + + @Override + public void consume(StringArrayKey key, List value) { + attrBuilder.setAttribute(key, value); + } + + @Override + public void consume(BooleanArrayKey key, List value) { + attrBuilder.setAttribute(key, value); + } + + @Override + public void consume(DoubleArrayKey key, List value) { + attrBuilder.setAttribute(key, value); + } + + @Override + public void consume(LongArrayKey key, List value) { attrBuilder.setAttribute(key, value); } } @@ -179,7 +236,46 @@ private static void checkAttributes(ReadableAttributes attributes) { attributes.forEach( new AttributeConsumer() { @Override - public void consume(String key, AttributeValue value) { + public void consume(StringKey key, String value) { + check(key, value); + } + + @Override + public void consume(BooleanKey key, boolean value) { + check(key, value); + } + + @Override + public void consume(DoubleKey key, double value) { + check(key, value); + } + + @Override + public void consume(LongKey key, long value) { + check(key, value); + } + + @Override + public void consume(StringArrayKey key, List value) { + check(key, value); + } + + @Override + public void consume(BooleanArrayKey key, List value) { + check(key, value); + } + + @Override + public void consume(DoubleArrayKey key, List value) { + check(key, value); + } + + @Override + public void consume(LongArrayKey key, List value) { + check(key, value); + } + + private void check(AttributeKey key, T value) { Utils.checkArgument( isValidAndNotEmpty(key), "Attribute key" + ERROR_MESSAGE_INVALID_CHARS); Objects.requireNonNull(value, "Attribute value" + ERROR_MESSAGE_INVALID_VALUE); @@ -205,7 +301,7 @@ private static boolean isValid(String name) { * @param name the name to be validated. * @return whether the name is valid. */ - private static boolean isValidAndNotEmpty(String name) { - return !name.isEmpty() && isValid(name); + private static boolean isValidAndNotEmpty(AttributeKey name) { + return !name.get().isEmpty() && isValid(name.get()); } } diff --git a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java index 88baef2eb53..f27186f95d2 100644 --- a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java +++ b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java @@ -16,8 +16,11 @@ package io.opentelemetry.sdk.resources; -import io.opentelemetry.trace.attributes.LongAttributeSetter; -import io.opentelemetry.trace.attributes.StringAttributeSetter; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; + +import io.opentelemetry.common.AttributeKeyImpl.LongKey; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; /** * Provides constants for resource semantic conventions defined by the OpenTelemetry specification. @@ -29,126 +32,99 @@ public final class ResourceAttributes { /** The operating system type, such as {@code "WINDOWS"}, {@code "DARWIN"}, {@code "LINUX"}. */ - public static final StringAttributeSetter OS_NAME = StringAttributeSetter.create("os.name"); + public static final StringKey OS_NAME = stringKey("os.name"); /** * Human readable information about the OS version, e.g. {@code "Microsoft Windows [Version * 10.0.18363.778]"}, {@code "Ubuntu 18.04.1 LTS"}. */ - public static final StringAttributeSetter OS_DESCRIPTION = - StringAttributeSetter.create("os.description"); + public static final StringKey OS_DESCRIPTION = stringKey("os.description"); /** Process identifier (PID). */ - public static final LongAttributeSetter PROCESS_PID = LongAttributeSetter.create("process.pid"); + public static final LongKey PROCESS_PID = longKey("process.pid"); /** The name of the process executable. */ - public static final StringAttributeSetter PROCESS_EXECUTABLE_NAME = - StringAttributeSetter.create("process.executable.name"); + public static final StringKey PROCESS_EXECUTABLE_NAME = stringKey("process.executable.name"); /** The full path to the process executable. */ - public static final StringAttributeSetter PROCESS_EXECUTABLE_PATH = - StringAttributeSetter.create("process.executable.path"); + public static final StringKey PROCESS_EXECUTABLE_PATH = stringKey("process.executable.path"); /** The command used to launch the process (i.e. the command name). */ - public static final StringAttributeSetter PROCESS_COMMAND = - StringAttributeSetter.create("process.command"); + public static final StringKey PROCESS_COMMAND = stringKey("process.command"); /** * The full command used to launch the process. The value can be either a list of strings * representing the ordered list of arguments, or a single string representing the full command. */ - public static final StringAttributeSetter PROCESS_COMMAND_LINE = - StringAttributeSetter.create("process.command_line"); + public static final StringKey PROCESS_COMMAND_LINE = stringKey("process.command_line"); /** The username of the user that owns the process. */ - public static final StringAttributeSetter PROCESS_OWNER = - StringAttributeSetter.create("process.owner"); + public static final StringKey PROCESS_OWNER = stringKey("process.owner"); /** * Logical name of the service. MUST be the same for all instances of horizontally scaled * services. */ - public static final StringAttributeSetter SERVICE_NAME = - StringAttributeSetter.create("service.name"); + public static final StringKey SERVICE_NAME = stringKey("service.name"); /** * A namespace for `service.name`. A string value having a meaning that helps to distinguish a * group of services, */ - public static final StringAttributeSetter SERVICE_NAMESPACE = - StringAttributeSetter.create("service.namespace"); + public static final StringKey SERVICE_NAMESPACE = stringKey("service.namespace"); /** * The string ID of the service instance. MUST be unique for each instance of the same * `service.namespace,service.name` pair. */ - public static final StringAttributeSetter SERVICE_INSTANCE = - StringAttributeSetter.create("service.instance.id"); + public static final StringKey SERVICE_INSTANCE = stringKey("service.instance.id"); /** The version string of the service API or implementation. */ - public static final StringAttributeSetter SERVICE_VERSION = - StringAttributeSetter.create("service.version"); + public static final StringKey SERVICE_VERSION = stringKey("service.version"); /** The name of the telemetry library. */ - public static final StringAttributeSetter LIBRARY_NAME = - StringAttributeSetter.create("library.name"); + public static final StringKey LIBRARY_NAME = stringKey("library.name"); /** The language of telemetry library and of the code instrumented with it. */ - public static final StringAttributeSetter LIBRARY_LANGUAGE = - StringAttributeSetter.create("library.language"); + public static final StringKey LIBRARY_LANGUAGE = stringKey("library.language"); /** The version string of the library. */ - public static final StringAttributeSetter LIBRARY_VERSION = - StringAttributeSetter.create("library.version"); + public static final StringKey LIBRARY_VERSION = stringKey("library.version"); /** Container name. */ - public static final StringAttributeSetter CONTAINER_NAME = - StringAttributeSetter.create("container.name"); + public static final StringKey CONTAINER_NAME = stringKey("container.name"); /** Container id. */ - public static final StringAttributeSetter CONTAINER_ID = - StringAttributeSetter.create("container.id"); + public static final StringKey CONTAINER_ID = stringKey("container.id"); /** Name of the image the container was built on. */ - public static final StringAttributeSetter CONTAINER_IMAGE_NAME = - StringAttributeSetter.create("container.image.name"); + public static final StringKey CONTAINER_IMAGE_NAME = stringKey("container.image.name"); /** Container image tag. */ - public static final StringAttributeSetter CONTAINER_IMAGE_TAG = - StringAttributeSetter.create("container.image.tag"); + public static final StringKey CONTAINER_IMAGE_TAG = stringKey("container.image.tag"); /** The name of the cluster that the pod is running in. */ - public static final StringAttributeSetter K8S_CLUSTER = - StringAttributeSetter.create("k8s.cluster.name"); + public static final StringKey K8S_CLUSTER = stringKey("k8s.cluster.name"); /** The name of the namespace that the pod is running in. */ - public static final StringAttributeSetter K8S_NAMESPACE = - StringAttributeSetter.create("k8s.namespace.name"); + public static final StringKey K8S_NAMESPACE = stringKey("k8s.namespace.name"); /** The name of the pod. */ - public static final StringAttributeSetter K8S_POD = StringAttributeSetter.create("k8s.pod.name"); + public static final StringKey K8S_POD = stringKey("k8s.pod.name"); /** The name of the deployment. */ - public static final StringAttributeSetter K8S_DEPLOYMENT = - StringAttributeSetter.create("k8s.deployment.name"); + public static final StringKey K8S_DEPLOYMENT = stringKey("k8s.deployment.name"); /** Hostname of the host. It contains what the `hostname` command returns on the host machine. */ - public static final StringAttributeSetter HOST_HOSTNAME = - StringAttributeSetter.create("host.hostname"); + public static final StringKey HOST_HOSTNAME = stringKey("host.hostname"); /** Unique host id. For Cloud this must be the instance_id assigned by the cloud provider. */ - public static final StringAttributeSetter HOST_ID = StringAttributeSetter.create("host.id"); + public static final StringKey HOST_ID = stringKey("host.id"); /** * Name of the host. It may contain what `hostname` returns on Unix systems, the fully qualified, * or a name specified by the user. */ - public static final StringAttributeSetter HOST_NAME = StringAttributeSetter.create("host.name"); + public static final StringKey HOST_NAME = stringKey("host.name"); /** Type of host. For Cloud this must be the machine type. */ - public static final StringAttributeSetter HOST_TYPE = StringAttributeSetter.create("host.type"); + public static final StringKey HOST_TYPE = stringKey("host.type"); /** Name of the VM image or OS install the host was instantiated from. */ - public static final StringAttributeSetter HOST_IMAGE_NAME = - StringAttributeSetter.create("host.image.name"); + public static final StringKey HOST_IMAGE_NAME = stringKey("host.image.name"); /** VM image id. For Cloud, this value is from the provider. */ - public static final StringAttributeSetter HOST_IMAGE_ID = - StringAttributeSetter.create("host.image.id"); + public static final StringKey HOST_IMAGE_ID = stringKey("host.image.id"); /** The version string of the VM image. */ - public static final StringAttributeSetter HOST_IMAGE_VERSION = - StringAttributeSetter.create("host.image.version"); + public static final StringKey HOST_IMAGE_VERSION = stringKey("host.image.version"); /** Name of the cloud provider. */ - public static final StringAttributeSetter CLOUD_PROVIDER = - StringAttributeSetter.create("cloud.provider"); + public static final StringKey CLOUD_PROVIDER = stringKey("cloud.provider"); /** The cloud account id used to identify different entities. */ - public static final StringAttributeSetter CLOUD_ACCOUNT = - StringAttributeSetter.create("cloud.account.id"); + public static final StringKey CLOUD_ACCOUNT = stringKey("cloud.account.id"); /** A specific geographical location where different entities can run. */ - public static final StringAttributeSetter CLOUD_REGION = - StringAttributeSetter.create("cloud.region"); + public static final StringKey CLOUD_REGION = stringKey("cloud.region"); /** Zones are a sub set of the region connected through low-latency links. */ - public static final StringAttributeSetter CLOUD_ZONE = StringAttributeSetter.create("cloud.zone"); + public static final StringKey CLOUD_ZONE = stringKey("cloud.zone"); /** The name of the function being executed. */ public static final StringAttributeSetter FAAS_NAME = StringAttributeSetter.create("faas.name"); diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/EnvAutodetectResourceTest.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/EnvAutodetectResourceTest.java index 86116a4b9de..c2583704dc7 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/EnvAutodetectResourceTest.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/EnvAutodetectResourceTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.resources; -import static io.opentelemetry.common.AttributeValue.stringAttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; @@ -44,7 +44,7 @@ void parseResourceAttributes_malformed() { @Test void parseResourceAttributes_single() { Attributes result = EnvAutodetectResource.parseResourceAttributes("value=foo"); - assertThat(result).isEqualTo(Attributes.of("value", stringAttributeValue("foo"))); + assertThat(result).isEqualTo(Attributes.of(stringKey("value"), "foo")); } @Test @@ -53,20 +53,20 @@ void parseResourceAttributes_multi() { assertThat(result) .isEqualTo( Attributes.of( - "value", stringAttributeValue("foo"), - "other", stringAttributeValue("bar"))); + stringKey("value"), "foo", + stringKey("other"), "bar")); } @Test void parseResourceAttributes_whitespace() { Attributes result = EnvAutodetectResource.parseResourceAttributes(" value = foo "); - assertThat(result).isEqualTo(Attributes.of("value", stringAttributeValue("foo"))); + assertThat(result).isEqualTo(Attributes.of(stringKey("value"), "foo")); } @Test void parseResourceAttributes_quotes() { Attributes result = EnvAutodetectResource.parseResourceAttributes("value=\"foo\""); - assertThat(result).isEqualTo(Attributes.of("value", stringAttributeValue("foo"))); + assertThat(result).isEqualTo(Attributes.of(stringKey("value"), "foo")); } @Test @@ -79,7 +79,7 @@ void getResourceAttributes_properties() { .readSystemProperties() .build(); Attributes result = (Attributes) resource.getAttributes(); - assertThat(result).isEqualTo(Attributes.of("value", stringAttributeValue("foo"))); + assertThat(result).isEqualTo(Attributes.of(stringKey("value"), "foo")); System.clearProperty(key); } @@ -94,7 +94,7 @@ public void getResourceAttributes_envvars() { .readSystemProperties() .build(); Attributes result = (Attributes) resource.getAttributes(); - assertThat(result).isEqualTo(Attributes.of("value", stringAttributeValue("foo"))); + assertThat(result).isEqualTo(Attributes.of(stringKey("value"), "foo")); } } } diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java index fe95e21dc38..ecdd7542c8c 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java @@ -16,9 +16,9 @@ package io.opentelemetry.sdk.resources; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import org.junit.jupiter.api.Test; class ResourceProvidersTest { @@ -27,8 +27,8 @@ class ResourceProvidersTest { void default_resource_includes_attributes_from_providers() { Resource resource = Resource.getDefault(); - AttributeValue providerAttribute = resource.getAttributes().get("providerAttribute"); + long providerAttribute = resource.getAttributes().get(longKey("providerAttribute")); assertThat(providerAttribute).isNotNull(); - assertThat(providerAttribute.getLongValue()).isEqualTo(42); + assertThat(providerAttribute).isEqualTo(42); } } diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java index 825784de60f..f5ae6220774 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java @@ -16,12 +16,22 @@ package io.opentelemetry.sdk.resources; +import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import com.google.common.testing.EqualsTester; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; +import java.util.Arrays; +import java.util.Collections; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -33,32 +43,16 @@ class ResourceTest { @BeforeEach void setUp() { - Attributes attributes1 = - Attributes.of( - "a", - AttributeValue.stringAttributeValue("1"), - "b", - AttributeValue.stringAttributeValue("2")); + Attributes attributes1 = Attributes.of(stringKey("a"), "1", stringKey("b"), "2"); Attributes attribute2 = - Attributes.of( - "a", - AttributeValue.stringAttributeValue("1"), - "b", - AttributeValue.stringAttributeValue("3"), - "c", - AttributeValue.stringAttributeValue("4")); + Attributes.of(stringKey("a"), "1", stringKey("b"), "3", stringKey("c"), "4"); resource1 = Resource.create(attributes1); resource2 = Resource.create(attribute2); } @Test void create() { - Attributes attributes = - Attributes.of( - "a", - AttributeValue.stringAttributeValue("1"), - "b", - AttributeValue.stringAttributeValue("2")); + Attributes attributes = Attributes.of(stringKey("a"), "1", stringKey("b"), "2"); Resource resource = Resource.create(attributes); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isEqualTo(2); @@ -73,38 +67,38 @@ void create() { void create_ignoreNull() { Attributes.Builder attributes = Attributes.newBuilder(); - attributes.setAttribute("string", AttributeValue.stringAttributeValue(null)); + attributes.setAttribute(stringKey("string"), null); Resource resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isZero(); - attributes.setAttribute("stringArray", AttributeValue.arrayAttributeValue(null, "a")); + attributes.setAttribute(stringArrayKey("stringArray"), Arrays.asList(null, "a")); resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isEqualTo(1); - attributes.setAttribute("bool", AttributeValue.booleanAttributeValue(true)); + attributes.setAttribute(booleanKey("bool"), true); resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isEqualTo(2); - attributes.setAttribute("boolArray", AttributeValue.arrayAttributeValue(null, true)); + attributes.setAttribute(booleanArrayKey("boolArray"), Arrays.asList(null, true)); resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isEqualTo(3); - attributes.setAttribute("long", AttributeValue.longAttributeValue(0L)); + attributes.setAttribute(longKey("long"), 0L); resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isEqualTo(4); - attributes.setAttribute("longArray", AttributeValue.arrayAttributeValue(1L, null)); + attributes.setAttribute(longArrayKey("longArray"), Arrays.asList(1L, null)); resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isEqualTo(5); - attributes.setAttribute("double", AttributeValue.doubleAttributeValue(1.1)); + attributes.setAttribute(doubleKey("double"), 1.1); resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isEqualTo(6); - attributes.setAttribute("doubleArray", AttributeValue.arrayAttributeValue(1.1, null)); + attributes.setAttribute(doubleArrayKey("doubleArray"), Arrays.asList(1.1, null)); resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isEqualTo(7); @@ -115,49 +109,39 @@ void create_NullEmptyArray() { Attributes.Builder attributes = Attributes.newBuilder(); // Empty arrays should be maintained - attributes.setAttribute( - "stringArrayAttribute", AttributeValue.arrayAttributeValue(new String[0])); - attributes.setAttribute( - "boolArrayAttribute", AttributeValue.arrayAttributeValue(new Boolean[0])); - attributes.setAttribute("longArrayAttribute", AttributeValue.arrayAttributeValue(new Long[0])); - attributes.setAttribute( - "doubleArrayAttribute", AttributeValue.arrayAttributeValue(new Double[0])); + attributes.setAttribute(stringArrayKey("stringArrayAttribute"), Collections.emptyList()); + attributes.setAttribute(booleanArrayKey("boolArrayAttribute"), Collections.emptyList()); + attributes.setAttribute(longArrayKey("longArrayAttribute"), Collections.emptyList()); + attributes.setAttribute(doubleArrayKey("doubleArrayAttribute"), Collections.emptyList()); Resource resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isEqualTo(4); // Arrays with null values should be maintained - attributes.setAttribute( - "ArrayWithNullStringKey", AttributeValue.arrayAttributeValue(new String[] {null})); - attributes.setAttribute( - "ArrayWithNullLongKey", AttributeValue.arrayAttributeValue(new Long[] {null})); - attributes.setAttribute( - "ArrayWithNullDoubleKey", AttributeValue.arrayAttributeValue(new Double[] {null})); - attributes.setAttribute( - "ArrayWithNullBooleanKey", AttributeValue.arrayAttributeValue(new Boolean[] {null})); + attributes.setAttribute(stringArrayKey("ArrayWithNullStringKey"), singletonList(null)); + attributes.setAttribute(longArrayKey("ArrayWithNullLongKey"), singletonList(null)); + attributes.setAttribute(doubleArrayKey("ArrayWithNullDoubleKey"), singletonList(null)); + attributes.setAttribute(booleanArrayKey("ArrayWithNullBooleanKey"), singletonList(null)); resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isEqualTo(8); // Null arrays should be dropped - attributes.setAttribute( - "NullArrayStringKey", AttributeValue.arrayAttributeValue((String[]) null)); - attributes.setAttribute("NullArrayLongKey", AttributeValue.arrayAttributeValue((Long[]) null)); - attributes.setAttribute( - "NullArrayDoubleKey", AttributeValue.arrayAttributeValue((Double[]) null)); - attributes.setAttribute( - "NullArrayBooleanKey", AttributeValue.arrayAttributeValue((Boolean[]) null)); + attributes.setAttribute(stringArrayKey("NullArrayStringKey"), null); + attributes.setAttribute(longArrayKey("NullArrayLongKey"), null); + attributes.setAttribute(doubleArrayKey("NullArrayDoubleKey"), null); + attributes.setAttribute(booleanArrayKey("NullArrayBooleanKey"), null); resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); assertThat(resource.getAttributes().size()).isEqualTo(8); - attributes.setAttribute("dropNullString", (AttributeValue) null); - attributes.setAttribute("dropNullLong", (AttributeValue) null); - attributes.setAttribute("dropNullDouble", (AttributeValue) null); - attributes.setAttribute("dropNullBool", (AttributeValue) null); + attributes.setAttribute(stringKey("dropNullString"), null); + attributes.setAttribute(longKey("dropNullLong"), null); + attributes.setAttribute(doubleKey("dropNullDouble"), null); + attributes.setAttribute(booleanKey("dropNullBool"), null); resource = Resource.create(attributes.build()); assertThat(resource.getAttributes()).isNotNull(); @@ -166,20 +150,9 @@ void create_NullEmptyArray() { @Test void testResourceEquals() { - Attributes attribute1 = - Attributes.of( - "a", - AttributeValue.stringAttributeValue("1"), - "b", - AttributeValue.stringAttributeValue("2")); + Attributes attribute1 = Attributes.of(stringKey("a"), "1", stringKey("b"), "2"); Attributes attribute2 = - Attributes.of( - "a", - AttributeValue.stringAttributeValue("1"), - "b", - AttributeValue.stringAttributeValue("3"), - "c", - AttributeValue.stringAttributeValue("4")); + Attributes.of(stringKey("a"), "1", stringKey("b"), "3", stringKey("c"), "4"); new EqualsTester() .addEqualityGroup(Resource.create(attribute1), Resource.create(attribute1), resource1) .addEqualityGroup(Resource.create(attribute2), resource2) @@ -189,13 +162,7 @@ void testResourceEquals() { @Test void testMergeResources() { Attributes expectedAttributes = - Attributes.of( - "a", - AttributeValue.stringAttributeValue("1"), - "b", - AttributeValue.stringAttributeValue("2"), - "c", - AttributeValue.stringAttributeValue("4")); + Attributes.of(stringKey("a"), "1", stringKey("b"), "2", stringKey("c"), "4"); Resource resource = DEFAULT_RESOURCE.merge(resource1).merge(resource2); assertThat(resource.getAttributes()).isEqualTo(expectedAttributes); @@ -203,12 +170,7 @@ void testMergeResources() { @Test void testMergeResources_Resource1() { - Attributes expectedAttributes = - Attributes.of( - "a", - AttributeValue.stringAttributeValue("1"), - "b", - AttributeValue.stringAttributeValue("2")); + Attributes expectedAttributes = Attributes.of(stringKey("a"), "1", stringKey("b"), "2"); Resource resource = DEFAULT_RESOURCE.merge(resource1); assertThat(resource.getAttributes()).isEqualTo(expectedAttributes); @@ -218,9 +180,9 @@ void testMergeResources_Resource1() { void testMergeResources_Resource1_Null() { Attributes expectedAttributes = Attributes.of( - "a", AttributeValue.stringAttributeValue("1"), - "b", AttributeValue.stringAttributeValue("3"), - "c", AttributeValue.stringAttributeValue("4")); + stringKey("a"), "1", + stringKey("b"), "3", + stringKey("c"), "4"); Resource resource = DEFAULT_RESOURCE.merge(null).merge(resource2); assertThat(resource.getAttributes()).isEqualTo(expectedAttributes); @@ -228,12 +190,7 @@ void testMergeResources_Resource1_Null() { @Test void testMergeResources_Resource2_Null() { - Attributes expectedAttributes = - Attributes.of( - "a", - AttributeValue.stringAttributeValue("1"), - "b", - AttributeValue.stringAttributeValue("2")); + Attributes expectedAttributes = Attributes.of(stringKey("a"), "1", stringKey("b"), "2"); Resource resource = DEFAULT_RESOURCE.merge(resource1).merge(null); assertThat(resource.getAttributes()).isEqualTo(expectedAttributes); } @@ -242,10 +199,8 @@ void testMergeResources_Resource2_Null() { void testSdkTelemetryResources() { Resource resource = Resource.getTelemetrySdk(); ReadableAttributes attributes = resource.getAttributes(); - assertThat(attributes.get("telemetry.sdk.name")) - .isEqualTo(AttributeValue.stringAttributeValue("opentelemetry")); - assertThat(attributes.get("telemetry.sdk.language")) - .isEqualTo(AttributeValue.stringAttributeValue("java")); - assertThat(attributes.get("telemetry.sdk.version").getStringValue()).isNotNull(); + assertThat(attributes.get(stringKey("telemetry.sdk.name"))).isEqualTo("opentelemetry"); + assertThat(attributes.get(stringKey("telemetry.sdk.language"))).isEqualTo("java"); + assertThat(attributes.get(stringKey("telemetry.sdk.version"))).isNotNull(); } } diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/TestResourceProvider.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/TestResourceProvider.java index b4fd107ae88..542a811d4aa 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/TestResourceProvider.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/TestResourceProvider.java @@ -16,13 +16,14 @@ package io.opentelemetry.sdk.resources; -import io.opentelemetry.common.AttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; + import io.opentelemetry.common.Attributes; public class TestResourceProvider extends ResourceProvider { @Override protected Attributes getAttributes() { - return Attributes.of("providerAttribute", AttributeValue.longAttributeValue(42)); + return Attributes.of(longKey("providerAttribute"), 42L); } } diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java index 768054cc7e2..032767f90b5 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -35,8 +35,7 @@ /** Unit tests for {@link BatchRecorderSdk}. */ class BatchRecorderSdkTest { private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create("io.opentelemetry.sdk.metrics.BatchRecorderSdkTest", null); private final TestClock testClock = TestClock.create(); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java index ea1ba3b3e22..e697e2ebbc6 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.metrics.DoubleCounter; @@ -38,8 +38,7 @@ class DoubleCounterSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create("io.opentelemetry.sdk.metrics.DoubleCounterSdkTest", null); private final TestClock testClock = TestClock.create(); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java index 7eb8e2db006..ec14a2ed760 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java @@ -16,9 +16,9 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -34,8 +34,7 @@ class DoubleSumObserverSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create( "io.opentelemetry.sdk.metrics.DoubleSumObserverSdkTest", null); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java index f963bf9b99e..f277ba6b26e 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.metrics.DoubleUpDownCounter; @@ -38,8 +38,7 @@ class DoubleUpDownCounterSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create( "io.opentelemetry.sdk.metrics.DoubleUpDownCounterSdkTest", null); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java index 63f2f16eb6e..474cbf52424 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java @@ -16,9 +16,9 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -34,8 +34,7 @@ class DoubleUpDownSumObserverSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create( "io.opentelemetry.sdk.metrics.DoubleUpDownSumObserverSdkTest", null); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java index 309627ffa47..74c9372b4c8 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java @@ -16,9 +16,9 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -37,8 +37,7 @@ class DoubleValueObserverSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create( "io.opentelemetry.sdk.metrics.DoubleValueObserverSdkTest", null); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java index 34652238823..7fc6b809621 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.metrics.DoubleValueRecorder; @@ -41,8 +41,7 @@ class DoubleValueRecorderSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create( "io.opentelemetry.sdk.metrics.DoubleValueRecorderSdkTest", null); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java index 731b69c9916..119e4a59129 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.metrics.LongCounter; @@ -39,8 +39,7 @@ class LongCounterSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create("io.opentelemetry.sdk.metrics.LongCounterSdkTest", null); private final TestClock testClock = TestClock.create(); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java index 1be718344ab..59b61e243d5 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java @@ -16,9 +16,9 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -34,8 +34,7 @@ class LongSumObserverSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create( "io.opentelemetry.sdk.metrics.LongSumObserverSdkTest", null); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java index e34417d9567..851da564dc0 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.metrics.LongUpDownCounter; @@ -38,8 +38,7 @@ class LongUpDownCounterSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create( "io.opentelemetry.sdk.metrics.LongUpDownCounterSdkTest", null); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java index 7760f70987e..496e4aa1963 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java @@ -16,9 +16,9 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -34,8 +34,7 @@ class LongUpDownSumObserverSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create( "io.opentelemetry.sdk.metrics.LongUpDownSumObserverSdkTest", null); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java index 4abe919fecb..85f8bd71784 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java @@ -16,9 +16,9 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -37,8 +37,7 @@ class LongValueObserverSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create( "io.opentelemetry.sdk.metrics.LongValueObserverSdkTest", null); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java index c2e17c07ff2..55516bc9d0b 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.metrics.LongValueRecorder; @@ -41,8 +41,7 @@ class LongValueRecorderSdkTest { private static final long SECOND_NANOS = 1_000_000_000; private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create( "io.opentelemetry.sdk.metrics.LongValueRecorderSdkTest", null); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java index f759dbce676..332f212a023 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.metrics; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.Labels; import io.opentelemetry.metrics.BatchRecorder; @@ -41,8 +41,7 @@ /** Unit tests for {@link MeterSdk}. */ class MeterSdkTest { private static final Resource RESOURCE = - Resource.create( - Attributes.of("resource_key", AttributeValue.stringAttributeValue("resource_value"))); + Resource.create(Attributes.of(stringKey("resource_key"), "resource_value")); private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO = InstrumentationLibraryInfo.create("io.opentelemetry.sdk.metrics.MeterSdkTest", null); private final TestClock testClock = TestClock.create(); diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java index d3425629f9d..b7ecd18c187 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java @@ -16,20 +16,32 @@ package io.opentelemetry.sdk.trace; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeConsumer; +import io.opentelemetry.common.AttributeKey; +import io.opentelemetry.common.AttributeKeyImpl; +import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; +import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.LongKey; +import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.common.Attributes; +import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import java.util.HashMap; +import java.util.List; import java.util.Map; -import javax.annotation.Nullable; /** * A map with a fixed capacity that drops attributes when the map gets full. * *

Note: this doesn't implement the Map interface, but behaves very similarly to one. */ +@SuppressWarnings({"rawtypes", "unchecked"}) final class AttributesMap implements ReadableAttributes { - private final Map data = new HashMap<>(); + private final Map data = new HashMap<>(); private final long capacity; private int totalAddedValues = 0; @@ -38,7 +50,7 @@ final class AttributesMap implements ReadableAttributes { this.capacity = capacity; } - public void put(String key, AttributeValue value) { + public void put(AttributeKey key, T value) { totalAddedValues++; if (data.size() >= capacity && !data.containsKey(key)) { return; @@ -46,7 +58,7 @@ public void put(String key, AttributeValue value) { data.put(key, value); } - void remove(String key) { + void remove(AttributeKey key) { data.remove(key); } @@ -54,6 +66,12 @@ int getTotalAddedValues() { return totalAddedValues; } + @SuppressWarnings("unchecked") + @Override + public T get(AttributeKey key) { + return (T) data.get(key); + } + @Override public int size() { return data.size(); @@ -64,17 +82,40 @@ public boolean isEmpty() { return data.isEmpty(); } + @SuppressWarnings({"unchecked", "rawtypes"}) @Override - public void forEach(KeyValueConsumer consumer) { - for (Map.Entry entry : data.entrySet()) { - consumer.consume(entry.getKey(), entry.getValue()); + public void forEach(AttributeConsumer consumer) { + for (Map.Entry entry : data.entrySet()) { + AttributeKey key = entry.getKey(); + Object value = entry.getValue(); + if (key instanceof AttributeKeyImpl.StringKey) { + consumer.consume((StringKey) key, (String) value); + } else if (key instanceof AttributeKeyImpl.BooleanKey) { + consumer.consume((BooleanKey) key, (boolean) value); + } else if (key instanceof AttributeKeyImpl.LongKey) { + consumer.consume((LongKey) key, (long) value); + } else if (key instanceof AttributeKeyImpl.DoubleKey) { + consumer.consume((DoubleKey) key, (double) value); + } else if (key instanceof AttributeKeyImpl.StringArrayKey) { + consumer.consume((StringArrayKey) key, (List) value); + } else if (key instanceof AttributeKeyImpl.BooleanArrayKey) { + consumer.consume((BooleanArrayKey) key, (List) value); + } else if (key instanceof AttributeKeyImpl.LongArrayKey) { + consumer.consume((LongArrayKey) key, (List) value); + } else if (key instanceof AttributeKeyImpl.DoubleArrayKey) { + consumer.consume((DoubleArrayKey) key, (List) value); + } } } - @Nullable + @SuppressWarnings({"rawtypes", "unchecked"}) @Override - public AttributeValue get(String key) { - return data.get(key); + public void forEachRaw(RawAttributeConsumer consumer) { + for (Map.Entry entry : data.entrySet()) { + AttributeKey key = entry.getKey(); + Object value = entry.getValue(); + consumer.consume(key, value); + } } @Override @@ -89,9 +130,10 @@ public String toString() { + '}'; } + @SuppressWarnings("rawtypes") ReadableAttributes immutableCopy() { Attributes.Builder builder = Attributes.newBuilder(); - for (Map.Entry entry : data.entrySet()) { + for (Map.Entry entry : data.entrySet()) { builder.setAttribute(entry.getKey(), entry.getValue()); } return builder.build(); diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java index 3ff00020730..a47bc9aa294 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java @@ -16,11 +16,16 @@ package io.opentelemetry.sdk.trace; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; + import com.google.common.collect.EvictingQueue; -import io.opentelemetry.common.AttributeConsumer; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; +import io.opentelemetry.common.ReadableKeyValuePairs; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.sdk.common.Clock; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -283,27 +288,27 @@ Clock getClock() { @Override public void setAttribute(String key, String value) { - setAttribute(key, AttributeValue.stringAttributeValue(value)); + setAttribute(stringKey(key), value); } @Override public void setAttribute(String key, long value) { - setAttribute(key, AttributeValue.longAttributeValue(value)); + setAttribute(longKey(key), value); } @Override public void setAttribute(String key, double value) { - setAttribute(key, AttributeValue.doubleAttributeValue(value)); + setAttribute(doubleKey(key), value); } @Override public void setAttribute(String key, boolean value) { - setAttribute(key, AttributeValue.booleanAttributeValue(value)); + setAttribute(booleanKey(key), value); } @Override - public void setAttribute(String key, AttributeValue value) { - if (key == null || key.length() == 0) { + public void setAttribute(AttributeKey key, T value) { + if (key == null || key.get() == null || key.get().length() == 0) { return; } synchronized (lock) { @@ -311,7 +316,7 @@ public void setAttribute(String key, AttributeValue value) { logger.log(Level.FINE, "Calling setAttribute() on an ended Span."); return; } - if (value == null || value.isNull()) { + if (value == null) { if (attributes != null) { attributes.remove(key); } @@ -322,7 +327,7 @@ public void setAttribute(String key, AttributeValue value) { } if (traceConfig.shouldTruncateStringAttributeValues()) { - value = StringUtils.truncateToSize(value, traceConfig.getMaxLengthOfAttributeValues()); + value = StringUtils.truncateToSize(key, value, traceConfig.getMaxLengthOfAttributeValues()); } attributes.put(key, value); @@ -437,13 +442,14 @@ public void recordException(Throwable exception, Attributes additionalAttributes long timestamp = clock.now(); Attributes.Builder attributes = additionalAttributes != null ? additionalAttributes.toBuilder() : Attributes.newBuilder(); - SemanticAttributes.EXCEPTION_TYPE.set(attributes, exception.getClass().getCanonicalName()); + attributes.setAttribute( + SemanticAttributes.EXCEPTION_TYPE, exception.getClass().getCanonicalName()); if (exception.getMessage() != null) { - SemanticAttributes.EXCEPTION_MESSAGE.set(attributes, exception.getMessage()); + attributes.setAttribute(SemanticAttributes.EXCEPTION_MESSAGE, exception.getMessage()); } StringWriter writer = new StringWriter(); exception.printStackTrace(new PrintWriter(writer)); - SemanticAttributes.EXCEPTION_STACKTRACE.set(attributes, writer.toString()); + attributes.setAttribute(SemanticAttributes.EXCEPTION_STACKTRACE, writer.toString()); addEvent(SemanticAttributes.EXCEPTION_EVENT_NAME, attributes.build(), timestamp); } @@ -628,7 +634,9 @@ public String toString() { return sb.toString(); } - private static class LimitingAttributeConsumer implements AttributeConsumer { + @SuppressWarnings({"rawtypes", "unchecked"}) + private static class LimitingAttributeConsumer + implements ReadableKeyValuePairs.KeyValueConsumer { private final int limit; private final Attributes.Builder builder; private int added; @@ -639,7 +647,7 @@ public LimitingAttributeConsumer(int limit, Attributes.Builder builder) { } @Override - public void consume(String key, AttributeValue value) { + public void consume(AttributeKey key, Object value) { if (added < limit) { builder.setAttribute(key, value); added++; diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java index 44974e481df..94ae36155ae 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java @@ -16,10 +16,11 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeValue.doubleAttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; import com.google.auto.value.AutoValue; import com.google.common.base.Preconditions; +import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.trace.Sampler.Decision; @@ -29,7 +30,6 @@ import io.opentelemetry.trace.Span.Kind; import io.opentelemetry.trace.SpanContext; import io.opentelemetry.trace.TraceId; -import io.opentelemetry.trace.attributes.DoubleAttributeSetter; import java.util.List; import java.util.Objects; import javax.annotation.Nullable; @@ -48,8 +48,7 @@ public final class Samplers { *

See https://github.com/open-telemetry/opentelemetry-specification/pull/570 */ // Visible for tests. - static final DoubleAttributeSetter SAMPLING_PROBABILITY = - DoubleAttributeSetter.create("sampling.probability"); + static final DoubleKey SAMPLING_PROBABILITY = doubleKey("sampling.probability"); private static final SamplingResult EMPTY_RECORDED_AND_SAMPLED_SAMPLING_RESULT = SamplingResultImpl.createWithoutAttributes(Decision.RECORD_AND_SAMPLED); @@ -495,7 +494,7 @@ abstract static class SamplingResultImpl implements SamplingResult { */ static SamplingResult createWithProbability(Decision decision, double probability) { return new AutoValue_Samplers_SamplingResultImpl( - decision, Attributes.of(SAMPLING_PROBABILITY.key(), doubleAttributeValue(probability))); + decision, Attributes.of(SAMPLING_PROBABILITY, probability)); } /** diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index fc1bd985f53..18fe953292c 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -16,10 +16,15 @@ package io.opentelemetry.sdk.trace; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; + import io.grpc.Context; -import io.opentelemetry.common.AttributeConsumer; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; +import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.internal.Utils; @@ -153,29 +158,28 @@ public Span.Builder addLink(io.opentelemetry.trace.Link link) { @Override public Span.Builder setAttribute(String key, String value) { - return setAttribute(key, AttributeValue.stringAttributeValue(value)); + return setAttribute(stringKey(key), value); } @Override public Span.Builder setAttribute(String key, long value) { - return setAttribute(key, AttributeValue.longAttributeValue(value)); + return setAttribute(longKey(key), value); } @Override public Span.Builder setAttribute(String key, double value) { - return setAttribute(key, AttributeValue.doubleAttributeValue(value)); + return setAttribute(doubleKey(key), value); } @Override public Span.Builder setAttribute(String key, boolean value) { - return setAttribute(key, AttributeValue.booleanAttributeValue(value)); + return setAttribute(booleanKey(key), value); } @Override - public Span.Builder setAttribute(String key, AttributeValue value) { + public Span.Builder setAttribute(AttributeKey key, T value) { Objects.requireNonNull(key, "key"); - if (value == null - || (value.getType() == AttributeValue.Type.STRING && value.getStringValue() == null)) { + if (value == null) { if (attributes != null) { attributes.remove(key); } @@ -186,7 +190,7 @@ public Span.Builder setAttribute(String key, AttributeValue value) { } if (traceConfig.shouldTruncateStringAttributeValues()) { - value = StringUtils.truncateToSize(value, traceConfig.getMaxLengthOfAttributeValues()); + value = StringUtils.truncateToSize(key, value, traceConfig.getMaxLengthOfAttributeValues()); } attributes.put(key, value); @@ -240,10 +244,10 @@ public Span startSpan() { if (attributes == null) { attributes = new AttributesMap(traceConfig.getMaxNumberOfAttributes()); } - samplingAttributes.forEach( - new AttributeConsumer() { + samplingAttributes.forEachRaw( + new RawAttributeConsumer() { @Override - public void consume(String key, AttributeValue value) { + public void consume(AttributeKey key, T value) { attributes.put(key, value); } }); diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java index 94742f3ab73..325ae514b60 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java @@ -16,12 +16,21 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeValue.stringAttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; +import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.internal.TestClock; @@ -37,6 +46,7 @@ import io.opentelemetry.trace.Status; import io.opentelemetry.trace.TraceFlags; import io.opentelemetry.trace.TraceState; +import io.opentelemetry.trace.attributes.SemanticAttributes; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Arrays; @@ -56,6 +66,7 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +@SuppressWarnings({"rawtypes", "unchecked"}) class RecordEventsReadableSpanTest { private static final String SPAN_NAME = "MySpanName"; private static final String SPAN_NEW_NAME = "NewName"; @@ -73,7 +84,7 @@ class RecordEventsReadableSpanTest { private final Resource resource = Resource.getEmpty(); private final InstrumentationLibraryInfo instrumentationLibraryInfo = InstrumentationLibraryInfo.create("theName", null); - private final Map attributes = new HashMap<>(); + private final Map attributes = new HashMap<>(); private Attributes expectedAttributes; private final io.opentelemetry.trace.Link link = Link.create(spanContext); @Mock private SpanProcessor spanProcessor; @@ -83,14 +94,13 @@ class RecordEventsReadableSpanTest { @BeforeEach void setUp() { MockitoAnnotations.initMocks(this); - attributes.put("MyStringAttributeKey", stringAttributeValue("MyStringAttributeValue")); - attributes.put("MyLongAttributeKey", AttributeValue.longAttributeValue(123L)); - attributes.put("MyBooleanAttributeKey", AttributeValue.booleanAttributeValue(false)); + attributes.put(stringKey("MyStringAttributeKey"), "MyStringAttributeValue"); + attributes.put(longKey("MyLongAttributeKey"), 123L); + attributes.put(booleanKey("MyBooleanAttributeKey"), false); Attributes.Builder builder = Attributes.newBuilder() - .setAttribute( - "MySingleStringAttributeKey", stringAttributeValue("MySingleStringAttributeValue")); - for (Map.Entry entry : attributes.entrySet()) { + .setAttribute("MySingleStringAttributeKey", "MySingleStringAttributeValue"); + for (Map.Entry entry : attributes.entrySet()) { builder.setAttribute(entry.getKey(), entry.getValue()); } expectedAttributes = builder.build(); @@ -119,7 +129,7 @@ void nothingChangedAfterEnd() { @Test void lazyLinksAreResolved() { - final Attributes attributes = Attributes.of("attr", stringAttributeValue("val")); + final Attributes attributes = Attributes.of(stringKey("attr"), "val"); io.opentelemetry.trace.Link link = new io.opentelemetry.trace.Link() { @Override @@ -265,7 +275,7 @@ void toSpanData_SpanDataDoesNotChangeWhenModifyingSpan() { // Assert that the snapshot does not reflect the modified state, but the state of the time when // toSpanData was called. assertThat(spanData.getAttributes().size()).isEqualTo(attributes.size()); - assertThat(spanData.getAttributes().get("anotherKey")).isNull(); + assertThat(spanData.getAttributes().get(stringKey("anotherKey"))).isNull(); assertThat(spanData.getHasEnded()).isFalse(); assertThat(spanData.getEndEpochNanos()).isEqualTo(0); assertThat(spanData.getName()).isEqualTo(SPAN_NAME); @@ -275,8 +285,7 @@ void toSpanData_SpanDataDoesNotChangeWhenModifyingSpan() { // state. spanData = span.toSpanData(); assertThat(spanData.getAttributes().size()).isEqualTo(attributes.size() + 1); - assertThat(spanData.getAttributes().get("anotherKey")) - .isEqualTo(AttributeValue.stringAttributeValue("anotherValue")); + assertThat(spanData.getAttributes().get(stringKey("anotherKey"))).isEqualTo("anotherValue"); assertThat(spanData.getHasEnded()).isTrue(); assertThat(spanData.getEndEpochNanos()).isGreaterThan(0); assertThat(spanData.getName()).isEqualTo("changedName"); @@ -372,74 +381,68 @@ void setAttribute() { span.setAttribute("StringKey", "StringVal"); span.setAttribute("NullStringKey", (String) null); span.setAttribute("EmptyStringKey", ""); - span.setAttribute("NullStringAttributeValue", stringAttributeValue(null)); - span.setAttribute("EmptyStringAttributeValue", stringAttributeValue("")); + span.setAttribute(stringKey("NullStringAttributeValue"), null); + span.setAttribute(stringKey("EmptyStringAttributeValue"), ""); span.setAttribute("LongKey", 1000L); span.setAttribute("DoubleKey", 10.0); span.setAttribute("BooleanKey", false); span.setAttribute( - "ArrayStringKey", - AttributeValue.arrayAttributeValue("StringVal", null, "", "StringVal2")); - span.setAttribute("ArrayLongKey", AttributeValue.arrayAttributeValue(1L, 2L, 3L, 4L, 5L)); + stringArrayKey("ArrayStringKey"), Arrays.asList("StringVal", null, "", "StringVal2")); + span.setAttribute(longArrayKey("ArrayLongKey"), Arrays.asList(1L, 2L, 3L, 4L, 5L)); + span.setAttribute(doubleArrayKey("ArrayDoubleKey"), Arrays.asList(0.1, 2.3, 4.5, 6.7, 8.9)); span.setAttribute( - "ArrayDoubleKey", AttributeValue.arrayAttributeValue(0.1, 2.3, 4.5, 6.7, 8.9)); - span.setAttribute( - "ArrayBooleanKey", AttributeValue.arrayAttributeValue(true, false, false, true)); + booleanArrayKey("ArrayBooleanKey"), Arrays.asList(true, false, false, true)); // These should be dropped - span.setAttribute("NullArrayStringKey", AttributeValue.arrayAttributeValue((String[]) null)); - span.setAttribute("NullArrayLongKey", AttributeValue.arrayAttributeValue((Long[]) null)); - span.setAttribute("NullArrayDoubleKey", AttributeValue.arrayAttributeValue((Double[]) null)); - span.setAttribute( - "NullArrayBooleanKey", AttributeValue.arrayAttributeValue((Boolean[]) null)); + span.setAttribute(stringArrayKey("NullArrayStringKey"), Arrays.asList((String[]) null)); + span.setAttribute(longArrayKey("NullArrayLongKey"), Arrays.asList((Long[]) null)); + span.setAttribute(doubleArrayKey("NullArrayDoubleKey"), Arrays.asList((Double[]) null)); + span.setAttribute(booleanArrayKey("NullArrayBooleanKey"), Arrays.asList((Boolean[]) null)); // These should be maintained + span.setAttribute(longArrayKey("ArrayWithNullLongKey"), Arrays.asList(new Long[] {null})); span.setAttribute( - "ArrayWithNullLongKey", AttributeValue.arrayAttributeValue(new Long[] {null})); - span.setAttribute( - "ArrayWithNullStringKey", AttributeValue.arrayAttributeValue(new String[] {null})); + stringArrayKey("ArrayWithNullStringKey"), Arrays.asList(new String[] {null})); span.setAttribute( - "ArrayWithNullDoubleKey", AttributeValue.arrayAttributeValue(new Double[] {null})); + doubleArrayKey("ArrayWithNullDoubleKey"), Arrays.asList(new Double[] {null})); span.setAttribute( - "ArrayWithNullBooleanKey", AttributeValue.arrayAttributeValue(new Boolean[] {null})); + booleanArrayKey("ArrayWithNullBooleanKey"), Arrays.asList(new Boolean[] {null})); } finally { span.end(); } SpanData spanData = span.toSpanData(); assertThat(spanData.getAttributes().size()).isEqualTo(14); - assertThat(spanData.getAttributes().get("StringKey")).isNotNull(); - assertThat(spanData.getAttributes().get("EmptyStringKey")).isNotNull(); - assertThat(spanData.getAttributes().get("EmptyStringAttributeValue")).isNotNull(); - assertThat(spanData.getAttributes().get("LongKey")).isNotNull(); - assertThat(spanData.getAttributes().get("DoubleKey")).isNotNull(); - assertThat(spanData.getAttributes().get("BooleanKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayStringKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayLongKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayDoubleKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayBooleanKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayWithNullLongKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayWithNullStringKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayWithNullDoubleKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayWithNullBooleanKey")).isNotNull(); - assertThat(spanData.getAttributes().get("ArrayStringKey").getStringArrayValue().size()) - .isEqualTo(4); - assertThat(spanData.getAttributes().get("ArrayLongKey").getLongArrayValue().size()) - .isEqualTo(5); - assertThat(spanData.getAttributes().get("ArrayDoubleKey").getDoubleArrayValue().size()) - .isEqualTo(5); - assertThat(spanData.getAttributes().get("ArrayBooleanKey").getBooleanArrayValue().size()) + assertThat(spanData.getAttributes().get(stringKey("StringKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(stringKey("EmptyStringKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(stringKey("EmptyStringAttributeValue"))).isNotNull(); + assertThat(spanData.getAttributes().get(longKey("LongKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(doubleKey("DoubleKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(booleanKey("BooleanKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(stringArrayKey("ArrayStringKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(longArrayKey("ArrayLongKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(doubleArrayKey("ArrayDoubleKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(booleanArrayKey("ArrayBooleanKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(longArrayKey("ArrayWithNullLongKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(stringArrayKey("ArrayWithNullStringKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(doubleArrayKey("ArrayWithNullDoubleKey"))).isNotNull(); + assertThat(spanData.getAttributes().get(booleanArrayKey("ArrayWithNullBooleanKey"))) + .isNotNull(); + assertThat(spanData.getAttributes().get(stringArrayKey("ArrayStringKey")).size()).isEqualTo(4); + assertThat(spanData.getAttributes().get(longArrayKey("ArrayLongKey")).size()).isEqualTo(5); + assertThat(spanData.getAttributes().get(doubleArrayKey("ArrayDoubleKey")).size()).isEqualTo(5); + assertThat(spanData.getAttributes().get(booleanArrayKey("ArrayBooleanKey")).size()) .isEqualTo(4); } @Test void setAttribute_emptyKeys() { RecordEventsReadableSpan span = createTestRootSpan(); - span.setAttribute("", AttributeValue.stringAttributeValue("")); + span.setAttribute("", ""); span.setAttribute("", 1000L); span.setAttribute("", 10.0); span.setAttribute("", false); - span.setAttribute("", AttributeValue.arrayAttributeValue(new String[0])); - span.setAttribute("", AttributeValue.arrayAttributeValue(new Boolean[0])); - span.setAttribute("", AttributeValue.arrayAttributeValue(new Long[0])); - span.setAttribute("", AttributeValue.arrayAttributeValue(new Double[0])); + span.setAttribute(stringArrayKey(""), Collections.emptyList()); + span.setAttribute(booleanArrayKey(""), Collections.emptyList()); + span.setAttribute(longArrayKey(""), Collections.emptyList()); + span.setAttribute(doubleArrayKey(""), Collections.emptyList()); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(0); } @@ -450,36 +453,36 @@ void setAttribute_nullKeys() { span.setAttribute(null, 1000L); span.setAttribute(null, 10.0); span.setAttribute(null, false); - span.setAttribute(null, AttributeValue.arrayAttributeValue(new String[0])); - span.setAttribute(null, AttributeValue.arrayAttributeValue(new Boolean[0])); - span.setAttribute(null, AttributeValue.arrayAttributeValue(new Long[0])); - span.setAttribute(null, AttributeValue.arrayAttributeValue(new Double[0])); + span.setAttribute(null, Collections.emptyList()); + span.setAttribute(null, Collections.emptyList()); + span.setAttribute(null, Collections.emptyList()); + span.setAttribute(null, Collections.emptyList()); assertThat(span.toSpanData().getAttributes().size()).isZero(); } @Test void setAttribute_emptyArrayAttributeValue() { RecordEventsReadableSpan span = createTestRootSpan(); - span.setAttribute("stringArrayAttribute", AttributeValue.arrayAttributeValue((String[]) null)); + span.setAttribute(stringArrayKey("stringArrayAttribute"), Arrays.asList((String[]) null)); assertThat(span.toSpanData().getAttributes().size()).isZero(); - span.setAttribute("stringArrayAttribute", AttributeValue.arrayAttributeValue(new String[0])); + span.setAttribute(stringArrayKey("stringArrayAttribute"), Collections.emptyList()); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(1); - span.setAttribute("boolArrayAttribute", AttributeValue.arrayAttributeValue((Boolean[]) null)); + span.setAttribute(booleanArrayKey("boolArrayAttribute"), Arrays.asList((Boolean[]) null)); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(1); - span.setAttribute("boolArrayAttribute", AttributeValue.arrayAttributeValue(new Boolean[0])); + span.setAttribute(booleanArrayKey("boolArrayAttribute"), Collections.emptyList()); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(2); - span.setAttribute("longArrayAttribute", AttributeValue.arrayAttributeValue((Long[]) null)); + span.setAttribute(longArrayKey("longArrayAttribute"), Arrays.asList((Long[]) null)); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(2); - span.setAttribute("longArrayAttribute", AttributeValue.arrayAttributeValue(new Long[0])); + span.setAttribute(longArrayKey("longArrayAttribute"), Collections.emptyList()); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(3); - span.setAttribute("doubleArrayAttribute", AttributeValue.arrayAttributeValue((Double[]) null)); + span.setAttribute(doubleArrayKey("doubleArrayAttribute"), Arrays.asList((Double[]) null)); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(3); - span.setAttribute("doubleArrayAttribute", AttributeValue.arrayAttributeValue(new Double[0])); + span.setAttribute(doubleArrayKey("doubleArrayAttribute"), Collections.emptyList()); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(4); - span.setAttribute("stringArrayAttribute", (AttributeValue) null); - span.setAttribute("boolArrayAttribute", (AttributeValue) null); - span.setAttribute("longArrayAttribute", (AttributeValue) null); - span.setAttribute("doubleArrayAttribute", (AttributeValue) null); + span.setAttribute(stringArrayKey("stringArrayAttribute"), null); + span.setAttribute(booleanArrayKey("boolArrayAttribute"), null); + span.setAttribute(longArrayKey("longArrayAttribute"), null); + span.setAttribute(doubleArrayKey("doubleArrayAttribute"), null); assertThat(span.toSpanData().getAttributes().size()).isZero(); } @@ -488,8 +491,8 @@ void setAttribute_nullStringValue() { RecordEventsReadableSpan span = createTestRootSpan(); span.setAttribute("nullString", (String) null); span.setAttribute("emptyString", ""); - span.setAttribute("nullStringAttributeValue", stringAttributeValue(null)); - span.setAttribute("emptyStringAttributeValue", stringAttributeValue("")); + span.setAttribute(stringKey("nullStringAttributeValue"), null); + span.setAttribute(stringKey("emptyStringAttributeValue"), ""); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(2); span.setAttribute("emptyString", (String) null); span.setAttribute("emptyStringAttributeValue", (String) null); @@ -500,26 +503,26 @@ void setAttribute_nullStringValue() { void setAttribute_nullAttributeValue() { RecordEventsReadableSpan span = createTestRootSpan(); span.setAttribute("emptyString", ""); - span.setAttribute("nullString", (AttributeValue) null); - span.setAttribute("nullStringAttributeValue", stringAttributeValue(null)); - span.setAttribute("emptyStringAttributeValue", stringAttributeValue("")); + span.setAttribute(stringKey("nullString"), null); + span.setAttribute(stringKey("nullStringAttributeValue"), null); + span.setAttribute(stringKey("emptyStringAttributeValue"), ""); span.setAttribute("longAttribute", 0L); span.setAttribute("boolAttribute", false); span.setAttribute("doubleAttribute", 0.12345f); - span.setAttribute("stringArrayAttribute", AttributeValue.arrayAttributeValue("", null)); - span.setAttribute("boolArrayAttribute", AttributeValue.arrayAttributeValue(true, null)); - span.setAttribute("longArrayAttribute", AttributeValue.arrayAttributeValue(12345L, null)); - span.setAttribute("doubleArrayAttribute", AttributeValue.arrayAttributeValue(1.2345, null)); + span.setAttribute(stringArrayKey("stringArrayAttribute"), Arrays.asList("", null)); + span.setAttribute(booleanArrayKey("boolArrayAttribute"), Arrays.asList(true, null)); + span.setAttribute(longArrayKey("longArrayAttribute"), Arrays.asList(12345L, null)); + span.setAttribute(doubleArrayKey("doubleArrayAttribute"), Arrays.asList(1.2345, null)); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(9); - span.setAttribute("emptyString", (AttributeValue) null); - span.setAttribute("emptyStringAttributeValue", (AttributeValue) null); - span.setAttribute("longAttribute", (AttributeValue) null); - span.setAttribute("boolAttribute", (AttributeValue) null); - span.setAttribute("doubleAttribute", (AttributeValue) null); - span.setAttribute("stringArrayAttribute", (AttributeValue) null); - span.setAttribute("boolArrayAttribute", (AttributeValue) null); - span.setAttribute("longArrayAttribute", (AttributeValue) null); - span.setAttribute("doubleArrayAttribute", (AttributeValue) null); + span.setAttribute(stringKey("emptyString"), null); + span.setAttribute(stringKey("emptyStringAttributeValue"), null); + span.setAttribute(longKey("longAttribute"), null); + span.setAttribute(booleanKey("boolAttribute"), null); + span.setAttribute(doubleKey("doubleAttribute"), null); + span.setAttribute(stringArrayKey("stringArrayAttribute"), null); + span.setAttribute(booleanArrayKey("boolArrayAttribute"), null); + span.setAttribute(longArrayKey("longArrayAttribute"), null); + span.setAttribute(doubleArrayKey("doubleArrayAttribute"), null); assertThat(span.toSpanData().getAttributes().isEmpty()).isTrue(); } @@ -540,7 +543,7 @@ public Attributes getAttributes() { }; try { span.addEvent("event1"); - span.addEvent("event2", Attributes.of("e1key", stringAttributeValue("e1Value"))); + span.addEvent("event2", Attributes.of(stringKey("e1key"), "e1Value")); span.addEvent(customEvent); } finally { span.end(); @@ -566,7 +569,7 @@ void droppingAttributes() { RecordEventsReadableSpan span = createTestSpan(traceConfig); try { for (int i = 0; i < 2 * maxNumberOfAttributes; i++) { - span.setAttribute("MyStringAttributeKey" + i, AttributeValue.longAttributeValue(i)); + span.setAttribute(longKey("MyStringAttributeKey" + i), (long) i); } SpanData spanData = span.toSpanData(); assertThat(spanData.getAttributes().size()).isEqualTo(maxNumberOfAttributes); @@ -590,7 +593,7 @@ void droppingAndAddingAttributes() { RecordEventsReadableSpan span = createTestSpan(traceConfig); try { for (int i = 0; i < 2 * maxNumberOfAttributes; i++) { - span.setAttribute("MyStringAttributeKey" + i, AttributeValue.longAttributeValue(i)); + span.setAttribute(longKey("MyStringAttributeKey" + i), (long) i); } SpanData spanData = span.toSpanData(); assertThat(spanData.getAttributes().size()).isEqualTo(maxNumberOfAttributes); @@ -598,7 +601,7 @@ void droppingAndAddingAttributes() { for (int i = 0; i < maxNumberOfAttributes / 2; i++) { int val = i + maxNumberOfAttributes * 3 / 2; - span.setAttribute("MyStringAttributeKey" + i, AttributeValue.longAttributeValue(val)); + span.setAttribute(longKey("MyStringAttributeKey" + i), (long) val); } spanData = span.toSpanData(); assertThat(spanData.getAttributes().size()).isEqualTo(maxNumberOfAttributes); @@ -606,13 +609,13 @@ void droppingAndAddingAttributes() { for (int i = 0; i < maxNumberOfAttributes / 2; i++) { int val = i + maxNumberOfAttributes * 3 / 2; AttributeValue expectedValue = AttributeValue.longAttributeValue(val); - assertThat(spanData.getAttributes().get("MyStringAttributeKey" + i)) + assertThat(spanData.getAttributes().get(longKey("MyStringAttributeKey" + i))) .isEqualTo(expectedValue); } // Test that we have the newest re-added initial entries. for (int i = maxNumberOfAttributes / 2; i < maxNumberOfAttributes; i++) { AttributeValue expectedValue = AttributeValue.longAttributeValue(i); - assertThat(spanData.getAttributes().get("MyStringAttributeKey" + i)) + assertThat(spanData.getAttributes().get(longKey("MyStringAttributeKey" + i))) .isEqualTo(expectedValue); } } finally { @@ -682,9 +685,9 @@ void recordException() { assertThat(event.getAttributes()) .isEqualTo( Attributes.newBuilder() - .setAttribute("exception.type", "java.lang.IllegalStateException") - .setAttribute("exception.message", "there was an exception") - .setAttribute("exception.stacktrace", stacktrace) + .setAttribute(SemanticAttributes.EXCEPTION_TYPE, "java.lang.IllegalStateException") + .setAttribute(SemanticAttributes.EXCEPTION_MESSAGE, "there was an exception") + .setAttribute(SemanticAttributes.EXCEPTION_STACKTRACE, stacktrace) .build()); } @@ -698,7 +701,7 @@ void recordException_noMessage() { List events = span.toSpanData().getEvents(); assertThat(events).hasSize(1); Event event = events.get(0); - assertThat(event.getAttributes().get("exception.message")).isNull(); + assertThat(event.getAttributes().get(SemanticAttributes.EXCEPTION_MESSAGE)).isNull(); } private static class InnerClassException extends Exception {} @@ -713,10 +716,8 @@ void recordException_innerClassException() { List events = span.toSpanData().getEvents(); assertThat(events).hasSize(1); Event event = events.get(0); - assertThat(event.getAttributes().get("exception.type")) - .isEqualTo( - stringAttributeValue( - "io.opentelemetry.sdk.trace.RecordEventsReadableSpanTest.InnerClassException")); + assertThat(event.getAttributes().get(SemanticAttributes.EXCEPTION_TYPE)) + .isEqualTo("io.opentelemetry.sdk.trace.RecordEventsReadableSpanTest.InnerClassException"); } @Test @@ -734,10 +735,10 @@ void recordException_additionalAttributes() { span.recordException( exception, Attributes.of( - "key1", - stringAttributeValue("this is an additional attribute"), - "exception.message", - stringAttributeValue("this is a precedence attribute"))); + stringKey("key1"), + "this is an additional attribute", + stringKey("exception.message"), + "this is a precedence attribute")); List events = span.toSpanData().getEvents(); assertThat(events).hasSize(1); @@ -779,7 +780,7 @@ void badArgsIgnored() { } private RecordEventsReadableSpan createTestSpanWithAttributes( - Map attributes) { + Map attributes) { AttributesMap attributesMap = new AttributesMap(TraceConfig.getDefault().getMaxNumberOfAttributes()); attributes.forEach(attributesMap::put); @@ -838,11 +839,10 @@ private RecordEventsReadableSpan createTestSpan( } private void spanDoWork(RecordEventsReadableSpan span, @Nullable Status status) { - span.setAttribute( - "MySingleStringAttributeKey", stringAttributeValue("MySingleStringAttributeValue")); - for (Map.Entry attribute : attributes.entrySet()) { - span.setAttribute(attribute.getKey(), attribute.getValue()); - } + span.setAttribute("MySingleStringAttributeKey", "MySingleStringAttributeValue"); + attributes + .entrySet() + .forEach(attribute -> span.setAttribute(attribute.getKey(), attribute.getValue())); testClock.advanceMillis(MILLIS_PER_SECOND); span.addEvent("event2", Attributes.empty()); testClock.advanceMillis(MILLIS_PER_SECOND); @@ -880,7 +880,13 @@ private void verifySpanData( // verify equality manually, since the implementations don't all equals with each other. ReadableAttributes spanDataAttributes = spanData.getAttributes(); assertThat(spanDataAttributes.size()).isEqualTo(attributes.size()); - spanDataAttributes.forEach((key, value) -> assertThat(attributes.get(key)).isEqualTo(value)); + spanDataAttributes.forEachRaw( + new RawAttributeConsumer() { + @Override + public void consume(AttributeKey key, T value) { + assertThat(attributes.get(key)).isEqualTo(value); + } + }); } @Test diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java index db88484f2b7..064fe86e565 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java @@ -16,11 +16,11 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeValue.doubleAttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.sdk.trace.Sampler.Decision; import io.opentelemetry.sdk.trace.Sampler.SamplingResult; @@ -86,10 +86,7 @@ void samplingDecisionEmpty() { @Test void samplingDecisionAttrs() { - final Attributes attrs = - Attributes.of( - "foo", AttributeValue.longAttributeValue(42), - "bar", AttributeValue.stringAttributeValue("baz")); + final Attributes attrs = Attributes.of(longKey("foo"), 42L, stringKey("bar"), "baz"); final SamplingResult sampledSamplingResult = Samplers.samplingResult(Sampler.Decision.RECORD_AND_SAMPLED, attrs); assertThat(sampledSamplingResult.getDecision()).isEqualTo(Decision.RECORD_AND_SAMPLED); @@ -720,8 +717,7 @@ void probabilitySampler_SampleBasedOnTraceId() { Collections.emptyList()); assertThat(samplingResult1.getDecision()).isEqualTo(Decision.NOT_RECORD); assertThat(samplingResult1.getAttributes()) - .isEqualTo( - Attributes.of(Samplers.SAMPLING_PROBABILITY.key(), doubleAttributeValue(0.0001))); + .isEqualTo(Attributes.of(Samplers.SAMPLING_PROBABILITY, 0.0001)); // This traceId will be sampled by the Probability Sampler because the last 8 bytes as long // is less than probability * Long.MAX_VALUE; String sampledTraceId = @@ -754,8 +750,7 @@ void probabilitySampler_SampleBasedOnTraceId() { Collections.emptyList()); assertThat(samplingResult2.getDecision()).isEqualTo(Decision.RECORD_AND_SAMPLED); assertThat(samplingResult1.getAttributes()) - .isEqualTo( - Attributes.of(Samplers.SAMPLING_PROBABILITY.key(), doubleAttributeValue(0.0001))); + .isEqualTo(Attributes.of(Samplers.SAMPLING_PROBABILITY, 0.0001)); } @Test diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index ed0e243d014..1ed3515fdec 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -16,12 +16,21 @@ package io.opentelemetry.sdk.trace; +import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static java.util.Collections.emptyList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import io.grpc.Context; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.context.Scope; @@ -37,6 +46,7 @@ import io.opentelemetry.trace.TraceId; import io.opentelemetry.trace.TraceState; import io.opentelemetry.trace.TracingContextUtils; +import java.util.Arrays; import java.util.Collections; import java.util.List; import javax.annotation.Nullable; @@ -132,18 +142,15 @@ void truncateLinkAttributes() { Span.Builder spanBuilder = tracerSdk.spanBuilder(SPAN_NAME); Attributes attributes = Attributes.of( - "key0", AttributeValue.stringAttributeValue("str"), - "key1", AttributeValue.stringAttributeValue("str"), - "key2", AttributeValue.stringAttributeValue("str")); + stringKey("key0"), "str", + stringKey("key1"), "str", + stringKey("key2"), "str"); spanBuilder.addLink(sampledSpanContext, attributes); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); try { assertThat(span.toSpanData().getLinks()) .containsExactly( - Link.create( - sampledSpanContext, - Attributes.of("key0", AttributeValue.stringAttributeValue("str")), - 3)); + Link.create(sampledSpanContext, Attributes.of(stringKey("key0"), "str"), 3)); } finally { span.end(); tracerSdkFactory.updateActiveTraceConfig(TraceConfig.getDefault()); @@ -211,19 +218,18 @@ void setAttribute() { .setAttribute("long", 12345L) .setAttribute("double", .12345) .setAttribute("boolean", true) - .setAttribute("stringAttribute", AttributeValue.stringAttributeValue("attrvalue")); + .setAttribute(stringKey("stringAttribute"), "attrvalue"); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); try { SpanData spanData = span.toSpanData(); ReadableAttributes attrs = spanData.getAttributes(); assertThat(attrs.size()).isEqualTo(5); - assertThat(attrs.get("string")).isEqualTo(AttributeValue.stringAttributeValue("value")); - assertThat(attrs.get("long")).isEqualTo(AttributeValue.longAttributeValue(12345L)); - assertThat(attrs.get("double")).isEqualTo(AttributeValue.doubleAttributeValue(0.12345)); - assertThat(attrs.get("boolean")).isEqualTo(AttributeValue.booleanAttributeValue(true)); - assertThat(attrs.get("stringAttribute")) - .isEqualTo(AttributeValue.stringAttributeValue("attrvalue")); + assertThat(attrs.get(stringKey("string"))).isEqualTo("value"); + assertThat(attrs.get(longKey("long"))).isEqualTo(12345L); + assertThat(attrs.get(doubleKey("double"))).isEqualTo(0.12345); + assertThat(attrs.get(booleanKey("boolean"))).isEqualTo(true); + assertThat(attrs.get(stringKey("stringAttribute"))).isEqualTo("attrvalue"); assertThat(spanData.getTotalAttributeCount()).isEqualTo(5); } finally { span.end(); @@ -237,18 +243,17 @@ void setAttribute_afterEnd() { spanBuilder.setAttribute("long", 12345L); spanBuilder.setAttribute("double", .12345); spanBuilder.setAttribute("boolean", true); - spanBuilder.setAttribute("stringAttribute", AttributeValue.stringAttributeValue("attrvalue")); + spanBuilder.setAttribute(stringKey("stringAttribute"), "attrvalue"); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); try { ReadableAttributes attrs = span.toSpanData().getAttributes(); assertThat(attrs.size()).isEqualTo(5); - assertThat(attrs.get("string")).isEqualTo(AttributeValue.stringAttributeValue("value")); - assertThat(attrs.get("long")).isEqualTo(AttributeValue.longAttributeValue(12345L)); - assertThat(attrs.get("double")).isEqualTo(AttributeValue.doubleAttributeValue(.12345)); - assertThat(attrs.get("boolean")).isEqualTo(AttributeValue.booleanAttributeValue(true)); - assertThat(attrs.get("stringAttribute")) - .isEqualTo(AttributeValue.stringAttributeValue("attrvalue")); + assertThat(attrs.get(stringKey("string"))).isEqualTo("value"); + assertThat(attrs.get(longKey("long"))).isEqualTo(12345L); + assertThat(attrs.get(doubleKey("double"))).isEqualTo(0.12345); + assertThat(attrs.get(booleanKey("boolean"))).isEqualTo(true); + assertThat(attrs.get(stringKey("stringAttribute"))).isEqualTo("attrvalue"); } finally { span.end(); } @@ -257,27 +262,24 @@ void setAttribute_afterEnd() { span.setAttribute("long2", 12345L); span.setAttribute("double2", .12345); span.setAttribute("boolean2", true); - span.setAttribute("stringAttribute2", AttributeValue.stringAttributeValue("attrvalue")); + span.setAttribute(stringKey("stringAttribute2"), "attrvalue"); ReadableAttributes attrs = span.toSpanData().getAttributes(); assertThat(attrs.size()).isEqualTo(5); - assertThat(attrs.get("string2")).isNull(); - assertThat(attrs.get("long2")).isNull(); - assertThat(attrs.get("double2")).isNull(); - assertThat(attrs.get("boolean2")).isNull(); - assertThat(attrs.get("stringAttribute2")).isNull(); + assertThat(attrs.get(stringKey("string2"))).isNull(); + assertThat(attrs.get(longKey("long2"))).isNull(); + assertThat(attrs.get(doubleKey("double2"))).isNull(); + assertThat(attrs.get(booleanKey("boolean2"))).isNull(); + assertThat(attrs.get(stringKey("stringAttribute2"))).isNull(); } @Test void setAttribute_emptyArrayAttributeValue() { Span.Builder spanBuilder = tracerSdk.spanBuilder(SPAN_NAME); - spanBuilder.setAttribute( - "stringArrayAttribute", AttributeValue.arrayAttributeValue(new String[0])); - spanBuilder.setAttribute( - "boolArrayAttribute", AttributeValue.arrayAttributeValue(new Boolean[0])); - spanBuilder.setAttribute("longArrayAttribute", AttributeValue.arrayAttributeValue(new Long[0])); - spanBuilder.setAttribute( - "doubleArrayAttribute", AttributeValue.arrayAttributeValue(new Double[0])); + spanBuilder.setAttribute(stringArrayKey("stringArrayAttribute"), emptyList()); + spanBuilder.setAttribute(booleanArrayKey("boolArrayAttribute"), emptyList()); + spanBuilder.setAttribute(longArrayKey("longArrayAttribute"), emptyList()); + spanBuilder.setAttribute(doubleArrayKey("doubleArrayAttribute"), emptyList()); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(4); } @@ -286,20 +288,20 @@ void setAttribute_emptyArrayAttributeValue() { void setAttribute_nullStringValue() { Span.Builder spanBuilder = tracerSdk.spanBuilder(SPAN_NAME); spanBuilder.setAttribute("emptyString", ""); - spanBuilder.setAttribute("nullString", (String) null); - spanBuilder.setAttribute("nullStringAttributeValue", AttributeValue.stringAttributeValue(null)); - spanBuilder.setAttribute("emptyStringAttributeValue", AttributeValue.stringAttributeValue("")); + spanBuilder.setAttribute("nullString", null); + spanBuilder.setAttribute(stringKey("nullStringAttributeValue"), null); + spanBuilder.setAttribute(stringKey("emptyStringAttributeValue"), ""); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(2); - span.setAttribute("emptyString", (String) null); - span.setAttribute("emptyStringAttributeValue", (String) null); + span.setAttribute("emptyString", null); + span.setAttribute("emptyStringAttributeValue", null); assertThat(span.toSpanData().getAttributes().isEmpty()).isTrue(); } @Test void setAttribute_onlyNullStringValue() { Span.Builder spanBuilder = tracerSdk.spanBuilder(SPAN_NAME); - spanBuilder.setAttribute("nullStringAttributeValue", AttributeValue.stringAttributeValue(null)); + spanBuilder.setAttribute(stringKey("nullStringAttributeValue"), null); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); assertThat(span.toSpanData().getAttributes().isEmpty()).isTrue(); } @@ -313,48 +315,42 @@ void setAttribute_NoEffectAfterStartSpan() { ReadableAttributes beforeAttributes = span.toSpanData().getAttributes(); assertThat(beforeAttributes.size()).isEqualTo(2); - assertThat(beforeAttributes.get("key1")) - .isEqualTo(AttributeValue.stringAttributeValue("value1")); - assertThat(beforeAttributes.get("key2")) - .isEqualTo(AttributeValue.stringAttributeValue("value2")); + assertThat(beforeAttributes.get(stringKey("key1"))).isEqualTo("value1"); + assertThat(beforeAttributes.get(stringKey("key2"))).isEqualTo("value2"); spanBuilder.setAttribute("key3", "value3"); ReadableAttributes afterAttributes = span.toSpanData().getAttributes(); assertThat(afterAttributes.size()).isEqualTo(2); - assertThat(afterAttributes.get("key1")) - .isEqualTo(AttributeValue.stringAttributeValue("value1")); - assertThat(afterAttributes.get("key2")) - .isEqualTo(AttributeValue.stringAttributeValue("value2")); + assertThat(afterAttributes.get(stringKey("key1"))).isEqualTo("value1"); + assertThat(afterAttributes.get(stringKey("key2"))).isEqualTo("value2"); } @Test void setAttribute_nullAttributeValue() { Span.Builder spanBuilder = tracerSdk.spanBuilder(SPAN_NAME); spanBuilder.setAttribute("emptyString", ""); - spanBuilder.setAttribute("nullString", (AttributeValue) null); - spanBuilder.setAttribute("nullStringAttributeValue", AttributeValue.stringAttributeValue(null)); - spanBuilder.setAttribute("emptyStringAttributeValue", AttributeValue.stringAttributeValue("")); + spanBuilder.setAttribute("nullString", null); + spanBuilder.setAttribute(stringKey("nullStringAttributeValue"), null); + spanBuilder.setAttribute(stringKey("emptyStringAttributeValue"), ""); spanBuilder.setAttribute("longAttribute", 0L); spanBuilder.setAttribute("boolAttribute", false); spanBuilder.setAttribute("doubleAttribute", 0.12345f); - spanBuilder.setAttribute("stringArrayAttribute", AttributeValue.arrayAttributeValue("", null)); - spanBuilder.setAttribute("boolArrayAttribute", AttributeValue.arrayAttributeValue(true, null)); - spanBuilder.setAttribute( - "longArrayAttribute", AttributeValue.arrayAttributeValue(12345L, null)); - spanBuilder.setAttribute( - "doubleArrayAttribute", AttributeValue.arrayAttributeValue(1.2345, null)); + spanBuilder.setAttribute(stringArrayKey("stringArrayAttribute"), Arrays.asList("", null)); + spanBuilder.setAttribute(booleanArrayKey("boolArrayAttribute"), Arrays.asList(true, null)); + spanBuilder.setAttribute(longArrayKey("longArrayAttribute"), Arrays.asList(12345L, null)); + spanBuilder.setAttribute(doubleArrayKey("doubleArrayAttribute"), Arrays.asList(1.2345, null)); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(9); - span.setAttribute("emptyString", (AttributeValue) null); - span.setAttribute("emptyStringAttributeValue", (AttributeValue) null); - span.setAttribute("longAttribute", (AttributeValue) null); - span.setAttribute("boolAttribute", (AttributeValue) null); - span.setAttribute("doubleAttribute", (AttributeValue) null); - span.setAttribute("stringArrayAttribute", (AttributeValue) null); - span.setAttribute("boolArrayAttribute", (AttributeValue) null); - span.setAttribute("longArrayAttribute", (AttributeValue) null); - span.setAttribute("doubleArrayAttribute", (AttributeValue) null); + span.setAttribute("emptyString", null); + span.setAttribute(stringKey("emptyStringAttributeValue"), null); + span.setAttribute(longKey("longAttribute"), null); + span.setAttribute(booleanKey("boolAttribute"), null); + span.setAttribute(doubleKey("doubleAttribute"), null); + span.setAttribute(stringArrayKey("stringArrayAttribute"), null); + span.setAttribute(booleanArrayKey("boolArrayAttribute"), null); + span.setAttribute(longArrayKey("longArrayAttribute"), null); + span.setAttribute(doubleArrayKey("doubleArrayAttribute"), null); assertThat(span.toSpanData().getAttributes().isEmpty()).isTrue(); } @@ -362,28 +358,26 @@ void setAttribute_nullAttributeValue() { void setAttribute_nullAttributeValue_afterEnd() { Span.Builder spanBuilder = tracerSdk.spanBuilder(SPAN_NAME); spanBuilder.setAttribute("emptyString", ""); - spanBuilder.setAttribute("emptyStringAttributeValue", AttributeValue.stringAttributeValue("")); + spanBuilder.setAttribute(stringKey("emptyStringAttributeValue"), ""); spanBuilder.setAttribute("longAttribute", 0L); spanBuilder.setAttribute("boolAttribute", false); spanBuilder.setAttribute("doubleAttribute", 0.12345f); - spanBuilder.setAttribute("stringArrayAttribute", AttributeValue.arrayAttributeValue("", null)); - spanBuilder.setAttribute("boolArrayAttribute", AttributeValue.arrayAttributeValue(true, null)); - spanBuilder.setAttribute( - "longArrayAttribute", AttributeValue.arrayAttributeValue(12345L, null)); - spanBuilder.setAttribute( - "doubleArrayAttribute", AttributeValue.arrayAttributeValue(1.2345, null)); + spanBuilder.setAttribute(stringArrayKey("stringArrayAttribute"), Arrays.asList("", null)); + spanBuilder.setAttribute(booleanArrayKey("boolArrayAttribute"), Arrays.asList(true, null)); + spanBuilder.setAttribute(longArrayKey("longArrayAttribute"), Arrays.asList(12345L, null)); + spanBuilder.setAttribute(doubleArrayKey("doubleArrayAttribute"), Arrays.asList(1.2345, null)); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(9); span.end(); - span.setAttribute("emptyString", (AttributeValue) null); - span.setAttribute("emptyStringAttributeValue", (AttributeValue) null); - span.setAttribute("longAttribute", (AttributeValue) null); - span.setAttribute("boolAttribute", (AttributeValue) null); - span.setAttribute("doubleAttribute", (AttributeValue) null); - span.setAttribute("stringArrayAttribute", (AttributeValue) null); - span.setAttribute("boolArrayAttribute", (AttributeValue) null); - span.setAttribute("longArrayAttribute", (AttributeValue) null); - span.setAttribute("doubleArrayAttribute", (AttributeValue) null); + span.setAttribute("emptyString", null); + span.setAttribute(stringKey("emptyStringAttributeValue"), null); + span.setAttribute(longKey("longAttribute"), null); + span.setAttribute(booleanKey("boolAttribute"), null); + span.setAttribute(doubleKey("doubleAttribute"), null); + span.setAttribute(stringArrayKey("stringArrayAttribute"), null); + span.setAttribute(booleanArrayKey("boolArrayAttribute"), null); + span.setAttribute(longArrayKey("longArrayAttribute"), null); + span.setAttribute(doubleArrayKey("doubleArrayAttribute"), null); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(9); } @@ -406,7 +400,7 @@ void droppingAttributes() { ReadableAttributes attrs = span.toSpanData().getAttributes(); assertThat(attrs.size()).isEqualTo(maxNumberOfAttrs); for (int i = 0; i < maxNumberOfAttrs; i++) { - assertThat(attrs.get("key" + i)).isEqualTo(AttributeValue.longAttributeValue(i)); + assertThat(attrs.get(longKey("key" + i))).isEqualTo(i); } } finally { span.end(); @@ -429,45 +423,36 @@ public void tooLargeAttributeValuesAreTruncated() { spanBuilder.setAttribute("builderStringLarge", "very large string that we have to cut"); spanBuilder.setAttribute("builderLong", 42L); spanBuilder.setAttribute( - "builderStringLargeValue", - AttributeValue.stringAttributeValue("very large string that we have to cut")); + stringKey("builderStringLargeValue"), "very large string that we have to cut"); spanBuilder.setAttribute( - "builderStringArray", - AttributeValue.arrayAttributeValue("small", null, "very large string that we have to cut")); + stringArrayKey("builderStringArray"), + Arrays.asList("small", null, "very large string that we have to cut")); RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); span.setAttribute("spanStringSmall", "small"); span.setAttribute("spanStringLarge", "very large string that we have to cut"); span.setAttribute("spanLong", 42L); + span.setAttribute(stringKey("spanStringLarge"), "very large string that we have to cut"); span.setAttribute( - "spanStringLarge", - AttributeValue.stringAttributeValue("very large string that we have to cut")); - span.setAttribute( - "spanStringArray", - AttributeValue.arrayAttributeValue("small", null, "very large string that we have to cut")); + stringArrayKey("spanStringArray"), + Arrays.asList("small", null, "very large string that we have to cut")); try { ReadableAttributes attrs = span.toSpanData().getAttributes(); - assertThat(attrs.get("builderStringNull")).isEqualTo(null); - assertThat(attrs.get("builderStringSmall")) - .isEqualTo(AttributeValue.stringAttributeValue("small")); - assertThat(attrs.get("builderStringLarge")) - .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.get("builderLong")).isEqualTo(AttributeValue.longAttributeValue(42L)); - assertThat(attrs.get("builderStringLargeValue")) - .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.get("builderStringArray")) - .isEqualTo(AttributeValue.arrayAttributeValue("small", null, "very large")); - - assertThat(attrs.get("spanStringSmall")) - .isEqualTo(AttributeValue.stringAttributeValue("small")); - assertThat(attrs.get("spanStringLarge")) - .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.get("spanLong")).isEqualTo(AttributeValue.longAttributeValue(42L)); - assertThat(attrs.get("spanStringLarge")) - .isEqualTo(AttributeValue.stringAttributeValue("very large")); - assertThat(attrs.get("spanStringArray")) - .isEqualTo(AttributeValue.arrayAttributeValue("small", null, "very large")); + assertThat(attrs.get(stringKey("builderStringNull"))).isEqualTo(null); + assertThat(attrs.get(stringKey("builderStringSmall"))).isEqualTo("small"); + assertThat(attrs.get(stringKey("builderStringLarge"))).isEqualTo("very large"); + assertThat(attrs.get(longKey("builderLong"))).isEqualTo(42L); + assertThat(attrs.get(stringKey("builderStringLargeValue"))).isEqualTo("very large"); + assertThat(attrs.get(stringArrayKey("builderStringArray"))) + .isEqualTo(Arrays.asList("small", null, "very large")); + + assertThat(attrs.get(stringKey("spanStringSmall"))).isEqualTo("small"); + assertThat(attrs.get(stringKey("spanStringLarge"))).isEqualTo("very large"); + assertThat(attrs.get(longKey("spanLong"))).isEqualTo(42L); + assertThat(attrs.get(stringKey("spanStringLarge"))).isEqualTo("very large"); + assertThat(attrs.get(stringArrayKey("spanStringArray"))) + .isEqualTo(Arrays.asList("small", null, "very large")); } finally { span.end(); tracerSdkFactory.updateActiveTraceConfig(TraceConfig.getDefault()); @@ -487,8 +472,7 @@ void addAttributes_OnlyViaSampler() { RecordEventsReadableSpan span = (RecordEventsReadableSpan) spanBuilder.startSpan(); try { assertThat(span.toSpanData().getAttributes().size()).isEqualTo(1); - assertThat(span.toSpanData().getAttributes().get(Samplers.SAMPLING_PROBABILITY.key())) - .isEqualTo(AttributeValue.doubleAttributeValue(1)); + assertThat(span.toSpanData().getAttributes().get(Samplers.SAMPLING_PROBABILITY)).isEqualTo(1); } finally { span.end(); tracerSdkFactory.updateActiveTraceConfig(TraceConfig.getDefault()); @@ -543,6 +527,7 @@ void sampler() { @Test void sampler_decisionAttributes() { final String samplerAttributeName = "sampler-attribute"; + StringKey samplerAttributeKey = stringKey(samplerAttributeName); RecordEventsReadableSpan span = (RecordEventsReadableSpan) TestUtils.startSpanWithSampler( @@ -566,8 +551,7 @@ public Decision getDecision() { @Override public Attributes getAttributes() { - return Attributes.of( - samplerAttributeName, AttributeValue.stringAttributeValue("bar")); + return Attributes.of(samplerAttributeKey, "bar"); } }; } @@ -577,12 +561,11 @@ public String getDescription() { return "test sampler"; } }, - Collections.singletonMap( - samplerAttributeName, AttributeValue.stringAttributeValue("none"))) + Collections.singletonMap(samplerAttributeKey.get(), "none")) .startSpan(); try { assertThat(span.getContext().isSampled()).isTrue(); - assertThat(span.toSpanData().getAttributes().get(samplerAttributeName)).isNotNull(); + assertThat(span.toSpanData().getAttributes().get(samplerAttributeKey)).isNotNull(); } finally { span.end(); } diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java index 5c0c99d5c33..f11e75dcffa 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java @@ -16,7 +16,8 @@ package io.opentelemetry.sdk.trace; -import io.opentelemetry.common.AttributeValue; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; + import io.opentelemetry.common.Attributes; import io.opentelemetry.sdk.trace.config.TraceConfig; import io.opentelemetry.sdk.trace.data.SpanData; @@ -42,9 +43,7 @@ private TestUtils() {} * @return some {@link io.opentelemetry.common.Attributes} */ static Attributes generateRandomAttributes() { - return Attributes.of( - UUID.randomUUID().toString(), - AttributeValue.stringAttributeValue(UUID.randomUUID().toString())); + return Attributes.of(stringKey(UUID.randomUUID().toString()), UUID.randomUUID().toString()); } /** @@ -91,15 +90,13 @@ public static Span.Builder startSpanWithSampler( Tracer tracer, String spanName, Sampler sampler, - Map attributes) { + Map attributes) { TraceConfig originalConfig = tracerSdkFactory.getActiveTraceConfig(); tracerSdkFactory.updateActiveTraceConfig( originalConfig.toBuilder().setSampler(sampler).build()); try { Span.Builder builder = tracer.spanBuilder(spanName); - for (Map.Entry entry : attributes.entrySet()) { - builder.setAttribute(entry.getKey(), entry.getValue()); - } + attributes.forEach(builder::setAttribute); return builder; } finally { diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java index 2cebb7ca41e..37c517b8067 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java @@ -16,9 +16,9 @@ package io.opentelemetry.sdk.trace; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.trace.Event; import org.junit.jupiter.api.Test; @@ -28,10 +28,8 @@ class TimedEventTest { private static final String NAME = "event"; private static final String NAME_2 = "event2"; - private static final Attributes ATTRIBUTES = - Attributes.of("attribute", AttributeValue.stringAttributeValue("value")); - private static final Attributes ATTRIBUTES_2 = - Attributes.of("attribute2", AttributeValue.stringAttributeValue("value2")); + private static final Attributes ATTRIBUTES = Attributes.of(stringKey("attribute"), "value"); + private static final Attributes ATTRIBUTES_2 = Attributes.of(stringKey("attribute2"), "value2"); private static final Event EVENT = new Event() { @Override diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java index ec51015e931..3a3ec854b7b 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TracerSdkTest.java @@ -19,7 +19,6 @@ import static org.assertj.core.api.Assertions.assertThat; import io.grpc.Context; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.context.Scope; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -208,7 +207,7 @@ public SimpleSpanOperation(TracerSdk tracer) { public void update() { Span span = tracer.spanBuilder("testSpan").startSpan(); try (Scope ignored = tracer.withSpan(span)) { - span.setAttribute("testAttribute", AttributeValue.stringAttributeValue("testValue")); + span.setAttribute("testAttribute", "testValue"); } finally { span.end(); } diff --git a/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/BeanstalkResource.java b/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/BeanstalkResource.java index ffc87b519c0..c99b6cdac10 100644 --- a/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/BeanstalkResource.java +++ b/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/BeanstalkResource.java @@ -78,13 +78,13 @@ public Attributes getAttributes() { String value = parser.getText(); switch (parser.getCurrentName()) { case DEVELOPMENT_ID: - ResourceAttributes.SERVICE_INSTANCE.set(attrBuilders, value); + attrBuilders.setAttribute(ResourceAttributes.SERVICE_INSTANCE, value); break; case VERSION_LABEL: - ResourceAttributes.SERVICE_VERSION.set(attrBuilders, value); + attrBuilders.setAttribute(ResourceAttributes.SERVICE_VERSION, value); break; case ENVIRONMENT_NAME: - ResourceAttributes.SERVICE_NAMESPACE.set(attrBuilders, value); + attrBuilders.setAttribute(ResourceAttributes.SERVICE_NAMESPACE, value); break; default: parser.skipChildren(); diff --git a/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/Ec2Resource.java b/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/Ec2Resource.java index 88c73677b5f..818dc3d42d3 100644 --- a/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/Ec2Resource.java +++ b/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/Ec2Resource.java @@ -172,22 +172,22 @@ public Attributes getAttributes() { String value = parser.nextTextValue(); switch (parser.getCurrentName()) { case "instanceId": - ResourceAttributes.HOST_ID.set(attrBuilders, value); + attrBuilders.setAttribute(ResourceAttributes.HOST_ID, value); break; case "availabilityZone": - ResourceAttributes.CLOUD_ZONE.set(attrBuilders, value); + attrBuilders.setAttribute(ResourceAttributes.CLOUD_ZONE, value); break; case "instanceType": - ResourceAttributes.HOST_TYPE.set(attrBuilders, value); + attrBuilders.setAttribute(ResourceAttributes.HOST_TYPE, value); break; case "imageId": - ResourceAttributes.HOST_IMAGE_ID.set(attrBuilders, value); + attrBuilders.setAttribute(ResourceAttributes.HOST_IMAGE_ID, value); break; case "accountId": - ResourceAttributes.CLOUD_ACCOUNT.set(attrBuilders, value); + attrBuilders.setAttribute(ResourceAttributes.CLOUD_ACCOUNT, value); break; case "region": - ResourceAttributes.CLOUD_REGION.set(attrBuilders, value); + attrBuilders.setAttribute(ResourceAttributes.CLOUD_REGION, value); break; default: parser.skipChildren(); @@ -198,8 +198,8 @@ public Attributes getAttributes() { return Attributes.empty(); } - ResourceAttributes.HOST_HOSTNAME.set(attrBuilders, hostname); - ResourceAttributes.HOST_NAME.set(attrBuilders, hostname); + attrBuilders.setAttribute(ResourceAttributes.HOST_HOSTNAME, hostname); + attrBuilders.setAttribute(ResourceAttributes.HOST_NAME, hostname); return attrBuilders.build(); } diff --git a/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/EcsResource.java b/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/EcsResource.java index d1788adb080..d684bfe4f68 100644 --- a/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/EcsResource.java +++ b/sdk_extensions/aws_v1_support/src/main/java/io/opentelemetry/sdk/extensions/trace/aws/resource/EcsResource.java @@ -64,14 +64,14 @@ public Attributes getAttributes() { Attributes.Builder attrBuilders = Attributes.newBuilder(); try { String hostName = InetAddress.getLocalHost().getHostName(); - ResourceAttributes.CONTAINER_NAME.set(attrBuilders, hostName); + attrBuilders.setAttribute(ResourceAttributes.CONTAINER_NAME, hostName); } catch (UnknownHostException e) { logger.log(Level.WARNING, "Could not get docker container name from hostname.", e); } String containerId = dockerHelper.getContainerId(); if (!Strings.isNullOrEmpty(containerId)) { - ResourceAttributes.CONTAINER_ID.set(attrBuilders, containerId); + attrBuilders.setAttribute(ResourceAttributes.CONTAINER_ID, containerId); } return attrBuilders.build(); diff --git a/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/BeanstalkResourceTest.java b/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/BeanstalkResourceTest.java index 9edb4f60dfe..f4f9b1c4737 100644 --- a/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/BeanstalkResourceTest.java +++ b/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/BeanstalkResourceTest.java @@ -16,7 +16,6 @@ package io.opentelemetry.sdk.extensions.trace.aws.resource; -import static io.opentelemetry.common.AttributeValue.stringAttributeValue; import static org.assertj.core.api.Assertions.assertThat; import com.google.common.base.Charsets; @@ -44,14 +43,13 @@ void testCreateAttributes(@TempDir File tempFolder) throws IOException { assertThat(attributes) .isEqualTo( Attributes.of( - ResourceAttributes.SERVICE_INSTANCE.key(), stringAttributeValue("4"), - ResourceAttributes.SERVICE_VERSION.key(), stringAttributeValue("2"), - ResourceAttributes.SERVICE_NAMESPACE.key(), - stringAttributeValue("HttpSubscriber-env"))); + ResourceAttributes.SERVICE_INSTANCE, "4", + ResourceAttributes.SERVICE_VERSION, "2", + ResourceAttributes.SERVICE_NAMESPACE, "HttpSubscriber-env")); } @Test - void testConfigFileMissing() throws IOException { + void testConfigFileMissing() { BeanstalkResource populator = new BeanstalkResource("a_file_never_existing"); Attributes attributes = populator.getAttributes(); assertThat(attributes.isEmpty()).isTrue(); diff --git a/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/Ec2ResourceTest.java b/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/Ec2ResourceTest.java index 10727c049fa..75f4d58dbea 100644 --- a/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/Ec2ResourceTest.java +++ b/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/Ec2ResourceTest.java @@ -82,14 +82,14 @@ public void imdsv2() { Attributes attributes = populator.getAttributes(); Attributes.Builder expectedAttrBuilders = Attributes.newBuilder(); - ResourceAttributes.HOST_ID.set(expectedAttrBuilders, "i-1234567890abcdef0"); - ResourceAttributes.CLOUD_ZONE.set(expectedAttrBuilders, "us-west-2b"); - ResourceAttributes.HOST_TYPE.set(expectedAttrBuilders, "t2.micro"); - ResourceAttributes.HOST_IMAGE_ID.set(expectedAttrBuilders, "ami-5fb8c835"); - ResourceAttributes.CLOUD_ACCOUNT.set(expectedAttrBuilders, "123456789012"); - ResourceAttributes.CLOUD_REGION.set(expectedAttrBuilders, "us-west-2"); - ResourceAttributes.HOST_HOSTNAME.set(expectedAttrBuilders, "ec2-1-2-3-4"); - ResourceAttributes.HOST_NAME.set(expectedAttrBuilders, "ec2-1-2-3-4"); + expectedAttrBuilders.setAttribute(ResourceAttributes.HOST_ID, "i-1234567890abcdef0"); + expectedAttrBuilders.setAttribute(ResourceAttributes.CLOUD_ZONE, "us-west-2b"); + expectedAttrBuilders.setAttribute(ResourceAttributes.HOST_TYPE, "t2.micro"); + expectedAttrBuilders.setAttribute(ResourceAttributes.HOST_IMAGE_ID, "ami-5fb8c835"); + expectedAttrBuilders.setAttribute(ResourceAttributes.CLOUD_ACCOUNT, "123456789012"); + expectedAttrBuilders.setAttribute(ResourceAttributes.CLOUD_REGION, "us-west-2"); + expectedAttrBuilders.setAttribute(ResourceAttributes.HOST_HOSTNAME, "ec2-1-2-3-4"); + expectedAttrBuilders.setAttribute(ResourceAttributes.HOST_NAME, "ec2-1-2-3-4"); assertThat(attributes).isEqualTo(expectedAttrBuilders.build()); verify( @@ -112,16 +112,17 @@ public void imdsv1() { stubFor(any(urlPathEqualTo("/latest/meta-data/hostname")).willReturn(ok("ec2-1-2-3-4"))); Attributes attributes = populator.getAttributes(); - Attributes.Builder expectedAttrBuilders = Attributes.newBuilder(); - ResourceAttributes.HOST_ID.set(expectedAttrBuilders, "i-1234567890abcdef0"); - ResourceAttributes.CLOUD_ZONE.set(expectedAttrBuilders, "us-west-2b"); - ResourceAttributes.HOST_TYPE.set(expectedAttrBuilders, "t2.micro"); - ResourceAttributes.HOST_IMAGE_ID.set(expectedAttrBuilders, "ami-5fb8c835"); - ResourceAttributes.CLOUD_ACCOUNT.set(expectedAttrBuilders, "123456789012"); - ResourceAttributes.CLOUD_REGION.set(expectedAttrBuilders, "us-west-2"); - ResourceAttributes.HOST_HOSTNAME.set(expectedAttrBuilders, "ec2-1-2-3-4"); - ResourceAttributes.HOST_NAME.set(expectedAttrBuilders, "ec2-1-2-3-4"); + Attributes.Builder expectedAttrBuilders = + Attributes.newBuilder() + .setAttribute(ResourceAttributes.HOST_ID, "i-1234567890abcdef0") + .setAttribute(ResourceAttributes.CLOUD_ZONE, "us-west-2b") + .setAttribute(ResourceAttributes.HOST_TYPE, "t2.micro") + .setAttribute(ResourceAttributes.HOST_IMAGE_ID, "ami-5fb8c835") + .setAttribute(ResourceAttributes.CLOUD_ACCOUNT, "123456789012") + .setAttribute(ResourceAttributes.CLOUD_REGION, "us-west-2") + .setAttribute(ResourceAttributes.HOST_HOSTNAME, "ec2-1-2-3-4") + .setAttribute(ResourceAttributes.HOST_NAME, "ec2-1-2-3-4"); assertThat(attributes).isEqualTo(expectedAttrBuilders.build()); verify( diff --git a/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/EcsResourceTest.java b/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/EcsResourceTest.java index 6ba3b44037c..7d406102b06 100644 --- a/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/EcsResourceTest.java +++ b/sdk_extensions/aws_v1_support/src/test/java/io/opentelemetry/sdk/extensions/trace/aws/resource/EcsResourceTest.java @@ -16,7 +16,6 @@ package io.opentelemetry.sdk.extensions.trace.aws.resource; -import static io.opentelemetry.common.AttributeValue.stringAttributeValue; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; @@ -50,10 +49,10 @@ void testCreateAttributes() throws UnknownHostException { assertThat(attributes) .isEqualTo( Attributes.of( - ResourceAttributes.CONTAINER_NAME.key(), - stringAttributeValue(InetAddress.getLocalHost().getHostName()), - ResourceAttributes.CONTAINER_ID.key(), - stringAttributeValue("0123456789A"))); + ResourceAttributes.CONTAINER_NAME, + InetAddress.getLocalHost().getHostName(), + ResourceAttributes.CONTAINER_ID, + "0123456789A")); } @Test @@ -76,8 +75,7 @@ void testContainerIdMissing() throws UnknownHostException { assertThat(attributes) .isEqualTo( Attributes.of( - ResourceAttributes.CONTAINER_NAME.key(), - stringAttributeValue(InetAddress.getLocalHost().getHostName()))); + ResourceAttributes.CONTAINER_NAME, InetAddress.getLocalHost().getHostName())); } @Test diff --git a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java index 55ad088aeea..1f09fa7f38c 100644 --- a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java +++ b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java @@ -17,7 +17,9 @@ package io.opentelemetry.sdk.extensions.trace.jaeger.sampler; import com.google.common.annotations.VisibleForTesting; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKeyImpl; +import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.internal.MillisClock; @@ -34,8 +36,8 @@ */ class RateLimitingSampler implements Sampler { static final String TYPE = "ratelimiting"; - static final String SAMPLER_TYPE = "sampler.type"; - static final String SAMPLER_PARAM = "sampler.param"; + static final StringKey SAMPLER_TYPE = AttributeKeyImpl.stringKey("sampler.type"); + static final DoubleKey SAMPLER_PARAM = AttributeKeyImpl.doubleKey("sampler.param"); private final double maxTracesPerSecond; private final RateLimiter rateLimiter; @@ -52,9 +54,7 @@ class RateLimitingSampler implements Sampler { double maxBalance = maxTracesPerSecond < 1.0 ? 1.0 : maxTracesPerSecond; this.rateLimiter = new RateLimiter(maxTracesPerSecond, maxBalance, MillisClock.getInstance()); Attributes attributes = - Attributes.of( - SAMPLER_TYPE, AttributeValue.stringAttributeValue(TYPE), - SAMPLER_PARAM, AttributeValue.doubleAttributeValue(maxTracesPerSecond)); + Attributes.of(SAMPLER_TYPE, TYPE, SAMPLER_PARAM, (double) maxTracesPerSecond); this.onSamplingResult = Samplers.samplingResult(Decision.RECORD_AND_SAMPLED, attributes); this.offSamplingResult = Samplers.samplingResult(Decision.NOT_RECORD, attributes); } diff --git a/sdk_extensions/jaeger_remote_sampler/src/test/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSamplerTest.java b/sdk_extensions/jaeger_remote_sampler/src/test/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSamplerTest.java index 502415e8b14..76b3f3680ee 100644 --- a/sdk_extensions/jaeger_remote_sampler/src/test/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSamplerTest.java +++ b/sdk_extensions/jaeger_remote_sampler/src/test/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSamplerTest.java @@ -19,7 +19,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.sdk.trace.Sampler.Decision; import io.opentelemetry.sdk.trace.Sampler.SamplingResult; @@ -94,11 +93,9 @@ void sampleOneTrace() { .getDecision()) .isEqualTo(Decision.NOT_RECORD); assertEquals(2, samplingResult.getAttributes().size()); + assertEquals(1d, samplingResult.getAttributes().get(RateLimitingSampler.SAMPLER_PARAM)); assertEquals( - AttributeValue.doubleAttributeValue(1), - samplingResult.getAttributes().get(RateLimitingSampler.SAMPLER_PARAM)); - assertEquals( - AttributeValue.stringAttributeValue(RateLimitingSampler.TYPE), + RateLimitingSampler.TYPE, samplingResult.getAttributes().get(RateLimitingSampler.SAMPLER_TYPE)); } diff --git a/sdk_extensions/resources/src/main/java/io/opentelemetry/sdk/extensions/resources/OsResource.java b/sdk_extensions/resources/src/main/java/io/opentelemetry/sdk/extensions/resources/OsResource.java index 2ad74d7e808..101fa89dd7e 100644 --- a/sdk_extensions/resources/src/main/java/io/opentelemetry/sdk/extensions/resources/OsResource.java +++ b/sdk_extensions/resources/src/main/java/io/opentelemetry/sdk/extensions/resources/OsResource.java @@ -42,7 +42,7 @@ protected Attributes getAttributes() { String osName = getOs(os); if (osName != null) { - attributes.setAttribute(ResourceAttributes.OS_NAME.key(), osName); + attributes.setAttribute(ResourceAttributes.OS_NAME, osName); } String version = null; @@ -52,7 +52,7 @@ protected Attributes getAttributes() { // Ignore } String osDescription = version != null ? os + ' ' + version : os; - attributes.setAttribute(ResourceAttributes.OS_DESCRIPTION.key(), osDescription); + attributes.setAttribute(ResourceAttributes.OS_DESCRIPTION, osDescription); return attributes.build(); } diff --git a/sdk_extensions/resources/src/main/java/io/opentelemetry/sdk/extensions/resources/ProcessResource.java b/sdk_extensions/resources/src/main/java/io/opentelemetry/sdk/extensions/resources/ProcessResource.java index d0ab1e4e44d..e38a0bc41ee 100644 --- a/sdk_extensions/resources/src/main/java/io/opentelemetry/sdk/extensions/resources/ProcessResource.java +++ b/sdk_extensions/resources/src/main/java/io/opentelemetry/sdk/extensions/resources/ProcessResource.java @@ -46,7 +46,7 @@ protected Attributes getAttributes() { } if (pid >= 0) { - ResourceAttributes.PROCESS_PID.set(attributes, pid); + attributes.setAttribute(ResourceAttributes.PROCESS_PID, pid); } String javaHome = null; @@ -68,13 +68,14 @@ protected Attributes getAttributes() { executablePath.append(".exe"); } - ResourceAttributes.PROCESS_EXECUTABLE_PATH.set(attributes, executablePath.toString()); + attributes.setAttribute( + ResourceAttributes.PROCESS_EXECUTABLE_PATH, executablePath.toString()); StringBuilder commandLine = new StringBuilder(executablePath); for (String arg : runtime.getInputArguments()) { commandLine.append(' ').append(arg); } - ResourceAttributes.PROCESS_COMMAND_LINE.set(attributes, commandLine.toString()); + attributes.setAttribute(ResourceAttributes.PROCESS_COMMAND_LINE, commandLine.toString()); } return attributes.build(); diff --git a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java index cd8cf07e9b4..f74db1861df 100644 --- a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java +++ b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/OsResourceTest.java @@ -33,36 +33,30 @@ class OsResourceTest { void linux() { assumeThat(System.getProperty("os.name").toLowerCase()).startsWith("linux"); Attributes attributes = RESOURCE.getAttributes(); - assertThat(attributes.get(ResourceAttributes.OS_NAME.key()).getStringValue()) - .isEqualTo("LINUX"); - assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION.key()).getStringValue()) - .isNotEmpty(); + assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("LINUX"); + assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty(); } @Test void macos() { assumeThat(System.getProperty("os.name").toLowerCase()).startsWith("mac"); Attributes attributes = RESOURCE.getAttributes(); - assertThat(attributes.get(ResourceAttributes.OS_NAME.key()).getStringValue()) - .isEqualTo("DARWIN"); - assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION.key()).getStringValue()) - .isNotEmpty(); + assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("DARWIN"); + assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty(); } @Test void windows() { assumeThat(System.getProperty("os.name").toLowerCase()).startsWith("windows"); Attributes attributes = RESOURCE.getAttributes(); - assertThat(attributes.get(ResourceAttributes.OS_NAME.key()).getStringValue()) - .isEqualTo("WINDOWS"); - assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION.key()).getStringValue()) - .isNotEmpty(); + assertThat(attributes.get(ResourceAttributes.OS_NAME)).isEqualTo("WINDOWS"); + assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotEmpty(); } @Test void inDefault() { ReadableAttributes attributes = Resource.getDefault().getAttributes(); - assertThat(attributes.get(ResourceAttributes.OS_NAME.key())).isNotNull(); - assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION.key())).isNotNull(); + assertThat(attributes.get(ResourceAttributes.OS_NAME)).isNotNull(); + assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION)).isNotNull(); } } diff --git a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java index 43946e139f9..58c436ef8e5 100644 --- a/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java +++ b/sdk_extensions/resources/src/test/java/io/opentelemetry/sdk/extensions/resources/ProcessResourceTest.java @@ -32,20 +32,17 @@ class ProcessResourceTest { void normal() { Attributes attributes = RESOURCE.getAttributes(); - assertThat(attributes.get(ResourceAttributes.PROCESS_PID.key()).getLongValue()) - .isGreaterThan(1); - assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH.key()).getStringValue()) - .contains("java"); - assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE.key()).getStringValue()) - .contains( - attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH.key()).getStringValue()); + assertThat(attributes.get(ResourceAttributes.PROCESS_PID)).isGreaterThan(1); + assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH)).contains("java"); + assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE)) + .contains(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH)); } @Test void inDefault() { ReadableAttributes attributes = Resource.getDefault().getAttributes(); - assertThat(attributes.get(ResourceAttributes.PROCESS_PID.key())).isNotNull(); - assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH.key())).isNotNull(); - assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE.key())).isNotNull(); + assertThat(attributes.get(ResourceAttributes.PROCESS_PID)).isNotNull(); + assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH)).isNotNull(); + assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE)).isNotNull(); } } diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java index b585debb839..33bd9cf888e 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/TestUtils.java @@ -18,7 +18,7 @@ import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.exporters.inmemory.InMemorySpanExporter; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.trace.Span.Kind; @@ -39,35 +39,16 @@ public static Callable finishedSpansSize(final InMemorySpanExporter tra } /** Returns a {@code List} with the {@code Span} matching the specified attribute. */ - public static List getByAttr( - List spans, final String key, final Object value) { + public static List getByAttr( + List spans, final AttributeKey key, final T value) { return getByCondition( spans, span -> { - AttributeValue attrValue = span.getAttributes().get(key); + T attrValue = span.getAttributes().get(key); if (attrValue == null) { return false; } - - switch (attrValue.getType()) { - case BOOLEAN: - return value.equals(attrValue.getBooleanValue()); - case STRING: - return value.equals(attrValue.getStringValue()); - case DOUBLE: - return value.equals(attrValue.getDoubleValue()); - case LONG: - return value.equals(attrValue.getLongValue()); - case STRING_ARRAY: - return value.equals(attrValue.getStringArrayValue()); - case LONG_ARRAY: - return value.equals(attrValue.getLongArrayValue()); - case BOOLEAN_ARRAY: - return value.equals(attrValue.getBooleanArrayValue()); - case DOUBLE_ARRAY: - return value.equals(attrValue.getDoubleArrayValue()); - } - return false; + return value.equals(attrValue); }); } @@ -76,7 +57,7 @@ public static List getByAttr( * instance being matched, an {@code IllegalArgumentException} will be thrown. */ @Nullable - public static SpanData getOneByAttr(List spans, String key, Object value) { + public static SpanData getOneByAttr(List spans, AttributeKey key, T value) { List found = getByAttr(spans, key, value); if (found.size() > 1) { throw new IllegalArgumentException( diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java index 21242718578..762168577c1 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -16,6 +16,7 @@ package io.opentelemetry.sdk.extensions.trace.testbed.nestedcallbacks; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; import static org.hamcrest.core.IsEqual.equalTo; @@ -61,7 +62,7 @@ void test() { ReadableAttributes attrs = spans.get(0).getAttributes(); assertThat(attrs.size()).isEqualTo(3); for (int i = 1; i <= 3; i++) { - assertThat(attrs.get("key" + i).getStringValue()).isEqualTo(Integer.toString(i)); + assertThat(attrs.get(stringKey("key" + i))).isEqualTo(Integer.toString(i)); } assertThat(tracer.getCurrentSpan()).isSameAs(DefaultSpan.getInvalid()); diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/promisepropagation/PromisePropagationTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/promisepropagation/PromisePropagationTest.java index e78c2947fa0..afa803958a8 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/promisepropagation/PromisePropagationTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/promisepropagation/PromisePropagationTest.java @@ -16,8 +16,10 @@ package io.opentelemetry.sdk.extensions.trace.testbed.promisepropagation; +import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static org.assertj.core.api.Assertions.assertThat; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.context.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.extensions.trace.testbed.TestUtils; @@ -103,7 +105,7 @@ void testPromiseCallback() { List finished = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(finished.size()).isEqualTo(4); - String component = "component"; + StringKey component = stringKey("component"); SpanData parentSpanProto = TestUtils.getOneByAttr(finished, component, "example-promises"); assertThat(parentSpanProto).isNotNull(); assertThat(SpanId.isValid(parentSpanProto.getParentSpanId())).isFalse(); diff --git a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java index 58dc6d6dc32..4ed9b38e18e 100644 --- a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java +++ b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java @@ -21,8 +21,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; -import io.opentelemetry.common.AttributeConsumer; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeKey; +import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Event; @@ -393,12 +393,12 @@ private static void emitSingleEvent( private static String renderAttributes(ReadableAttributes attributes) { final StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Attributes:{"); - attributes.forEach( - new AttributeConsumer() { + attributes.forEachRaw( + new RawAttributeConsumer() { private boolean first = true; @Override - public void consume(String key, AttributeValue value) { + public void consume(AttributeKey key, T value) { if (first) { first = false; } else { @@ -406,32 +406,7 @@ public void consume(String key, AttributeValue value) { } stringBuilder.append(key); stringBuilder.append("="); - switch (value.getType()) { - case STRING: - stringBuilder.append(value.getStringValue()); - break; - case BOOLEAN: - stringBuilder.append(value.getBooleanValue()); - break; - case LONG: - stringBuilder.append(value.getLongValue()); - break; - case DOUBLE: - stringBuilder.append(value.getDoubleValue()); - break; - case STRING_ARRAY: - stringBuilder.append(value.getStringArrayValue().toString()); - break; - case BOOLEAN_ARRAY: - stringBuilder.append(value.getBooleanArrayValue().toString()); - break; - case LONG_ARRAY: - stringBuilder.append(value.getLongArrayValue().toString()); - break; - case DOUBLE_ARRAY: - stringBuilder.append(value.getDoubleArrayValue().toString()); - break; - } + stringBuilder.append(value.toString()); } }); stringBuilder.append("}"); From e5723812a2cd63cedc85551a4d20578f8fd9e16f Mon Sep 17 00:00:00 2001 From: jwatson Date: Wed, 9 Sep 2020 11:49:09 -0700 Subject: [PATCH 06/20] little tweaks --- .../trace/RecordEventsReadableSpanTest.java | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java index 325ae514b60..c9365dd3d1d 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java @@ -379,7 +379,7 @@ void setAttribute() { RecordEventsReadableSpan span = createTestRootSpan(); try { span.setAttribute("StringKey", "StringVal"); - span.setAttribute("NullStringKey", (String) null); + span.setAttribute("NullStringKey", null); span.setAttribute("EmptyStringKey", ""); span.setAttribute(stringKey("NullStringAttributeValue"), null); span.setAttribute(stringKey("EmptyStringAttributeValue"), ""); @@ -489,13 +489,13 @@ void setAttribute_emptyArrayAttributeValue() { @Test void setAttribute_nullStringValue() { RecordEventsReadableSpan span = createTestRootSpan(); - span.setAttribute("nullString", (String) null); + span.setAttribute("nullString", null); span.setAttribute("emptyString", ""); span.setAttribute(stringKey("nullStringAttributeValue"), null); span.setAttribute(stringKey("emptyStringAttributeValue"), ""); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(2); - span.setAttribute("emptyString", (String) null); - span.setAttribute("emptyStringAttributeValue", (String) null); + span.setAttribute("emptyString", null); + span.setAttribute("emptyStringAttributeValue", null); assertThat(span.toSpanData().getAttributes().isEmpty()).isTrue(); } @@ -608,15 +608,12 @@ void droppingAndAddingAttributes() { // Test that we still have in the attributes map the latest maxNumberOfAttributes / 2 entries. for (int i = 0; i < maxNumberOfAttributes / 2; i++) { int val = i + maxNumberOfAttributes * 3 / 2; - AttributeValue expectedValue = AttributeValue.longAttributeValue(val); assertThat(spanData.getAttributes().get(longKey("MyStringAttributeKey" + i))) - .isEqualTo(expectedValue); + .isEqualTo(val); } // Test that we have the newest re-added initial entries. for (int i = maxNumberOfAttributes / 2; i < maxNumberOfAttributes; i++) { - AttributeValue expectedValue = AttributeValue.longAttributeValue(i); - assertThat(spanData.getAttributes().get(longKey("MyStringAttributeKey" + i))) - .isEqualTo(expectedValue); + assertThat(spanData.getAttributes().get(longKey("MyStringAttributeKey" + i))).isEqualTo(i); } } finally { span.end(); @@ -840,9 +837,7 @@ private RecordEventsReadableSpan createTestSpan( private void spanDoWork(RecordEventsReadableSpan span, @Nullable Status status) { span.setAttribute("MySingleStringAttributeKey", "MySingleStringAttributeValue"); - attributes - .entrySet() - .forEach(attribute -> span.setAttribute(attribute.getKey(), attribute.getValue())); + attributes.forEach(span::setAttribute); testClock.advanceMillis(MILLIS_PER_SECOND); span.addEvent("event2", Attributes.empty()); testClock.advanceMillis(MILLIS_PER_SECOND); From 230de0af164dce5e10f16f3aaed3f67a6c17329a Mon Sep 17 00:00:00 2001 From: jwatson Date: Wed, 9 Sep 2020 13:18:54 -0700 Subject: [PATCH 07/20] some cleanup, tests are passing --- .../common/AttributeConsumer.java | 32 +------ .../io/opentelemetry/common/AttributeKey.java | 3 +- .../common/AttributeKeyImpl.java | 6 ++ .../io/opentelemetry/common/Attributes.java | 82 ++---------------- .../common/ImmutableKeyValuePairs.java | 35 ++++++-- .../common/RawAttributeConsumer.java | 21 ----- .../common/ReadableAttributes.java | 3 - .../opentelemetry/internal/StringUtils.java | 8 +- .../opentelemetry/common/AttributesTest.java | 12 ++- .../exporters/jaeger/Adapter.java | 6 +- .../exporters/otlp/ResourceAdapter.java | 6 +- .../exporters/otlp/SpanAdapter.java | 14 +-- .../exporters/zipkin/ZipkinSpanExporter.java | 6 +- .../opentelemetry/sdk/resources/Resource.java | 86 +------------------ .../sdk/trace/AttributesMap.java | 39 +-------- .../sdk/trace/SpanBuilderSdk.java | 6 +- .../trace/RecordEventsReadableSpanTest.java | 24 +++--- .../extensions/zpages/TracezZPageHandler.java | 6 +- 18 files changed, 96 insertions(+), 299 deletions(-) delete mode 100644 api/src/main/java/io/opentelemetry/common/RawAttributeConsumer.java diff --git a/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java b/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java index 5f0b2598261..5018e15d873 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java @@ -16,35 +16,7 @@ package io.opentelemetry.common; -import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; -import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.LongKey; -import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; -import java.util.List; - -/** - * Convenience interface for consuming {@link ReadableAttributes}. - * - * @since 0.9.0 - */ +/** Used for iterating over all the key/value pairs in an {@link Attributes} instance. */ public interface AttributeConsumer { - void consume(StringKey key, String value); - - void consume(BooleanKey key, boolean value); - - void consume(DoubleKey key, double value); - - void consume(LongKey key, long value); - - void consume(StringArrayKey key, List value); - - void consume(BooleanArrayKey key, List value); - - void consume(DoubleArrayKey key, List value); - - void consume(LongArrayKey key, List value); + void consume(AttributeKey key, T value); } diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKey.java b/api/src/main/java/io/opentelemetry/common/AttributeKey.java index ea7d91f9f2c..efe958e2cfb 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeKey.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeKey.java @@ -22,7 +22,8 @@ * * @param The type of value that can be set with the key. */ -public interface AttributeKey { +@SuppressWarnings("rawtypes") +public interface AttributeKey extends Comparable { /** Returns the underlying String representation of the key. */ String get(); diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java index 760cffce9d4..78674c4039b 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java @@ -18,6 +18,7 @@ import java.util.List; +@SuppressWarnings("rawtypes") public abstract class AttributeKeyImpl implements AttributeKey { private final String key; @@ -54,6 +55,11 @@ public String toString() { return "AttributeKeyImpl{" + "key='" + key + '\'' + '}'; } + @Override + public int compareTo(AttributeKey o) { + return get().compareTo(o.get()); + } + public static StringKey stringKey(String key) { return new StringKey(key); } diff --git a/api/src/main/java/io/opentelemetry/common/Attributes.java b/api/src/main/java/io/opentelemetry/common/Attributes.java index dad3d04f8f1..5a856a9eab6 100644 --- a/api/src/main/java/io/opentelemetry/common/Attributes.java +++ b/api/src/main/java/io/opentelemetry/common/Attributes.java @@ -26,14 +26,6 @@ import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import com.google.auto.value.AutoValue; -import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; -import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.LongKey; -import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -71,39 +63,12 @@ public T get(AttributeKey key) { return (T) super.get(key); } - @SuppressWarnings("unchecked") - @Override - public void forEachRaw(RawAttributeConsumer consumer) { - List data = data(); - for (int i = 0; i < data.size(); i += 2) { - consumer.consume((AttributeKey) data.get(i), data.get(i + 1)); - } - } - @SuppressWarnings("unchecked") @Override public void forEach(AttributeConsumer consumer) { List data = data(); for (int i = 0; i < data.size(); i += 2) { - Object key = data.get(i); - Object value = data.get(i + 1); - if (key instanceof StringKey) { - consumer.consume((StringKey) key, (String) value); - } else if (key instanceof BooleanKey) { - consumer.consume((BooleanKey) key, (boolean) value); - } else if (key instanceof LongKey) { - consumer.consume((LongKey) key, (long) value); - } else if (key instanceof DoubleKey) { - consumer.consume((DoubleKey) key, (double) value); - } else if (key instanceof StringArrayKey) { - consumer.consume((StringArrayKey) key, (List) value); - } else if (key instanceof BooleanArrayKey) { - consumer.consume((BooleanArrayKey) key, (List) value); - } else if (key instanceof LongArrayKey) { - consumer.consume((LongArrayKey) key, (List) value); - } else if (key instanceof DoubleArrayKey) { - consumer.consume((DoubleArrayKey) key, (List) value); - } + consumer.consume((AttributeKey) data.get(i), data.get(i + 1)); } } @@ -194,42 +159,7 @@ public static Builder newBuilder(ReadableAttributes attributes) { attributes.forEach( new AttributeConsumer() { @Override - public void consume(StringKey key, String value) { - builder.setAttribute(key, value); - } - - @Override - public void consume(BooleanKey key, boolean value) { - builder.setAttribute(key, value); - } - - @Override - public void consume(DoubleKey key, double value) { - builder.setAttribute(key, value); - } - - @Override - public void consume(LongKey key, long value) { - builder.setAttribute(key, value); - } - - @Override - public void consume(StringArrayKey key, List value) { - builder.setAttribute(key, value); - } - - @Override - public void consume(BooleanArrayKey key, List value) { - builder.setAttribute(key, value); - } - - @Override - public void consume(DoubleArrayKey key, List value) { - builder.setAttribute(key, value); - } - - @Override - public void consume(LongArrayKey key, List value) { + public void consume(AttributeKey key, T value) { builder.setAttribute(key, value); } }); @@ -332,7 +262,7 @@ public Builder setAttribute(String key, boolean value) { * @return this Builder */ public Builder setAttribute(String key, String... value) { - return setAttribute(stringArrayKey(key), Arrays.asList(value)); + return setAttribute(stringArrayKey(key), value == null ? null : Arrays.asList(value)); } /** @@ -341,7 +271,7 @@ public Builder setAttribute(String key, String... value) { * @return this Builder */ public Builder setAttribute(String key, Long... value) { - return setAttribute(longArrayKey(key), Arrays.asList(value)); + return setAttribute(longArrayKey(key), value == null ? null : Arrays.asList(value)); } /** @@ -350,7 +280,7 @@ public Builder setAttribute(String key, Long... value) { * @return this Builder */ public Builder setAttribute(String key, Double... value) { - return setAttribute(doubleArrayKey(key), Arrays.asList(value)); + return setAttribute(doubleArrayKey(key), value == null ? null : Arrays.asList(value)); } /** @@ -359,7 +289,7 @@ public Builder setAttribute(String key, Double... value) { * @return this Builder */ public Builder setAttribute(String key, Boolean... value) { - return setAttribute(booleanArrayKey(key), Arrays.asList(value)); + return setAttribute(booleanArrayKey(key), value == null ? null : Arrays.asList(value)); } } } diff --git a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java index 6dc7fce713d..4be249be1f5 100644 --- a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java +++ b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java @@ -34,6 +34,7 @@ * @see Labels * @see Attributes */ +@SuppressWarnings("rawtypes") @Immutable abstract class ImmutableKeyValuePairs implements ReadableKeyValuePairs { @@ -70,6 +71,7 @@ public V get(K key) { return null; } + @SuppressWarnings("unchecked") static List sortAndFilter(Object[] data) { checkArgument( data.length % 2 == 0, "You must provide an even number of key/value pair arguments."); @@ -78,17 +80,20 @@ static List sortAndFilter(Object[] data) { return dedupe(data); } - private static void quickSort(Object[] data, int leftIndex, int rightIndex) { + @SuppressWarnings("unchecked") + private static > void quickSort( + Object[] data, int leftIndex, int rightIndex) { if (leftIndex >= rightIndex) { return; } - String pivotKey = data[rightIndex] == null ? "" : (String) data[rightIndex]; + K pivotKey = (data[rightIndex] == null) ? null : (K) data[rightIndex]; int counter = leftIndex; for (int i = leftIndex; i <= rightIndex; i += 2) { - String value = data[i] == null ? "" : (String) data[i]; - if (value.compareTo(pivotKey) <= 0) { + K value = data[i] == null ? null : (K) data[i]; + + if (compareTo(value, pivotKey) <= 0) { swap(data, counter, i); counter += 2; } @@ -98,6 +103,16 @@ private static void quickSort(Object[] data, int leftIndex, int rightIndex) { quickSort(data, counter, rightIndex); } + private static > int compareTo(K value, K pivotKey) { + if (value == null) { + return pivotKey == null ? 0 : -1; + } + if (pivotKey == null) { + return 1; + } + return value.compareTo(pivotKey); + } + private static List dedupe(Object[] data) { List result = new ArrayList<>(data.length); Object previousKey = null; @@ -105,9 +120,19 @@ private static List dedupe(Object[] data) { for (int i = 0; i < data.length; i += 2) { Object key = data[i]; Object value = data[i + 1]; - if (key == null || "".equals(key)) { + if (key == null) { continue; } + // ugh + if (key instanceof String) { + if ("".equals(key)) { + continue; + } + } else if (key instanceof AttributeKey) { + if ("".equals(((AttributeKey) key).get())) { + continue; + } + } if (key.equals(previousKey)) { continue; } diff --git a/api/src/main/java/io/opentelemetry/common/RawAttributeConsumer.java b/api/src/main/java/io/opentelemetry/common/RawAttributeConsumer.java deleted file mode 100644 index 135f6b83217..00000000000 --- a/api/src/main/java/io/opentelemetry/common/RawAttributeConsumer.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.common; - -public interface RawAttributeConsumer { - void consume(AttributeKey key, T value); -} diff --git a/api/src/main/java/io/opentelemetry/common/ReadableAttributes.java b/api/src/main/java/io/opentelemetry/common/ReadableAttributes.java index d14fcb43867..a7428d7b076 100644 --- a/api/src/main/java/io/opentelemetry/common/ReadableAttributes.java +++ b/api/src/main/java/io/opentelemetry/common/ReadableAttributes.java @@ -32,7 +32,4 @@ public interface ReadableAttributes { /** Iterates over all the key-value pairs of attributes contained by this instance. */ void forEach(AttributeConsumer consumer); - - /** Iterates over all the key-value pairs of attributes contained by this instance. */ - void forEachRaw(RawAttributeConsumer consumer); } diff --git a/api/src/main/java/io/opentelemetry/internal/StringUtils.java b/api/src/main/java/io/opentelemetry/internal/StringUtils.java index 18a63594c5a..8d78038367c 100644 --- a/api/src/main/java/io/opentelemetry/internal/StringUtils.java +++ b/api/src/main/java/io/opentelemetry/internal/StringUtils.java @@ -18,6 +18,7 @@ import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.AttributeKeyImpl; +import java.util.ArrayList; import java.util.List; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; @@ -88,10 +89,9 @@ public static T truncateToSize(AttributeKey key, T value, int limit) { return value; } - String[] newStrings = new String[strings.size()]; - for (int i = 0; i < strings.size(); i++) { - String string = strings.get(i); - newStrings[i] = truncateToSize(string, limit); + List newStrings = new ArrayList<>(strings.size()); + for (String string : strings) { + newStrings.add(truncateToSize(string, limit)); } return (T) newStrings; diff --git a/api/src/test/java/io/opentelemetry/common/AttributesTest.java b/api/src/test/java/io/opentelemetry/common/AttributesTest.java index 1bb80c91427..a40a94fa7f2 100644 --- a/api/src/test/java/io/opentelemetry/common/AttributesTest.java +++ b/api/src/test/java/io/opentelemetry/common/AttributesTest.java @@ -43,7 +43,7 @@ void forEach() { Attributes attributes = Attributes.of(stringKey("key1"), "value1", longKey("key2"), 333L); - attributes.forEach(entriesSeen::put); + attributes.forEach((AttributeConsumer) entriesSeen::put); assertThat(entriesSeen) .containsExactly(entry(stringKey("key1"), "value1"), entry(stringKey("key2"), 333L)); @@ -54,7 +54,7 @@ void forEach_singleAttribute() { final Map entriesSeen = new HashMap<>(); Attributes attributes = Attributes.of(stringKey("key"), "value"); - attributes.forEach(entriesSeen::put); + attributes.forEach((AttributeConsumer) entriesSeen::put); assertThat(entriesSeen).containsExactly(entry(stringKey("key"), "value")); } @@ -62,7 +62,13 @@ void forEach_singleAttribute() { void forEach_empty() { final AtomicBoolean sawSomething = new AtomicBoolean(false); Attributes emptyAttributes = Attributes.empty(); - emptyAttributes.forEach((key, value) -> sawSomething.set(true)); + emptyAttributes.forEach( + new AttributeConsumer() { + @Override + public void consume(AttributeKey key, T value) { + sawSomething.set(true); + } + }); assertThat(sawSomething.get()).isFalse(); } diff --git a/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java b/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java index 2a951cf9ce8..674ad6ed500 100644 --- a/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java +++ b/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java @@ -22,10 +22,10 @@ import com.google.gson.Gson; import com.google.protobuf.Timestamp; import com.google.protobuf.util.Timestamps; +import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.exporters.jaeger.proto.api_v2.Model; import io.opentelemetry.sdk.extensions.otproto.TraceProtoUtils; @@ -185,8 +185,8 @@ static Model.Log toJaegerLog(Event event) { @VisibleForTesting static Collection toKeyValues(ReadableAttributes attributes) { final List tags = new ArrayList<>(attributes.size()); - attributes.forEachRaw( - new RawAttributeConsumer() { + attributes.forEach( + new AttributeConsumer() { @Override public void consume(AttributeKey key, T value) { tags.add(toKeyValue(key, value)); diff --git a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/ResourceAdapter.java b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/ResourceAdapter.java index e049d973654..d34ec6c08d9 100644 --- a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/ResourceAdapter.java +++ b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/ResourceAdapter.java @@ -16,8 +16,8 @@ package io.opentelemetry.exporters.otlp; +import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; -import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.proto.resource.v1.Resource; final class ResourceAdapter { @@ -25,8 +25,8 @@ static Resource toProtoResource(io.opentelemetry.sdk.resources.Resource resource final Resource.Builder builder = Resource.newBuilder(); resource .getAttributes() - .forEachRaw( - new RawAttributeConsumer() { + .forEach( + new AttributeConsumer() { @Override public void consume(AttributeKey key, T value) { builder.addAttributes(CommonAdapter.toProtoAttribute(key, value)); diff --git a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java index 6b4f05201d2..23a2ac4722f 100644 --- a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java +++ b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/SpanAdapter.java @@ -22,9 +22,9 @@ import static io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_PRODUCER; import static io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_SERVER; +import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; -import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.proto.trace.v1.InstrumentationLibrarySpans; import io.opentelemetry.proto.trace.v1.ResourceSpans; import io.opentelemetry.proto.trace.v1.Span; @@ -107,8 +107,8 @@ static Span toProtoSpan(SpanData spanData) { builder.setEndTimeUnixNano(spanData.getEndEpochNanos()); spanData .getAttributes() - .forEachRaw( - new RawAttributeConsumer() { + .forEach( + new AttributeConsumer() { @Override public void consume(AttributeKey key, T value) { builder.addAttributes(CommonAdapter.toProtoAttribute(key, value)); @@ -150,8 +150,8 @@ static Span.Event toProtoSpanEvent(Event event) { builder.setTimeUnixNano(event.getEpochNanos()); event .getAttributes() - .forEachRaw( - new RawAttributeConsumer() { + .forEach( + new AttributeConsumer() { @Override public void consume(AttributeKey key, T value) { builder.addAttributes(CommonAdapter.toProtoAttribute(key, value)); @@ -168,8 +168,8 @@ static Span.Link toProtoSpanLink(Link link) { builder.setSpanId(TraceProtoUtils.toProtoSpanId(link.getContext().getSpanIdAsHexString())); // TODO: Set TraceState; Attributes attributes = link.getAttributes(); - attributes.forEachRaw( - new RawAttributeConsumer() { + attributes.forEach( + new AttributeConsumer() { @Override public void consume(AttributeKey key, T value) { builder.addAttributes(CommonAdapter.toProtoAttribute(key, value)); diff --git a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java index d96105adbdd..7063624a484 100644 --- a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java +++ b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java @@ -19,10 +19,10 @@ import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import static java.util.concurrent.TimeUnit.NANOSECONDS; +import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.common.AttributeValue; -import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -150,8 +150,8 @@ static Span generateSpan(SpanData spanData, Endpoint localEndpoint) { } ReadableAttributes spanAttributes = spanData.getAttributes(); - spanAttributes.forEachRaw( - new RawAttributeConsumer() { + spanAttributes.forEach( + new AttributeConsumer() { @Override public void consume(AttributeKey key, T value) { spanBuilder.putTag(key.get(), attributeValueToString(key, value)); diff --git a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java index 039ffd41929..59b3fe14d77 100644 --- a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java +++ b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java @@ -22,19 +22,11 @@ import com.google.auto.value.extension.memoized.Memoized; import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; -import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; -import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.LongKey; -import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.internal.Utils; -import java.util.List; import java.util.Objects; import java.util.Properties; import java.util.ServiceLoader; @@ -192,42 +184,7 @@ private Merger(Attributes.Builder attrBuilder) { } @Override - public void consume(StringKey key, String value) { - attrBuilder.setAttribute(key, value); - } - - @Override - public void consume(BooleanKey key, boolean value) { - attrBuilder.setAttribute(key, value); - } - - @Override - public void consume(DoubleKey key, double value) { - attrBuilder.setAttribute(key, value); - } - - @Override - public void consume(LongKey key, long value) { - attrBuilder.setAttribute(key, value); - } - - @Override - public void consume(StringArrayKey key, List value) { - attrBuilder.setAttribute(key, value); - } - - @Override - public void consume(BooleanArrayKey key, List value) { - attrBuilder.setAttribute(key, value); - } - - @Override - public void consume(DoubleArrayKey key, List value) { - attrBuilder.setAttribute(key, value); - } - - @Override - public void consume(LongArrayKey key, List value) { + public void consume(AttributeKey key, T value) { attrBuilder.setAttribute(key, value); } } @@ -236,46 +193,7 @@ private static void checkAttributes(ReadableAttributes attributes) { attributes.forEach( new AttributeConsumer() { @Override - public void consume(StringKey key, String value) { - check(key, value); - } - - @Override - public void consume(BooleanKey key, boolean value) { - check(key, value); - } - - @Override - public void consume(DoubleKey key, double value) { - check(key, value); - } - - @Override - public void consume(LongKey key, long value) { - check(key, value); - } - - @Override - public void consume(StringArrayKey key, List value) { - check(key, value); - } - - @Override - public void consume(BooleanArrayKey key, List value) { - check(key, value); - } - - @Override - public void consume(DoubleArrayKey key, List value) { - check(key, value); - } - - @Override - public void consume(LongArrayKey key, List value) { - check(key, value); - } - - private void check(AttributeKey key, T value) { + public void consume(AttributeKey key, T value) { Utils.checkArgument( isValidAndNotEmpty(key), "Attribute key" + ERROR_MESSAGE_INVALID_CHARS); Objects.requireNonNull(value, "Attribute value" + ERROR_MESSAGE_INVALID_VALUE); diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java index b7ecd18c187..c208bdb3e4c 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/AttributesMap.java @@ -18,20 +18,9 @@ import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; -import io.opentelemetry.common.AttributeKeyImpl; -import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; -import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.LongKey; -import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.common.Attributes; -import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import java.util.HashMap; -import java.util.List; import java.util.Map; /** @@ -82,35 +71,9 @@ public boolean isEmpty() { return data.isEmpty(); } - @SuppressWarnings({"unchecked", "rawtypes"}) - @Override - public void forEach(AttributeConsumer consumer) { - for (Map.Entry entry : data.entrySet()) { - AttributeKey key = entry.getKey(); - Object value = entry.getValue(); - if (key instanceof AttributeKeyImpl.StringKey) { - consumer.consume((StringKey) key, (String) value); - } else if (key instanceof AttributeKeyImpl.BooleanKey) { - consumer.consume((BooleanKey) key, (boolean) value); - } else if (key instanceof AttributeKeyImpl.LongKey) { - consumer.consume((LongKey) key, (long) value); - } else if (key instanceof AttributeKeyImpl.DoubleKey) { - consumer.consume((DoubleKey) key, (double) value); - } else if (key instanceof AttributeKeyImpl.StringArrayKey) { - consumer.consume((StringArrayKey) key, (List) value); - } else if (key instanceof AttributeKeyImpl.BooleanArrayKey) { - consumer.consume((BooleanArrayKey) key, (List) value); - } else if (key instanceof AttributeKeyImpl.LongArrayKey) { - consumer.consume((LongArrayKey) key, (List) value); - } else if (key instanceof AttributeKeyImpl.DoubleArrayKey) { - consumer.consume((DoubleArrayKey) key, (List) value); - } - } - } - @SuppressWarnings({"rawtypes", "unchecked"}) @Override - public void forEachRaw(RawAttributeConsumer consumer) { + public void forEach(AttributeConsumer consumer) { for (Map.Entry entry : data.entrySet()) { AttributeKey key = entry.getKey(); Object value = entry.getValue(); diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index 18fe953292c..41ef9149b96 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -22,9 +22,9 @@ import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import io.grpc.Context; +import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; -import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.internal.Utils; @@ -244,8 +244,8 @@ public Span startSpan() { if (attributes == null) { attributes = new AttributesMap(traceConfig.getMaxNumberOfAttributes()); } - samplingAttributes.forEachRaw( - new RawAttributeConsumer() { + samplingAttributes.forEach( + new AttributeConsumer() { @Override public void consume(AttributeKey key, T value) { attributes.put(key, value); diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java index c9365dd3d1d..7c091ebbf11 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java @@ -27,10 +27,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; +import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; -import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; import io.opentelemetry.sdk.internal.TestClock; @@ -393,10 +393,10 @@ void setAttribute() { span.setAttribute( booleanArrayKey("ArrayBooleanKey"), Arrays.asList(true, false, false, true)); // These should be dropped - span.setAttribute(stringArrayKey("NullArrayStringKey"), Arrays.asList((String[]) null)); - span.setAttribute(longArrayKey("NullArrayLongKey"), Arrays.asList((Long[]) null)); - span.setAttribute(doubleArrayKey("NullArrayDoubleKey"), Arrays.asList((Double[]) null)); - span.setAttribute(booleanArrayKey("NullArrayBooleanKey"), Arrays.asList((Boolean[]) null)); + span.setAttribute(stringArrayKey("NullArrayStringKey"), null); + span.setAttribute(longArrayKey("NullArrayLongKey"), null); + span.setAttribute(doubleArrayKey("NullArrayDoubleKey"), null); + span.setAttribute(booleanArrayKey("NullArrayBooleanKey"), null); // These should be maintained span.setAttribute(longArrayKey("ArrayWithNullLongKey"), Arrays.asList(new Long[] {null})); span.setAttribute( @@ -463,19 +463,19 @@ void setAttribute_nullKeys() { @Test void setAttribute_emptyArrayAttributeValue() { RecordEventsReadableSpan span = createTestRootSpan(); - span.setAttribute(stringArrayKey("stringArrayAttribute"), Arrays.asList((String[]) null)); + span.setAttribute(stringArrayKey("stringArrayAttribute"), null); assertThat(span.toSpanData().getAttributes().size()).isZero(); span.setAttribute(stringArrayKey("stringArrayAttribute"), Collections.emptyList()); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(1); - span.setAttribute(booleanArrayKey("boolArrayAttribute"), Arrays.asList((Boolean[]) null)); + span.setAttribute(booleanArrayKey("boolArrayAttribute"), null); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(1); span.setAttribute(booleanArrayKey("boolArrayAttribute"), Collections.emptyList()); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(2); - span.setAttribute(longArrayKey("longArrayAttribute"), Arrays.asList((Long[]) null)); + span.setAttribute(longArrayKey("longArrayAttribute"), null); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(2); span.setAttribute(longArrayKey("longArrayAttribute"), Collections.emptyList()); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(3); - span.setAttribute(doubleArrayKey("doubleArrayAttribute"), Arrays.asList((Double[]) null)); + span.setAttribute(doubleArrayKey("doubleArrayAttribute"), null); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(3); span.setAttribute(doubleArrayKey("doubleArrayAttribute"), Collections.emptyList()); assertThat(span.toSpanData().getAttributes().size()).isEqualTo(4); @@ -875,8 +875,8 @@ private void verifySpanData( // verify equality manually, since the implementations don't all equals with each other. ReadableAttributes spanDataAttributes = spanData.getAttributes(); assertThat(spanDataAttributes.size()).isEqualTo(attributes.size()); - spanDataAttributes.forEachRaw( - new RawAttributeConsumer() { + spanDataAttributes.forEach( + new AttributeConsumer() { @Override public void consume(AttributeKey key, T value) { assertThat(attributes.get(key)).isEqualTo(value); @@ -897,7 +897,7 @@ void testAsSpanData() { Resource resource = this.resource; Attributes attributes = TestUtils.generateRandomAttributes(); final AttributesMap attributesWithCapacity = new AttributesMap(32); - attributes.forEach(attributesWithCapacity::put); + attributes.forEach((AttributeConsumer) attributesWithCapacity::put); Attributes event1Attributes = TestUtils.generateRandomAttributes(); Attributes event2Attributes = TestUtils.generateRandomAttributes(); SpanContext context = diff --git a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java index 4ed9b38e18e..72ed810ef49 100644 --- a/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java +++ b/sdk_extensions/zpages/src/main/java/io/opentelemetry/sdk/extensions/zpages/TracezZPageHandler.java @@ -21,8 +21,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; -import io.opentelemetry.common.RawAttributeConsumer; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.SpanData.Event; @@ -393,8 +393,8 @@ private static void emitSingleEvent( private static String renderAttributes(ReadableAttributes attributes) { final StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Attributes:{"); - attributes.forEachRaw( - new RawAttributeConsumer() { + attributes.forEach( + new AttributeConsumer() { private boolean first = true; @Override From fc1154447a8e96f94dfa40b07243b0c6575318f1 Mon Sep 17 00:00:00 2001 From: jwatson Date: Wed, 9 Sep 2020 13:39:20 -0700 Subject: [PATCH 08/20] remove AttributeValue and clean up misc. references to it. --- .../io/opentelemetry/common/AttributeKey.java | 2 +- .../common/AttributeKeyImpl.java | 32 +- .../opentelemetry/common/AttributeType.java | 34 ++ .../opentelemetry/common/AttributeValue.java | 468 ------------------ .../io/opentelemetry/common/Attributes.java | 24 +- .../opentelemetry/internal/StringUtils.java | 6 +- .../common/AttributeValueTest.java | 114 ----- .../exporters/jaeger/Adapter.java | 3 +- .../exporters/zipkin/ZipkinSpanExporter.java | 8 +- .../io/opentelemetry/sdk/trace/Sampler.java | 3 +- .../trace/RecordEventsReadableSpanTest.java | 3 +- .../opentelemetry/sdk/trace/TestSpanData.java | 9 +- 12 files changed, 75 insertions(+), 631 deletions(-) create mode 100644 api/src/main/java/io/opentelemetry/common/AttributeType.java delete mode 100644 api/src/main/java/io/opentelemetry/common/AttributeValue.java delete mode 100644 api/src/test/java/io/opentelemetry/common/AttributeValueTest.java diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKey.java b/api/src/main/java/io/opentelemetry/common/AttributeKey.java index efe958e2cfb..d902319a1d2 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeKey.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeKey.java @@ -28,5 +28,5 @@ public interface AttributeKey extends Comparable { String get(); /** Returns the type of attribute for this key. Useful for building switch statements. */ - AttributeValue.Type getType(); + AttributeType getType(); } diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java index 78674c4039b..10931149ccf 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java @@ -70,8 +70,8 @@ private StringKey(String key) { } @Override - public AttributeValue.Type getType() { - return AttributeValue.Type.STRING; + public AttributeType getType() { + return AttributeType.STRING; } } @@ -85,8 +85,8 @@ private BooleanKey(String key) { } @Override - public AttributeValue.Type getType() { - return AttributeValue.Type.BOOLEAN; + public AttributeType getType() { + return AttributeType.BOOLEAN; } } @@ -100,8 +100,8 @@ private LongKey(String key) { } @Override - public AttributeValue.Type getType() { - return AttributeValue.Type.LONG; + public AttributeType getType() { + return AttributeType.LONG; } } @@ -115,8 +115,8 @@ private DoubleKey(String key) { } @Override - public AttributeValue.Type getType() { - return AttributeValue.Type.DOUBLE; + public AttributeType getType() { + return AttributeType.DOUBLE; } } @@ -130,8 +130,8 @@ private StringArrayKey(String key) { } @Override - public AttributeValue.Type getType() { - return AttributeValue.Type.STRING_ARRAY; + public AttributeType getType() { + return AttributeType.STRING_ARRAY; } } @@ -145,8 +145,8 @@ private BooleanArrayKey(String key) { } @Override - public AttributeValue.Type getType() { - return AttributeValue.Type.BOOLEAN_ARRAY; + public AttributeType getType() { + return AttributeType.BOOLEAN_ARRAY; } } @@ -160,8 +160,8 @@ private LongArrayKey(String key) { } @Override - public AttributeValue.Type getType() { - return AttributeValue.Type.LONG_ARRAY; + public AttributeType getType() { + return AttributeType.LONG_ARRAY; } } @@ -175,8 +175,8 @@ private DoubleArrayKey(String key) { } @Override - public AttributeValue.Type getType() { - return AttributeValue.Type.DOUBLE_ARRAY; + public AttributeType getType() { + return AttributeType.DOUBLE_ARRAY; } } } diff --git a/api/src/main/java/io/opentelemetry/common/AttributeType.java b/api/src/main/java/io/opentelemetry/common/AttributeType.java new file mode 100644 index 00000000000..3f8cc7a6d17 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/common/AttributeType.java @@ -0,0 +1,34 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.common; + +/** + * An enum that represents all the possible value types for an {@code AttributeKey} and hence the + * types of values that are allowed for {@link Attributes}. + * + * @since 0.1.0 + */ +public enum AttributeType { + STRING, + BOOLEAN, + LONG, + DOUBLE, + STRING_ARRAY, + BOOLEAN_ARRAY, + LONG_ARRAY, + DOUBLE_ARRAY +} diff --git a/api/src/main/java/io/opentelemetry/common/AttributeValue.java b/api/src/main/java/io/opentelemetry/common/AttributeValue.java deleted file mode 100644 index 90fd6a426bd..00000000000 --- a/api/src/main/java/io/opentelemetry/common/AttributeValue.java +++ /dev/null @@ -1,468 +0,0 @@ -/* - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.common; - -import com.google.auto.value.AutoValue; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -/** - * A class that represents all the possible values for an attribute. An attribute can have 4 types - * of values: {@code String}, {@code boolean}, {@code long} or {@code double}, represented through - * {@code AttributeValue.Type}. - * - * @since 0.1.0 - */ -@Immutable -public abstract class AttributeValue { - - /** - * An enum that represents all the possible value types for an {@code AttributeValue}. - * - * @since 0.1.0 - */ - public enum Type { - STRING, - BOOLEAN, - LONG, - DOUBLE, - STRING_ARRAY, - BOOLEAN_ARRAY, - LONG_ARRAY, - DOUBLE_ARRAY - } - - /** - * Returns an {@code AttributeValue} with a string value. - * - * @param stringValue The new value. - * @return an {@code AttributeValue} with a string value. - * @since 0.1.0 - */ - public static AttributeValue stringAttributeValue(String stringValue) { - return AttributeValueString.create(stringValue); - } - - /** - * Returns an {@code AttributeValue} with a boolean value. - * - * @param booleanValue The new value. - * @return an {@code AttributeValue} with a boolean value. - * @since 0.1.0 - */ - public static AttributeValue booleanAttributeValue(boolean booleanValue) { - return AttributeValueBoolean.create(booleanValue); - } - - /** - * Returns an {@code AttributeValue} with a long value. - * - * @param longValue The new value. - * @return an {@code AttributeValue} with a long value. - * @since 0.1.0 - */ - public static AttributeValue longAttributeValue(long longValue) { - return AttributeValueLong.create(longValue); - } - - /** - * Returns an {@code AttributeValue} with a double value. - * - * @param doubleValue The new value. - * @return an {@code AttributeValue} with a double value. - * @since 0.1.0 - */ - public static AttributeValue doubleAttributeValue(double doubleValue) { - return AttributeValueDouble.create(doubleValue); - } - - /** - * Returns an {@code AttributeValue} with a String array value. - * - * @param stringValues The new values. - * @return an {@code AttributeValue} with a String array value. - * @since 0.3.0 - */ - public static AttributeValue arrayAttributeValue(String... stringValues) { - return AttributeValueStringArray.create(stringValues); - } - - /** - * Returns an {@code AttributeValue} with a boolean array value. - * - * @param booleanValues The new values. - * @return an {@code AttributeValue} with a boolean array value. - * @since 0.3.0 - */ - public static AttributeValue arrayAttributeValue(Boolean... booleanValues) { - return AttributeValueBooleanArray.create(booleanValues); - } - - /** - * Returns an {@code AttributeValue} with a long array value. - * - * @param longValues The new values. - * @return an {@code AttributeValue} with a long array value. - * @since 0.3.0 - */ - public static AttributeValue arrayAttributeValue(Long... longValues) { - return AttributeValueLongArray.create(longValues); - } - - /** - * Returns an {@code AttributeValue} with a double array value. - * - * @param doubleValues The new values. - * @return an {@code AttributeValue} with a double array value. - * @since 0.3.0 - */ - public static AttributeValue arrayAttributeValue(Double... doubleValues) { - return AttributeValueDoubleArray.create(doubleValues); - } - - AttributeValue() {} - - /** - * Returns the string value of this {@code AttributeValue}. An UnsupportedOperationException will - * be thrown if getType() is not {@link Type#STRING}. - * - * @return the string value of this {@code AttributeValue}. - * @since 0.1.0 - */ - public String getStringValue() { - throw new UnsupportedOperationException( - String.format("This type can only return %s data", getType().name())); - } - - /** - * Returns the boolean value of this {@code AttributeValue}. An UnsupportedOperationException will - * be thrown if getType() is not {@link Type#BOOLEAN}. - * - * @return the boolean value of this {@code AttributeValue}. - * @since 0.1.0 - */ - public boolean getBooleanValue() { - throw new UnsupportedOperationException( - String.format("This type can only return %s data", getType().name())); - } - - /** - * Returns the long value of this {@code AttributeValue}. An UnsupportedOperationException will be - * thrown if getType() is not {@link Type#LONG}. - * - * @return the long value of this {@code AttributeValue}. - * @since 0.1.0 - */ - public long getLongValue() { - throw new UnsupportedOperationException( - String.format("This type can only return %s data", getType().name())); - } - - /** - * Returns the double value of this {@code AttributeValue}. An UnsupportedOperationException will - * be thrown if getType() is not {@link Type#DOUBLE}. - * - * @return the double value of this {@code AttributeValue}. - * @since 0.1.0 - */ - public double getDoubleValue() { - throw new UnsupportedOperationException( - String.format("This type can only return %s data", getType().name())); - } - - /** - * Returns the String array value of this {@code AttributeValue}. An UnsupportedOperationException - * will be thrown if getType() is not {@link Type#STRING_ARRAY}. - * - * @return the array values of this {@code AttributeValue}. - * @since 0.3.0 - */ - public List getStringArrayValue() { - throw new UnsupportedOperationException( - String.format("This type can only return %s data", getType().name())); - } - - /** - * Returns the boolean array value of this {@code AttributeValue}. An - * UnsupportedOperationException will be thrown if getType() is not {@link Type#BOOLEAN_ARRAY}. - * - * @return the array values of this {@code AttributeValue}. - * @since 0.3.0 - */ - public List getBooleanArrayValue() { - throw new UnsupportedOperationException( - String.format("This type can only return %s data", getType().name())); - } - - /** - * Returns the long array value of this {@code AttributeValue}. An UnsupportedOperationException - * will be thrown if getType() is not {@link Type#LONG_ARRAY}. - * - * @return the array values of this {@code AttributeValue}. - * @since 0.3.0 - */ - public List getLongArrayValue() { - throw new UnsupportedOperationException( - String.format("This type can only return %s data", getType().name())); - } - - /** - * Returns the double array value of this {@code AttributeValue}. An UnsupportedOperationException - * will be thrown if getType() is not {@link Type#DOUBLE_ARRAY}. - * - * @return the array values of this {@code AttributeValue}. - * @since 0.3.0 - */ - public List getDoubleArrayValue() { - throw new UnsupportedOperationException( - String.format("This type can only return %s data", getType().name())); - } - - /** - * Returns a {@code Type} corresponding to the underlying value of this {@code AttributeValue}. - * - * @return the {@code Type} for the value of this {@code AttributeValue}. - * @since 0.1.0 - */ - public abstract Type getType(); - - /** - * Returns {@code true} if the {@code AttributeValue} contains a {@code null} value. - * - * @return {@code true} if the {@code AttributeValue} contains a {@code null} value. - * @since 0.8.0 - */ - public boolean isNull() { - return false; - } - - @Immutable - @AutoValue - abstract static class AttributeValueString extends AttributeValue { - - AttributeValueString() {} - - static AttributeValue create(String stringValue) { - return new AutoValue_AttributeValue_AttributeValueString(stringValue); - } - - @Override - public final Type getType() { - return Type.STRING; - } - - @Override - public boolean isNull() { - return getStringValue() == null; - } - - @Override - @Nullable - public abstract String getStringValue(); - } - - @Immutable - @AutoValue - abstract static class AttributeValueBoolean extends AttributeValue { - - AttributeValueBoolean() {} - - static AttributeValue create(boolean booleanValue) { - return new AutoValue_AttributeValue_AttributeValueBoolean(booleanValue); - } - - @Override - public final Type getType() { - return Type.BOOLEAN; - } - - @Override - public abstract boolean getBooleanValue(); - } - - @Immutable - @AutoValue - abstract static class AttributeValueLong extends AttributeValue { - - AttributeValueLong() {} - - static AttributeValue create(long longValue) { - return new AutoValue_AttributeValue_AttributeValueLong(longValue); - } - - @Override - public final Type getType() { - return Type.LONG; - } - - @Override - public abstract long getLongValue(); - } - - @Immutable - @AutoValue - abstract static class AttributeValueDouble extends AttributeValue { - - AttributeValueDouble() {} - - static AttributeValue create(double doubleValue) { - return new AutoValue_AttributeValue_AttributeValueDouble(doubleValue); - } - - @Override - public final Type getType() { - return Type.DOUBLE; - } - - @Override - public abstract double getDoubleValue(); - } - - @Immutable - @AutoValue - abstract static class AttributeValueStringArray extends AttributeValue { - - private static final AttributeValue EMPTY = - new AutoValue_AttributeValue_AttributeValueStringArray(Collections.emptyList()); - - AttributeValueStringArray() {} - - static AttributeValue create(String... stringValues) { - if (stringValues == null) { - return EMPTY; - } - return new AutoValue_AttributeValue_AttributeValueStringArray( - Collections.unmodifiableList(Arrays.asList(stringValues))); - } - - @Override - public final Type getType() { - return Type.STRING_ARRAY; - } - - @Override - public boolean isNull() { - return this == EMPTY; - } - - @Override - public abstract List getStringArrayValue(); - } - - @Immutable - @AutoValue - abstract static class AttributeValueBooleanArray extends AttributeValue { - - private static final AttributeValue EMPTY = - new AutoValue_AttributeValue_AttributeValueBooleanArray(Collections.emptyList()); - - AttributeValueBooleanArray() {} - - static AttributeValue create(Boolean... booleanValues) { - if (booleanValues == null) { - return EMPTY; - } - List values = new ArrayList<>(booleanValues.length); - values.addAll(Arrays.asList(booleanValues)); - return new AutoValue_AttributeValue_AttributeValueBooleanArray( - Collections.unmodifiableList(values)); - } - - @Override - public final Type getType() { - return Type.BOOLEAN_ARRAY; - } - - @Override - public boolean isNull() { - return this == EMPTY; - } - - @Override - public abstract List getBooleanArrayValue(); - } - - @Immutable - @AutoValue - abstract static class AttributeValueLongArray extends AttributeValue { - - private static final AttributeValue EMPTY = - new AutoValue_AttributeValue_AttributeValueLongArray(Collections.emptyList()); - - AttributeValueLongArray() {} - - static AttributeValue create(Long... longValues) { - if (longValues == null) { - return EMPTY; - } - List values = new ArrayList<>(longValues.length); - values.addAll(Arrays.asList(longValues)); - return new AutoValue_AttributeValue_AttributeValueLongArray( - Collections.unmodifiableList(values)); - } - - @Override - public final Type getType() { - return Type.LONG_ARRAY; - } - - @Override - public boolean isNull() { - return this == EMPTY; - } - - @Override - public abstract List getLongArrayValue(); - } - - @Immutable - @AutoValue - abstract static class AttributeValueDoubleArray extends AttributeValue { - - private static final AttributeValue EMPTY = - new AutoValue_AttributeValue_AttributeValueDoubleArray(Collections.emptyList()); - - AttributeValueDoubleArray() {} - - static AttributeValue create(Double... doubleValues) { - if (doubleValues == null) { - return EMPTY; - } - List values = new ArrayList<>(doubleValues.length); - values.addAll(Arrays.asList(doubleValues)); - return new AutoValue_AttributeValue_AttributeValueDoubleArray( - Collections.unmodifiableList(values)); - } - - @Override - public final Type getType() { - return Type.DOUBLE_ARRAY; - } - - @Override - public boolean isNull() { - return this == EMPTY; - } - - @Override - public abstract List getDoubleArrayValue(); - } -} diff --git a/api/src/main/java/io/opentelemetry/common/Attributes.java b/api/src/main/java/io/opentelemetry/common/Attributes.java index 5a856a9eab6..98c7435128d 100644 --- a/api/src/main/java/io/opentelemetry/common/Attributes.java +++ b/api/src/main/java/io/opentelemetry/common/Attributes.java @@ -35,7 +35,7 @@ /** * An immutable container for attributes. * - *

The keys are {@link String}s and the values are {@link AttributeValue} instances. + *

The keys are {@link AttributeKey}s and the values are Object instances. */ @SuppressWarnings("rawtypes") @Immutable @@ -189,11 +189,7 @@ public Attributes build() { return sortAndFilterToAttributes(data.toArray()); } - /** - * Sets a bare {@link AttributeValue} into this. - * - * @return this Builder - */ + /** Sets a {@link AttributeKey} with associated value into this. */ public Builder setAttribute(AttributeKey key, T value) { if (key == null || key.get().length() == 0) { return this; @@ -221,7 +217,7 @@ public Builder setAttribute(AttributeKey key, T value) { } /** - * Sets a String {@link AttributeValue} into this. + * Sets a String attribute into this. * * @return this Builder */ @@ -230,7 +226,7 @@ public Builder setAttribute(String key, String value) { } /** - * Sets a long {@link AttributeValue} into this. + * Sets a long attribute into this. * * @return this Builder */ @@ -239,7 +235,7 @@ public Builder setAttribute(String key, long value) { } /** - * Sets a double {@link AttributeValue} into this. + * Sets a double attribute into this. * * @return this Builder */ @@ -248,7 +244,7 @@ public Builder setAttribute(String key, double value) { } /** - * Sets a boolean {@link AttributeValue} into this. + * Sets a boolean attribute into this. * * @return this Builder */ @@ -257,7 +253,7 @@ public Builder setAttribute(String key, boolean value) { } /** - * Sets a String array {@link AttributeValue} into this. + * Sets a String array attribute into this. * * @return this Builder */ @@ -266,7 +262,7 @@ public Builder setAttribute(String key, String... value) { } /** - * Sets a Long array {@link AttributeValue} into this. + * Sets a Long array attribute into this. * * @return this Builder */ @@ -275,7 +271,7 @@ public Builder setAttribute(String key, Long... value) { } /** - * Sets a Double array {@link AttributeValue} into this. + * Sets a Double array attribute into this. * * @return this Builder */ @@ -284,7 +280,7 @@ public Builder setAttribute(String key, Double... value) { } /** - * Sets a Boolean array {@link AttributeValue} into this. + * Sets a Boolean array attribute into this. * * @return this Builder */ diff --git a/api/src/main/java/io/opentelemetry/internal/StringUtils.java b/api/src/main/java/io/opentelemetry/internal/StringUtils.java index 8d78038367c..d67712d7d65 100644 --- a/api/src/main/java/io/opentelemetry/internal/StringUtils.java +++ b/api/src/main/java/io/opentelemetry/internal/StringUtils.java @@ -64,10 +64,10 @@ public static boolean isValidMetricName(String metricName) { /** * If given attribute is of type STRING and has more characters than given {@code limit} then - * return new AttributeValue with string truncated to {@code limit} characters. + * return new value with string truncated to {@code limit} characters. * - *

If given attribute is of type STRING_ARRAY and non-empty then return new AttributeValue with - * every element truncated to {@code limit} characters. + *

If given attribute is of type STRING_ARRAY and non-empty then return new value with every + * element truncated to {@code limit} characters. * *

Otherwise return given {@code value} * diff --git a/api/src/test/java/io/opentelemetry/common/AttributeValueTest.java b/api/src/test/java/io/opentelemetry/common/AttributeValueTest.java deleted file mode 100644 index 01706be6cb7..00000000000 --- a/api/src/test/java/io/opentelemetry/common/AttributeValueTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2019, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.common; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.google.common.testing.EqualsTester; -import org.junit.jupiter.api.Test; - -class AttributeValueTest { - - @Test - void attributeValue_EqualsAndHashCode() { - EqualsTester tester = new EqualsTester(); - tester.addEqualityGroup( - AttributeValue.stringAttributeValue("MyStringAttributeValue"), - AttributeValue.stringAttributeValue("MyStringAttributeValue")); - tester.addEqualityGroup(AttributeValue.stringAttributeValue("MyStringAttributeDiffValue")); - tester.addEqualityGroup( - AttributeValue.booleanAttributeValue(true), AttributeValue.booleanAttributeValue(true)); - tester.addEqualityGroup(AttributeValue.booleanAttributeValue(false)); - tester.addEqualityGroup( - AttributeValue.longAttributeValue(123456L), AttributeValue.longAttributeValue(123456L)); - tester.addEqualityGroup(AttributeValue.longAttributeValue(1234567L)); - tester.addEqualityGroup( - AttributeValue.doubleAttributeValue(1.23456), AttributeValue.doubleAttributeValue(1.23456)); - tester.addEqualityGroup(AttributeValue.doubleAttributeValue(1.234567)); - tester.addEqualityGroup( - AttributeValue.arrayAttributeValue( - "MyArrayStringAttributeValue1", "MyArrayStringAttributeValue2"), - AttributeValue.arrayAttributeValue( - "MyArrayStringAttributeValue1", "MyArrayStringAttributeValue2")); - tester.addEqualityGroup(AttributeValue.arrayAttributeValue("MyArrayStringAttributeDiffValue")); - tester.addEqualityGroup( - AttributeValue.arrayAttributeValue(true, false, true), - AttributeValue.arrayAttributeValue(true, false, true)); - tester.addEqualityGroup(AttributeValue.arrayAttributeValue(false)); - tester.addEqualityGroup( - AttributeValue.arrayAttributeValue(123456L, 7890L), - AttributeValue.arrayAttributeValue(123456L, 7890L)); - tester.addEqualityGroup(AttributeValue.arrayAttributeValue(1234567L)); - tester.addEqualityGroup( - AttributeValue.arrayAttributeValue(1.23456, 7.890), - AttributeValue.arrayAttributeValue(1.23456, 7.890)); - tester.addEqualityGroup(AttributeValue.arrayAttributeValue(1.234567)); - tester.testEquals(); - } - - @Test - void doNotCrashOnNull() { - AttributeValue.stringAttributeValue(null); - AttributeValue.arrayAttributeValue((String[]) null); - AttributeValue.arrayAttributeValue((Boolean[]) null); - AttributeValue.arrayAttributeValue((Long[]) null); - AttributeValue.arrayAttributeValue((Double[]) null); - } - - @Test - void attributeValue_ToString() { - AttributeValue attribute = AttributeValue.stringAttributeValue("MyStringAttributeValue"); - assertThat(attribute.toString()).contains("MyStringAttributeValue"); - attribute = AttributeValue.booleanAttributeValue(true); - assertThat(attribute.toString()).contains("true"); - attribute = AttributeValue.longAttributeValue(123456L); - assertThat(attribute.toString()).contains("123456"); - attribute = AttributeValue.doubleAttributeValue(1.23456); - assertThat(attribute.toString()).contains("1.23456"); - attribute = - AttributeValue.arrayAttributeValue( - "MyArrayStringAttributeValue1", "MyArrayStringAttributeValue2"); - assertThat(attribute.toString()).contains("MyArrayStringAttributeValue1"); - assertThat(attribute.toString()).contains("MyArrayStringAttributeValue2"); - attribute = AttributeValue.arrayAttributeValue(true, false); - assertThat(attribute.toString()).contains("true"); - assertThat(attribute.toString()).contains("false"); - attribute = AttributeValue.arrayAttributeValue(12345L, 67890L); - assertThat(attribute.toString()).contains("12345"); - assertThat(attribute.toString()).contains("67890"); - attribute = AttributeValue.arrayAttributeValue(1.2345, 6.789); - assertThat(attribute.toString()).contains("1.2345"); - assertThat(attribute.toString()).contains("6.789"); - } - - @Test - void arrayAttributeValue_nullValuesWithinArray() { - AttributeValue attribute; - - attribute = AttributeValue.arrayAttributeValue("string", null, "", "string"); - assertThat(attribute.getStringArrayValue().size()).isEqualTo(4); - - attribute = AttributeValue.arrayAttributeValue(10L, null, 20L); - assertThat(attribute.getLongArrayValue().size()).isEqualTo(3); - - attribute = AttributeValue.arrayAttributeValue(true, null, false); - assertThat(attribute.getBooleanArrayValue().size()).isEqualTo(3); - - attribute = AttributeValue.arrayAttributeValue(1.2, null, 3.4); - assertThat(attribute.getDoubleArrayValue().size()).isEqualTo(3); - } -} diff --git a/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java b/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java index 674ad6ed500..c259453b418 100644 --- a/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java +++ b/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java @@ -25,7 +25,6 @@ import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.exporters.jaeger.proto.api_v2.Model; import io.opentelemetry.sdk.extensions.otproto.TraceProtoUtils; @@ -196,7 +195,7 @@ public void consume(AttributeKey key, T value) { } /** - * Converts the given key and {@link AttributeValue} into Jaeger's {@link Model.KeyValue}. + * Converts the given {@link AttributeKey} and value into Jaeger's {@link Model.KeyValue}. * * @param key the entry key as string * @param value the entry value diff --git a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java index 7063624a484..e0f00d734e6 100644 --- a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java +++ b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java @@ -22,7 +22,7 @@ import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.AttributeKeyImpl.StringKey; -import io.opentelemetry.common.AttributeValue; +import io.opentelemetry.common.AttributeType; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -154,7 +154,7 @@ static Span generateSpan(SpanData spanData, Endpoint localEndpoint) { new AttributeConsumer() { @Override public void consume(AttributeKey key, T value) { - spanBuilder.putTag(key.get(), attributeValueToString(key, value)); + spanBuilder.putTag(key.get(), valueToString(key, value)); } }); Status status = spanData.getStatus(); @@ -226,8 +226,8 @@ private static long toEpochMicros(long epochNanos) { return NANOSECONDS.toMicros(epochNanos); } - private static String attributeValueToString(AttributeKey key, T attributeValue) { - AttributeValue.Type type = key.getType(); + private static String valueToString(AttributeKey key, T attributeValue) { + AttributeType type = key.getType(); switch (type) { case STRING: case BOOLEAN: diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Sampler.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Sampler.java index 51825da9127..1bb748ac2c8 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Sampler.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Sampler.java @@ -16,7 +16,6 @@ package io.opentelemetry.sdk.trace; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.trace.Link; @@ -39,7 +38,7 @@ public interface Sampler { * the parentContext, unless this is a root span. * @param name the name of the new {@code Span}. * @param spanKind the {@link Kind} of the {@code Span}. - * @param attributes list of {@link AttributeValue} with their keys. + * @param attributes {@link ReadableAttributes} associated with the span. * @param parentLinks the parentLinks associated with the new {@code Span}. * @return sampling samplingResult whether span should be sampled or not. */ diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java index 7c091ebbf11..bfa64df4efb 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java @@ -29,7 +29,6 @@ import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -449,7 +448,7 @@ void setAttribute_emptyKeys() { @Test void setAttribute_nullKeys() { RecordEventsReadableSpan span = createTestRootSpan(); - span.setAttribute(null, AttributeValue.stringAttributeValue("")); + span.setAttribute(stringKey(null), ""); span.setAttribute(null, 1000L); span.setAttribute(null, 10.0); span.setAttribute(null, false); diff --git a/testing_internal/src/main/java/io/opentelemetry/sdk/trace/TestSpanData.java b/testing_internal/src/main/java/io/opentelemetry/sdk/trace/TestSpanData.java index 6329a29aae4..d4d2717fbb7 100644 --- a/testing_internal/src/main/java/io/opentelemetry/sdk/trace/TestSpanData.java +++ b/testing_internal/src/main/java/io/opentelemetry/sdk/trace/TestSpanData.java @@ -17,7 +17,6 @@ package io.opentelemetry.sdk.trace; import com.google.auto.value.AutoValue; -import io.opentelemetry.common.AttributeValue; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -173,12 +172,12 @@ public abstract Builder setInstrumentationLibraryInfo( public abstract Builder setEndEpochNanos(long epochNanos); /** - * Set the attributes that are associated with this span, as a Map of String keys to - * AttributeValue instances. Must not be null, may be empty. + * Set the attributes that are associated with this span, in the form of {@link + * ReadableAttributes}. * - * @param attributes a Map<String, AttributeValue> of attributes. + * @param attributes {@link ReadableAttributes} for this span. * @return this - * @see AttributeValue + * @see ReadableAttributes * @since 0.1.0 */ public abstract Builder setAttributes(ReadableAttributes attributes); From 01c89f1378693435cc8576a1ff668871e4a209bd Mon Sep 17 00:00:00 2001 From: jwatson Date: Wed, 9 Sep 2020 14:01:07 -0700 Subject: [PATCH 09/20] clean up some unused bits --- .../common/ImmutableKeyValuePairs.java | 26 +++-------- .../opentelemetry/common/LabelConsumer.java | 5 +-- .../java/io/opentelemetry/common/Labels.java | 10 +++++ .../common/ReadableKeyValuePairs.java | 43 ------------------- .../opentelemetry/common/AttributesTest.java | 4 +- .../sdk/trace/RecordEventsReadableSpan.java | 5 +-- .../trace/RecordEventsReadableSpanTest.java | 2 +- 7 files changed, 22 insertions(+), 73 deletions(-) delete mode 100644 api/src/main/java/io/opentelemetry/common/ReadableKeyValuePairs.java diff --git a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java index 4be249be1f5..916d01c384f 100644 --- a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java +++ b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java @@ -25,8 +25,7 @@ import javax.annotation.concurrent.Immutable; /** - * An immutable set of key-value pairs. Keys are only {@link String} typed. Can be iterated over - * using the {@link #forEach(KeyValueConsumer)} method. + * An immutable set of key-value pairs. Keys are only {@link String} typed. * *

Key-value pairs are dropped for {@code null} or empty keys. * @@ -36,30 +35,20 @@ */ @SuppressWarnings("rawtypes") @Immutable -abstract class ImmutableKeyValuePairs implements ReadableKeyValuePairs { +abstract class ImmutableKeyValuePairs { List data() { return Collections.emptyList(); } - @Override public int size() { return data().size() / 2; } - @Override public boolean isEmpty() { return data().isEmpty(); } - @Override - @SuppressWarnings("unchecked") - public void forEach(KeyValueConsumer consumer) { - for (int i = 0; i < data().size(); i += 2) { - consumer.consume((K) data().get(i), (V) data().get(i + 1)); - } - } - @Nullable @SuppressWarnings("unchecked") public V get(K key) { @@ -156,13 +145,10 @@ private static void swap(Object[] data, int a, int b) { @Override public String toString() { final StringBuilder sb = new StringBuilder("{"); - forEach( - new KeyValueConsumer() { - @Override - public void consume(K key, V value) { - sb.append(key).append("=").append(value).append(", "); - } - }); + List data = data(); + for (int i = 0; i < data.size(); i += 2) { + sb.append(data.get(i)).append("=").append(data.get(i + 1)).append(", "); + } // get rid of that last pesky comma if (sb.length() > 1) { sb.setLength(sb.length() - 2); diff --git a/api/src/main/java/io/opentelemetry/common/LabelConsumer.java b/api/src/main/java/io/opentelemetry/common/LabelConsumer.java index 95b16148536..1a1bfcb4cf7 100644 --- a/api/src/main/java/io/opentelemetry/common/LabelConsumer.java +++ b/api/src/main/java/io/opentelemetry/common/LabelConsumer.java @@ -16,8 +16,6 @@ package io.opentelemetry.common; -import io.opentelemetry.common.ReadableKeyValuePairs.KeyValueConsumer; - /** * Convenience interface for consuming {@link Labels}. * @@ -26,7 +24,6 @@ * * @since 0.9.0 */ -public interface LabelConsumer extends KeyValueConsumer { - @Override +public interface LabelConsumer { void consume(String key, String value); } diff --git a/api/src/main/java/io/opentelemetry/common/Labels.java b/api/src/main/java/io/opentelemetry/common/Labels.java index 82ae9988f71..5498325aa61 100644 --- a/api/src/main/java/io/opentelemetry/common/Labels.java +++ b/api/src/main/java/io/opentelemetry/common/Labels.java @@ -27,6 +27,8 @@ public abstract class Labels extends ImmutableKeyValuePairs { private static final Labels EMPTY = Labels.newBuilder().build(); + public abstract void forEach(LabelConsumer consumer); + @AutoValue @Immutable abstract static class ArrayBackedLabels extends Labels { @@ -34,6 +36,14 @@ abstract static class ArrayBackedLabels extends Labels { @Override abstract List data(); + + @Override + public void forEach(LabelConsumer consumer) { + List data = data(); + for (int i = 0; i < data.size(); i += 2) { + consumer.consume((String) data.get(i), (String) data.get(i + 1)); + } + } } /** Returns a {@link Labels} instance with no attributes. */ diff --git a/api/src/main/java/io/opentelemetry/common/ReadableKeyValuePairs.java b/api/src/main/java/io/opentelemetry/common/ReadableKeyValuePairs.java deleted file mode 100644 index 56d48d98bdf..00000000000 --- a/api/src/main/java/io/opentelemetry/common/ReadableKeyValuePairs.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2020, OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.common; - -/** - * A read-only container for String-keyed attributes. - * - * @param The type of the values contained in this. - */ -public interface ReadableKeyValuePairs { - /** The number of attributes contained in this. */ - int size(); - - /** Whether there are any attributes contained in this. */ - boolean isEmpty(); - - /** Iterates over all the key-value pairs of attributes contained by this instance. */ - void forEach(KeyValueConsumer consumer); - - /** - * Used for iterating over the key-value pairs in a key-value pair container, such as {@link - * Attributes} or {@link Labels}. The key is always a {@link String}. - * - * @param The type of the values contained in the key-value pairs. - */ - interface KeyValueConsumer { - void consume(K key, V value); - } -} diff --git a/api/src/test/java/io/opentelemetry/common/AttributesTest.java b/api/src/test/java/io/opentelemetry/common/AttributesTest.java index a40a94fa7f2..4203c6bf490 100644 --- a/api/src/test/java/io/opentelemetry/common/AttributesTest.java +++ b/api/src/test/java/io/opentelemetry/common/AttributesTest.java @@ -43,7 +43,7 @@ void forEach() { Attributes attributes = Attributes.of(stringKey("key1"), "value1", longKey("key2"), 333L); - attributes.forEach((AttributeConsumer) entriesSeen::put); + attributes.forEach(entriesSeen::put); assertThat(entriesSeen) .containsExactly(entry(stringKey("key1"), "value1"), entry(stringKey("key2"), 333L)); @@ -54,7 +54,7 @@ void forEach_singleAttribute() { final Map entriesSeen = new HashMap<>(); Attributes attributes = Attributes.of(stringKey("key"), "value"); - attributes.forEach((AttributeConsumer) entriesSeen::put); + attributes.forEach(entriesSeen::put); assertThat(entriesSeen).containsExactly(entry(stringKey("key"), "value")); } diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java index a47bc9aa294..5804334c894 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java @@ -22,10 +22,10 @@ import static io.opentelemetry.common.AttributeKeyImpl.stringKey; import com.google.common.collect.EvictingQueue; +import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; -import io.opentelemetry.common.ReadableKeyValuePairs; import io.opentelemetry.internal.StringUtils; import io.opentelemetry.sdk.common.Clock; import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; @@ -635,8 +635,7 @@ public String toString() { } @SuppressWarnings({"rawtypes", "unchecked"}) - private static class LimitingAttributeConsumer - implements ReadableKeyValuePairs.KeyValueConsumer { + private static class LimitingAttributeConsumer implements AttributeConsumer { private final int limit; private final Attributes.Builder builder; private int added; diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java index bfa64df4efb..e0b7996bb97 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java @@ -896,7 +896,7 @@ void testAsSpanData() { Resource resource = this.resource; Attributes attributes = TestUtils.generateRandomAttributes(); final AttributesMap attributesWithCapacity = new AttributesMap(32); - attributes.forEach((AttributeConsumer) attributesWithCapacity::put); + attributes.forEach(attributesWithCapacity::put); Attributes event1Attributes = TestUtils.generateRandomAttributes(); Attributes event2Attributes = TestUtils.generateRandomAttributes(); SpanContext context = From a7dbe164cd0eb6e0b711d0a95f5bed752adb2ea7 Mon Sep 17 00:00:00 2001 From: jwatson Date: Wed, 9 Sep 2020 14:08:20 -0700 Subject: [PATCH 10/20] add a TODO --- .../java/io/opentelemetry/common/ImmutableKeyValuePairs.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java index 916d01c384f..403806d383a 100644 --- a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java +++ b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java @@ -112,7 +112,9 @@ private static List dedupe(Object[] data) { if (key == null) { continue; } - // ugh + // TODO: this is super ugly. It needs to be fixed. + // Obviously, this isn't truly generic if we have to peek into the key types for these empty + // checks. if (key instanceof String) { if ("".equals(key)) { continue; From 55e22bf092621bcacc4fac65006bada9e86185ff Mon Sep 17 00:00:00 2001 From: jwatson Date: Wed, 9 Sep 2020 15:29:41 -0700 Subject: [PATCH 11/20] a bit of cleanup; fix a todo --- .../io/opentelemetry/common/Attributes.java | 7 ++++++ .../common/ImmutableKeyValuePairs.java | 24 +++++-------------- .../opentelemetry/internal/StringUtils.java | 8 +++---- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/common/Attributes.java b/api/src/main/java/io/opentelemetry/common/Attributes.java index 98c7435128d..f38fbf94d12 100644 --- a/api/src/main/java/io/opentelemetry/common/Attributes.java +++ b/api/src/main/java/io/opentelemetry/common/Attributes.java @@ -145,6 +145,13 @@ public static Attributes of( } private static Attributes sortAndFilterToAttributes(Object... data) { + // null out any empty keys + for (int i = 0; i < data.length; i += 2) { + AttributeKey key = (AttributeKey) data[i]; + if (key != null && (key.get() == null || "".equals(key.get()))) { + data[i] = null; + } + } return new AutoValue_Attributes_ArrayBackedAttributes(sortAndFilter(data)); } diff --git a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java index 403806d383a..37faf56aeb3 100644 --- a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java +++ b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java @@ -76,13 +76,13 @@ private static > void quickSort( return; } - K pivotKey = (data[rightIndex] == null) ? null : (K) data[rightIndex]; + K pivotKey = (K) data[rightIndex]; int counter = leftIndex; for (int i = leftIndex; i <= rightIndex; i += 2) { - K value = data[i] == null ? null : (K) data[i]; + K value = (K) data[i]; - if (compareTo(value, pivotKey) <= 0) { + if (compareToNullSafe(value, pivotKey) <= 0) { swap(data, counter, i); counter += 2; } @@ -92,14 +92,14 @@ private static > void quickSort( quickSort(data, counter, rightIndex); } - private static > int compareTo(K value, K pivotKey) { - if (value == null) { + private static > int compareToNullSafe(K key, K pivotKey) { + if (key == null) { return pivotKey == null ? 0 : -1; } if (pivotKey == null) { return 1; } - return value.compareTo(pivotKey); + return key.compareTo(pivotKey); } private static List dedupe(Object[] data) { @@ -112,18 +112,6 @@ private static List dedupe(Object[] data) { if (key == null) { continue; } - // TODO: this is super ugly. It needs to be fixed. - // Obviously, this isn't truly generic if we have to peek into the key types for these empty - // checks. - if (key instanceof String) { - if ("".equals(key)) { - continue; - } - } else if (key instanceof AttributeKey) { - if ("".equals(((AttributeKey) key).get())) { - continue; - } - } if (key.equals(previousKey)) { continue; } diff --git a/api/src/main/java/io/opentelemetry/internal/StringUtils.java b/api/src/main/java/io/opentelemetry/internal/StringUtils.java index d67712d7d65..96bfb383276 100644 --- a/api/src/main/java/io/opentelemetry/internal/StringUtils.java +++ b/api/src/main/java/io/opentelemetry/internal/StringUtils.java @@ -17,7 +17,7 @@ package io.opentelemetry.internal; import io.opentelemetry.common.AttributeKey; -import io.opentelemetry.common.AttributeKeyImpl; +import io.opentelemetry.common.AttributeType; import java.util.ArrayList; import java.util.List; import javax.annotation.Nullable; @@ -78,12 +78,12 @@ public static T truncateToSize(AttributeKey key, T value, int limit) { Utils.checkArgument(limit > 0, "attribute value limit must be positive, got %d", limit); if (value == null - || (!(key instanceof AttributeKeyImpl.StringKey) - && !(key instanceof AttributeKeyImpl.StringArrayKey))) { + || ((key.getType() != AttributeType.STRING) + && (key.getType() != AttributeType.STRING_ARRAY))) { return value; } - if (key instanceof AttributeKeyImpl.StringArrayKey) { + if (key.getType() == AttributeType.STRING_ARRAY) { List strings = (List) value; if (strings.isEmpty()) { return value; From 269e259e7b913a54408206624e8ec9ed55b7ae4d Mon Sep 17 00:00:00 2001 From: jwatson Date: Fri, 11 Sep 2020 12:25:39 -0700 Subject: [PATCH 12/20] update for changes from the main branch --- .../trace/attributes/SemanticAttributes.java | 34 +++++++------------ .../sdk/resources/ResourceAttributes.java | 10 +++--- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java b/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java index d4b0cb72742..0685a0e43a8 100644 --- a/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java +++ b/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java @@ -255,50 +255,40 @@ public final class SemanticAttributes { public static final StringKey THREAD_NAME = stringKey("thread.name"); /** Type of the trigger on which the function is executed. */ - public static final StringAttributeSetter FAAS_TRIGGER = - StringAttributeSetter.create("faas.trigger"); + public static final StringKey FAAS_TRIGGER = stringKey("faas.trigger"); /** String containing the execution id of the function. */ - public static final StringAttributeSetter FAAS_EXECUTION = - StringAttributeSetter.create("faas.execution"); + public static final StringKey FAAS_EXECUTION = stringKey("faas.execution"); /** Indicates that the serverless function is executed for the first time (aka cold start). */ - public static final BooleanAttributeSetter FAAS_COLDSTART = - BooleanAttributeSetter.create("faas.coldstart"); + public static final BooleanKey FAAS_COLDSTART = booleanKey("faas.coldstart"); /** The name of the invoked function. */ - public static final StringAttributeSetter FAAS_INVOKED_NAME = - StringAttributeSetter.create("faas.invoked_name"); + public static final StringKey FAAS_INVOKED_NAME = stringKey("faas.invoked_name"); /** The cloud provider of the invoked function. */ - public static final StringAttributeSetter FAAS_INVOKED_PROVIDER = - StringAttributeSetter.create("faas.invoked_provider"); + public static final StringKey FAAS_INVOKED_PROVIDER = stringKey("faas.invoked_provider"); /** The cloud region of the invoked function. */ - public static final StringAttributeSetter FAAS_INVOKED_REGION = - StringAttributeSetter.create("faas.invoked_region"); + public static final StringKey FAAS_INVOKED_REGION = stringKey("faas.invoked_region"); /** For faas.trigger == datasource, the name of the source on which the operation was perfomed. */ - public static final StringAttributeSetter FAAS_DOCUMENT_COLLECTION = - StringAttributeSetter.create("faas.document.collection"); + public static final StringKey FAAS_DOCUMENT_COLLECTION = stringKey("faas.document.collection"); /** * For faas.trigger == datasource, describes the type of the operation that was performed on the * data. */ - public static final StringAttributeSetter FAAS_DOCUMENT_OPERATION = - StringAttributeSetter.create("faas.document.operation"); + public static final StringKey FAAS_DOCUMENT_OPERATION = stringKey("faas.document.operation"); /** * For faas.trigger == datasource, a string containing the time when the data was accessed in the * ISO 8601 format expressed in UTC. */ - public static final StringAttributeSetter FAAS_DOCUMENT_TIME = - StringAttributeSetter.create("faas.document.time"); + public static final StringKey FAAS_DOCUMENT_TIME = stringKey("faas.document.time"); /** For faas.trigger == datasource, the document name/table subjected to the operation. */ - public static final StringAttributeSetter FAAS_DOCUMENT_NAME = - StringAttributeSetter.create("faas.document.name"); + public static final StringKey FAAS_DOCUMENT_NAME = stringKey("faas.document.name"); /** * For faas.trigger == time, a string containing the function invocation time in the ISO 8601 * format expressed in UTC. */ - public static final StringAttributeSetter FAAS_TIME = StringAttributeSetter.create("faas.time"); + public static final StringKey FAAS_TIME = stringKey("faas.time"); /** For faas.trigger == time, a string containing the schedule period as Cron Expression. */ - public static final StringAttributeSetter FAAS_CRON = StringAttributeSetter.create("faas.cron"); + public static final StringKey FAAS_CRON = stringKey("faas.cron"); private SemanticAttributes() {} } diff --git a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java index f27186f95d2..51ab45d9141 100644 --- a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java +++ b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java @@ -127,15 +127,13 @@ public final class ResourceAttributes { public static final StringKey CLOUD_ZONE = stringKey("cloud.zone"); /** The name of the function being executed. */ - public static final StringAttributeSetter FAAS_NAME = StringAttributeSetter.create("faas.name"); + public static final StringKey FAAS_NAME = stringKey("faas.name"); /** The unique ID of the function being executed. */ - public static final StringAttributeSetter FAAS_ID = StringAttributeSetter.create("faas.id"); + public static final StringKey FAAS_ID = stringKey("faas.id"); /** The version string of the function being executed. */ - public static final StringAttributeSetter FAAS_VERSION = - StringAttributeSetter.create("faas.version"); + public static final StringKey FAAS_VERSION = stringKey("faas.version"); /** The execution environment ID as a string. */ - public static final StringAttributeSetter FAAS_INSTANCE = - StringAttributeSetter.create("faas.instance"); + public static final StringKey FAAS_INSTANCE = stringKey("faas.instance"); private ResourceAttributes() {} } From 1155c27505571494b01b5d3ec92c31b846f6c46c Mon Sep 17 00:00:00 2001 From: jwatson Date: Fri, 11 Sep 2020 13:13:02 -0700 Subject: [PATCH 13/20] Change method to get the underlying key. Move the key creators to a AttributesKeys class. Hide the implementation classes and replace with interface use. --- .../io/opentelemetry/common/AttributeKey.java | 7 +- .../common/AttributeKeyImpl.java | 70 ++------ .../io/opentelemetry/common/Attributes.java | 23 +-- .../opentelemetry/common/AttributesKeys.java | 76 ++++++++ .../trace/attributes/SemanticAttributes.java | 170 +++++++++--------- .../opentelemetry/common/AttributesTest.java | 16 +- .../opentelemetry/trace/DefaultSpanTest.java | 14 +- .../opentelemetry/trace/SpanBuilderTest.java | 2 +- .../exporters/jaeger/Adapter.java | 7 +- .../exporters/jaeger/AdapterTest.java | 16 +- .../logging/LoggingMetricExporterTest.java | 2 +- .../logging/LoggingSpanExporterTest.java | 2 +- .../exporters/otlp/CommonAdapter.java | 58 +++--- .../exporters/otlp/CommonAdapterTest.java | 16 +- .../exporters/otlp/MetricAdapterTest.java | 2 +- .../exporters/otlp/ResourceAdapterTest.java | 8 +- .../exporters/otlp/SpanAdapterTest.java | 4 +- .../prometheus/MetricAdapterTest.java | 2 +- .../prometheus/PrometheusCollectorTest.java | 2 +- .../exporters/zipkin/ZipkinSpanExporter.java | 9 +- .../zipkin/ZipkinSpanExporterTest.java | 24 +-- .../extensions/trace/MessageEvent.java | 15 +- .../opentracingshim/SpanBuilderShim.java | 8 +- .../opentracingshim/SpanShim.java | 8 +- .../nestedcallbacks/NestedCallbacksTest.java | 2 +- .../PromisePropagationTest.java | 6 +- .../sdk/trace/SpanPipelineBenchmark.java | 10 +- .../opentelemetry/sdk/resources/Resource.java | 11 +- .../sdk/resources/ResourceAttributes.java | 85 ++++----- .../resources/EnvAutodetectResourceTest.java | 2 +- .../sdk/resources/ResourceProvidersTest.java | 2 +- .../sdk/resources/ResourceTest.java | 16 +- .../sdk/resources/TestResourceProvider.java | 2 +- .../sdk/metrics/BatchRecorderSdkTest.java | 2 +- .../sdk/metrics/DoubleCounterSdkTest.java | 2 +- .../sdk/metrics/DoubleSumObserverSdkTest.java | 2 +- .../metrics/DoubleUpDownCounterSdkTest.java | 2 +- .../DoubleUpDownSumObserverSdkTest.java | 2 +- .../metrics/DoubleValueObserverSdkTest.java | 2 +- .../metrics/DoubleValueRecorderSdkTest.java | 2 +- .../sdk/metrics/LongCounterSdkTest.java | 2 +- .../sdk/metrics/LongSumObserverSdkTest.java | 2 +- .../sdk/metrics/LongUpDownCounterSdkTest.java | 2 +- .../metrics/LongUpDownSumObserverSdkTest.java | 2 +- .../sdk/metrics/LongValueObserverSdkTest.java | 2 +- .../sdk/metrics/LongValueRecorderSdkTest.java | 2 +- .../sdk/metrics/MeterSdkTest.java | 2 +- .../sdk/trace/RecordEventsReadableSpan.java | 10 +- .../io/opentelemetry/sdk/trace/Samplers.java | 6 +- .../sdk/trace/SpanBuilderSdk.java | 8 +- .../trace/RecordEventsReadableSpanTest.java | 16 +- .../opentelemetry/sdk/trace/SamplersTest.java | 4 +- .../sdk/trace/SpanBuilderSdkTest.java | 22 +-- .../io/opentelemetry/sdk/trace/TestUtils.java | 2 +- .../sdk/trace/TimedEventTest.java | 2 +- .../jaeger/sampler/RateLimitingSampler.java | 9 +- .../nestedcallbacks/NestedCallbacksTest.java | 2 +- .../PromisePropagationTest.java | 6 +- 58 files changed, 429 insertions(+), 383 deletions(-) create mode 100644 api/src/main/java/io/opentelemetry/common/AttributesKeys.java diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKey.java b/api/src/main/java/io/opentelemetry/common/AttributeKey.java index d902319a1d2..58cff61a5b4 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeKey.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeKey.java @@ -16,16 +16,21 @@ package io.opentelemetry.common; +import javax.annotation.concurrent.Immutable; + /** * This interface provides a handle for setting the values of {@link Attributes}. The type of value * that can be set with an implementation of this key is denoted by the type parameter. * + *

Implementations MUST be immutable, as these are used as the keys to Maps. + * * @param The type of value that can be set with the key. */ @SuppressWarnings("rawtypes") +@Immutable public interface AttributeKey extends Comparable { /** Returns the underlying String representation of the key. */ - String get(); + String getKey(); /** Returns the type of attribute for this key. Useful for building switch statements. */ AttributeType getType(); diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java index 10931149ccf..12f049a396a 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java @@ -19,7 +19,7 @@ import java.util.List; @SuppressWarnings("rawtypes") -public abstract class AttributeKeyImpl implements AttributeKey { +abstract class AttributeKeyImpl implements AttributeKey { private final String key; AttributeKeyImpl(String key) { @@ -27,7 +27,7 @@ public abstract class AttributeKeyImpl implements AttributeKey { } @Override - public String get() { + public String getKey() { return key; } @@ -57,15 +57,11 @@ public String toString() { @Override public int compareTo(AttributeKey o) { - return get().compareTo(o.get()); + return getKey().compareTo(o.getKey()); } - public static StringKey stringKey(String key) { - return new StringKey(key); - } - - public static class StringKey extends AttributeKeyImpl { - private StringKey(String key) { + static class StringKey extends AttributeKeyImpl { + StringKey(String key) { super(key); } @@ -75,12 +71,8 @@ public AttributeType getType() { } } - public static BooleanKey booleanKey(String key) { - return new BooleanKey(key); - } - - public static class BooleanKey extends AttributeKeyImpl { - private BooleanKey(String key) { + static class BooleanKey extends AttributeKeyImpl { + BooleanKey(String key) { super(key); } @@ -90,12 +82,8 @@ public AttributeType getType() { } } - public static LongKey longKey(String key) { - return new LongKey(key); - } - - public static class LongKey extends AttributeKeyImpl { - private LongKey(String key) { + static class LongKey extends AttributeKeyImpl { + LongKey(String key) { super(key); } @@ -105,12 +93,8 @@ public AttributeType getType() { } } - public static DoubleKey doubleKey(String key) { - return new DoubleKey(key); - } - - public static class DoubleKey extends AttributeKeyImpl { - private DoubleKey(String key) { + static class DoubleKey extends AttributeKeyImpl { + DoubleKey(String key) { super(key); } @@ -120,12 +104,8 @@ public AttributeType getType() { } } - public static StringArrayKey stringArrayKey(String key) { - return new StringArrayKey(key); - } - - public static class StringArrayKey extends AttributeKeyImpl> { - private StringArrayKey(String key) { + static class StringArrayKey extends AttributeKeyImpl> { + StringArrayKey(String key) { super(key); } @@ -135,12 +115,8 @@ public AttributeType getType() { } } - public static BooleanArrayKey booleanArrayKey(String key) { - return new BooleanArrayKey(key); - } - - public static class BooleanArrayKey extends AttributeKeyImpl> { - private BooleanArrayKey(String key) { + static class BooleanArrayKey extends AttributeKeyImpl> { + BooleanArrayKey(String key) { super(key); } @@ -150,12 +126,8 @@ public AttributeType getType() { } } - public static LongArrayKey longArrayKey(String key) { - return new LongArrayKey(key); - } - - public static class LongArrayKey extends AttributeKeyImpl> { - private LongArrayKey(String key) { + static class LongArrayKey extends AttributeKeyImpl> { + LongArrayKey(String key) { super(key); } @@ -165,12 +137,8 @@ public AttributeType getType() { } } - public static DoubleArrayKey doubleArrayKey(String key) { - return new DoubleArrayKey(key); - } - - public static class DoubleArrayKey extends AttributeKeyImpl> { - private DoubleArrayKey(String key) { + static class DoubleArrayKey extends AttributeKeyImpl> { + DoubleArrayKey(String key) { super(key); } diff --git a/api/src/main/java/io/opentelemetry/common/Attributes.java b/api/src/main/java/io/opentelemetry/common/Attributes.java index f38fbf94d12..69501743c64 100644 --- a/api/src/main/java/io/opentelemetry/common/Attributes.java +++ b/api/src/main/java/io/opentelemetry/common/Attributes.java @@ -16,14 +16,14 @@ package io.opentelemetry.common; -import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanArrayKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleArrayKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longArrayKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringArrayKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import com.google.auto.value.AutoValue; import java.util.ArrayList; @@ -35,7 +35,8 @@ /** * An immutable container for attributes. * - *

The keys are {@link AttributeKey}s and the values are Object instances. + *

The keys are {@link AttributeKey}s and the values are Object instances that match the type of + * the provided key. */ @SuppressWarnings("rawtypes") @Immutable @@ -148,7 +149,7 @@ private static Attributes sortAndFilterToAttributes(Object... data) { // null out any empty keys for (int i = 0; i < data.length; i += 2) { AttributeKey key = (AttributeKey) data[i]; - if (key != null && (key.get() == null || "".equals(key.get()))) { + if (key != null && (key.getKey() == null || "".equals(key.getKey()))) { data[i] = null; } } @@ -198,7 +199,7 @@ public Attributes build() { /** Sets a {@link AttributeKey} with associated value into this. */ public Builder setAttribute(AttributeKey key, T value) { - if (key == null || key.get().length() == 0) { + if (key == null || key.getKey().length() == 0) { return this; } if (value == null) { diff --git a/api/src/main/java/io/opentelemetry/common/AttributesKeys.java b/api/src/main/java/io/opentelemetry/common/AttributesKeys.java new file mode 100644 index 00000000000..58ca0fcf2b7 --- /dev/null +++ b/api/src/main/java/io/opentelemetry/common/AttributesKeys.java @@ -0,0 +1,76 @@ +/* + * Copyright 2020, OpenTelemetry Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opentelemetry.common; + +import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; +import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.LongKey; +import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; +import io.opentelemetry.common.AttributeKeyImpl.StringKey; +import java.util.List; + +/** + * Creation methods for {@link AttributeKey} implementations. + * + * @see Attributes + */ +public class AttributesKeys { + private AttributesKeys() {} + + /** Create a new AttributeKey for String valued attributes. */ + public static AttributeKey stringKey(String key) { + return new StringKey(key); + } + + /** Create a new AttributeKey for Boolean valued attributes. */ + public static AttributeKey booleanKey(String key) { + return new BooleanKey(key); + } + + /** Create a new AttributeKey for Long valued attributes. */ + public static AttributeKey longKey(String key) { + return new LongKey(key); + } + + /** Create a new AttributeKey for Double valued attributes. */ + public static AttributeKey doubleKey(String key) { + return new DoubleKey(key); + } + + /** Create a new AttributeKey for List<String> valued attributes. */ + public static AttributeKey> stringArrayKey(String key) { + return new StringArrayKey(key); + } + + /** Create a new AttributeKey for List<Boolean> valued attributes. */ + public static AttributeKey> booleanArrayKey(String key) { + return new BooleanArrayKey(key); + } + + /** Create a new AttributeKey for List<Long> valued attributes. */ + public static AttributeKey> longArrayKey(String key) { + return new LongArrayKey(key); + } + + /** Create a new AttributeKey for List<Double> valued attributes. */ + public static AttributeKey> doubleArrayKey(String key) { + return new DoubleArrayKey(key); + } +} diff --git a/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java b/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java index 0685a0e43a8..7eaa23cea3f 100644 --- a/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java +++ b/api/src/main/java/io/opentelemetry/trace/attributes/SemanticAttributes.java @@ -16,13 +16,11 @@ package io.opentelemetry.trace.attributes; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; -import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; -import io.opentelemetry.common.AttributeKeyImpl.LongKey; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; +import io.opentelemetry.common.AttributeKey; /** * Defines constants for all attribute names defined in the OpenTelemetry Semantic Conventions @@ -35,104 +33,107 @@ public final class SemanticAttributes { /** Transport protocol used. */ - public static final StringKey NET_TRANSPORT = stringKey("net.transport"); + public static final AttributeKey NET_TRANSPORT = stringKey("net.transport"); /** Remote address of the peer (dotted decimal for IPv4 or RFC5952 for IPv6). */ - public static final StringKey NET_PEER_IP = stringKey("net.peer.ip"); + public static final AttributeKey NET_PEER_IP = stringKey("net.peer.ip"); /** Remote port number as an integer. E.g., 80. */ - public static final LongKey NET_PEER_PORT = longKey("net.peer.port"); + public static final AttributeKey NET_PEER_PORT = longKey("net.peer.port"); /** Remote hostname or similar. */ - public static final StringKey NET_PEER_NAME = stringKey("net.peer.name"); + public static final AttributeKey NET_PEER_NAME = stringKey("net.peer.name"); /** Like net.peer.ip but for the host IP. Useful in case of a multi-IP host. */ - public static final StringKey NET_HOST_IP = stringKey("net.host.ip"); + public static final AttributeKey NET_HOST_IP = stringKey("net.host.ip"); /** Like net.peer.port but for the host port. */ - public static final LongKey NET_HOST_PORT = longKey("net.host.port"); + public static final AttributeKey NET_HOST_PORT = longKey("net.host.port"); /** Local hostname or similar. */ - public static final StringKey NET_HOST_NAME = stringKey("net.host.name"); + public static final AttributeKey NET_HOST_NAME = stringKey("net.host.name"); /** Logical name of a remote service. */ - public static final StringKey PEER_SERVICE = stringKey("peer.service"); + public static final AttributeKey PEER_SERVICE = stringKey("peer.service"); /** * Username or client_id extracted from the access token or Authorization header in the inbound * request from outside the system. */ - public static final StringKey ENDUSER_ID = stringKey("enduser.id"); + public static final AttributeKey ENDUSER_ID = stringKey("enduser.id"); /** * Actual/assumed role the client is making the request under extracted from token or application * security context. */ - public static final StringKey ENDUSER_ROLE = stringKey("enduser.role"); + public static final AttributeKey ENDUSER_ROLE = stringKey("enduser.role"); /** * Scopes or granted authorities the client currently possesses extracted from token or * application security context. The value would come from the scope associated with an OAuth 2.0 * Access Token or an attribute value in a SAML 2.0 Assertion. */ - public static final StringKey ENDUSER_SCOPE = stringKey("enduser.scope"); + public static final AttributeKey ENDUSER_SCOPE = stringKey("enduser.scope"); /** HTTP request method. E.g. "GET". */ - public static final StringKey HTTP_METHOD = stringKey("http.method"); + public static final AttributeKey HTTP_METHOD = stringKey("http.method"); /** Full HTTP request URL in the form scheme://host[:port]/path?query[#fragment]. */ - public static final StringKey HTTP_URL = stringKey("http.url"); + public static final AttributeKey HTTP_URL = stringKey("http.url"); /** The full request target as passed in a HTTP request line or equivalent. */ - public static final StringKey HTTP_TARGET = stringKey("http.target"); + public static final AttributeKey HTTP_TARGET = stringKey("http.target"); /** The value of the HTTP host header. */ - public static final StringKey HTTP_HOST = stringKey("http.host"); + public static final AttributeKey HTTP_HOST = stringKey("http.host"); /** The URI scheme identifying the used protocol: "http" or "https". */ - public static final StringKey HTTP_SCHEME = stringKey("http.scheme"); + public static final AttributeKey HTTP_SCHEME = stringKey("http.scheme"); /** HTTP response status code. E.g. 200 (integer) If and only if one was received/sent. */ - public static final LongKey HTTP_STATUS_CODE = longKey("http.status_code"); + public static final AttributeKey HTTP_STATUS_CODE = longKey("http.status_code"); /** HTTP reason phrase. E.g. "OK" */ - public static final StringKey HTTP_STATUS_TEXT = stringKey("http.status_text"); + public static final AttributeKey HTTP_STATUS_TEXT = stringKey("http.status_text"); /** Kind of HTTP protocol used: "1.0", "1.1", "2", "SPDY" or "QUIC". */ - public static final StringKey HTTP_FLAVOR = stringKey("http.flavor"); + public static final AttributeKey HTTP_FLAVOR = stringKey("http.flavor"); /** Value of the HTTP "User-Agent" header sent by the client. */ - public static final StringKey HTTP_USER_AGENT = stringKey("http.user_agent"); + public static final AttributeKey HTTP_USER_AGENT = stringKey("http.user_agent"); /** The primary server name of the matched virtual host. Usually obtained via configuration. */ - public static final StringKey HTTP_SERVER_NAME = stringKey("http.server_name"); + public static final AttributeKey HTTP_SERVER_NAME = stringKey("http.server_name"); /** The matched route (path template). */ - public static final StringKey HTTP_ROUTE = stringKey("http.route"); + public static final AttributeKey HTTP_ROUTE = stringKey("http.route"); /** The IP address of the original client behind all proxies, if known. */ - public static final StringKey HTTP_CLIENT_IP = stringKey("http.client_ip"); + public static final AttributeKey HTTP_CLIENT_IP = stringKey("http.client_ip"); /** * The size of the request payload body, in bytes. For payloads using transport encoding, this is * the compressed size. */ - public static final LongKey HTTP_REQUEST_CONTENT_LENGTH = longKey("http.request_content_length"); + public static final AttributeKey HTTP_REQUEST_CONTENT_LENGTH = + longKey("http.request_content_length"); /** * The size of the uncompressed request payload body, in bytes. Only set for requests that use * transport encoding. */ - public static final LongKey HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED = + public static final AttributeKey HTTP_REQUEST_CONTENT_LENGTH_UNCOMPRESSED = longKey("http.request_content_length_uncompressed"); /** * The size of the response payload body, in bytes. For payloads using transport encoding, this is * the compressed size. */ - public static final LongKey HTTP_RESPONSE_CONTENT_LENGTH = + public static final AttributeKey HTTP_RESPONSE_CONTENT_LENGTH = longKey("http.response_content_length"); /** * The size of the uncompressed response payload body, in bytes. Only set for responses that use * transport encoding. */ - public static final LongKey HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED = + public static final AttributeKey HTTP_RESPONSE_CONTENT_LENGTH_UNCOMPRESSED = longKey("http.response_content_length_uncompressed"); /** A string identifying the remoting system, e.g., "grpc", "java_rmi" or "wcf". */ - public static final StringKey RPC_SYSTEM = stringKey("rpc.system"); + public static final AttributeKey RPC_SYSTEM = stringKey("rpc.system"); /** The full name of the service being called, including its package name, if applicable. */ - public static final StringKey RPC_SERVICE = stringKey("rpc.service"); + public static final AttributeKey RPC_SERVICE = stringKey("rpc.service"); /* The name of the method being called, must be equal to the $method part in the span name */ - public static final StringKey RPC_METHOD = stringKey("rpc.method"); + public static final AttributeKey RPC_METHOD = stringKey("rpc.method"); /** The name of a gRPC span event to populate for each message sent / received. */ public static final String GRPC_MESSAGE_EVENT_NAME = "message"; /** gRPC span event attribute with value "SENT" or "RECEIVED". */ - public static final StringKey GRPC_MESSAGE_TYPE = stringKey("message.type"); + public static final AttributeKey GRPC_MESSAGE_TYPE = stringKey("message.type"); /** gRPC span event attribute starting from 1 for each of sent messages and received messages. */ - public static final LongKey GRPC_MESSAGE_ID = longKey("message.id"); + public static final AttributeKey GRPC_MESSAGE_ID = longKey("message.id"); /** gRPC span event attribute for compressed size of a message. */ - public static final LongKey GRPC_MESSAGE_COMPRESSED_SIZE = longKey("message.compressed_size"); + public static final AttributeKey GRPC_MESSAGE_COMPRESSED_SIZE = + longKey("message.compressed_size"); /** gRPC span event attribute for uncompressed size of a message. */ - public static final LongKey GRPC_MESSAGE_UNCOMPRESSED_SIZE = longKey("message.uncompressed_size"); + public static final AttributeKey GRPC_MESSAGE_UNCOMPRESSED_SIZE = + longKey("message.uncompressed_size"); /** * An identifier for the database management system (DBMS) product being used. @@ -141,154 +142,161 @@ public final class SemanticAttributes { * href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/database.md#notes-and-well-known-identifiers-for-dbsystem">A * list of well-known identifiers */ - public static final StringKey DB_SYSTEM = stringKey("db.system"); + public static final AttributeKey DB_SYSTEM = stringKey("db.system"); /** Database name. */ - public static final StringKey DB_NAME = stringKey("db.name"); + public static final AttributeKey DB_NAME = stringKey("db.name"); /** * The connection string used to connect to the database. It's recommended to remove embedded * credentials. This will replace db.url. */ - public static final StringKey DB_CONNECTION_STRING = stringKey("db.connection_string"); + public static final AttributeKey DB_CONNECTION_STRING = stringKey("db.connection_string"); /** Database statement for the given database type. */ - public static final StringKey DB_STATEMENT = stringKey("db.statement"); + public static final AttributeKey DB_STATEMENT = stringKey("db.statement"); /** Database operation that is being executed. */ - public static final StringKey DB_OPERATION = stringKey("db.operation"); + public static final AttributeKey DB_OPERATION = stringKey("db.operation"); /** Username for accessing database. */ - public static final StringKey DB_USER = stringKey("db.user"); + public static final AttributeKey DB_USER = stringKey("db.user"); /** * For db.system == mssql, the instance name connecting to. This name is used to determine the * port of a named instance. When set, {@link #NET_PEER_PORT} is not required, but recommended * when connecting to a non-standard port. */ - public static final StringKey MSSQL_SQL_SERVER = stringKey("db.mssql.instance_name"); + public static final AttributeKey MSSQL_SQL_SERVER = stringKey("db.mssql.instance_name"); /** * For JDBC clients, the fully-qualified class name of the Java Database Connectivity (JDBC) * driver used to connect, e.g. "org.postgresql.Driver" or * "com.microsoft.sqlserver.jdbc.SQLServerDriver". */ - public static final StringKey JDBC_DRIVER_CLASSNAME = stringKey("db.jdbc.driver_classname"); + public static final AttributeKey JDBC_DRIVER_CLASSNAME = + stringKey("db.jdbc.driver_classname"); /** * For db.system == cassandra, the name of the keyspace being accessed. To be used instead of the * generic db.name attribute. */ - public static final StringKey CASSANDRA_KEYSPACE = stringKey("db.cassandra.keyspace"); + public static final AttributeKey CASSANDRA_KEYSPACE = stringKey("db.cassandra.keyspace"); /** * For db.system == hbase, the namespace being accessed. To be used instead of the generic db.name * attribute. */ - public static final StringKey HBASE_NAMESPACE = stringKey("db.hbase.namespace"); + public static final AttributeKey HBASE_NAMESPACE = stringKey("db.hbase.namespace"); /** * For db.system == redis, the index of the database being accessed as used in the SELECT command, * provided as an integer. To be used instead of the generic db.name attribute. */ - public static final StringKey REDIS_DATABASE_INDEX = stringKey("db.redis.database_index"); + public static final AttributeKey REDIS_DATABASE_INDEX = + stringKey("db.redis.database_index"); /** * For db.system == mongodb, the collection being accessed within the database stated in db.name */ - public static final StringKey MONGODB_COLLECTION = stringKey("db.mongodb.collection"); + public static final AttributeKey MONGODB_COLLECTION = stringKey("db.mongodb.collection"); /** A string identifying the messaging system such as kafka, rabbitmq or activemq. */ - public static final StringKey MESSAGING_SYSTEM = stringKey("messaging.system"); + public static final AttributeKey MESSAGING_SYSTEM = stringKey("messaging.system"); /** * The message destination name, e.g. MyQueue or MyTopic. This might be equal to the span name but * is required nevertheless */ - public static final StringKey MESSAGING_DESTINATION = stringKey("messaging.destination"); + public static final AttributeKey MESSAGING_DESTINATION = + stringKey("messaging.destination"); /** The kind of message destination. Either queue or topic. */ - public static final StringKey MESSAGING_DESTINATION_KIND = + public static final AttributeKey MESSAGING_DESTINATION_KIND = stringKey("messaging.destination_kind"); /** A boolean that is true if the message destination is temporary. */ - public static final BooleanKey MESSAGING_TEMP_DESTINATION = + public static final AttributeKey MESSAGING_TEMP_DESTINATION = booleanKey("messaging.temp_destination"); /** The name of the transport protocol such as AMQP or MQTT. */ - public static final StringKey MESSAGING_PROTOCOL = stringKey("messaging.protocol"); + public static final AttributeKey MESSAGING_PROTOCOL = stringKey("messaging.protocol"); /** The version of the transport protocol such as 0.9.1. */ - public static final StringKey MESSAGING_PROTOCOL_VERSION = + public static final AttributeKey MESSAGING_PROTOCOL_VERSION = stringKey("messaging.protocol_version"); /** * Connection string such as tibjmsnaming://localhost:7222 or * https://queue.amazonaws.com/80398EXAMPLE/MyQueue */ - public static final StringKey MESSAGING_URL = stringKey("messaging.url"); + public static final AttributeKey MESSAGING_URL = stringKey("messaging.url"); /** * A value used by the messaging system as an identifier for the message, represented as a string. */ - public static final StringKey MESSAGING_MESSAGE_ID = stringKey("messaging.message_id"); + public static final AttributeKey MESSAGING_MESSAGE_ID = stringKey("messaging.message_id"); /** * A value identifying the conversation to which the message belongs, represented as a string. * Sometimes called "Correlation ID". */ - public static final StringKey MESSAGING_CONVERSATION_ID = stringKey("messaging.conversation_id"); + public static final AttributeKey MESSAGING_CONVERSATION_ID = + stringKey("messaging.conversation_id"); /** * The (uncompressed) size of the message payload in bytes. Also use this attribute if it is * unknown whether the compressed or uncompressed payload size is reported. */ - public static final LongKey MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES = + public static final AttributeKey MESSAGING_MESSAGE_PAYLOAD_SIZE_BYTES = longKey("messaging.message_payload_size_bytes"); /** The compressed size of the message payload in bytes. */ - public static final LongKey MESSAGING_MESSAGE_PAYLOAD_COMPRESSED_SIZE_BYTES = + public static final AttributeKey MESSAGING_MESSAGE_PAYLOAD_COMPRESSED_SIZE_BYTES = longKey("messaging.message_payload_compressed_size_bytes"); /** * A string identifying which part and kind of message consumption this span describes: either * "receive" or "process". If the operation is "send", this attribute must not be set: the * operation can be inferred from the span kind in that case. */ - public static final StringKey MESSAGING_OPERATION = stringKey("messaging.operation"); + public static final AttributeKey MESSAGING_OPERATION = stringKey("messaging.operation"); /** The name of an {@link io.opentelemetry.trace.Event} describing an exception. */ public static final String EXCEPTION_EVENT_NAME = "exception"; /** The type of the exception, i.e., it's fully qualified name. */ - public static final StringKey EXCEPTION_TYPE = stringKey("exception.type"); + public static final AttributeKey EXCEPTION_TYPE = stringKey("exception.type"); /** The exception message. */ - public static final StringKey EXCEPTION_MESSAGE = stringKey("exception.message"); + public static final AttributeKey EXCEPTION_MESSAGE = stringKey("exception.message"); /** * A string representing the stacktrace of an exception, as produced by {@link * Throwable#printStackTrace()}. */ - public static final StringKey EXCEPTION_STACKTRACE = stringKey("exception.stacktrace"); + public static final AttributeKey EXCEPTION_STACKTRACE = stringKey("exception.stacktrace"); /** Id of the thread that has started a span, as produced by {@link Thread#getId()}. */ - public static final LongKey THREAD_ID = longKey("thread.id"); + public static final AttributeKey THREAD_ID = longKey("thread.id"); /** Name of the thread that has started a span, as produced by {@link Thread#getName()}. */ - public static final StringKey THREAD_NAME = stringKey("thread.name"); + public static final AttributeKey THREAD_NAME = stringKey("thread.name"); /** Type of the trigger on which the function is executed. */ - public static final StringKey FAAS_TRIGGER = stringKey("faas.trigger"); + public static final AttributeKey FAAS_TRIGGER = stringKey("faas.trigger"); /** String containing the execution id of the function. */ - public static final StringKey FAAS_EXECUTION = stringKey("faas.execution"); + public static final AttributeKey FAAS_EXECUTION = stringKey("faas.execution"); /** Indicates that the serverless function is executed for the first time (aka cold start). */ - public static final BooleanKey FAAS_COLDSTART = booleanKey("faas.coldstart"); + public static final AttributeKey FAAS_COLDSTART = booleanKey("faas.coldstart"); /** The name of the invoked function. */ - public static final StringKey FAAS_INVOKED_NAME = stringKey("faas.invoked_name"); + public static final AttributeKey FAAS_INVOKED_NAME = stringKey("faas.invoked_name"); /** The cloud provider of the invoked function. */ - public static final StringKey FAAS_INVOKED_PROVIDER = stringKey("faas.invoked_provider"); + public static final AttributeKey FAAS_INVOKED_PROVIDER = + stringKey("faas.invoked_provider"); /** The cloud region of the invoked function. */ - public static final StringKey FAAS_INVOKED_REGION = stringKey("faas.invoked_region"); + public static final AttributeKey FAAS_INVOKED_REGION = stringKey("faas.invoked_region"); /** For faas.trigger == datasource, the name of the source on which the operation was perfomed. */ - public static final StringKey FAAS_DOCUMENT_COLLECTION = stringKey("faas.document.collection"); + public static final AttributeKey FAAS_DOCUMENT_COLLECTION = + stringKey("faas.document.collection"); /** * For faas.trigger == datasource, describes the type of the operation that was performed on the * data. */ - public static final StringKey FAAS_DOCUMENT_OPERATION = stringKey("faas.document.operation"); + public static final AttributeKey FAAS_DOCUMENT_OPERATION = + stringKey("faas.document.operation"); /** * For faas.trigger == datasource, a string containing the time when the data was accessed in the * ISO 8601 format expressed in UTC. */ - public static final StringKey FAAS_DOCUMENT_TIME = stringKey("faas.document.time"); + public static final AttributeKey FAAS_DOCUMENT_TIME = stringKey("faas.document.time"); /** For faas.trigger == datasource, the document name/table subjected to the operation. */ - public static final StringKey FAAS_DOCUMENT_NAME = stringKey("faas.document.name"); + public static final AttributeKey FAAS_DOCUMENT_NAME = stringKey("faas.document.name"); /** * For faas.trigger == time, a string containing the function invocation time in the ISO 8601 * format expressed in UTC. */ - public static final StringKey FAAS_TIME = stringKey("faas.time"); + public static final AttributeKey FAAS_TIME = stringKey("faas.time"); /** For faas.trigger == time, a string containing the schedule period as Cron Expression. */ - public static final StringKey FAAS_CRON = stringKey("faas.cron"); + public static final AttributeKey FAAS_CRON = stringKey("faas.cron"); private SemanticAttributes() {} } diff --git a/api/src/test/java/io/opentelemetry/common/AttributesTest.java b/api/src/test/java/io/opentelemetry/common/AttributesTest.java index 4203c6bf490..1a901ce5ebd 100644 --- a/api/src/test/java/io/opentelemetry/common/AttributesTest.java +++ b/api/src/test/java/io/opentelemetry/common/AttributesTest.java @@ -16,14 +16,14 @@ package io.opentelemetry.common; -import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanArrayKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleArrayKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longArrayKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringArrayKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; diff --git a/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java b/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java index fac846d06ce..2bbabe6c58a 100644 --- a/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java +++ b/api/src/test/java/io/opentelemetry/trace/DefaultSpanTest.java @@ -16,13 +16,13 @@ package io.opentelemetry.trace; -import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanArrayKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleArrayKey; +import static io.opentelemetry.common.AttributesKeys.longArrayKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringArrayKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; diff --git a/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java b/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java index da855d7a918..fb298963d6d 100644 --- a/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java +++ b/api/src/test/java/io/opentelemetry/trace/SpanBuilderTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.trace; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java b/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java index c259453b418..2bca212a1c8 100644 --- a/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java +++ b/exporters/jaeger/src/main/java/io/opentelemetry/exporters/jaeger/Adapter.java @@ -16,7 +16,7 @@ package io.opentelemetry.exporters.jaeger; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; import com.google.common.annotations.VisibleForTesting; import com.google.gson.Gson; @@ -24,7 +24,6 @@ import com.google.protobuf.util.Timestamps; import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; -import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.exporters.jaeger.proto.api_v2.Model; import io.opentelemetry.sdk.extensions.otproto.TraceProtoUtils; @@ -41,7 +40,7 @@ /** Adapts OpenTelemetry objects to Jaeger objects. */ @ThreadSafe final class Adapter { - static final BooleanKey KEY_ERROR = booleanKey("error"); + static final AttributeKey KEY_ERROR = booleanKey("error"); static final String KEY_LOG_MESSAGE = "message"; static final String KEY_SPAN_KIND = "span.kind"; static final String KEY_SPAN_STATUS_MESSAGE = "span.status.message"; @@ -204,7 +203,7 @@ public void consume(AttributeKey key, T value) { @VisibleForTesting static Model.KeyValue toKeyValue(AttributeKey key, T value) { Model.KeyValue.Builder builder = Model.KeyValue.newBuilder(); - builder.setKey(key.get()); + builder.setKey(key.getKey()); switch (key.getType()) { case STRING: diff --git a/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java b/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java index 5fe1f253c0b..e428b778c30 100644 --- a/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java +++ b/exporters/jaeger/src/test/java/io/opentelemetry/exporters/jaeger/AdapterTest.java @@ -16,14 +16,14 @@ package io.opentelemetry.exporters.jaeger; -import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanArrayKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleArrayKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longArrayKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringArrayKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; diff --git a/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingMetricExporterTest.java b/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingMetricExporterTest.java index 3f2cc539e2a..acf70cf1d84 100644 --- a/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingMetricExporterTest.java +++ b/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingMetricExporterTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.exporters.logging; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; diff --git a/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java b/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java index c2cbc14de3d..c339c4a7345 100644 --- a/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java +++ b/exporters/logging/src/test/java/io/opentelemetry/exporters/logging/LoggingSpanExporterTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.exporters.logging; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; diff --git a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/CommonAdapter.java b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/CommonAdapter.java index 552ee817485..5abc38aa4f0 100644 --- a/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/CommonAdapter.java +++ b/exporters/otlp/src/main/java/io/opentelemetry/exporters/otlp/CommonAdapter.java @@ -17,14 +17,6 @@ package io.opentelemetry.exporters.otlp; import io.opentelemetry.common.AttributeKey; -import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; -import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.LongKey; -import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.proto.common.v1.AnyValue; import io.opentelemetry.proto.common.v1.ArrayValue; import io.opentelemetry.proto.common.v1.InstrumentationLibrary; @@ -35,95 +27,95 @@ final class CommonAdapter { @SuppressWarnings("unchecked") public static KeyValue toProtoAttribute(AttributeKey key, T value) { - KeyValue.Builder builder = KeyValue.newBuilder().setKey(key.get()); + KeyValue.Builder builder = KeyValue.newBuilder().setKey(key.getKey()); switch (key.getType()) { case STRING: - return makeStringKeyValue((StringKey) key, (String) value); + return makeStringKeyValue(key, (String) value); case BOOLEAN: - return makeBooleanKeyValue((BooleanKey) key, (boolean) value); + return makeBooleanKeyValue(key, (boolean) value); case LONG: - return makeLongKeyValue((LongKey) key, (Long) value); + return makeLongKeyValue(key, (Long) value); case DOUBLE: - return makeDoubleKeyValue((DoubleKey) key, (Double) value); + return makeDoubleKeyValue(key, (Double) value); case BOOLEAN_ARRAY: - return makeBooleanArrayKeyValue((BooleanArrayKey) key, (List) value); + return makeBooleanArrayKeyValue(key, (List) value); case LONG_ARRAY: - return makeLongArrayKeyValue((LongArrayKey) key, (List) value); + return makeLongArrayKeyValue(key, (List) value); case DOUBLE_ARRAY: - return makeDoubleArrayKeyValue((DoubleArrayKey) key, (List) value); + return makeDoubleArrayKeyValue(key, (List) value); case STRING_ARRAY: - return makeStringArrayKeyValue((StringArrayKey) key, (List) value); + return makeStringArrayKeyValue(key, (List) value); } return builder.setValue(AnyValue.getDefaultInstance()).build(); } - private static KeyValue makeLongArrayKeyValue(LongArrayKey key, List value) { + private static KeyValue makeLongArrayKeyValue(AttributeKey key, List value) { KeyValue.Builder keyValueBuilder = KeyValue.newBuilder() - .setKey(key.get()) + .setKey(key.getKey()) .setValue(AnyValue.newBuilder().setArrayValue(makeLongArrayAnyValue(value)).build()); return keyValueBuilder.build(); } - private static KeyValue makeDoubleArrayKeyValue(DoubleArrayKey key, List value) { + private static KeyValue makeDoubleArrayKeyValue(AttributeKey key, List value) { KeyValue.Builder keyValueBuilder = KeyValue.newBuilder() - .setKey(key.get()) + .setKey(key.getKey()) .setValue(AnyValue.newBuilder().setArrayValue(makeDoubleArrayAnyValue(value)).build()); return keyValueBuilder.build(); } - private static KeyValue makeBooleanArrayKeyValue(BooleanArrayKey key, List value) { + private static KeyValue makeBooleanArrayKeyValue(AttributeKey key, List value) { KeyValue.Builder keyValueBuilder = KeyValue.newBuilder() - .setKey(key.get()) + .setKey(key.getKey()) .setValue(AnyValue.newBuilder().setArrayValue(makeBooleanArrayAnyValue(value)).build()); return keyValueBuilder.build(); } - private static KeyValue makeStringArrayKeyValue(StringArrayKey key, List value) { + private static KeyValue makeStringArrayKeyValue(AttributeKey key, List value) { KeyValue.Builder keyValueBuilder = KeyValue.newBuilder() - .setKey(key.get()) + .setKey(key.getKey()) .setValue(AnyValue.newBuilder().setArrayValue(makeStringArrayAnyValue(value)).build()); return keyValueBuilder.build(); } - private static KeyValue makeLongKeyValue(LongKey key, long value) { + private static KeyValue makeLongKeyValue(AttributeKey key, long value) { KeyValue.Builder keyValueBuilder = KeyValue.newBuilder() - .setKey(key.get()) + .setKey(key.getKey()) .setValue(AnyValue.newBuilder().setIntValue(value).build()); return keyValueBuilder.build(); } - private static KeyValue makeDoubleKeyValue(DoubleKey key, double value) { + private static KeyValue makeDoubleKeyValue(AttributeKey key, double value) { KeyValue.Builder keyValueBuilder = KeyValue.newBuilder() - .setKey(key.get()) + .setKey(key.getKey()) .setValue(AnyValue.newBuilder().setDoubleValue(value).build()); return keyValueBuilder.build(); } - private static KeyValue makeBooleanKeyValue(BooleanKey key, boolean value) { + private static KeyValue makeBooleanKeyValue(AttributeKey key, boolean value) { KeyValue.Builder keyValueBuilder = KeyValue.newBuilder() - .setKey(key.get()) + .setKey(key.getKey()) .setValue(AnyValue.newBuilder().setBoolValue(value).build()); return keyValueBuilder.build(); } - private static KeyValue makeStringKeyValue(StringKey key, String value) { + private static KeyValue makeStringKeyValue(AttributeKey key, String value) { KeyValue.Builder keyValueBuilder = KeyValue.newBuilder() - .setKey(key.get()) + .setKey(key.getKey()) .setValue(AnyValue.newBuilder().setStringValue(value).build()); return keyValueBuilder.build(); diff --git a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/CommonAdapterTest.java b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/CommonAdapterTest.java index 570838046d8..82ba21e3b64 100644 --- a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/CommonAdapterTest.java +++ b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/CommonAdapterTest.java @@ -16,14 +16,14 @@ package io.opentelemetry.exporters.otlp; -import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanArrayKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleArrayKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longArrayKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringArrayKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.proto.common.v1.AnyValue; diff --git a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/MetricAdapterTest.java b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/MetricAdapterTest.java index 05ac5d7870b..c3723070492 100644 --- a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/MetricAdapterTest.java +++ b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/MetricAdapterTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.exporters.otlp; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static io.opentelemetry.proto.metrics.v1.AggregationTemporality.AGGREGATION_TEMPORALITY_CUMULATIVE; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; diff --git a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/ResourceAdapterTest.java b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/ResourceAdapterTest.java index d45c66e81f2..f1cb7108580 100644 --- a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/ResourceAdapterTest.java +++ b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/ResourceAdapterTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.exporters.otlp; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; diff --git a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java index f46b4bb5d76..4293aa47724 100644 --- a/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java +++ b/exporters/otlp/src/test/java/io/opentelemetry/exporters/otlp/SpanAdapterTest.java @@ -16,8 +16,8 @@ package io.opentelemetry.exporters.otlp; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_CLIENT; import static io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_CONSUMER; import static io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_INTERNAL; diff --git a/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/MetricAdapterTest.java b/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/MetricAdapterTest.java index 082d4b4eb34..140ad1fb308 100644 --- a/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/MetricAdapterTest.java +++ b/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/MetricAdapterTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.exporters.prometheus; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import com.google.common.collect.ImmutableList; diff --git a/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/PrometheusCollectorTest.java b/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/PrometheusCollectorTest.java index 394e59bebd4..fe2180124f9 100644 --- a/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/PrometheusCollectorTest.java +++ b/exporters/prometheus/src/test/java/io/opentelemetry/exporters/prometheus/PrometheusCollectorTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.exporters.prometheus; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; diff --git a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java index e0f00d734e6..0457503d4a7 100644 --- a/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java +++ b/exporters/zipkin/src/main/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporter.java @@ -16,12 +16,11 @@ package io.opentelemetry.exporters.zipkin; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static java.util.concurrent.TimeUnit.NANOSECONDS; import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.common.AttributeType; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.common.CompletableResultCode; @@ -88,7 +87,7 @@ public final class ZipkinSpanExporter implements SpanExporter { // Note: these 3 fields are non-private for testing static final String GRPC_STATUS_CODE = "grpc.status_code"; static final String GRPC_STATUS_DESCRIPTION = "grpc.status_description"; - static final StringKey STATUS_ERROR = stringKey("error"); + static final AttributeKey STATUS_ERROR = stringKey("error"); static final String KEY_INSTRUMENTATION_LIBRARY_NAME = "otel.instrumentation_library.name"; static final String KEY_INSTRUMENTATION_LIBRARY_VERSION = "otel.instrumentation_library.version"; @@ -154,7 +153,7 @@ static Span generateSpan(SpanData spanData, Endpoint localEndpoint) { new AttributeConsumer() { @Override public void consume(AttributeKey key, T value) { - spanBuilder.putTag(key.get(), valueToString(key, value)); + spanBuilder.putTag(key.getKey(), valueToString(key, value)); } }); Status status = spanData.getStatus(); @@ -167,7 +166,7 @@ public void consume(AttributeKey key, T value) { } // add the error tag, if it isn't already in the source span. if (status != null && !status.isOk() && spanAttributes.get(STATUS_ERROR) == null) { - spanBuilder.putTag(STATUS_ERROR.get(), status.getCanonicalCode().toString()); + spanBuilder.putTag(STATUS_ERROR.getKey(), status.getCanonicalCode().toString()); } InstrumentationLibraryInfo instrumentationLibraryInfo = diff --git a/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java b/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java index 7ff8ac293f1..8497ec9a615 100644 --- a/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java +++ b/exporters/zipkin/src/test/java/io/opentelemetry/exporters/zipkin/ZipkinSpanExporterTest.java @@ -16,14 +16,14 @@ package io.opentelemetry.exporters.zipkin; -import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanArrayKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleArrayKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longArrayKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringArrayKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doAnswer; @@ -210,8 +210,8 @@ void generateSpan_AlreadyHasHttpStatusInfo() { buildZipkinSpan(Span.Kind.CLIENT) .toBuilder() .clearTags() - .putTag(SemanticAttributes.HTTP_STATUS_CODE.get(), "404") - .putTag(SemanticAttributes.HTTP_STATUS_TEXT.get(), "NOT FOUND") + .putTag(SemanticAttributes.HTTP_STATUS_CODE.getKey(), "404") + .putTag(SemanticAttributes.HTTP_STATUS_TEXT.getKey(), "NOT FOUND") .putTag("error", "A user provided error") .build()); } @@ -233,9 +233,9 @@ void generateSpan_WithRpcErrorStatus() { buildZipkinSpan(Span.Kind.SERVER) .toBuilder() .putTag(ZipkinSpanExporter.GRPC_STATUS_DESCRIPTION, errorMessage) - .putTag(SemanticAttributes.RPC_SERVICE.get(), "my service name") + .putTag(SemanticAttributes.RPC_SERVICE.getKey(), "my service name") .putTag(ZipkinSpanExporter.GRPC_STATUS_CODE, "DEADLINE_EXCEEDED") - .putTag(ZipkinSpanExporter.STATUS_ERROR.get(), "DEADLINE_EXCEEDED") + .putTag(ZipkinSpanExporter.STATUS_ERROR.getKey(), "DEADLINE_EXCEEDED") .build()); } diff --git a/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/MessageEvent.java b/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/MessageEvent.java index 707ab4ff0af..de8cad11930 100644 --- a/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/MessageEvent.java +++ b/extensions/trace_utils/src/main/java/io/opentelemetry/extensions/trace/MessageEvent.java @@ -16,11 +16,10 @@ package io.opentelemetry.extensions.trace; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; -import io.opentelemetry.common.AttributeKeyImpl.LongKey; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.trace.Event; import javax.annotation.concurrent.Immutable; @@ -39,10 +38,10 @@ public final class MessageEvent implements Event { private static final String EVENT_NAME = "message"; - private static final StringKey TYPE = stringKey("message.type"); - private static final LongKey ID = longKey("message.id"); - private static final LongKey COMPRESSED_SIZE = longKey("message.compressed_size"); - private static final LongKey UNCOMPRESSED_SIZE = longKey("message.uncompressed_size"); + private static final AttributeKey TYPE = stringKey("message.type"); + private static final AttributeKey ID = longKey("message.id"); + private static final AttributeKey COMPRESSED_SIZE = longKey("message.compressed_size"); + private static final AttributeKey UNCOMPRESSED_SIZE = longKey("message.uncompressed_size"); /** * Available types for a {@code MessageEvent}. diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java index 9c61cdcdad4..cd16949231e 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java @@ -16,10 +16,10 @@ package io.opentelemetry.opentracingshim; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import io.opentelemetry.common.AttributeKey; import io.opentelemetry.correlationcontext.CorrelationContext; diff --git a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java index 3078a131e43..ee3ba79f5a4 100644 --- a/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java +++ b/opentracing_shim/src/main/java/io/opentelemetry/opentracingshim/SpanShim.java @@ -16,10 +16,10 @@ package io.opentelemetry.opentracingshim; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.trace.Status; diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java index 754ff6218ec..4dd658580c2 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.opentracingshim.testbed.nestedcallbacks; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static io.opentelemetry.opentracingshim.testbed.TestUtils.finishedSpansSize; import static org.awaitility.Awaitility.await; import static org.hamcrest.core.IsEqual.equalTo; diff --git a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java index 8bc0f605358..937d746e1a0 100644 --- a/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java +++ b/opentracing_shim/src/test/java/io/opentelemetry/opentracingshim/testbed/promisepropagation/PromisePropagationTest.java @@ -16,11 +16,11 @@ package io.opentelemetry.opentracingshim.testbed.promisepropagation; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static io.opentelemetry.opentracingshim.testbed.TestUtils.getByAttr; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.opentracingshim.TraceShim; import io.opentelemetry.sdk.correlationcontext.CorrelationContextManagerSdk; @@ -110,7 +110,7 @@ void testPromiseCallback() { List finished = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(finished.size()).isEqualTo(4); - StringKey component = stringKey(Tags.COMPONENT.getKey()); + AttributeKey component = stringKey(Tags.COMPONENT.getKey()); List spanExamplePromise = getByAttr(finished, component, "example-promises"); assertThat(spanExamplePromise).hasSize(1); assertThat(spanExamplePromise.get(0).getParentSpanId()).isEqualTo(SpanId.getInvalid()); diff --git a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java index a60cad5ec49..48880b34d44 100644 --- a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java +++ b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; -import io.opentelemetry.common.AttributeKeyImpl; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.sdk.OpenTelemetrySdk; import io.opentelemetry.sdk.common.CompletableResultCode; @@ -48,8 +48,8 @@ @State(Scope.Benchmark) public class SpanPipelineBenchmark { - private static final AttributeKeyImpl.StringKey LINK_ATTRIBUTE_KEY = stringKey("linkAttr"); - private static final AttributeKeyImpl.BooleanKey FINALIZED_KEY = booleanKey("finalized"); + private static final AttributeKey LINK_ATTRIBUTE_KEY = stringKey("linkAttr"); + private static final AttributeKey FINALIZED_KEY = booleanKey("finalized"); private final TracerSdk tracerSdk = OpenTelemetrySdk.getTracerProvider().get("benchmarkTracer"); @Setup(Level.Trial) diff --git a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java index 59b3fe14d77..1a6e2f001bd 100644 --- a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java +++ b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java @@ -16,13 +16,12 @@ package io.opentelemetry.sdk.resources; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import com.google.auto.value.AutoValue; import com.google.auto.value.extension.memoized.Memoized; import io.opentelemetry.common.AttributeConsumer; import io.opentelemetry.common.AttributeKey; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.internal.StringUtils; @@ -50,9 +49,9 @@ public abstract class Resource { private static final Resource EMPTY = create(Attributes.empty()); // todo: move to ResourceAttributes - private static final StringKey SDK_NAME = stringKey("telemetry.sdk.name"); - private static final StringKey SDK_LANGUAGE = stringKey("telemetry.sdk.language"); - private static final StringKey SDK_VERSION = stringKey("telemetry.sdk.version"); + private static final AttributeKey SDK_NAME = stringKey("telemetry.sdk.name"); + private static final AttributeKey SDK_LANGUAGE = stringKey("telemetry.sdk.language"); + private static final AttributeKey SDK_VERSION = stringKey("telemetry.sdk.version"); private static final Resource TELEMETRY_SDK; @@ -220,6 +219,6 @@ private static boolean isValid(String name) { * @return whether the name is valid. */ private static boolean isValidAndNotEmpty(AttributeKey name) { - return !name.get().isEmpty() && isValid(name.get()); + return !name.getKey().isEmpty() && isValid(name.getKey()); } } diff --git a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java index 51ab45d9141..704cc057361 100644 --- a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java +++ b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java @@ -16,11 +16,10 @@ package io.opentelemetry.sdk.resources; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; -import io.opentelemetry.common.AttributeKeyImpl.LongKey; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; +import io.opentelemetry.common.AttributeKey; /** * Provides constants for resource semantic conventions defined by the OpenTelemetry specification. @@ -32,108 +31,110 @@ public final class ResourceAttributes { /** The operating system type, such as {@code "WINDOWS"}, {@code "DARWIN"}, {@code "LINUX"}. */ - public static final StringKey OS_NAME = stringKey("os.name"); + public static final AttributeKey OS_NAME = stringKey("os.name"); /** * Human readable information about the OS version, e.g. {@code "Microsoft Windows [Version * 10.0.18363.778]"}, {@code "Ubuntu 18.04.1 LTS"}. */ - public static final StringKey OS_DESCRIPTION = stringKey("os.description"); + public static final AttributeKey OS_DESCRIPTION = stringKey("os.description"); /** Process identifier (PID). */ - public static final LongKey PROCESS_PID = longKey("process.pid"); + public static final AttributeKey PROCESS_PID = longKey("process.pid"); /** The name of the process executable. */ - public static final StringKey PROCESS_EXECUTABLE_NAME = stringKey("process.executable.name"); + public static final AttributeKey PROCESS_EXECUTABLE_NAME = + stringKey("process.executable.name"); /** The full path to the process executable. */ - public static final StringKey PROCESS_EXECUTABLE_PATH = stringKey("process.executable.path"); + public static final AttributeKey PROCESS_EXECUTABLE_PATH = + stringKey("process.executable.path"); /** The command used to launch the process (i.e. the command name). */ - public static final StringKey PROCESS_COMMAND = stringKey("process.command"); + public static final AttributeKey PROCESS_COMMAND = stringKey("process.command"); /** * The full command used to launch the process. The value can be either a list of strings * representing the ordered list of arguments, or a single string representing the full command. */ - public static final StringKey PROCESS_COMMAND_LINE = stringKey("process.command_line"); + public static final AttributeKey PROCESS_COMMAND_LINE = stringKey("process.command_line"); /** The username of the user that owns the process. */ - public static final StringKey PROCESS_OWNER = stringKey("process.owner"); + public static final AttributeKey PROCESS_OWNER = stringKey("process.owner"); /** * Logical name of the service. MUST be the same for all instances of horizontally scaled * services. */ - public static final StringKey SERVICE_NAME = stringKey("service.name"); + public static final AttributeKey SERVICE_NAME = stringKey("service.name"); /** * A namespace for `service.name`. A string value having a meaning that helps to distinguish a * group of services, */ - public static final StringKey SERVICE_NAMESPACE = stringKey("service.namespace"); + public static final AttributeKey SERVICE_NAMESPACE = stringKey("service.namespace"); /** * The string ID of the service instance. MUST be unique for each instance of the same * `service.namespace,service.name` pair. */ - public static final StringKey SERVICE_INSTANCE = stringKey("service.instance.id"); + public static final AttributeKey SERVICE_INSTANCE = stringKey("service.instance.id"); /** The version string of the service API or implementation. */ - public static final StringKey SERVICE_VERSION = stringKey("service.version"); + public static final AttributeKey SERVICE_VERSION = stringKey("service.version"); /** The name of the telemetry library. */ - public static final StringKey LIBRARY_NAME = stringKey("library.name"); + public static final AttributeKey LIBRARY_NAME = stringKey("library.name"); /** The language of telemetry library and of the code instrumented with it. */ - public static final StringKey LIBRARY_LANGUAGE = stringKey("library.language"); + public static final AttributeKey LIBRARY_LANGUAGE = stringKey("library.language"); /** The version string of the library. */ - public static final StringKey LIBRARY_VERSION = stringKey("library.version"); + public static final AttributeKey LIBRARY_VERSION = stringKey("library.version"); /** Container name. */ - public static final StringKey CONTAINER_NAME = stringKey("container.name"); + public static final AttributeKey CONTAINER_NAME = stringKey("container.name"); /** Container id. */ - public static final StringKey CONTAINER_ID = stringKey("container.id"); + public static final AttributeKey CONTAINER_ID = stringKey("container.id"); /** Name of the image the container was built on. */ - public static final StringKey CONTAINER_IMAGE_NAME = stringKey("container.image.name"); + public static final AttributeKey CONTAINER_IMAGE_NAME = stringKey("container.image.name"); /** Container image tag. */ - public static final StringKey CONTAINER_IMAGE_TAG = stringKey("container.image.tag"); + public static final AttributeKey CONTAINER_IMAGE_TAG = stringKey("container.image.tag"); /** The name of the cluster that the pod is running in. */ - public static final StringKey K8S_CLUSTER = stringKey("k8s.cluster.name"); + public static final AttributeKey K8S_CLUSTER = stringKey("k8s.cluster.name"); /** The name of the namespace that the pod is running in. */ - public static final StringKey K8S_NAMESPACE = stringKey("k8s.namespace.name"); + public static final AttributeKey K8S_NAMESPACE = stringKey("k8s.namespace.name"); /** The name of the pod. */ - public static final StringKey K8S_POD = stringKey("k8s.pod.name"); + public static final AttributeKey K8S_POD = stringKey("k8s.pod.name"); /** The name of the deployment. */ - public static final StringKey K8S_DEPLOYMENT = stringKey("k8s.deployment.name"); + public static final AttributeKey K8S_DEPLOYMENT = stringKey("k8s.deployment.name"); /** Hostname of the host. It contains what the `hostname` command returns on the host machine. */ - public static final StringKey HOST_HOSTNAME = stringKey("host.hostname"); + public static final AttributeKey HOST_HOSTNAME = stringKey("host.hostname"); /** Unique host id. For Cloud this must be the instance_id assigned by the cloud provider. */ - public static final StringKey HOST_ID = stringKey("host.id"); + public static final AttributeKey HOST_ID = stringKey("host.id"); /** * Name of the host. It may contain what `hostname` returns on Unix systems, the fully qualified, * or a name specified by the user. */ - public static final StringKey HOST_NAME = stringKey("host.name"); + public static final AttributeKey HOST_NAME = stringKey("host.name"); /** Type of host. For Cloud this must be the machine type. */ - public static final StringKey HOST_TYPE = stringKey("host.type"); + public static final AttributeKey HOST_TYPE = stringKey("host.type"); /** Name of the VM image or OS install the host was instantiated from. */ - public static final StringKey HOST_IMAGE_NAME = stringKey("host.image.name"); + public static final AttributeKey HOST_IMAGE_NAME = stringKey("host.image.name"); /** VM image id. For Cloud, this value is from the provider. */ - public static final StringKey HOST_IMAGE_ID = stringKey("host.image.id"); + public static final AttributeKey HOST_IMAGE_ID = stringKey("host.image.id"); /** The version string of the VM image. */ - public static final StringKey HOST_IMAGE_VERSION = stringKey("host.image.version"); + public static final AttributeKey HOST_IMAGE_VERSION = stringKey("host.image.version"); /** Name of the cloud provider. */ - public static final StringKey CLOUD_PROVIDER = stringKey("cloud.provider"); + public static final AttributeKey CLOUD_PROVIDER = stringKey("cloud.provider"); /** The cloud account id used to identify different entities. */ - public static final StringKey CLOUD_ACCOUNT = stringKey("cloud.account.id"); + public static final AttributeKey CLOUD_ACCOUNT = stringKey("cloud.account.id"); /** A specific geographical location where different entities can run. */ - public static final StringKey CLOUD_REGION = stringKey("cloud.region"); + public static final AttributeKey CLOUD_REGION = stringKey("cloud.region"); /** Zones are a sub set of the region connected through low-latency links. */ - public static final StringKey CLOUD_ZONE = stringKey("cloud.zone"); + public static final AttributeKey CLOUD_ZONE = stringKey("cloud.zone"); /** The name of the function being executed. */ - public static final StringKey FAAS_NAME = stringKey("faas.name"); + public static final AttributeKey FAAS_NAME = stringKey("faas.name"); /** The unique ID of the function being executed. */ - public static final StringKey FAAS_ID = stringKey("faas.id"); + public static final AttributeKey FAAS_ID = stringKey("faas.id"); /** The version string of the function being executed. */ - public static final StringKey FAAS_VERSION = stringKey("faas.version"); + public static final AttributeKey FAAS_VERSION = stringKey("faas.version"); /** The execution environment ID as a string. */ - public static final StringKey FAAS_INSTANCE = stringKey("faas.instance"); + public static final AttributeKey FAAS_INSTANCE = stringKey("faas.instance"); private ResourceAttributes() {} } diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/EnvAutodetectResourceTest.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/EnvAutodetectResourceTest.java index c2583704dc7..6e89ea74ad6 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/EnvAutodetectResourceTest.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/EnvAutodetectResourceTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.resources; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java index ecdd7542c8c..81957e54510 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceProvidersTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.resources; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributesKeys.longKey; import static org.assertj.core.api.Assertions.assertThat; import org.junit.jupiter.api.Test; diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java index f5ae6220774..d8069c0bea1 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java @@ -16,14 +16,14 @@ package io.opentelemetry.sdk.resources; -import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanArrayKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleArrayKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longArrayKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringArrayKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/TestResourceProvider.java b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/TestResourceProvider.java index 542a811d4aa..8300994c072 100644 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/resources/TestResourceProvider.java +++ b/sdk/common/src/test/java/io/opentelemetry/sdk/resources/TestResourceProvider.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.resources; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; +import static io.opentelemetry.common.AttributesKeys.longKey; import io.opentelemetry.common.Attributes; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java index 032767f90b5..7d05b77c3c8 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/BatchRecorderSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java index e697e2ebbc6..3d0631f11de 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleCounterSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java index ec14a2ed760..9be84178587 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleSumObserverSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java index f277ba6b26e..85d3266ff04 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownCounterSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java index 474cbf52424..64108ab3f15 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleUpDownSumObserverSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java index 74c9372b4c8..cf7b74bac19 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueObserverSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java index 7fc6b809621..e48be0d036b 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/DoubleValueRecorderSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java index 119e4a59129..a9afd15f98b 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongCounterSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java index 59b61e243d5..d51ecc6e505 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongSumObserverSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java index 851da564dc0..5d0de322c82 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownCounterSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java index 496e4aa1963..ac6049e0f21 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongUpDownSumObserverSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java index 85f8bd71784..5ef0d0d3d16 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueObserverSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java index 55516bc9d0b..e36a8563f9b 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/LongValueRecorderSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java index 332f212a023..033ff965382 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.metrics; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java index 5804334c894..1b9c6255ae7 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpan.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import com.google.common.collect.EvictingQueue; import io.opentelemetry.common.AttributeConsumer; @@ -308,7 +308,7 @@ public void setAttribute(String key, boolean value) { @Override public void setAttribute(AttributeKey key, T value) { - if (key == null || key.get() == null || key.get().length() == 0) { + if (key == null || key.getKey() == null || key.getKey().length() == 0) { return; } synchronized (lock) { diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java index 94ae36155ae..438899036d4 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/Samplers.java @@ -16,11 +16,11 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; import com.google.auto.value.AutoValue; import com.google.common.base.Preconditions; -import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.trace.Sampler.Decision; @@ -48,7 +48,7 @@ public final class Samplers { *

See https://github.com/open-telemetry/opentelemetry-specification/pull/570 */ // Visible for tests. - static final DoubleKey SAMPLING_PROBABILITY = doubleKey("sampling.probability"); + static final AttributeKey SAMPLING_PROBABILITY = doubleKey("sampling.probability"); private static final SamplingResult EMPTY_RECORDED_AND_SAMPLED_SAMPLING_RESULT = SamplingResultImpl.createWithoutAttributes(Decision.RECORD_AND_SAMPLED); diff --git a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java index 41ef9149b96..a361cd09263 100644 --- a/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java +++ b/sdk/tracing/src/main/java/io/opentelemetry/sdk/trace/SpanBuilderSdk.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import io.grpc.Context; import io.opentelemetry.common.AttributeConsumer; diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java index e0b7996bb97..9a9a59234b5 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/RecordEventsReadableSpanTest.java @@ -16,14 +16,14 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanArrayKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleArrayKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longArrayKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringArrayKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java index 064fe86e565..460172c942a 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SamplersTest.java @@ -16,8 +16,8 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java index 1ed3515fdec..e2ade54710c 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/SpanBuilderSdkTest.java @@ -16,21 +16,21 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeKeyImpl.booleanArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.booleanKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.doubleKey; -import static io.opentelemetry.common.AttributeKeyImpl.longArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.longKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringArrayKey; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.booleanArrayKey; +import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleArrayKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longArrayKey; +import static io.opentelemetry.common.AttributesKeys.longKey; +import static io.opentelemetry.common.AttributesKeys.stringArrayKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static java.util.Collections.emptyList; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; import io.grpc.Context; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.context.Scope; @@ -527,7 +527,7 @@ void sampler() { @Test void sampler_decisionAttributes() { final String samplerAttributeName = "sampler-attribute"; - StringKey samplerAttributeKey = stringKey(samplerAttributeName); + AttributeKey samplerAttributeKey = stringKey(samplerAttributeName); RecordEventsReadableSpan span = (RecordEventsReadableSpan) TestUtils.startSpanWithSampler( @@ -561,7 +561,7 @@ public String getDescription() { return "test sampler"; } }, - Collections.singletonMap(samplerAttributeKey.get(), "none")) + Collections.singletonMap(samplerAttributeKey.getKey(), "none")) .startSpan(); try { assertThat(span.getContext().isSampled()).isTrue(); diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java index f11e75dcffa..2035f531a56 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TestUtils.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import io.opentelemetry.common.Attributes; import io.opentelemetry.sdk.trace.config.TraceConfig; diff --git a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java index 37c517b8067..7e35e538901 100644 --- a/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java +++ b/sdk/tracing/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.trace; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.common.Attributes; diff --git a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java index 1f09fa7f38c..2308432dae8 100644 --- a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java +++ b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java @@ -17,10 +17,9 @@ package io.opentelemetry.sdk.extensions.trace.jaeger.sampler; import com.google.common.annotations.VisibleForTesting; -import io.opentelemetry.common.AttributeKeyImpl; -import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.common.Attributes; +import io.opentelemetry.common.AttributesKeys; import io.opentelemetry.common.ReadableAttributes; import io.opentelemetry.sdk.internal.MillisClock; import io.opentelemetry.sdk.trace.Sampler; @@ -36,8 +35,8 @@ */ class RateLimitingSampler implements Sampler { static final String TYPE = "ratelimiting"; - static final StringKey SAMPLER_TYPE = AttributeKeyImpl.stringKey("sampler.type"); - static final DoubleKey SAMPLER_PARAM = AttributeKeyImpl.doubleKey("sampler.param"); + static final AttributeKey SAMPLER_TYPE = AttributesKeys.stringKey("sampler.type"); + static final AttributeKey SAMPLER_PARAM = AttributesKeys.doubleKey("sampler.param"); private final double maxTracesPerSecond; private final RateLimiter rateLimiter; diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java index 762168577c1..6dedd06a7cd 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/nestedcallbacks/NestedCallbacksTest.java @@ -16,7 +16,7 @@ package io.opentelemetry.sdk.extensions.trace.testbed.nestedcallbacks; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.awaitility.Awaitility.await; import static org.hamcrest.core.IsEqual.equalTo; diff --git a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/promisepropagation/PromisePropagationTest.java b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/promisepropagation/PromisePropagationTest.java index afa803958a8..aaa4c381521 100644 --- a/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/promisepropagation/PromisePropagationTest.java +++ b/sdk_extensions/testbed/src/test/java/io/opentelemetry/sdk/extensions/trace/testbed/promisepropagation/PromisePropagationTest.java @@ -16,10 +16,10 @@ package io.opentelemetry.sdk.extensions.trace.testbed.promisepropagation; -import static io.opentelemetry.common.AttributeKeyImpl.stringKey; +import static io.opentelemetry.common.AttributesKeys.stringKey; import static org.assertj.core.api.Assertions.assertThat; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; +import io.opentelemetry.common.AttributeKey; import io.opentelemetry.context.Scope; import io.opentelemetry.exporters.inmemory.InMemoryTracing; import io.opentelemetry.sdk.extensions.trace.testbed.TestUtils; @@ -105,7 +105,7 @@ void testPromiseCallback() { List finished = inMemoryTracing.getSpanExporter().getFinishedSpanItems(); assertThat(finished.size()).isEqualTo(4); - StringKey component = stringKey("component"); + AttributeKey component = stringKey("component"); SpanData parentSpanProto = TestUtils.getOneByAttr(finished, component, "example-promises"); assertThat(parentSpanProto).isNotNull(); assertThat(SpanId.isValid(parentSpanProto.getParentSpanId())).isFalse(); From e1ab103d1b4ef4d5e5ad9ac595a73ca68eccb2bb Mon Sep 17 00:00:00 2001 From: jwatson Date: Fri, 11 Sep 2020 13:54:35 -0700 Subject: [PATCH 14/20] use autovalue for the key implementations --- .../io/opentelemetry/common/AttributeKey.java | 4 + .../common/AttributeKeyImpl.java | 117 ++---------------- .../opentelemetry/common/AttributesKeys.java | 24 ++-- .../opentelemetry/common/AttributesTest.java | 2 +- 4 files changed, 25 insertions(+), 122 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKey.java b/api/src/main/java/io/opentelemetry/common/AttributeKey.java index 58cff61a5b4..ed7c9d4a391 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeKey.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeKey.java @@ -16,6 +16,8 @@ package io.opentelemetry.common; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; /** @@ -30,8 +32,10 @@ @Immutable public interface AttributeKey extends Comparable { /** Returns the underlying String representation of the key. */ + @Nullable String getKey(); /** Returns the type of attribute for this key. Useful for building switch statements. */ + @Nonnull AttributeType getType(); } diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java index 12f049a396a..5d97f6f5743 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java @@ -16,23 +16,23 @@ package io.opentelemetry.common; -import java.util.List; +import com.google.auto.value.AutoValue; @SuppressWarnings("rawtypes") +@AutoValue abstract class AttributeKeyImpl implements AttributeKey { - private final String key; - AttributeKeyImpl(String key) { - this.key = key; + static AttributeKeyImpl create(String key, AttributeType type) { + return new AutoValue_AttributeKeyImpl<>(key, type); } - @Override - public String getKey() { - return key; - } + ////////////////////////////////// + // IMPORTANT: the equals/hashcode/compareTo *only* include the key, and not the type, + // so that de-duping of attributes is based on the key, and not also based on the type. + ////////////////////////////////// @Override - public boolean equals(Object o) { + public final boolean equals(Object o) { if (this == o) { return true; } @@ -42,109 +42,16 @@ public boolean equals(Object o) { AttributeKeyImpl that = (AttributeKeyImpl) o; - return key != null ? key.equals(that.key) : that.key == null; - } - - @Override - public int hashCode() { - return key != null ? key.hashCode() : 0; + return getKey() != null ? getKey().equals(that.getKey()) : that.getKey() == null; } @Override - public String toString() { - return "AttributeKeyImpl{" + "key='" + key + '\'' + '}'; + public final int hashCode() { + return getKey() != null ? getKey().hashCode() : 0; } @Override public int compareTo(AttributeKey o) { return getKey().compareTo(o.getKey()); } - - static class StringKey extends AttributeKeyImpl { - StringKey(String key) { - super(key); - } - - @Override - public AttributeType getType() { - return AttributeType.STRING; - } - } - - static class BooleanKey extends AttributeKeyImpl { - BooleanKey(String key) { - super(key); - } - - @Override - public AttributeType getType() { - return AttributeType.BOOLEAN; - } - } - - static class LongKey extends AttributeKeyImpl { - LongKey(String key) { - super(key); - } - - @Override - public AttributeType getType() { - return AttributeType.LONG; - } - } - - static class DoubleKey extends AttributeKeyImpl { - DoubleKey(String key) { - super(key); - } - - @Override - public AttributeType getType() { - return AttributeType.DOUBLE; - } - } - - static class StringArrayKey extends AttributeKeyImpl> { - StringArrayKey(String key) { - super(key); - } - - @Override - public AttributeType getType() { - return AttributeType.STRING_ARRAY; - } - } - - static class BooleanArrayKey extends AttributeKeyImpl> { - BooleanArrayKey(String key) { - super(key); - } - - @Override - public AttributeType getType() { - return AttributeType.BOOLEAN_ARRAY; - } - } - - static class LongArrayKey extends AttributeKeyImpl> { - LongArrayKey(String key) { - super(key); - } - - @Override - public AttributeType getType() { - return AttributeType.LONG_ARRAY; - } - } - - static class DoubleArrayKey extends AttributeKeyImpl> { - DoubleArrayKey(String key) { - super(key); - } - - @Override - public AttributeType getType() { - return AttributeType.DOUBLE_ARRAY; - } - } } diff --git a/api/src/main/java/io/opentelemetry/common/AttributesKeys.java b/api/src/main/java/io/opentelemetry/common/AttributesKeys.java index 58ca0fcf2b7..4111c647eff 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributesKeys.java +++ b/api/src/main/java/io/opentelemetry/common/AttributesKeys.java @@ -16,14 +16,6 @@ package io.opentelemetry.common; -import io.opentelemetry.common.AttributeKeyImpl.BooleanArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.BooleanKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.DoubleKey; -import io.opentelemetry.common.AttributeKeyImpl.LongArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.LongKey; -import io.opentelemetry.common.AttributeKeyImpl.StringArrayKey; -import io.opentelemetry.common.AttributeKeyImpl.StringKey; import java.util.List; /** @@ -36,41 +28,41 @@ private AttributesKeys() {} /** Create a new AttributeKey for String valued attributes. */ public static AttributeKey stringKey(String key) { - return new StringKey(key); + return AttributeKeyImpl.create(key, AttributeType.STRING); } /** Create a new AttributeKey for Boolean valued attributes. */ public static AttributeKey booleanKey(String key) { - return new BooleanKey(key); + return AttributeKeyImpl.create(key, AttributeType.BOOLEAN); } /** Create a new AttributeKey for Long valued attributes. */ public static AttributeKey longKey(String key) { - return new LongKey(key); + return AttributeKeyImpl.create(key, AttributeType.LONG); } /** Create a new AttributeKey for Double valued attributes. */ public static AttributeKey doubleKey(String key) { - return new DoubleKey(key); + return AttributeKeyImpl.create(key, AttributeType.DOUBLE); } /** Create a new AttributeKey for List<String> valued attributes. */ public static AttributeKey> stringArrayKey(String key) { - return new StringArrayKey(key); + return AttributeKeyImpl.create(key, AttributeType.STRING_ARRAY); } /** Create a new AttributeKey for List<Boolean> valued attributes. */ public static AttributeKey> booleanArrayKey(String key) { - return new BooleanArrayKey(key); + return AttributeKeyImpl.create(key, AttributeType.BOOLEAN_ARRAY); } /** Create a new AttributeKey for List<Long> valued attributes. */ public static AttributeKey> longArrayKey(String key) { - return new LongArrayKey(key); + return AttributeKeyImpl.create(key, AttributeType.LONG_ARRAY); } /** Create a new AttributeKey for List<Double> valued attributes. */ public static AttributeKey> doubleArrayKey(String key) { - return new DoubleArrayKey(key); + return AttributeKeyImpl.create(key, AttributeType.DOUBLE_ARRAY); } } diff --git a/api/src/test/java/io/opentelemetry/common/AttributesTest.java b/api/src/test/java/io/opentelemetry/common/AttributesTest.java index 1a901ce5ebd..f7a7df4a7bc 100644 --- a/api/src/test/java/io/opentelemetry/common/AttributesTest.java +++ b/api/src/test/java/io/opentelemetry/common/AttributesTest.java @@ -46,7 +46,7 @@ void forEach() { attributes.forEach(entriesSeen::put); assertThat(entriesSeen) - .containsExactly(entry(stringKey("key1"), "value1"), entry(stringKey("key2"), 333L)); + .containsExactly(entry(stringKey("key1"), "value1"), entry(longKey("key2"), 333L)); } @Test From cac0ae57ee960f47bda26029339db0e49acd1516 Mon Sep 17 00:00:00 2001 From: jwatson Date: Fri, 11 Sep 2020 14:05:25 -0700 Subject: [PATCH 15/20] fix javadoc issues --- .../main/java/io/opentelemetry/common/AttributesKeys.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/common/AttributesKeys.java b/api/src/main/java/io/opentelemetry/common/AttributesKeys.java index 4111c647eff..4bf4bf25ad6 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributesKeys.java +++ b/api/src/main/java/io/opentelemetry/common/AttributesKeys.java @@ -46,22 +46,22 @@ public static AttributeKey doubleKey(String key) { return AttributeKeyImpl.create(key, AttributeType.DOUBLE); } - /** Create a new AttributeKey for List<String> valued attributes. */ + /** Create a new AttributeKey for List<String> valued attributes. */ public static AttributeKey> stringArrayKey(String key) { return AttributeKeyImpl.create(key, AttributeType.STRING_ARRAY); } - /** Create a new AttributeKey for List<Boolean> valued attributes. */ + /** Create a new AttributeKey for List<Boolean> valued attributes. */ public static AttributeKey> booleanArrayKey(String key) { return AttributeKeyImpl.create(key, AttributeType.BOOLEAN_ARRAY); } - /** Create a new AttributeKey for List<Long> valued attributes. */ + /** Create a new AttributeKey for List<Long> valued attributes. */ public static AttributeKey> longArrayKey(String key) { return AttributeKeyImpl.create(key, AttributeType.LONG_ARRAY); } - /** Create a new AttributeKey for List<Double> valued attributes. */ + /** Create a new AttributeKey for List<Double> valued attributes. */ public static AttributeKey> doubleArrayKey(String key) { return AttributeKeyImpl.create(key, AttributeType.DOUBLE_ARRAY); } From d98eb2a08678c667b5465bf01ae57b4d803996a4 Mon Sep 17 00:00:00 2001 From: jwatson Date: Mon, 14 Sep 2020 08:42:28 -0700 Subject: [PATCH 16/20] update benchmark keys, and a few tweaks from PR review --- .../opentelemetry/common/AttributeConsumer.java | 6 +++++- .../io/opentelemetry/common/Attributes.java | 2 +- .../io/opentelemetry/common/AttributesTest.java | 6 ++++++ .../sdk/trace/SpanPipelineBenchmark.java | 17 ++++++++++++----- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java b/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java index 5018e15d873..90b9e6d00ee 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeConsumer.java @@ -16,7 +16,11 @@ package io.opentelemetry.common; -/** Used for iterating over all the key/value pairs in an {@link Attributes} instance. */ +/** + * Used for iterating over all the key/value pairs in an {@link Attributes} instance. + * + * @since 0.9.0 + */ public interface AttributeConsumer { void consume(AttributeKey key, T value); } diff --git a/api/src/main/java/io/opentelemetry/common/Attributes.java b/api/src/main/java/io/opentelemetry/common/Attributes.java index 69501743c64..b67f3dd09ab 100644 --- a/api/src/main/java/io/opentelemetry/common/Attributes.java +++ b/api/src/main/java/io/opentelemetry/common/Attributes.java @@ -199,7 +199,7 @@ public Attributes build() { /** Sets a {@link AttributeKey} with associated value into this. */ public Builder setAttribute(AttributeKey key, T value) { - if (key == null || key.getKey().length() == 0) { + if (key == null || key.getKey() == null || key.getKey().length() == 0) { return this; } if (value == null) { diff --git a/api/src/test/java/io/opentelemetry/common/AttributesTest.java b/api/src/test/java/io/opentelemetry/common/AttributesTest.java index f7a7df4a7bc..5f9f9cbf4bd 100644 --- a/api/src/test/java/io/opentelemetry/common/AttributesTest.java +++ b/api/src/test/java/io/opentelemetry/common/AttributesTest.java @@ -58,6 +58,12 @@ void forEach_singleAttribute() { assertThat(entriesSeen).containsExactly(entry(stringKey("key"), "value")); } + @Test + void builder_nullKey() { + Attributes attributes = Attributes.newBuilder().setAttribute(stringKey(null), "value").build(); + assertThat(attributes).isEqualTo(Attributes.empty()); + } + @Test void forEach_empty() { final AtomicBoolean sawSomething = new AtomicBoolean(false); diff --git a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java index 48880b34d44..15f8a3512b0 100644 --- a/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java +++ b/sdk/all/src/jmh/java/io/opentelemetry/sdk/trace/SpanPipelineBenchmark.java @@ -17,6 +17,8 @@ package io.opentelemetry.sdk.trace; import static io.opentelemetry.common.AttributesKeys.booleanKey; +import static io.opentelemetry.common.AttributesKeys.doubleKey; +import static io.opentelemetry.common.AttributesKeys.longKey; import static io.opentelemetry.common.AttributesKeys.stringKey; import io.opentelemetry.common.AttributeKey; @@ -50,6 +52,11 @@ public class SpanPipelineBenchmark { private static final AttributeKey LINK_ATTRIBUTE_KEY = stringKey("linkAttr"); private static final AttributeKey FINALIZED_KEY = booleanKey("finalized"); + private static final AttributeKey OPERATION_KEY = stringKey("operation"); + private static final AttributeKey LONG_ATTRIBUTE_KEY = longKey("longAttribute"); + private static final AttributeKey STRING_ATTRIBUTE_KEY = stringKey("stringAttribute"); + private static final AttributeKey DOUBLE_ATTRIBUTE_KEY = doubleKey("doubleAttribute"); + private static final AttributeKey BOOLEAN_ATTRIBUTE_KEY = booleanKey("booleanAttribute"); private final TracerSdk tracerSdk = OpenTelemetrySdk.getTracerProvider().get("benchmarkTracer"); @Setup(Level.Trial) @@ -77,11 +84,11 @@ private void doWork() { .setAttribute("key", "value") .addLink(new TestLink()) .startSpan(); - span.addEvent("started", Attributes.of(stringKey("operation"), "some_work")); - span.setAttribute("longAttribute", 33L); - span.setAttribute("stringAttribute", "test_value"); - span.setAttribute("doubleAttribute", 4844.44d); - span.setAttribute("booleanAttribute", false); + span.addEvent("started", Attributes.of(OPERATION_KEY, "some_work")); + span.setAttribute(LONG_ATTRIBUTE_KEY, 33L); + span.setAttribute(STRING_ATTRIBUTE_KEY, "test_value"); + span.setAttribute(DOUBLE_ATTRIBUTE_KEY, 4844.44d); + span.setAttribute(BOOLEAN_ATTRIBUTE_KEY, false); span.setStatus(Status.OK); span.addEvent("testEvent"); From 77f0fb5698d16d1f0e05f0fba0261b45e423b51e Mon Sep 17 00:00:00 2001 From: jwatson Date: Mon, 14 Sep 2020 09:23:05 -0700 Subject: [PATCH 17/20] Add javadoc notes to encourage using the lower-overhead attribute options. --- .../io/opentelemetry/common/Attributes.java | 24 +++++++++++++++++++ .../java/io/opentelemetry/trace/Span.java | 24 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/api/src/main/java/io/opentelemetry/common/Attributes.java b/api/src/main/java/io/opentelemetry/common/Attributes.java index b67f3dd09ab..8dd83e97047 100644 --- a/api/src/main/java/io/opentelemetry/common/Attributes.java +++ b/api/src/main/java/io/opentelemetry/common/Attributes.java @@ -227,6 +227,9 @@ public Builder setAttribute(AttributeKey key, T value) { /** * Sets a String attribute into this. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @return this Builder */ public Builder setAttribute(String key, String value) { @@ -236,6 +239,9 @@ public Builder setAttribute(String key, String value) { /** * Sets a long attribute into this. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @return this Builder */ public Builder setAttribute(String key, long value) { @@ -245,6 +251,9 @@ public Builder setAttribute(String key, long value) { /** * Sets a double attribute into this. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @return this Builder */ public Builder setAttribute(String key, double value) { @@ -254,6 +263,9 @@ public Builder setAttribute(String key, double value) { /** * Sets a boolean attribute into this. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @return this Builder */ public Builder setAttribute(String key, boolean value) { @@ -263,6 +275,9 @@ public Builder setAttribute(String key, boolean value) { /** * Sets a String array attribute into this. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @return this Builder */ public Builder setAttribute(String key, String... value) { @@ -272,6 +287,9 @@ public Builder setAttribute(String key, String... value) { /** * Sets a Long array attribute into this. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @return this Builder */ public Builder setAttribute(String key, Long... value) { @@ -281,6 +299,9 @@ public Builder setAttribute(String key, Long... value) { /** * Sets a Double array attribute into this. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @return this Builder */ public Builder setAttribute(String key, Double... value) { @@ -290,6 +311,9 @@ public Builder setAttribute(String key, Double... value) { /** * Sets a Boolean array attribute into this. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @return this Builder */ public Builder setAttribute(String key, Boolean... value) { diff --git a/api/src/main/java/io/opentelemetry/trace/Span.java b/api/src/main/java/io/opentelemetry/trace/Span.java index 7e8fabcfc81..4fbbe3ff093 100644 --- a/api/src/main/java/io/opentelemetry/trace/Span.java +++ b/api/src/main/java/io/opentelemetry/trace/Span.java @@ -88,6 +88,9 @@ enum Kind { *

If a null or empty String {@code value} is passed in, the attribute will be silently * dropped. Note: this behavior could change in the future. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @param key the key for this attribute. * @param value the value for this attribute. * @since 0.1.0 @@ -98,6 +101,9 @@ enum Kind { * Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for * the key, the old value is replaced by the specified value. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @param key the key for this attribute. * @param value the value for this attribute. * @since 0.1.0 @@ -108,6 +114,9 @@ enum Kind { * Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for * the key, the old value is replaced by the specified value. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @param key the key for this attribute. * @param value the value for this attribute. * @since 0.1.0 @@ -118,6 +127,9 @@ enum Kind { * Sets an attribute to the {@code Span}. If the {@code Span} previously contained a mapping for * the key, the old value is replaced by the specified value. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @param key the key for this attribute. * @param value the value for this attribute. * @since 0.1.0 @@ -501,6 +513,9 @@ interface Builder { *

If a null or empty String {@code value} is passed in, the attribute will be silently * dropped. Note: this behavior could change in the future. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @param key the key for this attribute. * @param value the value for this attribute. * @return this. @@ -513,6 +528,9 @@ interface Builder { * Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} previously * contained a mapping for the key, the old value is replaced by the specified value. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @param key the key for this attribute. * @param value the value for this attribute. * @return this. @@ -525,6 +543,9 @@ interface Builder { * Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} previously * contained a mapping for the key, the old value is replaced by the specified value. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @param key the key for this attribute. * @param value the value for this attribute. * @return this. @@ -537,6 +558,9 @@ interface Builder { * Sets an attribute to the newly created {@code Span}. If {@code Span.Builder} previously * contained a mapping for the key, the old value is replaced by the specified value. * + *

Note: It is strongly recommended to use {@link #setAttribute(AttributeKey, Object)}, and + * pre-allocate your keys, if possible. + * * @param key the key for this attribute. * @param value the value for this attribute. * @return this. From 8f1f871588e911f7d2bae7de0594c2d162b6d4e9 Mon Sep 17 00:00:00 2001 From: jwatson Date: Mon, 14 Sep 2020 10:57:21 -0700 Subject: [PATCH 18/20] Add javadoc clarifying subclass responsibilities around empty keys. --- .../java/io/opentelemetry/common/ImmutableKeyValuePairs.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java index 37faf56aeb3..1653aef7bd6 100644 --- a/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java +++ b/api/src/main/java/io/opentelemetry/common/ImmutableKeyValuePairs.java @@ -29,6 +29,10 @@ * *

Key-value pairs are dropped for {@code null} or empty keys. * + *

Note: for subclasses of this, null keys will be removed, but if your key has another concept + * of being "empty", you'll need to remove them before calling {@link #sortAndFilter(Object[])}, + * assuming you don't want the "empty" keys to be kept in your collection. + * * @param The type of the values contained in this. * @see Labels * @see Attributes From dc86d829b11e2441d6d2668e004b1588edca8949 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 15 Sep 2020 08:09:02 -0700 Subject: [PATCH 19/20] make the compareTo on AttributeKeyImpl null-safe, for extra safety --- .../main/java/io/opentelemetry/common/AttributeKeyImpl.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java index 5d97f6f5743..40927eb594c 100644 --- a/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java +++ b/api/src/main/java/io/opentelemetry/common/AttributeKeyImpl.java @@ -52,6 +52,12 @@ public final int hashCode() { @Override public int compareTo(AttributeKey o) { + if (getKey() == null) { + return o.getKey() == null ? 0 : -1; + } + if (o.getKey() == null) { + return 1; + } return getKey().compareTo(o.getKey()); } } From 40f54ae9431320a61fa256b1fbf6cbf4b5073d1a Mon Sep 17 00:00:00 2001 From: john Date: Mon, 21 Sep 2020 08:32:14 -0700 Subject: [PATCH 20/20] fix formatting --- .../extensions/trace/jaeger/sampler/RateLimitingSampler.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java index 1ff726b3b0d..7edc1f847e1 100644 --- a/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java +++ b/sdk_extensions/jaeger_remote_sampler/src/main/java/io/opentelemetry/sdk/extensions/trace/jaeger/sampler/RateLimitingSampler.java @@ -53,9 +53,7 @@ class RateLimitingSampler implements Sampler { double maxBalance = maxTracesPerSecond < 1.0 ? 1.0 : maxTracesPerSecond; this.rateLimiter = new RateLimiter(maxTracesPerSecond, maxBalance, MillisClock.getInstance()); Attributes attributes = - Attributes.of( - SAMPLER_TYPE, TYPE, - SAMPLER_PARAM, (double) maxTracesPerSecond); + Attributes.of(SAMPLER_TYPE, TYPE, SAMPLER_PARAM, (double) maxTracesPerSecond); this.onSamplingResult = Samplers.samplingResult(Decision.RECORD_AND_SAMPLE, attributes); this.offSamplingResult = Samplers.samplingResult(Decision.DROP, attributes); }