Skip to content

Add context and severity params to ExtendedLogger#isEnabled #7268

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -26,6 +26,11 @@ static Logger getNoop() {
return INSTANCE;
}

@Override
public boolean isEnabled(Severity severity, Context context) {
return false;
}

@Override
public ExtendedLogRecordBuilder logRecordBuilder() {
return NOOP_LOG_RECORD_BUILDER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,41 @@
package io.opentelemetry.api.incubator.logs;

import io.opentelemetry.api.logs.Logger;
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.context.Context;

/** Extended {@link Logger} with experimental APIs. */
public interface ExtendedLogger extends Logger {

/**
* Returns {@code true} if the logger is enabled.
* Returns {@code true} if the logger is enabled for the given {@code context} and {@code
* severity}.
*
* <p>This allows callers to avoid unnecessary compute when nothing is consuming the data. Because
* the response is subject to change over the application, callers should call this before each
* call to {@link #logRecordBuilder()}.
*/
default boolean isEnabled() {
default boolean isEnabled(Severity severity, Context context) {
return true;
}

/** Overload of {@link #isEnabled(Severity, Context)} assuming {@link Context#current()}. */
default boolean isEnabled(Severity severity) {
return isEnabled(severity, Context.current());
Copy link
Member Author

Choose a reason for hiding this comment

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

The spec says this should accept Severity and Context parameters, but since we have implicit context in java, I think the most common case will be to use that. So I've included an overload where the context is omitted.

}

/**
* Overload of {@link #isEnabled(Severity, Context)} assuming {@link
* Severity#UNDEFINED_SEVERITY_NUMBER} and {@link Context#current()}.
*
* @deprecated for removal after 1.52.0. Use {@link #isEnabled(Severity, Context)} or {@link
* #isEnabled(Severity)} instead.
*/
@Deprecated
default boolean isEnabled() {
return isEnabled(Severity.UNDEFINED_SEVERITY_NUMBER);
}

@Override
ExtendedLogRecordBuilder logRecordBuilder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public BatchCallback batchCallback(
private ExtendedDefaultMeter() {}

private static class NoopLongCounter implements ExtendedLongCounter {
@Override
public boolean isEnabled() {
return false;
}

@Override
public void add(long value, Attributes attributes, Context context) {}

Expand All @@ -105,6 +110,11 @@ public void add(long value) {}
}

private static class NoopDoubleCounter implements ExtendedDoubleCounter {
@Override
public boolean isEnabled() {
return false;
}

@Override
public void add(double value, Attributes attributes, Context context) {}

Expand Down Expand Up @@ -186,6 +196,11 @@ public ObservableDoubleMeasurement buildObserver() {
}

private static class NoopLongUpDownCounter implements ExtendedLongUpDownCounter {
@Override
public boolean isEnabled() {
return false;
}

@Override
public void add(long value, Attributes attributes, Context context) {}

Expand All @@ -197,6 +212,11 @@ public void add(long value) {}
}

private static class NoopDoubleUpDownCounter implements ExtendedDoubleUpDownCounter {
@Override
public boolean isEnabled() {
return false;
}

@Override
public void add(double value, Attributes attributes, Context context) {}

Expand Down Expand Up @@ -281,6 +301,11 @@ public ObservableDoubleMeasurement buildObserver() {
}

private static class NoopDoubleHistogram implements ExtendedDoubleHistogram {
@Override
public boolean isEnabled() {
return false;
}

@Override
public void record(double value, Attributes attributes, Context context) {}

Expand All @@ -292,6 +317,11 @@ public void record(double value) {}
}

private static class NoopLongHistogram implements ExtendedLongHistogram {
@Override
public boolean isEnabled() {
return false;
}

@Override
public void record(long value, Attributes attributes, Context context) {}

Expand Down Expand Up @@ -385,6 +415,11 @@ public DoubleGauge build() {
}

private static class NoopDoubleGauge implements ExtendedDoubleGauge {
@Override
public boolean isEnabled() {
return false;
}

@Override
public void set(double value) {}

Expand Down Expand Up @@ -426,6 +461,11 @@ public LongGauge build() {
}

private static class NoopLongGauge implements ExtendedLongGauge {
@Override
public boolean isEnabled() {
return false;
}

@Override
public void set(long value) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ static Tracer getNoop() {
return INSTANCE;
}

@Override
public boolean isEnabled() {
return false;
}

@Override
public ExtendedSpanBuilder spanBuilder(String spanName) {
return NoopSpanBuilder.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import io.opentelemetry.api.common.Value;
import io.opentelemetry.api.logs.Logger;
import io.opentelemetry.api.logs.LoggerProvider;
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.api.testing.internal.AbstractDefaultLoggerTest;
import io.opentelemetry.context.Context;
import org.junit.jupiter.api.Test;

class ExtendedDefaultLoggerTest extends AbstractDefaultLoggerTest {
Expand All @@ -26,10 +28,18 @@ protected Logger getLogger() {
}

@Test
@SuppressWarnings("deprecation") // testing deprecated code
void incubatingApiIsLoaded() {
Logger logger = LoggerProvider.noop().get("test");

assertThat(logger).isInstanceOf(ExtendedLogger.class);
assertThat(logger)
.isInstanceOfSatisfying(
ExtendedLogger.class,
extendedLogger -> {
assertThat(extendedLogger.isEnabled(Severity.ERROR, Context.current())).isFalse();
assertThat(extendedLogger.isEnabled(Severity.ERROR)).isFalse();
assertThat(extendedLogger.isEnabled()).isFalse();
});
ExtendedLogRecordBuilder builder = (ExtendedLogRecordBuilder) logger.logRecordBuilder();
assertThat(builder).isInstanceOf(ExtendedLogRecordBuilder.class);
assertThat(builder.setBody(Value.of(0))).isSameAs(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.logs.Severity;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor;
Expand Down Expand Up @@ -42,24 +43,26 @@ void loggerEnabled() {
ExtendedLogger loggerB = (ExtendedLogger) loggerProvider.get("loggerB");

// Check if logger is enabled before emitting log and avoid unnecessary computation
if (loggerA.isEnabled()) {
if (loggerA.isEnabled(Severity.INFO)) {
loggerA
.logRecordBuilder()
.setSeverity(Severity.INFO)
.setBody("hello world!")
.setAllAttributes(Attributes.builder().put("result", flipCoin()).build())
.emit();
}
if (loggerB.isEnabled()) {
if (loggerB.isEnabled(Severity.INFO)) {
loggerB
.logRecordBuilder()
.setSeverity(Severity.INFO)
.setBody("hello world!")
.setAllAttributes(Attributes.builder().put("result", flipCoin()).build())
.emit();
}

// loggerA is enabled, loggerB is disabled
assertThat(loggerA.isEnabled()).isTrue();
assertThat(loggerB.isEnabled()).isFalse();
assertThat(loggerA.isEnabled(Severity.INFO)).isTrue();
assertThat(loggerB.isEnabled(Severity.INFO)).isFalse();

// Collected data only consists of logs from loggerA. Note, loggerB's logs would be
// omitted from the results even if logs were emitted. The check if enabled simply avoids
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.MeterProvider;
import io.opentelemetry.api.testing.internal.AbstractDefaultMeterTest;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;

class ExtendedDefaultMeterTest extends AbstractDefaultMeterTest {
Expand All @@ -27,44 +26,53 @@ protected MeterProvider getMeterProvider() {
}

@Test
public void incubatingApiIsLoaded() {
void incubatingApiIsLoaded() {
Meter meter = MeterProvider.noop().get("test");
assertThat(meter).isSameAs(OpenTelemetry.noop().getMeter("test"));

Assertions.assertThat(meter.gaugeBuilder("test").ofLongs())
.isInstanceOf(ExtendedLongGaugeBuilder.class);
Assertions.assertThat(meter.gaugeBuilder("test").ofLongs().build())
.isInstanceOf(ExtendedLongGauge.class);
Assertions.assertThat(meter.gaugeBuilder("test"))
.isInstanceOf(ExtendedDoubleGaugeBuilder.class);
Assertions.assertThat(meter.gaugeBuilder("test").build())
.isInstanceOf(ExtendedDoubleGauge.class);
assertThat(meter.gaugeBuilder("test").ofLongs()).isInstanceOf(ExtendedLongGaugeBuilder.class);
assertThat(meter.gaugeBuilder("test").ofLongs().build())
.isInstanceOfSatisfying(
ExtendedLongGauge.class, instrument -> assertThat(instrument.isEnabled()).isFalse());
assertThat(meter.gaugeBuilder("test")).isInstanceOf(ExtendedDoubleGaugeBuilder.class);
assertThat(meter.gaugeBuilder("test").build())
.isInstanceOfSatisfying(
ExtendedDoubleGauge.class, instrument -> assertThat(instrument.isEnabled()).isFalse());

Assertions.assertThat(meter.histogramBuilder("test").ofLongs())
assertThat(meter.histogramBuilder("test").ofLongs())
.isInstanceOf(ExtendedLongHistogramBuilder.class);
Assertions.assertThat(meter.histogramBuilder("test").ofLongs().build())
.isInstanceOf(ExtendedLongHistogram.class);
Assertions.assertThat(meter.histogramBuilder("test"))
.isInstanceOf(ExtendedDoubleHistogramBuilder.class);
Assertions.assertThat(meter.histogramBuilder("test").build())
.isInstanceOf(ExtendedDoubleHistogram.class);
assertThat(meter.histogramBuilder("test").ofLongs().build())
.isInstanceOfSatisfying(
ExtendedLongHistogram.class,
instrument -> assertThat(instrument.isEnabled()).isFalse());
assertThat(meter.histogramBuilder("test")).isInstanceOf(ExtendedDoubleHistogramBuilder.class);
assertThat(meter.histogramBuilder("test").build())
.isInstanceOfSatisfying(
ExtendedDoubleHistogram.class,
instrument -> assertThat(instrument.isEnabled()).isFalse());

Assertions.assertThat(meter.counterBuilder("test"))
.isInstanceOf(ExtendedLongCounterBuilder.class);
Assertions.assertThat(meter.counterBuilder("test").build())
.isInstanceOf(ExtendedLongCounter.class);
Assertions.assertThat(meter.counterBuilder("test").ofDoubles())
assertThat(meter.counterBuilder("test")).isInstanceOf(ExtendedLongCounterBuilder.class);
assertThat(meter.counterBuilder("test").build())
.isInstanceOfSatisfying(
ExtendedLongCounter.class, instrument -> assertThat(instrument.isEnabled()).isFalse());
assertThat(meter.counterBuilder("test").ofDoubles())
.isInstanceOf(ExtendedDoubleCounterBuilder.class);
Assertions.assertThat(meter.counterBuilder("test").ofDoubles().build())
.isInstanceOf(ExtendedDoubleCounter.class);
assertThat(meter.counterBuilder("test").ofDoubles().build())
.isInstanceOfSatisfying(
ExtendedDoubleCounter.class,
instrument -> assertThat(instrument.isEnabled()).isFalse());

Assertions.assertThat(meter.upDownCounterBuilder("test"))
assertThat(meter.upDownCounterBuilder("test"))
.isInstanceOf(ExtendedLongUpDownCounterBuilder.class);
Assertions.assertThat(meter.upDownCounterBuilder("test").build())
.isInstanceOf(ExtendedLongUpDownCounter.class);
Assertions.assertThat(meter.upDownCounterBuilder("test").ofDoubles())
assertThat(meter.upDownCounterBuilder("test").build())
.isInstanceOfSatisfying(
ExtendedLongUpDownCounter.class,
instrument -> assertThat(instrument.isEnabled()).isFalse());
assertThat(meter.upDownCounterBuilder("test").ofDoubles())
.isInstanceOf(ExtendedDoubleUpDownCounterBuilder.class);
Assertions.assertThat(meter.upDownCounterBuilder("test").ofDoubles().build())
.isInstanceOf(ExtendedDoubleUpDownCounter.class);
assertThat(meter.upDownCounterBuilder("test").ofDoubles().build())
.isInstanceOfSatisfying(
ExtendedDoubleUpDownCounter.class,
instrument -> assertThat(instrument.isEnabled()).isFalse());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package io.opentelemetry.api.incubator.trace;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.testing.internal.AbstractDefaultTracerTest;
Expand All @@ -27,39 +29,42 @@ public TracerProvider getTracerProvider() {
}

@Test
public void incubatingApiIsLoaded() {
void incubatingApiIsLoaded() {
Tracer tracer = TracerProvider.noop().get("test");
assertThat(tracer).isSameAs(OpenTelemetry.noop().getTracer("test"));

assertThat(tracer).isInstanceOf(ExtendedTracer.class);
assertThat(tracer)
.isInstanceOfSatisfying(
ExtendedTracer.class,
extendedTracer -> assertThat(extendedTracer.isEnabled()).isFalse());
assertThat(tracer.spanBuilder("test")).isInstanceOf(ExtendedSpanBuilder.class);
}

@SuppressWarnings("unchecked")
@Test
public void incubatingApi() {
void incubatingApi() {
ExtendedSpanBuilder spanBuilder =
(ExtendedSpanBuilder) ExtendedDefaultTracer.getNoop().spanBuilder("test");
assertThat(spanBuilder.setParentFrom(null, null)).isSameAs(spanBuilder);

SpanRunnable<RuntimeException> spanRunnable = Mockito.mock(SpanRunnable.class);

spanBuilder.startAndRun(spanRunnable);
Mockito.verify(spanRunnable).runInSpan();
Mockito.reset(spanRunnable);
verify(spanRunnable).runInSpan();
reset(spanRunnable);

spanBuilder.startAndRun(spanRunnable, null);
Mockito.verify(spanRunnable).runInSpan();
Mockito.reset(spanRunnable);
verify(spanRunnable).runInSpan();
reset(spanRunnable);

SpanCallable<String, RuntimeException> spanCallable = Mockito.mock(SpanCallable.class);

spanBuilder.startAndCall(spanCallable);
Mockito.verify(spanCallable).callInSpan();
Mockito.reset(spanCallable);
verify(spanCallable).callInSpan();
reset(spanCallable);

spanBuilder.startAndCall(spanCallable, null);
Mockito.verify(spanCallable).callInSpan();
Mockito.reset(spanCallable);
verify(spanCallable).callInSpan();
reset(spanCallable);
}
}
Loading
Loading