Skip to content

Commit c5b487f

Browse files
committed
Polish "Add datasource name to the contexts"
1 parent b873cf2 commit c5b487f

11 files changed

+115
-75
lines changed

datasource-micrometer/src/main/java/net/ttddyy/observation/tracing/BaseObservationConvention.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

datasource-micrometer/src/main/java/net/ttddyy/observation/tracing/ConnectionObservationConvention.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,20 +16,21 @@
1616

1717
package net.ttddyy.observation.tracing;
1818

19+
import java.util.HashSet;
20+
import java.util.Set;
21+
1922
import io.micrometer.common.KeyValue;
2023
import io.micrometer.common.KeyValues;
2124
import io.micrometer.observation.Observation.Context;
2225
import io.micrometer.observation.ObservationConvention;
23-
24-
import java.util.HashSet;
25-
import java.util.Set;
26+
import net.ttddyy.observation.tracing.JdbcObservationDocumentation.ConnectionKeyNames;
2627

2728
/**
2829
* A {@link ObservationConvention} for connection.
2930
*
3031
* @author Tadaya Tsuyukubo
3132
*/
32-
public interface ConnectionObservationConvention extends BaseObservationConvention<ConnectionContext> {
33+
public interface ConnectionObservationConvention extends ObservationConvention<ConnectionContext> {
3334

3435
@Override
3536
default boolean supportsContext(Context context) {
@@ -44,7 +45,10 @@ default String getName() {
4445
@Override
4546
default KeyValues getLowCardinalityKeyValues(ConnectionContext context) {
4647
Set<KeyValue> keyValues = new HashSet<>();
47-
getDatasourceName(context).ifPresent(keyValues::add);
48+
String dataSourceName = context.getDataSourceName();
49+
if (dataSourceName != null) {
50+
keyValues.add(KeyValue.of(ConnectionKeyNames.DATASOURCE_NAME, dataSourceName));
51+
}
4852
return KeyValues.of(keyValues);
4953
}
5054

datasource-micrometer/src/main/java/net/ttddyy/observation/tracing/DataSourceObservationListener.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 the original author or authors.
2+
* Copyright 2022-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -161,9 +161,11 @@ private void populateQueryContext(ExecutionInfo executionInfo, List<QueryInfo> q
161161
private void populateFromConnectionAttributes(DataSourceBaseContext context, String connectionId) {
162162
ConnectionAttributes connectionAttributes = this.connectionAttributesManager.get(connectionId);
163163
if (connectionAttributes != null) {
164+
String dataSourceName = connectionAttributes.connectionInfo.getDataSourceName();
164165
context.setHost(connectionAttributes.host);
165166
context.setPort(connectionAttributes.port);
166-
context.setRemoteServiceName(connectionAttributes.connectionInfo.getDataSourceName());
167+
context.setRemoteServiceName(dataSourceName);
168+
context.setDataSourceName(dataSourceName);
167169
context.setDataSource(connectionAttributes.dataSource);
168170
}
169171
}
@@ -260,23 +262,13 @@ else if (target instanceof ResultSet) {
260262

261263
private void handleGetConnectionBefore(MethodExecutionContext executionContext) {
262264
ConnectionContext connectionContext = new ConnectionContext();
265+
connectionContext.setDataSourceName(executionContext.getProxyConfig().getDataSourceName());
263266
executionContext.addCustomValue(ConnectionContext.class.getName(), connectionContext);
264-
populateConnectionContext(connectionContext, executionContext);
265267
Observation observation = createAndStartObservation(JdbcObservationDocumentation.CONNECTION, connectionContext,
266268
this.connectionObservationConvention);
267269
executionContext.addCustomValue(Observation.Scope.class.getName(), observation.openScope());
268270
}
269271

270-
private void populateConnectionContext(ConnectionContext connectionContext,
271-
MethodExecutionContext executionContext) {
272-
if (executionContext.getConnectionInfo() != null) {
273-
connectionContext.setDataSourceName(executionContext.getConnectionInfo().getDataSourceName());
274-
}
275-
else if (executionContext.getProxyConfig() != null) {
276-
connectionContext.setDataSourceName(executionContext.getProxyConfig().getDataSourceName());
277-
}
278-
}
279-
280272
private void handleGetConnectionAfter(MethodExecutionContext executionContext) {
281273
DataSource dataSource = (DataSource) executionContext.getTarget();
282274
Connection connection = (Connection) executionContext.getResult();
@@ -429,7 +421,6 @@ private ResultSetAttributes createResultSetAttributesAndStartObservation(MethodE
429421
// new ResultSet observation
430422
ResultSetContext resultSetContext = new ResultSetContext();
431423
populateFromConnectionAttributes(resultSetContext, executionContext.getConnectionInfo().getConnectionId());
432-
populateResultSetContext(resultSetContext, executionContext);
433424
Observation observation;
434425
if (isGeneratedKeys) {
435426
observation = createAndStartObservation(JdbcObservationDocumentation.GENERATED_KEYS, resultSetContext,
@@ -450,10 +441,6 @@ private ResultSetAttributes createResultSetAttributesAndStartObservation(MethodE
450441
return resultSetAttributes;
451442
}
452443

453-
private void populateResultSetContext(ResultSetContext resultSetContext, MethodExecutionContext executionContext) {
454-
resultSetContext.setDataSourceName(executionContext.getConnectionInfo().getDataSourceName());
455-
}
456-
457444
/**
458445
* This attempts to get the ip and port from the JDBC URL. Ex. localhost and 5555 from
459446
* {@code

datasource-micrometer/src/main/java/net/ttddyy/observation/tracing/JdbcObservationDocumentation.java

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2024 the original author or authors.
2+
* Copyright 2022-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ public String getContextualName() {
4343

4444
@Override
4545
public KeyName[] getLowCardinalityKeyNames() {
46-
return CommonLowCardinalityKeyNames.values();
46+
return ConnectionKeyNames.values();
4747
}
4848

4949
@Override
@@ -82,7 +82,7 @@ public KeyName[] getHighCardinalityKeyNames() {
8282

8383
@Override
8484
public KeyName[] getLowCardinalityKeyNames() {
85-
return CommonLowCardinalityKeyNames.values();
85+
return QueryLowCardinalityKeyNames.values();
8686
}
8787

8888
@Override
@@ -112,7 +112,7 @@ public KeyName[] getHighCardinalityKeyNames() {
112112

113113
@Override
114114
public KeyName[] getLowCardinalityKeyNames() {
115-
return CommonLowCardinalityKeyNames.values();
115+
return ResultSetLowCardinalityKeyNames.values();
116116
}
117117

118118
@Override
@@ -142,7 +142,7 @@ public KeyName[] getHighCardinalityKeyNames() {
142142

143143
@Override
144144
public KeyName[] getLowCardinalityKeyNames() {
145-
return CommonLowCardinalityKeyNames.values();
145+
return GeneratedKeysLowCardinalityKeyNames.values();
146146
}
147147

148148
@Override
@@ -187,17 +187,17 @@ public String asString() {
187187

188188
}
189189

190-
public enum CommonLowCardinalityKeyNames implements KeyName {
190+
public enum QueryLowCardinalityKeyNames implements KeyName {
191191

192192
/**
193193
* Name of the JDBC datasource.
194194
*/
195-
NAME {
195+
DATASOURCE_NAME {
196196
@Override
197197
public String asString() {
198-
return "name";
198+
return "jdbc.datasource.name";
199199
}
200-
},
200+
}
201201

202202
}
203203

@@ -215,6 +215,20 @@ public String asString() {
215215

216216
}
217217

218+
public enum ResultSetLowCardinalityKeyNames implements KeyName {
219+
220+
/**
221+
* Name of the JDBC datasource.
222+
*/
223+
DATASOURCE_NAME {
224+
@Override
225+
public String asString() {
226+
return "jdbc.datasource.name";
227+
}
228+
},
229+
230+
}
231+
218232
public enum GeneratedKeysHighCardinalityKeyNames implements KeyName {
219233

220234
/**
@@ -225,7 +239,21 @@ public enum GeneratedKeysHighCardinalityKeyNames implements KeyName {
225239
public String asString() {
226240
return "jdbc.generated-keys";
227241
}
228-
}
242+
};
243+
244+
}
245+
246+
public enum GeneratedKeysLowCardinalityKeyNames implements KeyName {
247+
248+
/**
249+
* Name of the JDBC datasource.
250+
*/
251+
DATASOURCE_NAME {
252+
@Override
253+
public String asString() {
254+
return "jdbc.datasource.name";
255+
}
256+
},
229257

230258
}
231259

@@ -251,6 +279,16 @@ public String asString() {
251279
}
252280
},
253281

282+
/**
283+
* Name of the JDBC datasource.
284+
*/
285+
DATASOURCE_NAME {
286+
@Override
287+
public String asString() {
288+
return "jdbc.datasource.name";
289+
}
290+
},
291+
254292
}
255293

256294
public enum JdbcEvents implements Event {

datasource-micrometer/src/main/java/net/ttddyy/observation/tracing/QueryObservationConvention.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,14 +25,14 @@
2525
import io.micrometer.observation.ObservationConvention;
2626
import net.ttddyy.observation.tracing.JdbcObservationDocumentation.QueryHighCardinalityKeyNames;
2727

28-
import static net.ttddyy.observation.tracing.JdbcObservationDocumentation.*;
28+
import static net.ttddyy.observation.tracing.JdbcObservationDocumentation.QueryLowCardinalityKeyNames;
2929

3030
/**
3131
* A {@link ObservationConvention} for query.
3232
*
3333
* @author Tadaya Tsuyukubo
3434
*/
35-
public interface QueryObservationConvention extends BaseObservationConvention<QueryContext> {
35+
public interface QueryObservationConvention extends ObservationConvention<QueryContext> {
3636

3737
@Override
3838
default boolean supportsContext(Context context) {
@@ -47,7 +47,10 @@ default String getName() {
4747
@Override
4848
default KeyValues getLowCardinalityKeyValues(QueryContext context) {
4949
Set<KeyValue> keyValues = new HashSet<>();
50-
getDatasourceName(context).ifPresent(keyValues::add);
50+
String dataSourceName = context.getDataSourceName();
51+
if (dataSourceName != null) {
52+
keyValues.add(KeyValue.of(QueryLowCardinalityKeyNames.DATASOURCE_NAME, dataSourceName));
53+
}
5154
return KeyValues.of(keyValues);
5255
}
5356

datasource-micrometer/src/main/java/net/ttddyy/observation/tracing/ResultSetObservationConvention.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,23 +16,22 @@
1616

1717
package net.ttddyy.observation.tracing;
1818

19+
import java.util.HashSet;
20+
import java.util.Set;
21+
1922
import io.micrometer.common.KeyValue;
2023
import io.micrometer.common.KeyValues;
2124
import io.micrometer.observation.Observation.Context;
2225
import io.micrometer.observation.ObservationConvention;
2326
import net.ttddyy.observation.tracing.JdbcObservationDocumentation.ResultSetHighCardinalityKeyNames;
24-
25-
import java.util.HashSet;
26-
import java.util.Set;
27-
28-
import static net.ttddyy.observation.tracing.JdbcObservationDocumentation.*;
27+
import net.ttddyy.observation.tracing.JdbcObservationDocumentation.ResultSetLowCardinalityKeyNames;
2928

3029
/**
3130
* A {@link ObservationConvention} for result-set operations.
3231
*
3332
* @author Tadaya Tsuyukubo
3433
*/
35-
public interface ResultSetObservationConvention extends BaseObservationConvention<ResultSetContext> {
34+
public interface ResultSetObservationConvention extends ObservationConvention<ResultSetContext> {
3635

3736
@Override
3837
default boolean supportsContext(Context context) {
@@ -48,7 +47,10 @@ default KeyValues getHighCardinalityKeyValues(ResultSetContext context) {
4847
@Override
4948
default KeyValues getLowCardinalityKeyValues(ResultSetContext context) {
5049
Set<KeyValue> keyValues = new HashSet<>();
51-
getDatasourceName(context).ifPresent(keyValues::add);
50+
String dataSourceName = context.getDataSourceName();
51+
if (dataSourceName != null) {
52+
keyValues.add(KeyValue.of(ResultSetLowCardinalityKeyNames.DATASOURCE_NAME, dataSourceName));
53+
}
5254
return KeyValues.of(keyValues);
5355
}
5456

datasource-micrometer/src/test/java/net/ttddyy/observation/tracing/DataSourceListenerFailureTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,7 +45,7 @@ public SampleTestRunnerConsumer yourCode() throws Exception {
4545
spanAssert
4646
.hasEventWithNameEqualTo("acquired")
4747
.hasEventWithNameEqualTo("rollback")
48-
.hasTag("name", "proxy-ds");
48+
.hasTag("jdbc.datasource.name", "proxy-ds");
4949
}))
5050
.hasASpanWithName("query");
5151
// @formatter:on

datasource-micrometer/src/test/java/net/ttddyy/observation/tracing/DataSourceListenerReferenceIntegrationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -67,12 +67,12 @@ public SampleTestRunnerConsumer yourCode() throws Exception {
6767
spanAssert
6868
.hasTag("jdbc.query[0]", "SELECT name FROM emp WHERE id = ?")
6969
.hasTag("jdbc.params[0]", "(20)")
70-
.hasTag("name", "proxy");
70+
.hasTag("jdbc.datasource.name", "proxy");
7171
}))
7272
.hasASpanWithName("result-set", (spanAssert) -> {
7373
spanAssert
7474
.hasTag("jdbc.row-count", "1")
75-
.hasTag("name", "proxy");
75+
.hasTag("jdbc.datasource.name", "proxy");
7676
});
7777
// @formatter:on
7878
};

datasource-micrometer/src/test/java/net/ttddyy/observation/tracing/DataSourceListenerUpdateIntegrationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,13 +46,13 @@ public SampleTestRunnerConsumer yourCode() throws Exception {
4646
spanAssert
4747
.hasEventWithNameEqualTo("acquired")
4848
.hasEventWithNameEqualTo("commit")
49-
.hasTag("name", "proxy-ds");
49+
.hasTag("jdbc.datasource.name", "proxy-ds");
5050
}))
5151
.hasASpanWithName("query", (spanAssert -> {
5252
spanAssert
5353
.hasTag("jdbc.query[0]", "INSERT INTO emp VALUES (?, ?)")
5454
.doesNotHaveTagWithKey("jdbc.params[0]")
55-
.hasTag("name", "proxy-ds"); // default is off
55+
.hasTag("jdbc.datasource.name", "proxy-ds"); // default is off
5656
}));
5757
// @formatter:on
5858
};

0 commit comments

Comments
 (0)