|
6 | 6 | import io.opentelemetry.api.metrics.LongCounterBuilder; |
7 | 7 | import io.opentelemetry.api.metrics.LongUpDownCounterBuilder; |
8 | 8 | import io.opentelemetry.api.metrics.Meter; |
| 9 | +import io.opentelemetry.api.metrics.MeterProvider; |
9 | 10 | import io.opentelemetry.api.metrics.ObservableMeasurement; |
10 | 11 | import org.slf4j.Logger; |
11 | 12 | import org.slf4j.LoggerFactory; |
12 | 13 |
|
13 | 14 | // https://www.javadoc.io/doc/io.opentelemetry/opentelemetry-api/1.47.0/io/opentelemetry/api/metrics/Meter.html |
14 | 15 | public class OtelMeter implements Meter { |
15 | 16 | private static final Logger LOGGER = LoggerFactory.getLogger(OtelMeter.class); |
16 | | - |
| 17 | + private static final Meter NOOP_METER = MeterProvider.noop().get("noop"); |
| 18 | + private static final String NOOP_INSTRUMENT_NAME = "noop"; |
17 | 19 | private final String instrumentationScopeName; |
18 | 20 | private final String schemaUrl; |
19 | 21 | private final String instrumentationVersion; |
@@ -76,4 +78,28 @@ public BatchCallback batchCallback( |
76 | 78 | LOGGER.info("batchCallback is not yet supported"); |
77 | 79 | return Meter.super.batchCallback(callback, observableMeasurement, additionalMeasurements); |
78 | 80 | } |
| 81 | + |
| 82 | + private static boolean isInstrumentNameInvalid(String instrumentName) { |
| 83 | + if ((null == instrumentName) |
| 84 | + || instrumentName.isEmpty() |
| 85 | + || instrumentName.length() > 255 |
| 86 | + || !Character.isLetter(instrumentName.charAt(0)) |
| 87 | + || HasAnyInvalidCharacter(instrumentName)) { |
| 88 | + LOGGER.warn( |
| 89 | + "Invalid instrument name {}. Instrument names must be ASCII, start with letter, contain only alphanumeric characters, '_', '.', '/', '-' and be max 255 characters.", |
| 90 | + instrumentName); |
| 91 | + return true; |
| 92 | + } |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + private static boolean HasAnyInvalidCharacter(String instrumentName) { |
| 97 | + for (int i = 1; i < instrumentName.length(); i++) { |
| 98 | + char c = instrumentName.charAt(i); |
| 99 | + if (!Character.isLetterOrDigit(c) && c != '_' && c != '.' && c != '/' && c != '-') { |
| 100 | + return true; |
| 101 | + } |
| 102 | + } |
| 103 | + return false; |
| 104 | + } |
79 | 105 | } |
0 commit comments