Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove validations for noop instrument names and units #5146

Merged
merged 7 commits into from
Mar 8, 2023
Merged
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 @@ -6,7 +6,7 @@
package io.opentelemetry.api.baggage;

import com.google.auto.value.AutoValue;
import io.opentelemetry.api.internal.ValidationUtil;
import io.opentelemetry.api.internal.ApiUsageLogger;
import javax.annotation.concurrent.Immutable;

@Immutable
Expand All @@ -25,7 +25,7 @@ abstract class ImmutableEntryMetadata implements BaggageEntryMetadata {
*/
static ImmutableEntryMetadata create(String metadata) {
if (metadata == null) {
ValidationUtil.log("metadata is null");
ApiUsageLogger.log("metadata is null");
return EMPTY;
}
return new AutoValue_ImmutableEntryMetadata(metadata);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.internal;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Helper for API misuse logging.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public final class ApiUsageLogger {

public static final String LOGGER_NAME = "io.opentelemetry.ApiUsageLogger";

private static final Logger API_USAGE_LOGGER = Logger.getLogger(LOGGER_NAME);

/**
* Log the {@code message} to the {@link #LOGGER_NAME API Usage Logger}.
*
* <p>Log at {@link Level#FINEST} and include a stack trace.
*/
public static void log(String message) {
log(message, Level.FINEST);
}

/**
* Log the {@code message} to the {@link #LOGGER_NAME API Usage Logger}.
*
* <p>Log includes a stack trace.
*/
public static void log(String message, Level level) {
if (API_USAGE_LOGGER.isLoggable(level)) {
API_USAGE_LOGGER.log(level, message, new AssertionError());
}
}

private ApiUsageLogger() {}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry.api.metrics;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.internal.ValidationUtil;
import io.opentelemetry.context.Context;
import java.util.function.Consumer;
import javax.annotation.concurrent.ThreadSafe;
Expand Down Expand Up @@ -39,25 +38,21 @@ static Meter getInstance() {

@Override
public LongCounterBuilder counterBuilder(String name) {
ValidationUtil.checkValidInstrumentName(name);
return NOOP_LONG_COUNTER_BUILDER;
}

@Override
public LongUpDownCounterBuilder upDownCounterBuilder(String name) {
ValidationUtil.checkValidInstrumentName(name);
return NOOP_LONG_UP_DOWN_COUNTER_BUILDER;
}

@Override
public DoubleHistogramBuilder histogramBuilder(String name) {
ValidationUtil.checkValidInstrumentName(name);
return NOOP_DOUBLE_HISTOGRAM_BUILDER;
}

@Override
public DoubleGaugeBuilder gaugeBuilder(String name) {
ValidationUtil.checkValidInstrumentName(name);
return NOOP_DOUBLE_GAUGE_BUILDER;
}

Expand Down Expand Up @@ -107,7 +102,6 @@ public LongCounterBuilder setDescription(String description) {

@Override
public LongCounterBuilder setUnit(String unit) {
ValidationUtil.checkValidInstrumentUnit(unit);
return this;
}

Expand Down Expand Up @@ -144,7 +138,6 @@ public DoubleCounterBuilder setDescription(String description) {

@Override
public DoubleCounterBuilder setUnit(String unit) {
ValidationUtil.checkValidInstrumentUnit(unit);
return this;
}

Expand Down Expand Up @@ -201,7 +194,6 @@ public LongUpDownCounterBuilder setDescription(String description) {

@Override
public LongUpDownCounterBuilder setUnit(String unit) {
ValidationUtil.checkValidInstrumentUnit(unit);
return this;
}

Expand Down Expand Up @@ -240,7 +232,6 @@ public DoubleUpDownCounterBuilder setDescription(String description) {

@Override
public DoubleUpDownCounterBuilder setUnit(String unit) {
ValidationUtil.checkValidInstrumentUnit(unit);
return this;
}

Expand Down Expand Up @@ -295,7 +286,6 @@ public DoubleHistogramBuilder setDescription(String description) {

@Override
public DoubleHistogramBuilder setUnit(String unit) {
ValidationUtil.checkValidInstrumentUnit(unit);
return this;
}

Expand All @@ -320,7 +310,6 @@ public LongHistogramBuilder setDescription(String description) {

@Override
public LongHistogramBuilder setUnit(String unit) {
ValidationUtil.checkValidInstrumentUnit(unit);
return this;
}

Expand All @@ -341,7 +330,6 @@ public DoubleGaugeBuilder setDescription(String description) {

@Override
public DoubleGaugeBuilder setUnit(String unit) {
ValidationUtil.checkValidInstrumentUnit(unit);
return this;
}

Expand Down Expand Up @@ -371,7 +359,6 @@ public LongGaugeBuilder setDescription(String description) {

@Override
public LongGaugeBuilder setUnit(String unit) {
ValidationUtil.checkValidInstrumentUnit(unit);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These validation utils can be moved to the SDK now. checkInstrumentName probably makes most sense in SdkMeter. checkValidInstrumentUnit should be removed given that the spec has clarified that SDKs aren't supposed to do any validation on unit. You can do that removal in this PR if you want, or just move the method to AbstractInstrumentBuilder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumping this @breedx-splk.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I moved checkValidInstrumentName() to a new internal utility class in the sdk, rather than SdkMeter. This seemed a little more SRPy to me.

I did move the unit check over to the AbstractInstrumentBuilder class, and I think removal of that should be tackled in a separate PR.

Once those were moved out of the validator, the only things that were left inValidationUtil were the logging methods, so I renamed it to ApiUsageLogger.

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.internal.ValidationUtil;
import io.opentelemetry.api.internal.ApiUsageLogger;
import io.opentelemetry.context.Context;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -50,7 +50,7 @@ public Span startSpan() {
@Override
public NoopSpanBuilder setParent(Context context) {
if (context == null) {
ValidationUtil.log("context is null");
ApiUsageLogger.log("context is null");
return this;
}
spanContext = Span.fromContext(context).getSpanContext();
Expand Down
8 changes: 4 additions & 4 deletions api/all/src/main/java/io/opentelemetry/api/trace/Span.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.internal.ValidationUtil;
import io.opentelemetry.api.internal.ApiUsageLogger;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ImplicitContextKeyed;
import java.time.Instant;
Expand Down Expand Up @@ -43,7 +43,7 @@ static Span current() {
*/
static Span fromContext(Context context) {
if (context == null) {
ValidationUtil.log("context is null");
ApiUsageLogger.log("context is null");
return Span.getInvalid();
}
Span span = context.get(SpanContextKey.KEY);
Expand All @@ -57,7 +57,7 @@ static Span fromContext(Context context) {
@Nullable
static Span fromContextOrNull(Context context) {
if (context == null) {
ValidationUtil.log("context is null");
ApiUsageLogger.log("context is null");
return null;
}
return context.get(SpanContextKey.KEY);
Expand All @@ -78,7 +78,7 @@ static Span getInvalid() {
*/
static Span wrap(SpanContext spanContext) {
if (spanContext == null) {
ValidationUtil.log("context is null");
ApiUsageLogger.log("context is null");
return getInvalid();
}
if (!spanContext.isValid()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

package io.opentelemetry.api.trace;

import io.opentelemetry.api.internal.ApiUsageLogger;
import io.opentelemetry.api.internal.OtelEncodingUtils;
import io.opentelemetry.api.internal.TemporaryBuffers;
import io.opentelemetry.api.internal.ValidationUtil;
import javax.annotation.concurrent.Immutable;

/**
Expand Down Expand Up @@ -73,7 +73,7 @@ public static boolean isValid(CharSequence spanId) {
*/
public static String fromBytes(byte[] spanIdBytes) {
if (spanIdBytes == null || spanIdBytes.length < BYTES_LENGTH) {
ValidationUtil.log("spanIdBytes is null or too short");
ApiUsageLogger.log("spanIdBytes is null or too short");
return INVALID;
}
char[] result = TemporaryBuffers.chars(HEX_LENGTH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

package io.opentelemetry.api.trace;

import io.opentelemetry.api.internal.ApiUsageLogger;
import io.opentelemetry.api.internal.OtelEncodingUtils;
import io.opentelemetry.api.internal.TemporaryBuffers;
import io.opentelemetry.api.internal.ValidationUtil;
import javax.annotation.concurrent.Immutable;

/**
Expand Down Expand Up @@ -77,7 +77,7 @@ public static boolean isValid(CharSequence traceId) {
*/
public static String fromBytes(byte[] traceIdBytes) {
if (traceIdBytes == null || traceIdBytes.length < BYTES_LENGTH) {
ValidationUtil.log("traceIdBytes is null or too short");
ApiUsageLogger.log("traceIdBytes is null or too short");
return INVALID;
}
char[] result = TemporaryBuffers.chars(HEX_LENGTH);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.api.internal;

import static java.util.logging.Level.WARNING;

import io.github.netmikey.logunit.api.LogCapturer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

class ApiUsageLoggerTest {

@RegisterExtension
LogCapturer apiUsageLogs = LogCapturer.create().captureForLogger(ApiUsageLogger.LOGGER_NAME);

@Test
void log() {
ApiUsageLogger.log("thing", WARNING);
apiUsageLogs.assertContains("thing");
}
}
Loading