Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ class NoSharedInternalCodeTest {

private static final Set<String> exemptions =
Set.of(
"opentelemetry-api-incubator",
"opentelemetry-exporter-common",
"opentelemetry-exporter-logging",
"opentelemetry-exporter-logging-otlp",
"opentelemetry-exporter-prometheus",
"opentelemetry-extension-trace-propagators",
"opentelemetry-opencensus-shim",
"opentelemetry-sdk-common",
"opentelemetry-sdk-logs",
"opentelemetry-sdk-metrics",
"opentelemetry-sdk-profiles",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

package io.opentelemetry.sdk.common.export;

import static io.opentelemetry.api.internal.Utils.checkArgument;

import com.google.auto.value.AutoValue;
import java.io.IOException;
import java.time.Duration;
Expand Down Expand Up @@ -133,5 +131,11 @@ public RetryPolicy build() {

return retryPolicy;
}

private static void checkArgument(boolean isValid, String errorMessage) {
if (!isValid) {
throw new IllegalArgumentException(errorMessage);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.sdk.resources.internal.Entity;
import io.opentelemetry.sdk.resources.internal.EntityUtil;
import java.util.ArrayList;
Expand Down Expand Up @@ -441,6 +440,6 @@ private static String deriveSchemaUrl(List<Entity> entities) {

@Nullable
static String normalizeSchemaUrl(@Nullable String schemaUrl) {
return StringUtils.isNullOrEmpty(schemaUrl) ? null : schemaUrl;
return schemaUrl == null || schemaUrl.isEmpty() ? null : schemaUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.api.internal.Utils;
import java.util.Objects;

/**
Expand All @@ -35,8 +33,9 @@ private AttributeCheckUtil() {}
public static void checkAttributes(Attributes attributes) {
attributes.forEach(
(key, value) -> {
Utils.checkArgument(
isValidAndNotEmpty(key), "Attribute key" + ERROR_MESSAGE_INVALID_CHARS);
if (!isValidAndNotEmpty(key)) {
throw new IllegalArgumentException("Attribute key" + ERROR_MESSAGE_INVALID_CHARS);
}
Objects.requireNonNull(value, "Attribute value" + ERROR_MESSAGE_INVALID_VALUE);
});
}
Expand All @@ -60,6 +59,16 @@ public static boolean isValidAndNotEmpty(AttributeKey<?> name) {
* @return whether the name is valid.
*/
public static boolean isValid(String name) {
return name.length() <= MAX_LENGTH && StringUtils.isPrintableString(name);
return name.length() <= MAX_LENGTH && isPrintableString(name);
}

private static boolean isPrintableString(String str) {
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch < ' ' || ch > '~') {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

package io.opentelemetry.sdk.resources.internal;

import static io.opentelemetry.api.internal.Utils.checkArgument;

import io.opentelemetry.api.common.Attributes;
import java.util.Objects;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -64,4 +62,10 @@ public EntityBuilder setDescription(Attributes description) {
public Entity build() {
return SdkEntity.create(entityType, id, description, schemaUrl);
}

private static void checkArgument(boolean isValid, String errorMessage) {
if (!isValid) {
throw new IllegalArgumentException(errorMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,24 @@ void create_NullEmptyValue() {
assertThat(resource.getAttributes().size()).isEqualTo(1);
}

@Test
void create_invalidAttributeKey() {
assertThatThrownBy(() -> Resource.create(Attributes.of(stringKey("\u0002ab"), "value")))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Attribute key should be a ASCII string");

assertThatThrownBy(() -> Resource.create(Attributes.of(stringKey("key\u007f"), "value")))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Attribute key should be a ASCII string");

char[] chars = new char[256];
Arrays.fill(chars, 'a');
String tooLongKey = new String(chars);
assertThatThrownBy(() -> Resource.create(Attributes.of(stringKey(tooLongKey), "value")))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Attribute key should be a ASCII string");
}

@Test
void testResourceEquals() {
Attributes attribute1 = Attributes.of(stringKey("a"), "1", stringKey("b"), "2");
Expand Down
Loading