Skip to content
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 @@ -8,6 +8,7 @@
import io.prometheus.metrics.model.registry.MetricType;
import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.StateSetSnapshot;
import io.prometheus.metrics.model.snapshots.Unit;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -170,6 +171,17 @@ public Builder states(String... stateNames) {
return this;
}

/**
* Throws an {@link UnsupportedOperationException} because StateSet metrics cannot have a unit.
*/
@Override
public Builder unit(@Nullable Unit unit) {
if (unit != null) {
throw new UnsupportedOperationException("StateSet metrics cannot have a unit.");
}
return this;
}

@Override
public StateSet build() {
if (names == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.StateSetSnapshot;
import io.prometheus.metrics.model.snapshots.Unit;
import java.util.Arrays;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -72,6 +73,14 @@ void illegalName() {
+ " metrics)");
}

@Test
void unit() {
assertThatExceptionOfType(UnsupportedOperationException.class)
.isThrownBy(
() -> StateSet.builder().name("test").states("a", "b").unit(Unit.SECONDS).build())
.withMessage("StateSet metrics cannot have a unit.");
}

private StateSetSnapshot.StateSetDataPointSnapshot getData(StateSet stateSet, String... labels) {
return stateSet.collect().getDataPoints().stream()
.filter(d -> d.getLabels().equals(Labels.of(labels)))
Expand Down
Loading