Remove version of checkArgument not used (#2213)
This PR fixes an inconsistency that MaxLengthOfAttributeValues was allowed to be set to 0, then thrown internal exception. We should check for arguments only at public APIs. Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
This commit is contained in:
parent
50c8f1f5c6
commit
69208f8f79
|
@ -27,77 +27,6 @@ public final class Utils {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an {@link IllegalArgumentException} if the argument is false. This method is similar to
|
||||
* {@code Preconditions.checkArgument(boolean, Object)} from Guava.
|
||||
*
|
||||
* @param expression a boolean expression
|
||||
* @param errorMessageTemplate a template for the exception message should the check fail. The
|
||||
* message is formed by replacing each {@code %s} placeholder in the template with an
|
||||
* argument. These are matched by position - the first {@code %s} gets {@code
|
||||
* errorMessageArgs[0]}, etc. Unmatched arguments will be appended to the formatted message in
|
||||
* square braces. Unmatched placeholders will be left as-is.
|
||||
* @param errorMessageArgs the arguments to be substituted into the message template. Arguments
|
||||
* are converted to strings using {@link String#valueOf(Object)}.
|
||||
* @throws IllegalArgumentException if {@code expression} is false
|
||||
* @throws NullPointerException if the check fails and either {@code errorMessageTemplate} or
|
||||
* {@code errorMessageArgs} is null (don't let this happen)
|
||||
*/
|
||||
public static void checkArgument(
|
||||
boolean expression, String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
if (!expression) {
|
||||
throw new IllegalArgumentException(format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Substitutes each {@code %s} in {@code template} with an argument. These are matched by
|
||||
* position: the first {@code %s} gets {@code args[0]}, etc. If there are more arguments than
|
||||
* placeholders, the unmatched arguments will be appended to the end of the formatted message in
|
||||
* square braces.
|
||||
*
|
||||
* <p>Copied from {@code Preconditions.format(String, Object...)} from Guava
|
||||
*
|
||||
* @param template a non-null string containing 0 or more {@code %s} placeholders.
|
||||
* @param args the arguments to be substituted into the message template. Arguments are converted
|
||||
* to strings using {@link String#valueOf(Object)}. Arguments can be null.
|
||||
*/
|
||||
// Note that this is somewhat-improperly used from Verify.java as well.
|
||||
private static String format(String template, Object... args) {
|
||||
// If no arguments return the template.
|
||||
if (args.length == 0) {
|
||||
return template;
|
||||
}
|
||||
|
||||
// start substituting the arguments into the '%s' placeholders
|
||||
StringBuilder builder = new StringBuilder(template.length() + 16 * args.length);
|
||||
int templateStart = 0;
|
||||
int i = 0;
|
||||
while (i < args.length) {
|
||||
int placeholderStart = template.indexOf("%s", templateStart);
|
||||
if (placeholderStart == -1) {
|
||||
break;
|
||||
}
|
||||
builder.append(template, templateStart, placeholderStart);
|
||||
builder.append(args[i++]);
|
||||
templateStart = placeholderStart + 2;
|
||||
}
|
||||
builder.append(template, templateStart, template.length());
|
||||
|
||||
// if we run out of placeholders, append the extra args in square braces
|
||||
if (i < args.length) {
|
||||
builder.append(" [");
|
||||
builder.append(args[i++]);
|
||||
while (i < args.length) {
|
||||
builder.append(", ");
|
||||
builder.append(args[i++]);
|
||||
}
|
||||
builder.append(']');
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates that the array of Strings is 1) even in length, and 2) they can be formed into valid
|
||||
* pairs where the first item in the pair is not null.
|
||||
|
|
|
@ -11,10 +11,6 @@ import org.junit.jupiter.api.Test;
|
|||
|
||||
class UtilsTest {
|
||||
private static final String TEST_MESSAGE = "test message";
|
||||
private static final String TEST_MESSAGE_TEMPLATE = "I ate %s eggs.";
|
||||
private static final int TEST_MESSAGE_VALUE = 2;
|
||||
private static final String FORMATTED_SIMPLE_TEST_MESSAGE = "I ate 2 eggs.";
|
||||
private static final String FORMATTED_COMPLEX_TEST_MESSAGE = "I ate 2 eggs. [2]";
|
||||
|
||||
@Test
|
||||
void checkArgument() {
|
||||
|
@ -24,22 +20,4 @@ class UtilsTest {
|
|||
() -> Utils.checkArgument(false, TEST_MESSAGE),
|
||||
TEST_MESSAGE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkArgument_WithSimpleFormat() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> Utils.checkArgument(false, TEST_MESSAGE_TEMPLATE, TEST_MESSAGE_VALUE),
|
||||
FORMATTED_SIMPLE_TEST_MESSAGE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkArgument_WithComplexFormat() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() ->
|
||||
Utils.checkArgument(
|
||||
false, TEST_MESSAGE_TEMPLATE, TEST_MESSAGE_VALUE, TEST_MESSAGE_VALUE),
|
||||
FORMATTED_COMPLEX_TEST_MESSAGE);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ package io.opentelemetry.sdk.trace;
|
|||
|
||||
import io.opentelemetry.api.common.AttributeKey;
|
||||
import io.opentelemetry.api.common.AttributeType;
|
||||
import io.opentelemetry.api.internal.Utils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.annotation.Nullable;
|
||||
|
@ -21,13 +20,9 @@ final class StringUtils {
|
|||
* element truncated to {@code limit} characters.
|
||||
*
|
||||
* <p>Otherwise return given {@code value}
|
||||
*
|
||||
* @throws IllegalArgumentException if limit is zero or negative
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
static <T> T truncateToSize(AttributeKey<T> key, T value, int limit) {
|
||||
Utils.checkArgument(limit > 0, "attribute value limit must be positive, got %d", limit);
|
||||
|
||||
if (value == null
|
||||
|| ((key.getType() != AttributeType.STRING)
|
||||
&& (key.getType() != AttributeType.STRING_ARRAY))) {
|
||||
|
|
|
@ -365,10 +365,11 @@ public abstract class TraceConfig {
|
|||
if (traceConfig.getMaxNumberOfAttributesPerLink() <= 0) {
|
||||
throw new IllegalArgumentException("maxNumberOfAttributesPerLink must be greater than 0");
|
||||
}
|
||||
if (traceConfig.getMaxLengthOfAttributeValues() < -1) {
|
||||
if (traceConfig.getMaxLengthOfAttributeValues() <= 0
|
||||
&& traceConfig.getMaxLengthOfAttributeValues() != UNLIMITED_ATTRIBUTE_LENGTH) {
|
||||
throw new IllegalArgumentException(
|
||||
"maxLengthOfAttributeValues must be -1 to "
|
||||
+ "disable length restriction, or 0 or higher to enable length restriction");
|
||||
+ "disable length restriction, or positive to enable length restriction");
|
||||
}
|
||||
return traceConfig;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue