Skip to content

Commit 73cab12

Browse files
committed
Added native instrumentation using OpenTelemetry API
Signed-off-by: Alexander Wert <[email protected]>
1 parent 4bd9029 commit 73cab12

File tree

15 files changed

+567
-76
lines changed

15 files changed

+567
-76
lines changed

java-client/build.gradle.kts

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919

2020
import com.github.jk1.license.ProjectData
21-
import com.github.jk1.license.render.ReportRenderer
2221
import com.github.jk1.license.render.LicenseDataCollector
22+
import com.github.jk1.license.render.ReportRenderer
2323
import java.io.FileWriter
2424

2525
plugins {
@@ -53,8 +53,8 @@ tasks.getByName<ProcessResources>("processResources") {
5353
if (name != "apis.json") {
5454
// Only process main source-set resources (test files are large)
5555
expand(
56-
"version" to version,
57-
"git_revision" to (if (rootProject.extra.has("gitHashFull")) rootProject.extra["gitHashFull"] else "unknown")
56+
"version" to version,
57+
"git_revision" to (if (rootProject.extra.has("gitHashFull")) rootProject.extra["gitHashFull"] else "unknown")
5858
)
5959
}
6060
}
@@ -69,7 +69,7 @@ tasks.withType<Jar> {
6969
if (rootProject.extra.has("gitHashFull")) {
7070
val jar = this as Jar
7171
jar.manifest.attributes["X-Git-Revision"] = rootProject.extra["gitHashFull"]
72-
jar.manifest.attributes["X-Git-Commit-Time"] = rootProject .extra["gitCommitTime"]
72+
jar.manifest.attributes["X-Git-Commit-Time"] = rootProject.extra["gitCommitTime"]
7373
} else {
7474
throw GradleException("No git information available")
7575
}
@@ -154,7 +154,7 @@ publishing {
154154
// are the same as the one used in the dependency section below.
155155
val xPathFactory = javax.xml.xpath.XPathFactory.newInstance()
156156
val depSelector = xPathFactory.newXPath()
157-
.compile("/project/dependencies/dependency[groupId/text() = 'org.elasticsearch.client']")
157+
.compile("/project/dependencies/dependency[groupId/text() = 'org.elasticsearch.client']")
158158
val versionSelector = xPathFactory.newXPath().compile("version")
159159

160160
var foundVersion = false;
@@ -183,6 +183,7 @@ dependencies {
183183
// the Java API client coexists with a 7.x HLRC work fine
184184
val elasticsearchVersion = "7.17.7"
185185
val jacksonVersion = "2.13.3"
186+
val openTelemetryVersion = "1.26.0"
186187

187188
// Apache 2.0
188189
// https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low.html
@@ -201,6 +202,13 @@ dependencies {
201202
// https://github.com/eclipse-ee4j/parsson
202203
api("org.eclipse.parsson:parsson:1.0.0")
203204

205+
// OpenTelemetry API for native instrumentation of the client.
206+
// Apache 2.0
207+
// https://github.com/open-telemetry/opentelemetry-java
208+
implementation("io.opentelemetry", "opentelemetry-api", openTelemetryVersion)
209+
implementation("io.opentelemetry", "opentelemetry-semconv", "$openTelemetryVersion-alpha")
210+
211+
204212
// EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
205213
// https://github.com/eclipse-ee4j/jsonb-api
206214
compileOnly("jakarta.json.bind", "jakarta.json.bind-api", "2.0.0")
@@ -236,6 +244,9 @@ dependencies {
236244
// https://www.testcontainers.org/
237245
testImplementation("org.testcontainers", "testcontainers", "1.17.3")
238246
testImplementation("org.testcontainers", "elasticsearch", "1.17.3")
247+
248+
249+
testImplementation("io.opentelemetry", "opentelemetry-sdk", openTelemetryVersion)
239250
}
240251

241252

@@ -247,17 +258,17 @@ licenseReport {
247258
class SpdxReporter(val dest: File) : ReportRenderer {
248259
// License names to their SPDX identifier
249260
val spdxIds = mapOf(
250-
"Apache License, Version 2.0" to "Apache-2.0",
251-
"The Apache Software License, Version 2.0" to "Apache-2.0",
252-
"BSD Zero Clause License" to "0BSD",
253-
"Eclipse Public License 2.0" to "EPL-2.0",
254-
"Eclipse Public License v. 2.0" to "EPL-2.0",
255-
"Eclipse Public License - v 2.0" to "EPL-2.0",
256-
"GNU General Public License, version 2 with the GNU Classpath Exception" to "GPL-2.0 WITH Classpath-exception-2.0",
257-
"COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0" to "CDDL-1.0"
261+
"Apache License, Version 2.0" to "Apache-2.0",
262+
"The Apache Software License, Version 2.0" to "Apache-2.0",
263+
"BSD Zero Clause License" to "0BSD",
264+
"Eclipse Public License 2.0" to "EPL-2.0",
265+
"Eclipse Public License v. 2.0" to "EPL-2.0",
266+
"Eclipse Public License - v 2.0" to "EPL-2.0",
267+
"GNU General Public License, version 2 with the GNU Classpath Exception" to "GPL-2.0 WITH Classpath-exception-2.0",
268+
"COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0" to "CDDL-1.0"
258269
)
259270

260-
private fun quote(str: String) : String {
271+
private fun quote(str: String): String {
261272
return if (str.contains(',') || str.contains("\"")) {
262273
"\"" + str.replace("\"", "\"\"") + "\""
263274
} else {

java-client/src/main/java/co/elastic/clients/transport/Endpoint.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ public interface Endpoint<RequestT, ResponseT, ErrorT> {
5656
*/
5757
String requestUrl(RequestT request);
5858

59+
/**
60+
* Get the route for a request (i.e. URL pattern).
61+
*/
62+
String route(RequestT request);
63+
64+
/**
65+
* Get the path parameters for a request.
66+
*/
67+
default Map<String, String> pathParameters(RequestT request) {
68+
return Collections.emptyMap();
69+
}
70+
5971
/**
6072
* Get the query parameters for a request.
6173
*/
@@ -104,6 +116,8 @@ default BinaryEndpoint<RequestT> withBinaryResponse() {
104116
this.id(),
105117
this::method,
106118
this::requestUrl,
119+
this::route,
120+
this::pathParameters,
107121
this::queryParameters,
108122
this::headers,
109123
this::body,

java-client/src/main/java/co/elastic/clients/transport/endpoints/BinaryEndpoint.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,32 @@ public BinaryEndpoint(
2828
String id,
2929
Function<RequestT, String> method,
3030
Function<RequestT, String> requestUrl,
31+
Function<RequestT, String> route,
32+
Function<RequestT,
33+
Map<String, String>> pathParameters,
3134
Function<RequestT,
3235
Map<String, String>> queryParameters,
3336
Function<RequestT, Map<String, String>> headers,
3437
Function<RequestT, Object> body,
3538
Object ignored // same number of arguments as SimpleEndpoint
3639
) {
37-
super(id, method, requestUrl, queryParameters, headers, body);
40+
super(id, method, requestUrl, route, pathParameters, queryParameters, headers, body);
3841
}
3942

4043
public BinaryEndpoint(
4144
String id,
4245
Function<RequestT, String> method,
4346
Function<RequestT, String> requestUrl,
47+
Function<RequestT, String> route,
48+
Function<RequestT,
49+
Map<String, String>> pathParameters,
4450
Function<RequestT,
4551
Map<String, String>> queryParameters,
4652
Function<RequestT, Map<String, String>> headers,
4753
boolean hasRequestBody,
4854
Object ignored // same number of arguments as SimpleEndpoint
4955
) {
50-
super(id, method, requestUrl, queryParameters, headers, hasRequestBody ? returnSelf() : returnNull());
56+
super(id, method, requestUrl, route, pathParameters, queryParameters, headers, hasRequestBody ? returnSelf() : returnNull());
5157
}
5258

5359
@Override

java-client/src/main/java/co/elastic/clients/transport/endpoints/BooleanEndpoint.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ public BooleanEndpoint(
2828
String id,
2929
Function<RequestT, String> method,
3030
Function<RequestT, String> requestUrl,
31+
Function<RequestT, String> route,
32+
Function<RequestT,
33+
Map<String, String>> pathParameters,
3134
Function<RequestT,
3235
Map<String, String>> queryParameters,
3336
Function<RequestT, Map<String, String>> headers,
3437
boolean hasRequestBody,
3538
Object ignored // same number of arguments as SimpleEndpoint
3639
) {
37-
super(id, method, requestUrl, queryParameters, headers, hasRequestBody ? returnSelf() : returnNull());
40+
super(id, method, requestUrl, route, pathParameters, queryParameters, headers, hasRequestBody ? returnSelf() : returnNull());
3841
}
3942

4043
@Override

java-client/src/main/java/co/elastic/clients/transport/endpoints/DelegatingJsonEndpoint.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ public String requestUrl(Req request) {
4848
return endpoint.requestUrl(request);
4949
}
5050

51+
@Override
52+
public String route(Req request) {
53+
return endpoint.route(request);
54+
}
55+
56+
@Override
57+
public Map<String, String> pathParameters(Req request) {
58+
return endpoint.pathParameters(request);
59+
}
60+
5161
@Override
5262
public Map<String, String> queryParameters(Req request) {
5363
return endpoint.queryParameters(request);

java-client/src/main/java/co/elastic/clients/transport/endpoints/EndpointBase.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ static <T, U> Function<T, U> returnSelf() {
6464
protected final String id;
6565
protected final Function<RequestT, String> method;
6666
protected final Function<RequestT, String> requestUrl;
67+
protected final Function<RequestT, String> route;
68+
protected final Function<RequestT, Map<String, String>> pathParameters;
6769
protected final Function<RequestT, Map<String, String>> queryParameters;
6870
protected final Function<RequestT, Map<String, String>> headers;
6971
protected final Function<RequestT, Object> body;
@@ -72,13 +74,17 @@ public EndpointBase(
7274
String id,
7375
Function<RequestT, String> method,
7476
Function<RequestT, String> requestUrl,
77+
Function<RequestT, String> route,
78+
Function<RequestT, Map<String, String>> pathParameters,
7579
Function<RequestT, Map<String, String>> queryParameters,
7680
Function<RequestT, Map<String, String>> headers,
7781
Function<RequestT, Object> body
7882
) {
7983
this.id = id;
8084
this.method = method;
8185
this.requestUrl = requestUrl;
86+
this.route = route;
87+
this.pathParameters = pathParameters;
8288
this.queryParameters = queryParameters;
8389
this.headers = headers;
8490
this.body = body;
@@ -99,6 +105,16 @@ public String requestUrl(RequestT request) {
99105
return this.requestUrl.apply(request);
100106
}
101107

108+
@Override
109+
public String route(RequestT request) {
110+
return this.route.apply(request);
111+
}
112+
113+
@Override
114+
public Map<String, String> pathParameters(RequestT request) {
115+
return this.pathParameters.apply(request);
116+
}
117+
102118
@Override
103119
public Map<String, String> queryParameters(RequestT request) {
104120
return this.queryParameters.apply(request);
@@ -133,6 +149,8 @@ public <NewResponseT> SimpleEndpoint<RequestT, NewResponseT> withResponseDeseria
133149
id,
134150
method,
135151
requestUrl,
152+
route,
153+
pathParameters,
136154
queryParameters,
137155
headers,
138156
body,

java-client/src/main/java/co/elastic/clients/transport/endpoints/SimpleEndpoint.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,23 @@ public SimpleEndpoint(
3636
String id,
3737
Function<RequestT, String> method,
3838
Function<RequestT, String> requestUrl,
39+
Function<RequestT, String> route,
40+
Function<RequestT, Map<String, String>> pathParameters,
3941
Function<RequestT, Map<String, String>> queryParameters,
4042
Function<RequestT, Map<String, String>> headers,
4143
Function<RequestT, Object> body,
4244
JsonpDeserializer<ResponseT> responseParser
4345
) {
44-
super(id, method, requestUrl, queryParameters, headers, body);
46+
super(id, method, requestUrl, route, pathParameters, queryParameters, headers, body);
4547
this.responseParser = responseParser;
4648
}
4749

4850
public SimpleEndpoint(
4951
String id,
5052
Function<RequestT, String> method,
5153
Function<RequestT, String> requestUrl,
54+
Function<RequestT, String> route,
55+
Function<RequestT, Map<String, String>> pathParameters,
5256
Function<RequestT, Map<String, String>> queryParameters,
5357
Function<RequestT, Map<String, String>> headers,
5458
boolean hasResponseBody,
@@ -58,6 +62,8 @@ public SimpleEndpoint(
5862
id,
5963
method,
6064
requestUrl,
65+
route,
66+
pathParameters,
6167
queryParameters,
6268
headers,
6369
hasResponseBody ? returnSelf() : returnNull(),
@@ -82,6 +88,8 @@ public <NewResponseT> SimpleEndpoint<RequestT, NewResponseT> withResponseDeseria
8288
id,
8389
method,
8490
requestUrl,
91+
route,
92+
pathParameters,
8593
queryParameters,
8694
headers,
8795
body,

java-client/src/main/java/co/elastic/clients/transport/endpoints/SimpleJsonEndpoint.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ public SimpleJsonEndpoint(
3333
String id,
3434
Function<RequestT, String> method,
3535
Function<RequestT, String> requestUrl,
36+
Function<RequestT, String> route,
3637
Function<RequestT,
37-
Map<String, String>> queryParameters,
38+
Map<String, String>> pathParameters,
39+
Function<RequestT,
40+
Map<String, String>> queryParameters,
3841
Function<RequestT, Map<String, String>> headers,
3942
boolean hasRequestBody,
4043
JsonpDeserializer<ResponseT> responseParser
4144
) {
42-
super(id, method, requestUrl, queryParameters, headers, hasRequestBody, responseParser);
45+
super(id, method, requestUrl, route, pathParameters, queryParameters, headers, hasRequestBody, responseParser);
4346
}
4447
}

0 commit comments

Comments
 (0)