Skip to content

Commit 9c3b35b

Browse files
authored
Refine observation assertion messages for keys (#4768)
Closes gh-4046
1 parent 8fa9eb8 commit 9c3b35b

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

micrometer-observation-test/src/main/java/io/micrometer/observation/tck/ObservationContextAssert.java

+10
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ private List<String> highCardinalityKeys() {
229229
public SELF hasLowCardinalityKeyValueWithKey(String key) {
230230
isNotNull();
231231
if (this.actual.getLowCardinalityKeyValues().stream().noneMatch(tag -> tag.getKey().equals(key))) {
232+
if (this.actual.getHighCardinalityKeyValue(key) != null) {
233+
failWithMessage(
234+
"Observation should have a low cardinality tag with key <%s> but it was in the wrong (high) cardinality keys. List of all low cardinality keys <%s>",
235+
key, lowCardinalityKeys());
236+
}
232237
failWithMessage(
233238
"Observation should have a low cardinality tag with key <%s> but it's not there. List of all keys <%s>",
234239
key, lowCardinalityKeys());
@@ -301,6 +306,11 @@ public SELF doesNotHaveLowCardinalityKeyValue(KeyValue keyValue) {
301306
public SELF hasHighCardinalityKeyValueWithKey(String key) {
302307
isNotNull();
303308
if (this.actual.getHighCardinalityKeyValues().stream().noneMatch(tag -> tag.getKey().equals(key))) {
309+
if (this.actual.getLowCardinalityKeyValue(key) != null) {
310+
failWithMessage(
311+
"Observation should have a high cardinality tag with key <%s> but it was in the wrong (low) cardinality keys. List of all high cardinality keys <%s>",
312+
key, highCardinalityKeys());
313+
}
304314
failWithMessage(
305315
"Observation should have a high cardinality tag with key <%s> but it's not there. List of all keys <%s>",
306316
key, highCardinalityKeys());

micrometer-observation-test/src/test/java/io/micrometer/observation/tck/ObservationContextAssertTests.java

+32
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,22 @@ void should_throw_exception_when_low_cardinality_key_value_missing() {
253253
.isInstanceOf(AssertionError.class);
254254
}
255255

256+
@Test
257+
void should_throw_exception_when_low_cardinality_key_value_missing_but_in_high_cardinality_keys() {
258+
Observation observation = Observation.start("foo", context, registry);
259+
observation.lowCardinalityKeyValue("low", "l");
260+
observation.highCardinalityKeyValue("high", "h");
261+
262+
thenThrownBy(() -> assertThat(context).hasLowCardinalityKeyValue("high", "h"))
263+
.isInstanceOf(AssertionError.class)
264+
.hasMessage(
265+
"Observation should have a low cardinality tag with key <high> but it was in the wrong (high) cardinality keys. List of all low cardinality keys <[low]>");
266+
thenThrownBy(() -> assertThat(context).hasLowCardinalityKeyValueWithKey("high"))
267+
.isInstanceOf(AssertionError.class)
268+
.hasMessage(
269+
"Observation should have a low cardinality tag with key <high> but it was in the wrong (high) cardinality keys. List of all low cardinality keys <[low]>");
270+
}
271+
256272
@Test
257273
void should_not_throw_exception_when_high_cardinality_key_value_exists() {
258274
Observation observation = Observation.start("foo", context, registry);
@@ -273,6 +289,22 @@ void should_throw_exception_when_high_cardinality_key_value_missing() {
273289
.isInstanceOf(AssertionError.class);
274290
}
275291

292+
@Test
293+
void should_throw_exception_when_high_cardinality_key_value_missing_but_in_low_cardinality_keys() {
294+
Observation observation = Observation.start("foo", context, registry);
295+
observation.lowCardinalityKeyValue("low", "l");
296+
observation.highCardinalityKeyValue("high", "h");
297+
298+
thenThrownBy(() -> assertThat(context).hasHighCardinalityKeyValue("low", "l"))
299+
.isInstanceOf(AssertionError.class)
300+
.hasMessage(
301+
"Observation should have a high cardinality tag with key <low> but it was in the wrong (low) cardinality keys. List of all high cardinality keys <[high]>");
302+
thenThrownBy(() -> assertThat(context).hasHighCardinalityKeyValueWithKey("low"))
303+
.isInstanceOf(AssertionError.class)
304+
.hasMessage(
305+
"Observation should have a high cardinality tag with key <low> but it was in the wrong (low) cardinality keys. List of all high cardinality keys <[high]>");
306+
}
307+
276308
@Test
277309
void should_not_throw_exception_when_high_cardinality_key_value_present() {
278310
thenNoException().isThrownBy(() -> assertThat(context).doesNotHaveHighCardinalityKeyValue("foo", "bar")

0 commit comments

Comments
 (0)