Skip to content

Commit bf27c62

Browse files
authored
Stabilize enabled api (#8200)
1 parent 2682f15 commit bf27c62

50 files changed

Lines changed: 325 additions & 259 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

api/all/src/main/java/io/opentelemetry/api/logs/DefaultLogger.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ static Logger getInstance() {
2323
return INSTANCE;
2424
}
2525

26+
@Override
27+
public boolean isEnabled(Severity severity, Context context) {
28+
return false;
29+
}
30+
2631
@Override
2732
public LogRecordBuilder logRecordBuilder() {
2833
return NOOP_LOG_RECORD_BUILDER;

api/all/src/main/java/io/opentelemetry/api/logs/Logger.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.api.logs;
77

8+
import io.opentelemetry.context.Context;
89
import javax.annotation.concurrent.ThreadSafe;
910

1011
/**
@@ -22,6 +23,23 @@
2223
@ThreadSafe
2324
public interface Logger {
2425

26+
/**
27+
* Returns {@code true} if the logger is enabled for the given {@code context} and {@code
28+
* severity}.
29+
*
30+
* <p>This allows callers to avoid unnecessary compute when nothing is consuming the data. Because
31+
* the response is subject to change over the application, callers should call this before each
32+
* call to {@link #logRecordBuilder()}.
33+
*/
34+
default boolean isEnabled(Severity severity, Context context) {
35+
return true;
36+
}
37+
38+
/** Overload of {@link #isEnabled(Severity, Context)} assuming {@link Context#current()}. */
39+
default boolean isEnabled(Severity severity) {
40+
return isEnabled(severity, Context.current());
41+
}
42+
2543
/**
2644
* Return a {@link LogRecordBuilder} to emit a log record.
2745
*

api/all/src/main/java/io/opentelemetry/api/metrics/DefaultMeter.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public BatchCallback batchCallback(
6767
private DefaultMeter() {}
6868

6969
private static class NoopLongCounter implements LongCounter {
70+
@Override
71+
public boolean isEnabled() {
72+
return false;
73+
}
74+
7075
@Override
7176
public void add(long value, Attributes attributes, Context context) {}
7277

@@ -78,6 +83,11 @@ public void add(long value) {}
7883
}
7984

8085
private static class NoopDoubleCounter implements DoubleCounter {
86+
@Override
87+
public boolean isEnabled() {
88+
return false;
89+
}
90+
8191
@Override
8292
public void add(double value, Attributes attributes, Context context) {}
8393

@@ -159,6 +169,11 @@ public ObservableDoubleMeasurement buildObserver() {
159169
}
160170

161171
private static class NoopLongUpDownCounter implements LongUpDownCounter {
172+
@Override
173+
public boolean isEnabled() {
174+
return false;
175+
}
176+
162177
@Override
163178
public void add(long value, Attributes attributes, Context context) {}
164179

@@ -170,6 +185,11 @@ public void add(long value) {}
170185
}
171186

172187
private static class NoopDoubleUpDownCounter implements DoubleUpDownCounter {
188+
@Override
189+
public boolean isEnabled() {
190+
return false;
191+
}
192+
173193
@Override
174194
public void add(double value, Attributes attributes, Context context) {}
175195

@@ -253,6 +273,11 @@ public ObservableDoubleMeasurement buildObserver() {
253273
}
254274

255275
private static class NoopDoubleHistogram implements DoubleHistogram {
276+
@Override
277+
public boolean isEnabled() {
278+
return false;
279+
}
280+
256281
@Override
257282
public void record(double value, Attributes attributes, Context context) {}
258283

@@ -264,6 +289,11 @@ public void record(double value) {}
264289
}
265290

266291
private static class NoopLongHistogram implements LongHistogram {
292+
@Override
293+
public boolean isEnabled() {
294+
return false;
295+
}
296+
267297
@Override
268298
public void record(long value, Attributes attributes, Context context) {}
269299

@@ -357,6 +387,11 @@ public DoubleGauge build() {
357387
}
358388

359389
private static class NoopDoubleGauge implements DoubleGauge {
390+
@Override
391+
public boolean isEnabled() {
392+
return false;
393+
}
394+
360395
@Override
361396
public void set(double value) {}
362397

@@ -398,6 +433,11 @@ public LongGauge build() {
398433
}
399434

400435
private static class NoopLongGauge implements LongGauge {
436+
@Override
437+
public boolean isEnabled() {
438+
return false;
439+
}
440+
401441
@Override
402442
public void set(long value) {}
403443

api/all/src/main/java/io/opentelemetry/api/metrics/DoubleCounter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616
*/
1717
@ThreadSafe
1818
public interface DoubleCounter {
19+
20+
/**
21+
* Returns {@code true} if the counter is enabled.
22+
*
23+
* <p>This allows callers to avoid unnecessary compute when nothing is consuming the data. Because
24+
* the response is subject to change over the application, callers should call this before each
25+
* call to {@link #add(double)}, {@link #add(double, Attributes)}, or {@link #add(double,
26+
* Attributes, Context)}.
27+
*/
28+
default boolean isEnabled() {
29+
return true;
30+
}
31+
1932
/**
2033
* Records a value.
2134
*

api/all/src/main/java/io/opentelemetry/api/metrics/DoubleGauge.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616
*/
1717
@ThreadSafe
1818
public interface DoubleGauge {
19+
20+
/**
21+
* Returns {@code true} if the gauge is enabled.
22+
*
23+
* <p>This allows callers to avoid unnecessary compute when nothing is consuming the data. Because
24+
* the response is subject to change over the application, callers should call this before each
25+
* call to {@link #set(double)}, {@link #set(double, Attributes)}, or {@link #set(double,
26+
* Attributes, Context)}.
27+
*/
28+
default boolean isEnabled() {
29+
return true;
30+
}
31+
1932
/**
2033
* Set the gauge value.
2134
*

api/all/src/main/java/io/opentelemetry/api/metrics/DoubleHistogram.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
@ThreadSafe
1818
public interface DoubleHistogram {
1919

20+
/**
21+
* Returns {@code true} if the histogram is enabled.
22+
*
23+
* <p>This allows callers to avoid unnecessary compute when nothing is consuming the data. Because
24+
* the response is subject to change over the application, callers should call this before each
25+
* call to {@link #record(double)}, {@link #record(double, Attributes)}, or {@link #record(double,
26+
* Attributes, Context)}.
27+
*/
28+
default boolean isEnabled() {
29+
return true;
30+
}
31+
2032
/**
2133
* Records a value.
2234
*

api/all/src/main/java/io/opentelemetry/api/metrics/DoubleUpDownCounter.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616
*/
1717
@ThreadSafe
1818
public interface DoubleUpDownCounter {
19+
20+
/**
21+
* Returns {@code true} if the up down counter is enabled.
22+
*
23+
* <p>This allows callers to avoid unnecessary compute when nothing is consuming the data. Because
24+
* the response is subject to change over the application, callers should call this before each
25+
* call to {@link #add(double)}, {@link #add(double, Attributes)}, or {@link #add(double,
26+
* Attributes, Context)}.
27+
*/
28+
default boolean isEnabled() {
29+
return true;
30+
}
31+
1932
/**
2033
* Records a value.
2134
*

api/all/src/main/java/io/opentelemetry/api/metrics/LongCounter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
@ThreadSafe
1818
public interface LongCounter {
1919

20+
/**
21+
* Returns {@code true} if the counter is enabled.
22+
*
23+
* <p>This allows callers to avoid unnecessary compute when nothing is consuming the data. Because
24+
* the response is subject to change over the application, callers should call this before each
25+
* call to {@link #add(long)}, {@link #add(long, Attributes)}, or {@link #add(long, Attributes,
26+
* Context)}.
27+
*/
28+
default boolean isEnabled() {
29+
return true;
30+
}
31+
2032
/**
2133
* Records a value.
2234
*

api/all/src/main/java/io/opentelemetry/api/metrics/LongGauge.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616
*/
1717
@ThreadSafe
1818
public interface LongGauge {
19+
20+
/**
21+
* Returns {@code true} if the gauge is enabled.
22+
*
23+
* <p>This allows callers to avoid unnecessary compute when nothing is consuming the data. Because
24+
* the response is subject to change over the application, callers should call this before each
25+
* call to {@link #set(long)}, {@link #set(long, Attributes)}, or {@link #set(long, Attributes,
26+
* Context)}.
27+
*/
28+
default boolean isEnabled() {
29+
return true;
30+
}
31+
1932
/**
2033
* Set the gauge value.
2134
*

api/all/src/main/java/io/opentelemetry/api/metrics/LongHistogram.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
@ThreadSafe
1818
public interface LongHistogram {
1919

20+
/**
21+
* Returns {@code true} if the histogram is enabled.
22+
*
23+
* <p>This allows callers to avoid unnecessary compute when nothing is consuming the data. Because
24+
* the response is subject to change over the application, callers should call this before each
25+
* call to {@link #record(long)}, {@link #record(long, Attributes)}, or {@link #record(long,
26+
* Attributes, Context)}.
27+
*/
28+
default boolean isEnabled() {
29+
return true;
30+
}
31+
2032
/**
2133
* Records a value.
2234
*

0 commit comments

Comments
 (0)