Skip to content
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

[4.x] Fix incorrect tags comparison when trying to match metric IDs #5550

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ private HelidonMetric getMetricLocked(String metricName, Tag... tags) {
return null;
}
for (MetricID metricID : metricIDsForName) {
if (metricID.getName().equals(metricName) && Arrays.equals(metricID.getTagsAsArray(), tags)) {
if (metricID.getName().equals(metricName) && tagsMatch(tags, metricID.getTags())) {
return allMetrics.get(metricID);
}
}
Expand Down Expand Up @@ -531,6 +531,14 @@ private <S> S access(Lock lock, Callable<S> action) {
}
}

private static boolean tagsMatch(Tag[] tags, Map<String, String> tagMap) {
Map<String, String> newTags = new TreeMap<>();
for (Tag tag : tags) {
newTags.put(tag.getTagName(), tag.getTagValue());
}
return newTags.equals(tagMap);
}

private static void enforceConsistentMetadata(Metadata existingMetadata, Metadata newMetadata) {
if (!metadataMatches(existingMetadata, newMetadata)) {
throw new IllegalArgumentException("New metadata conflicts with existing metadata with the same name; existing: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import org.eclipse.microprofile.metrics.Tag;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

import static org.junit.jupiter.api.Assertions.assertThrows;

class MetricStoreTests {
Expand Down Expand Up @@ -56,4 +60,70 @@ void testConflictingMetadata() {
assertThrows(IllegalArgumentException.class, () ->
store.getOrRegisterMetric(meta2, Counter.class, NO_TAGS));
}

@Test
void testSameNameNoTags() {
Metadata metadata = Metadata.builder()
.withName("a")
.withType(MetricType.COUNTER)
.build();

NoOpMetricRegistry registry = NoOpMetricRegistry.create(MetricRegistry.Type.APPLICATION);

MetricStore store = MetricStore.create(REGISTRY_SETTINGS,
NoOpMetricRegistry.NO_OP_METRIC_FACTORIES,
null,
null,
MetricRegistry.Type.APPLICATION,
registry::toImpl);

Counter counter1 = store.getOrRegisterMetric(metadata, Counter.class, NO_TAGS);
Counter counter2 = store.getOrRegisterMetric(metadata, Counter.class, NO_TAGS);
assertThat("Counters with no tags", counter1, is(counter2));
}

@Test
void testSameNameSameTwoTags() {
Tag[] tags = {new Tag("foo", "1"), new Tag("bar", "1")};
Metadata metadata = Metadata.builder()
.withName("a")
.withType(MetricType.COUNTER)
.build();

NoOpMetricRegistry registry = NoOpMetricRegistry.create(MetricRegistry.Type.APPLICATION);

MetricStore store = MetricStore.create(REGISTRY_SETTINGS,
NoOpMetricRegistry.NO_OP_METRIC_FACTORIES,
null,
null,
MetricRegistry.Type.APPLICATION,
registry::toImpl);

Counter counter1 = store.getOrRegisterMetric(metadata, Counter.class, tags);
Counter counter2 = store.getOrRegisterMetric(metadata, Counter.class, tags);
assertThat("Counters with same two tags", counter1, is(counter2));
}

@Test
void testSameNameOverlappingButDifferentTags() {
Tag[] tags1 = {new Tag("foo", "1"), new Tag("bar", "1"), new Tag("baz", "1")};
Tag[] tags2 = {new Tag("foo", "1"), new Tag("bar", "1")};
Metadata metadata = Metadata.builder()
.withName("a")
.withType(MetricType.COUNTER)
.build();

NoOpMetricRegistry registry = NoOpMetricRegistry.create(MetricRegistry.Type.APPLICATION);

MetricStore store = MetricStore.create(REGISTRY_SETTINGS,
NoOpMetricRegistry.NO_OP_METRIC_FACTORIES,
null,
null,
MetricRegistry.Type.APPLICATION,
registry::toImpl);

Counter counter1 = store.getOrRegisterMetric(metadata, Counter.class, tags1);
Counter counter2 = store.getOrRegisterMetric(metadata, Counter.class, tags2);
assertThat("Counters with overlapping but different tags", counter1, not(is(counter2)));
Copy link
Member

Choose a reason for hiding this comment

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

I would use either is(not( or just not( so the assertion reads like English sentence:
"assert that counter1 is not counter2" seems easier to understand than "assert that counter1 not is counter2"

}
}