Skip to content

Commit 52e9a5e

Browse files
GH-2102 - Add user agent to transaction meta data.
Closes #2102.
1 parent 525eaa2 commit 52e9a5e

File tree

4 files changed

+140
-12
lines changed

4 files changed

+140
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2011-2022 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+
package org.springframework.data.neo4j.core.support;
17+
18+
import org.neo4j.driver.Driver;
19+
import org.springframework.data.mapping.context.AbstractMappingContext;
20+
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
21+
import org.springframework.lang.Nullable;
22+
23+
/**
24+
* Representation of a user agent containing sensible information to identify queries generated by or executed via Spring Data Neo4j.
25+
*
26+
* @author Michael J. Simons
27+
* @since 6.1.11
28+
*/
29+
public enum UserAgent {
30+
31+
INSTANCE(
32+
getVersionOf(Driver.class),
33+
getVersionOf(AbstractMappingContext.class),
34+
getVersionOf(EnableNeo4jRepositories.class)
35+
);
36+
37+
@Nullable
38+
private final String driverVersion;
39+
40+
@Nullable
41+
private final String springDataVersion;
42+
43+
@Nullable
44+
private final String sdnVersion;
45+
46+
private final String representation;
47+
48+
UserAgent(@Nullable String driverVersion, @Nullable String springDataVersion, @Nullable String sdnVersion) {
49+
int idxOfDash = driverVersion == null ? -1 : driverVersion.indexOf('-');
50+
this.driverVersion = driverVersion == null ?
51+
null :
52+
driverVersion.substring(0, idxOfDash > 0 ? idxOfDash : driverVersion.length());
53+
this.springDataVersion = springDataVersion;
54+
this.sdnVersion = sdnVersion;
55+
56+
String unknown = "-";
57+
this.representation = String.format("Java/%s (%s %s %s) neo4j-java/%s spring-data/%s spring-data-neo4j/%s",
58+
System.getProperty("java.version"),
59+
System.getProperty("java.vm.vendor"),
60+
System.getProperty("java.vm.name"),
61+
System.getProperty("java.vm.version"),
62+
this.driverVersion == null ? unknown : this.driverVersion,
63+
this.springDataVersion == null ? unknown : this.springDataVersion,
64+
this.sdnVersion == null ? unknown : this.sdnVersion
65+
);
66+
}
67+
68+
@Nullable
69+
public String getDriverVersion() {
70+
return driverVersion;
71+
}
72+
73+
@Nullable
74+
public String getSpringDataVersion() {
75+
return springDataVersion;
76+
}
77+
78+
@Nullable
79+
public String getSdnVersion() {
80+
return sdnVersion;
81+
}
82+
83+
@Nullable
84+
private static String getVersionOf(Class<?> type) {
85+
86+
Package p = type.getPackage();
87+
String version = p.getImplementationVersion();
88+
if (!(version == null || version.trim().isEmpty())) {
89+
return version;
90+
}
91+
return null;
92+
}
93+
94+
@Override
95+
public String toString() {
96+
return this.representation;
97+
}
98+
}

src/main/java/org/springframework/data/neo4j/core/transaction/Neo4jTransactionUtils.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.neo4j.driver.SessionConfig;
2626
import org.neo4j.driver.TransactionConfig;
2727
import org.springframework.lang.Nullable;
28+
import org.springframework.data.neo4j.core.support.UserAgent;
2829
import org.springframework.transaction.IllegalTransactionStateException;
2930
import org.springframework.transaction.InvalidIsolationLevelException;
3031
import org.springframework.transaction.TransactionDefinition;
@@ -88,7 +89,9 @@ static TransactionConfig createTransactionConfigFrom(TransactionDefinition defin
8889
builder = builder.withTimeout(Duration.ofSeconds(defaultTxManagerTimeout));
8990
}
9091

91-
return builder.build();
92+
return builder
93+
.withMetadata(Collections.singletonMap("app", UserAgent.INSTANCE.toString()))
94+
.build();
9295
}
9396

9497
static boolean namesMapToTheSameDatabase(@Nullable String name1, @Nullable String name2) {

src/main/java/org/springframework/data/neo4j/repository/config/StartupLogger.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
import java.util.Optional;
1919

2020
import org.apache.commons.logging.LogFactory;
21-
import org.neo4j.driver.Driver;
2221
import org.springframework.core.log.LogAccessor;
23-
import org.springframework.data.mapping.context.AbstractMappingContext;
22+
import org.springframework.data.neo4j.core.support.UserAgent;
2423

2524
/**
2625
* Logs startup information.
@@ -59,23 +58,18 @@ void logStarting() {
5958
String getStartingMessage() {
6059

6160
StringBuilder sb = new StringBuilder();
61+
UserAgent userAgent = UserAgent.INSTANCE;
6262

63-
String sdnRx = getVersionOf(EnableNeo4jRepositories.class).map(v -> "SDN v" + v)
63+
String sdnRx = Optional.ofNullable(userAgent.getSdnVersion()).map(v -> "SDN v" + v)
6464
.orElse("an unknown version of SDN");
65-
String sdC = getVersionOf(AbstractMappingContext.class).map(v -> "Spring Data Commons v" + v)
65+
String sdC = Optional.ofNullable(userAgent.getSpringDataVersion()).map(v -> "Spring Data Commons v" + v)
6666
.orElse("an unknown version of Spring Data Commons");
67-
String driver = getVersionOf(Driver.class).map(v -> "Neo4j Driver v" + v)
67+
String driver = Optional.ofNullable(userAgent.getDriverVersion()).map(v -> "Neo4j Driver v" + v)
6868
.orElse("an unknown version of the Neo4j Java Driver");
6969

7070
sb.append("Bootstrapping ").append(mode.displayValue).append(" Neo4j repositories based on ").append(sdnRx)
7171
.append(" with ").append(sdC).append(" and ").append(driver).append(".");
7272

7373
return sb.toString();
7474
}
75-
76-
private Optional<String> getVersionOf(Class<?> clazz) {
77-
78-
return Optional.ofNullable(clazz).map(Class::getPackage).map(Package::getImplementationVersion).map(String::trim)
79-
.filter(v -> !v.isEmpty());
80-
}
8175
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2011-2022 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+
package org.springframework.data.neo4j.core.support;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
/**
23+
* @author Michael J. Simons
24+
*/
25+
class UserAgentTest {
26+
27+
@Test
28+
void toStringShouldWork() {
29+
30+
assertThat(UserAgent.INSTANCE.toString())
31+
.matches("Java/.+ \\(.+\\) neo4j-java/.+ spring-data/.+ spring-data-neo4j/.+");
32+
}
33+
}

0 commit comments

Comments
 (0)