Skip to content

Commit 3cb5e4e

Browse files
committed
Avoid shared internal code in sdk-common and api-incubator (#6970)
1 parent 0d0b0ca commit 3cb5e4e

4 files changed

Lines changed: 41 additions & 9 deletions

File tree

‎all/src/test/java/io/opentelemetry/all/NoSharedInternalCodeTest.java‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,12 @@ class NoSharedInternalCodeTest {
3333

3434
private static final Set<String> exemptions =
3535
Set.of(
36-
"opentelemetry-api-incubator",
3736
"opentelemetry-exporter-common",
3837
"opentelemetry-exporter-logging",
3938
"opentelemetry-exporter-logging-otlp",
4039
"opentelemetry-exporter-prometheus",
4140
"opentelemetry-extension-trace-propagators",
4241
"opentelemetry-opencensus-shim",
43-
"opentelemetry-sdk-common",
4442
"opentelemetry-sdk-logs",
4543
"opentelemetry-sdk-metrics",
4644
"opentelemetry-sdk-profiles",

‎sdk/common/src/main/java/io/opentelemetry/sdk/common/export/RetryPolicy.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
package io.opentelemetry.sdk.common.export;
77

8-
import static io.opentelemetry.api.internal.Utils.checkArgument;
9-
108
import com.google.auto.value.AutoValue;
119
import java.io.IOException;
1210
import java.time.Duration;
@@ -133,5 +131,11 @@ public RetryPolicy build() {
133131

134132
return retryPolicy;
135133
}
134+
135+
private static void checkArgument(boolean isValid, String errorMessage) {
136+
if (!isValid) {
137+
throw new IllegalArgumentException(errorMessage);
138+
}
139+
}
136140
}
137141
}

‎sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import io.opentelemetry.api.common.AttributeKey;
1010
import io.opentelemetry.api.common.Attributes;
1111
import io.opentelemetry.api.common.AttributesBuilder;
12-
import io.opentelemetry.api.internal.StringUtils;
13-
import io.opentelemetry.api.internal.Utils;
1412
import io.opentelemetry.sdk.common.internal.OtelVersion;
1513
import java.util.Objects;
1614
import java.util.logging.Logger;
@@ -177,8 +175,9 @@ public Resource merge(@Nullable Resource other) {
177175
private static void checkAttributes(Attributes attributes) {
178176
attributes.forEach(
179177
(key, value) -> {
180-
Utils.checkArgument(
181-
isValidAndNotEmpty(key), "Attribute key" + ERROR_MESSAGE_INVALID_CHARS);
178+
if (!isValidAndNotEmpty(key)) {
179+
throw new IllegalArgumentException("Attribute key" + ERROR_MESSAGE_INVALID_CHARS);
180+
}
182181
Objects.requireNonNull(value, "Attribute value" + ERROR_MESSAGE_INVALID_VALUE);
183182
});
184183
}
@@ -191,7 +190,17 @@ private static void checkAttributes(Attributes attributes) {
191190
* @return whether the name is valid.
192191
*/
193192
private static boolean isValid(String name) {
194-
return name.length() <= MAX_LENGTH && StringUtils.isPrintableString(name);
193+
return name.length() <= MAX_LENGTH && isPrintableString(name);
194+
}
195+
196+
private static boolean isPrintableString(String str) {
197+
for (int i = 0; i < str.length(); i++) {
198+
char ch = str.charAt(i);
199+
if (ch < ' ' || ch > '~') {
200+
return false;
201+
}
202+
}
203+
return true;
195204
}
196205

197206
/**

‎sdk/common/src/test/java/io/opentelemetry/sdk/resources/ResourceTest.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static io.opentelemetry.api.common.AttributeKey.valueKey;
1717
import static java.util.Collections.singletonList;
1818
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1920

2021
import com.google.common.testing.EqualsTester;
2122
import io.opentelemetry.api.common.AttributeKey;
@@ -187,6 +188,26 @@ void create_NullEmptyValue() {
187188
assertThat(resource.getAttributes().size()).isEqualTo(1);
188189
}
189190

191+
@Test
192+
void create_invalidAttributeKey() {
193+
assertThatThrownBy(() -> Resource.create(Attributes.of(stringKey("\u0002ab"), "value")))
194+
.isInstanceOf(IllegalArgumentException.class)
195+
.hasMessageContaining("Attribute key should be a ASCII string");
196+
197+
assertThatThrownBy(() -> Resource.create(Attributes.of(stringKey("key\u007f"), "value")))
198+
.isInstanceOf(IllegalArgumentException.class)
199+
.hasMessageContaining("Attribute key should be a ASCII string");
200+
201+
String tooLongKey = new String(new char[256]).replace('\0', 'a');
202+
assertThatThrownBy(() -> Resource.create(Attributes.of(stringKey(tooLongKey), "value")))
203+
.isInstanceOf(IllegalArgumentException.class)
204+
.hasMessageContaining("Attribute key should be a ASCII string");
205+
206+
String maxValidKey = new String(new char[255]).replace('\0', 'a');
207+
Resource resource = Resource.create(Attributes.of(stringKey(maxValidKey), "value"));
208+
assertThat(resource.getAttribute(stringKey(maxValidKey))).isEqualTo("value");
209+
}
210+
190211
@Test
191212
void testResourceEquals() {
192213
Attributes attribute1 = Attributes.of(stringKey("a"), "1", stringKey("b"), "2");

0 commit comments

Comments
 (0)