Skip to content
Merged
Changes from 1 commit
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 @@ -28,6 +28,8 @@ class IdentityTest {
@RegisterExtension
LogCapturer viewRegistryLogs = LogCapturer.create().captureForType(ViewRegistry.class);

@RegisterExtension LogCapturer sdkMeterLogs = LogCapturer.create().captureForType(SdkMeter.class);

private InMemoryMetricReader reader;
private SdkMeterProviderBuilder builder;

Expand Down Expand Up @@ -1092,6 +1094,51 @@ void sameMeterDifferentInstrumentIncompatibleViewAggregation() {
.hasSize(1);
}

@Test
void viewNameNotValidated() {
// View names are not validated against the instrument name pattern. When a view sets a name
// that would be rejected if used directly as an instrument name (e.g. starts with a digit),
// the SDK accepts it and produces a metric with the invalid name without logging a warning.
SdkMeterProvider meterProvider =
builder
.registerView(
InstrumentSelector.builder().setType(InstrumentType.COUNTER).build(),
View.builder().setName("1invalid-name").build())
.build();

meterProvider.get("meter1").counterBuilder("counter1").build().add(10);

assertThat(reader.collectAllMetrics())
.satisfiesExactlyInAnyOrder(
metricData ->
assertThat(metricData)
.hasInstrumentationScope(forMeter("meter1"))
.hasName("1invalid-name")
.hasLongSumSatisfying(
sum -> sum.hasPointsSatisfying(point -> point.hasValue(10))));

assertThat(metricStorageRegistryLogs.getEvents()).hasSize(0);
assertThat(viewRegistryLogs.getEvents()).hasSize(0);
}

@Test
@SuppressLogger(SdkMeter.class)
void instrumentWithInvalidName() {
// When an instrument is initialized with an invalid name, a noop instrument is returned
Comment thread
jack-berg marked this conversation as resolved.
// and a warning is logged. No metric data is produced.
SdkMeterProvider meterProvider = builder.build();

meterProvider.get("meter1").counterBuilder("1invalid-name").build().add(10);

assertThat(reader.collectAllMetrics()).isEmpty();
assertThat(sdkMeterLogs.getEvents())
.allSatisfy(
logEvent ->
assertThat(logEvent.getMessage())
.contains("Instrument name \"1invalid-name\" is invalid"))
.hasSize(1);
}

private static InstrumentationScopeInfo forMeter(String meterName) {
return InstrumentationScopeInfo.create(meterName);
}
Expand Down
Loading