Skip to content

Commit 080ddf4

Browse files
committed
Add VirtualThreadQueryObservationConvention
In a virtual thread environment, thread names are always unique. Tagging them as low-cardinality keys can lead to high-cardinality issues in observability backends. This change introduces `VirtualThreadQueryObservationConvention`, which avoids tagging the thread name. Fixes #149
1 parent 3a29ac2 commit 080ddf4

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.r2dbc.proxy.observation;
18+
19+
import io.micrometer.common.KeyValue;
20+
import io.micrometer.common.KeyValues;
21+
22+
/**
23+
* An extended {@link QueryObservationConvention} that excludes the thread name from low cardinality tags.
24+
* This is particularly useful in environments using virtual threads, where thread names are always unique
25+
* and no longer suitable for low cardinality tags.
26+
*
27+
* @author Tadaya Tsuyukubo
28+
* @since 1.1.6
29+
*/
30+
public interface VirtualThreadQueryObservationConvention extends QueryObservationConvention {
31+
32+
@Override
33+
default KeyValues getLowCardinalityKeyValues(QueryContext context) {
34+
// does not include thread-name
35+
return KeyValues.of(KeyValue.of(R2dbcObservationDocumentation.LowCardinalityKeys.CONNECTION, context.getConnectionName()));
36+
}
37+
38+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.r2dbc.proxy.observation;
18+
19+
import io.micrometer.common.KeyValue;
20+
import io.micrometer.common.KeyValues;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
25+
/**
26+
* Test for {@link VirtualThreadQueryObservationConvention}.
27+
*
28+
* @author Tadaya Tsuyukubo
29+
*/
30+
class VirtualThreadQueryObservationConventionTest {
31+
32+
VirtualThreadQueryObservationConvention convention = new VirtualThreadQueryObservationConvention() {
33+
34+
};
35+
36+
@Test
37+
void keyValues() {
38+
QueryContext context = new QueryContext();
39+
context.setConnectionName("my-connection");
40+
context.setThreadName("my-thread");
41+
42+
KeyValues keyValues = convention.getLowCardinalityKeyValues(context);
43+
44+
assertThat(keyValues).containsOnly(KeyValue.of(R2dbcObservationDocumentation.LowCardinalityKeys.CONNECTION.asString(), "my-connection"));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)