Skip to content

Commit bdbf27b

Browse files
committed
Merge pull request #62 from stefano-salmaso
* #62: Polish "Add datasource name to the contexts" Add datasource name to the contexts Closes gh-58
2 parents 01b3c89 + c5b487f commit bdbf27b

File tree

12 files changed

+204
-26
lines changed

12 files changed

+204
-26
lines changed

datasource-micrometer-spring-boot/src/test/java/net/ttddyy/observation/boot/event/JdbcEventPublishingListenerTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void methodEvents() {
5959
});
6060
});
6161

62-
jdbcEventHolder.set(null); // reset
62+
jdbcEventHolder.set(null); // reset
6363

6464
listener.afterMethod(methodContext);
6565

@@ -81,7 +81,7 @@ void queryEvents() {
8181

8282
Statement source = mock(Statement.class);
8383
ExecutionInfo executionInfo = new ExecutionInfo();
84-
executionInfo.setStatement(source); // used as event source
84+
executionInfo.setStatement(source); // used as event source
8585
listener.beforeQuery(executionInfo, Collections.emptyList());
8686

8787
assertThat(jdbcEventHolder).hasValueSatisfying((event) -> {
@@ -107,7 +107,7 @@ void queryEvents() {
107107
});
108108
}
109109

110-
private ApplicationContext createApplicationContextWithListener(AtomicReference<JdbcEvent> eventHolder){
110+
private ApplicationContext createApplicationContextWithListener(AtomicReference<JdbcEvent> eventHolder) {
111111
GenericApplicationContext context = new GenericApplicationContext();
112112
context.addApplicationListener((event) -> {
113113
if (event instanceof JdbcEvent) {

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

Lines changed: 17 additions & 1 deletion
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,8 +16,14 @@
1616

1717
package net.ttddyy.observation.tracing;
1818

19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
import io.micrometer.common.KeyValue;
23+
import io.micrometer.common.KeyValues;
1924
import io.micrometer.observation.Observation.Context;
2025
import io.micrometer.observation.ObservationConvention;
26+
import net.ttddyy.observation.tracing.JdbcObservationDocumentation.ConnectionKeyNames;
2127

2228
/**
2329
* A {@link ObservationConvention} for connection.
@@ -36,4 +42,14 @@ default String getName() {
3642
return "jdbc.connection";
3743
}
3844

45+
@Override
46+
default KeyValues getLowCardinalityKeyValues(ConnectionContext context) {
47+
Set<KeyValue> keyValues = new HashSet<>();
48+
String dataSourceName = context.getDataSourceName();
49+
if (dataSourceName != null) {
50+
keyValues.add(KeyValue.of(ConnectionKeyNames.DATASOURCE_NAME, dataSourceName));
51+
}
52+
return KeyValues.of(keyValues);
53+
}
54+
3955
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public DataSourceBaseContext() {
3838

3939
private DataSource dataSource;
4040

41+
private String dataSourceName;
42+
4143
private String host;
4244

4345
private int port;
@@ -50,6 +52,15 @@ public void setDataSource(DataSource dataSource) {
5052
this.dataSource = dataSource;
5153
}
5254

55+
@Nullable
56+
public String getDataSourceName() {
57+
return this.dataSourceName;
58+
}
59+
60+
public void setDataSourceName(@Nullable String dataSourceName) {
61+
this.dataSourceName = dataSourceName;
62+
}
63+
5364
@Nullable
5465
public String getHost() {
5566
return this.host;

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

Lines changed: 6 additions & 4 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.
@@ -155,14 +155,17 @@ private void populateQueryContext(ExecutionInfo executionInfo, List<QueryInfo> q
155155
context.getParams().add(params);
156156
}
157157
}
158+
context.setDataSourceName(executionInfo.getDataSourceName());
158159
}
159160

160161
private void populateFromConnectionAttributes(DataSourceBaseContext context, String connectionId) {
161162
ConnectionAttributes connectionAttributes = this.connectionAttributesManager.get(connectionId);
162163
if (connectionAttributes != null) {
164+
String dataSourceName = connectionAttributes.connectionInfo.getDataSourceName();
163165
context.setHost(connectionAttributes.host);
164166
context.setPort(connectionAttributes.port);
165-
context.setRemoteServiceName(connectionAttributes.connectionInfo.getDataSourceName());
167+
context.setRemoteServiceName(dataSourceName);
168+
context.setDataSourceName(dataSourceName);
166169
context.setDataSource(connectionAttributes.dataSource);
167170
}
168171
}
@@ -259,8 +262,8 @@ else if (target instanceof ResultSet) {
259262

260263
private void handleGetConnectionBefore(MethodExecutionContext executionContext) {
261264
ConnectionContext connectionContext = new ConnectionContext();
265+
connectionContext.setDataSourceName(executionContext.getProxyConfig().getDataSourceName());
262266
executionContext.addCustomValue(ConnectionContext.class.getName(), connectionContext);
263-
264267
Observation observation = createAndStartObservation(JdbcObservationDocumentation.CONNECTION, connectionContext,
265268
this.connectionObservationConvention);
266269
executionContext.addCustomValue(Observation.Scope.class.getName(), observation.openScope());
@@ -418,7 +421,6 @@ private ResultSetAttributes createResultSetAttributesAndStartObservation(MethodE
418421
// new ResultSet observation
419422
ResultSetContext resultSetContext = new ResultSetContext();
420423
populateFromConnectionAttributes(resultSetContext, executionContext.getConnectionInfo().getConnectionId());
421-
422424
Observation observation;
423425
if (isGeneratedKeys) {
424426
observation = createAndStartObservation(JdbcObservationDocumentation.GENERATED_KEYS, resultSetContext,

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

Lines changed: 69 additions & 2 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.
@@ -80,6 +80,11 @@ public KeyName[] getHighCardinalityKeyNames() {
8080
return QueryHighCardinalityKeyNames.values();
8181
}
8282

83+
@Override
84+
public KeyName[] getLowCardinalityKeyNames() {
85+
return QueryLowCardinalityKeyNames.values();
86+
}
87+
8388
@Override
8489
public String getPrefix() {
8590
return "jdbc";
@@ -105,6 +110,11 @@ public KeyName[] getHighCardinalityKeyNames() {
105110
return ResultSetHighCardinalityKeyNames.values();
106111
}
107112

113+
@Override
114+
public KeyName[] getLowCardinalityKeyNames() {
115+
return ResultSetLowCardinalityKeyNames.values();
116+
}
117+
108118
@Override
109119
public String getPrefix() {
110120
return "jdbc";
@@ -130,6 +140,11 @@ public KeyName[] getHighCardinalityKeyNames() {
130140
return GeneratedKeysHighCardinalityKeyNames.values();
131141
}
132142

143+
@Override
144+
public KeyName[] getLowCardinalityKeyNames() {
145+
return GeneratedKeysLowCardinalityKeyNames.values();
146+
}
147+
133148
@Override
134149
public String getPrefix() {
135150
return "jdbc";
@@ -172,6 +187,20 @@ public String asString() {
172187

173188
}
174189

190+
public enum QueryLowCardinalityKeyNames implements KeyName {
191+
192+
/**
193+
* Name of the JDBC datasource.
194+
*/
195+
DATASOURCE_NAME {
196+
@Override
197+
public String asString() {
198+
return "jdbc.datasource.name";
199+
}
200+
}
201+
202+
}
203+
175204
public enum ResultSetHighCardinalityKeyNames implements KeyName {
176205

177206
/**
@@ -186,6 +215,20 @@ public String asString() {
186215

187216
}
188217

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+
189232
public enum GeneratedKeysHighCardinalityKeyNames implements KeyName {
190233

191234
/**
@@ -196,7 +239,21 @@ public enum GeneratedKeysHighCardinalityKeyNames implements KeyName {
196239
public String asString() {
197240
return "jdbc.generated-keys";
198241
}
199-
}
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+
},
200257

201258
}
202259

@@ -222,6 +279,16 @@ public String asString() {
222279
}
223280
},
224281

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+
225292
}
226293

227294
public enum JdbcEvents implements Event {

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

Lines changed: 13 additions & 1 deletion
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,6 +25,8 @@
2525
import io.micrometer.observation.ObservationConvention;
2626
import net.ttddyy.observation.tracing.JdbcObservationDocumentation.QueryHighCardinalityKeyNames;
2727

28+
import static net.ttddyy.observation.tracing.JdbcObservationDocumentation.QueryLowCardinalityKeyNames;
29+
2830
/**
2931
* A {@link ObservationConvention} for query.
3032
*
@@ -42,6 +44,16 @@ default String getName() {
4244
return "jdbc.query";
4345
}
4446

47+
@Override
48+
default KeyValues getLowCardinalityKeyValues(QueryContext context) {
49+
Set<KeyValue> keyValues = new HashSet<>();
50+
String dataSourceName = context.getDataSourceName();
51+
if (dataSourceName != null) {
52+
keyValues.add(KeyValue.of(QueryLowCardinalityKeyNames.DATASOURCE_NAME, dataSourceName));
53+
}
54+
return KeyValues.of(keyValues);
55+
}
56+
4557
@Override
4658
default KeyValues getHighCardinalityKeyValues(QueryContext context) {
4759
Set<KeyValue> keyValues = new HashSet<>();

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

Lines changed: 15 additions & 1 deletion
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,11 +16,15 @@
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;
27+
import net.ttddyy.observation.tracing.JdbcObservationDocumentation.ResultSetLowCardinalityKeyNames;
2428

2529
/**
2630
* A {@link ObservationConvention} for result-set operations.
@@ -40,6 +44,16 @@ default KeyValues getHighCardinalityKeyValues(ResultSetContext context) {
4044
KeyValue.of(ResultSetHighCardinalityKeyNames.ROW_COUNT.asString(), String.valueOf(context.getCount())));
4145
}
4246

47+
@Override
48+
default KeyValues getLowCardinalityKeyValues(ResultSetContext context) {
49+
Set<KeyValue> keyValues = new HashSet<>();
50+
String dataSourceName = context.getDataSourceName();
51+
if (dataSourceName != null) {
52+
keyValues.add(KeyValue.of(ResultSetLowCardinalityKeyNames.DATASOURCE_NAME, dataSourceName));
53+
}
54+
return KeyValues.of(keyValues);
55+
}
56+
4357
@Override
4458
default String getName() {
4559
return "jdbc.result-set";

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

Lines changed: 3 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.
@@ -44,7 +44,8 @@ public SampleTestRunnerConsumer yourCode() throws Exception {
4444
.hasASpanWithName("connection", (spanAssert -> {
4545
spanAssert
4646
.hasEventWithNameEqualTo("acquired")
47-
.hasEventWithNameEqualTo("rollback");
47+
.hasEventWithNameEqualTo("rollback")
48+
.hasTag("jdbc.datasource.name", "proxy-ds");
4849
}))
4950
.hasASpanWithName("query");
5051
// @formatter:on

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

Lines changed: 6 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.
@@ -66,10 +66,13 @@ public SampleTestRunnerConsumer yourCode() throws Exception {
6666
.hasASpanWithName("query", (spanAssert -> {
6767
spanAssert
6868
.hasTag("jdbc.query[0]", "SELECT name FROM emp WHERE id = ?")
69-
.hasTag("jdbc.params[0]", "(20)");
69+
.hasTag("jdbc.params[0]", "(20)")
70+
.hasTag("jdbc.datasource.name", "proxy");
7071
}))
7172
.hasASpanWithName("result-set", (spanAssert) -> {
72-
spanAssert.hasTag("jdbc.row-count", "1");
73+
spanAssert
74+
.hasTag("jdbc.row-count", "1")
75+
.hasTag("jdbc.datasource.name", "proxy");
7376
});
7477
// @formatter:on
7578
};

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

Lines changed: 5 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.
@@ -45,12 +45,14 @@ public SampleTestRunnerConsumer yourCode() throws Exception {
4545
.hasASpanWithName("connection", (spanAssert -> {
4646
spanAssert
4747
.hasEventWithNameEqualTo("acquired")
48-
.hasEventWithNameEqualTo("commit");
48+
.hasEventWithNameEqualTo("commit")
49+
.hasTag("jdbc.datasource.name", "proxy-ds");
4950
}))
5051
.hasASpanWithName("query", (spanAssert -> {
5152
spanAssert
5253
.hasTag("jdbc.query[0]", "INSERT INTO emp VALUES (?, ?)")
53-
.doesNotHaveTagWithKey("jdbc.params[0]"); // default is off
54+
.doesNotHaveTagWithKey("jdbc.params[0]")
55+
.hasTag("jdbc.datasource.name", "proxy-ds"); // default is off
5456
}));
5557
// @formatter:on
5658
};

0 commit comments

Comments
 (0)