Skip to content

Commit

Permalink
Added native instrumentation using OpenTelemetry API
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Wert <[email protected]>
  • Loading branch information
AlexanderWert committed May 28, 2023
1 parent 4bd9029 commit 73cab12
Show file tree
Hide file tree
Showing 15 changed files with 567 additions and 76 deletions.
39 changes: 25 additions & 14 deletions java-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/

import com.github.jk1.license.ProjectData
import com.github.jk1.license.render.ReportRenderer
import com.github.jk1.license.render.LicenseDataCollector
import com.github.jk1.license.render.ReportRenderer
import java.io.FileWriter

plugins {
Expand Down Expand Up @@ -53,8 +53,8 @@ tasks.getByName<ProcessResources>("processResources") {
if (name != "apis.json") {
// Only process main source-set resources (test files are large)
expand(
"version" to version,
"git_revision" to (if (rootProject.extra.has("gitHashFull")) rootProject.extra["gitHashFull"] else "unknown")
"version" to version,
"git_revision" to (if (rootProject.extra.has("gitHashFull")) rootProject.extra["gitHashFull"] else "unknown")
)
}
}
Expand All @@ -69,7 +69,7 @@ tasks.withType<Jar> {
if (rootProject.extra.has("gitHashFull")) {
val jar = this as Jar
jar.manifest.attributes["X-Git-Revision"] = rootProject.extra["gitHashFull"]
jar.manifest.attributes["X-Git-Commit-Time"] = rootProject .extra["gitCommitTime"]
jar.manifest.attributes["X-Git-Commit-Time"] = rootProject.extra["gitCommitTime"]
} else {
throw GradleException("No git information available")
}
Expand Down Expand Up @@ -154,7 +154,7 @@ publishing {
// are the same as the one used in the dependency section below.
val xPathFactory = javax.xml.xpath.XPathFactory.newInstance()
val depSelector = xPathFactory.newXPath()
.compile("/project/dependencies/dependency[groupId/text() = 'org.elasticsearch.client']")
.compile("/project/dependencies/dependency[groupId/text() = 'org.elasticsearch.client']")
val versionSelector = xPathFactory.newXPath().compile("version")

var foundVersion = false;
Expand Down Expand Up @@ -183,6 +183,7 @@ dependencies {
// the Java API client coexists with a 7.x HLRC work fine
val elasticsearchVersion = "7.17.7"
val jacksonVersion = "2.13.3"
val openTelemetryVersion = "1.26.0"

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

// OpenTelemetry API for native instrumentation of the client.
// Apache 2.0
// https://github.com/open-telemetry/opentelemetry-java
implementation("io.opentelemetry", "opentelemetry-api", openTelemetryVersion)
implementation("io.opentelemetry", "opentelemetry-semconv", "$openTelemetryVersion-alpha")


// EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
// https://github.com/eclipse-ee4j/jsonb-api
compileOnly("jakarta.json.bind", "jakarta.json.bind-api", "2.0.0")
Expand Down Expand Up @@ -236,6 +244,9 @@ dependencies {
// https://www.testcontainers.org/
testImplementation("org.testcontainers", "testcontainers", "1.17.3")
testImplementation("org.testcontainers", "elasticsearch", "1.17.3")


testImplementation("io.opentelemetry", "opentelemetry-sdk", openTelemetryVersion)
}


Expand All @@ -247,17 +258,17 @@ licenseReport {
class SpdxReporter(val dest: File) : ReportRenderer {
// License names to their SPDX identifier
val spdxIds = mapOf(
"Apache License, Version 2.0" to "Apache-2.0",
"The Apache Software License, Version 2.0" to "Apache-2.0",
"BSD Zero Clause License" to "0BSD",
"Eclipse Public License 2.0" to "EPL-2.0",
"Eclipse Public License v. 2.0" to "EPL-2.0",
"Eclipse Public License - v 2.0" to "EPL-2.0",
"GNU General Public License, version 2 with the GNU Classpath Exception" to "GPL-2.0 WITH Classpath-exception-2.0",
"COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0" to "CDDL-1.0"
"Apache License, Version 2.0" to "Apache-2.0",
"The Apache Software License, Version 2.0" to "Apache-2.0",
"BSD Zero Clause License" to "0BSD",
"Eclipse Public License 2.0" to "EPL-2.0",
"Eclipse Public License v. 2.0" to "EPL-2.0",
"Eclipse Public License - v 2.0" to "EPL-2.0",
"GNU General Public License, version 2 with the GNU Classpath Exception" to "GPL-2.0 WITH Classpath-exception-2.0",
"COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0" to "CDDL-1.0"
)

private fun quote(str: String) : String {
private fun quote(str: String): String {
return if (str.contains(',') || str.contains("\"")) {
"\"" + str.replace("\"", "\"\"") + "\""
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ public interface Endpoint<RequestT, ResponseT, ErrorT> {
*/
String requestUrl(RequestT request);

/**
* Get the route for a request (i.e. URL pattern).
*/
String route(RequestT request);

/**
* Get the path parameters for a request.
*/
default Map<String, String> pathParameters(RequestT request) {
return Collections.emptyMap();
}

/**
* Get the query parameters for a request.
*/
Expand Down Expand Up @@ -104,6 +116,8 @@ default BinaryEndpoint<RequestT> withBinaryResponse() {
this.id(),
this::method,
this::requestUrl,
this::route,
this::pathParameters,
this::queryParameters,
this::headers,
this::body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,32 @@ public BinaryEndpoint(
String id,
Function<RequestT, String> method,
Function<RequestT, String> requestUrl,
Function<RequestT, String> route,
Function<RequestT,
Map<String, String>> pathParameters,
Function<RequestT,
Map<String, String>> queryParameters,
Function<RequestT, Map<String, String>> headers,
Function<RequestT, Object> body,
Object ignored // same number of arguments as SimpleEndpoint
) {
super(id, method, requestUrl, queryParameters, headers, body);
super(id, method, requestUrl, route, pathParameters, queryParameters, headers, body);
}

public BinaryEndpoint(
String id,
Function<RequestT, String> method,
Function<RequestT, String> requestUrl,
Function<RequestT, String> route,
Function<RequestT,
Map<String, String>> pathParameters,
Function<RequestT,
Map<String, String>> queryParameters,
Function<RequestT, Map<String, String>> headers,
boolean hasRequestBody,
Object ignored // same number of arguments as SimpleEndpoint
) {
super(id, method, requestUrl, queryParameters, headers, hasRequestBody ? returnSelf() : returnNull());
super(id, method, requestUrl, route, pathParameters, queryParameters, headers, hasRequestBody ? returnSelf() : returnNull());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ public BooleanEndpoint(
String id,
Function<RequestT, String> method,
Function<RequestT, String> requestUrl,
Function<RequestT, String> route,
Function<RequestT,
Map<String, String>> pathParameters,
Function<RequestT,
Map<String, String>> queryParameters,
Function<RequestT, Map<String, String>> headers,
boolean hasRequestBody,
Object ignored // same number of arguments as SimpleEndpoint
) {
super(id, method, requestUrl, queryParameters, headers, hasRequestBody ? returnSelf() : returnNull());
super(id, method, requestUrl, route, pathParameters, queryParameters, headers, hasRequestBody ? returnSelf() : returnNull());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ public String requestUrl(Req request) {
return endpoint.requestUrl(request);
}

@Override
public String route(Req request) {
return endpoint.route(request);
}

@Override
public Map<String, String> pathParameters(Req request) {
return endpoint.pathParameters(request);
}

@Override
public Map<String, String> queryParameters(Req request) {
return endpoint.queryParameters(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ static <T, U> Function<T, U> returnSelf() {
protected final String id;
protected final Function<RequestT, String> method;
protected final Function<RequestT, String> requestUrl;
protected final Function<RequestT, String> route;
protected final Function<RequestT, Map<String, String>> pathParameters;
protected final Function<RequestT, Map<String, String>> queryParameters;
protected final Function<RequestT, Map<String, String>> headers;
protected final Function<RequestT, Object> body;
Expand All @@ -72,13 +74,17 @@ public EndpointBase(
String id,
Function<RequestT, String> method,
Function<RequestT, String> requestUrl,
Function<RequestT, String> route,
Function<RequestT, Map<String, String>> pathParameters,
Function<RequestT, Map<String, String>> queryParameters,
Function<RequestT, Map<String, String>> headers,
Function<RequestT, Object> body
) {
this.id = id;
this.method = method;
this.requestUrl = requestUrl;
this.route = route;
this.pathParameters = pathParameters;
this.queryParameters = queryParameters;
this.headers = headers;
this.body = body;
Expand All @@ -99,6 +105,16 @@ public String requestUrl(RequestT request) {
return this.requestUrl.apply(request);
}

@Override
public String route(RequestT request) {
return this.route.apply(request);
}

@Override
public Map<String, String> pathParameters(RequestT request) {
return this.pathParameters.apply(request);
}

@Override
public Map<String, String> queryParameters(RequestT request) {
return this.queryParameters.apply(request);
Expand Down Expand Up @@ -133,6 +149,8 @@ public <NewResponseT> SimpleEndpoint<RequestT, NewResponseT> withResponseDeseria
id,
method,
requestUrl,
route,
pathParameters,
queryParameters,
headers,
body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,23 @@ public SimpleEndpoint(
String id,
Function<RequestT, String> method,
Function<RequestT, String> requestUrl,
Function<RequestT, String> route,
Function<RequestT, Map<String, String>> pathParameters,
Function<RequestT, Map<String, String>> queryParameters,
Function<RequestT, Map<String, String>> headers,
Function<RequestT, Object> body,
JsonpDeserializer<ResponseT> responseParser
) {
super(id, method, requestUrl, queryParameters, headers, body);
super(id, method, requestUrl, route, pathParameters, queryParameters, headers, body);
this.responseParser = responseParser;
}

public SimpleEndpoint(
String id,
Function<RequestT, String> method,
Function<RequestT, String> requestUrl,
Function<RequestT, String> route,
Function<RequestT, Map<String, String>> pathParameters,
Function<RequestT, Map<String, String>> queryParameters,
Function<RequestT, Map<String, String>> headers,
boolean hasResponseBody,
Expand All @@ -58,6 +62,8 @@ public SimpleEndpoint(
id,
method,
requestUrl,
route,
pathParameters,
queryParameters,
headers,
hasResponseBody ? returnSelf() : returnNull(),
Expand All @@ -82,6 +88,8 @@ public <NewResponseT> SimpleEndpoint<RequestT, NewResponseT> withResponseDeseria
id,
method,
requestUrl,
route,
pathParameters,
queryParameters,
headers,
body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ public SimpleJsonEndpoint(
String id,
Function<RequestT, String> method,
Function<RequestT, String> requestUrl,
Function<RequestT, String> route,
Function<RequestT,
Map<String, String>> queryParameters,
Map<String, String>> pathParameters,
Function<RequestT,
Map<String, String>> queryParameters,
Function<RequestT, Map<String, String>> headers,
boolean hasRequestBody,
JsonpDeserializer<ResponseT> responseParser
) {
super(id, method, requestUrl, queryParameters, headers, hasRequestBody, responseParser);
super(id, method, requestUrl, route, pathParameters, queryParameters, headers, hasRequestBody, responseParser);
}
}
Loading

0 comments on commit 73cab12

Please sign in to comment.