Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add x-restate-server header in responses #285

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ allprojects {

configure<com.diffplug.gradle.spotless.SpotlessExtension> {
java {
targetExclude("build/generated/**/*.java")
targetExclude("build/**/*.java")

googleJavaFormat()

Expand Down
64 changes: 64 additions & 0 deletions sdk-common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,67 @@ dependencies {
testImplementation(testingLibs.junit.jupiter)
testImplementation(testingLibs.assertj)
}

val generatedVersionDir = layout.buildDirectory.dir("version")

generatedVersionDir.get().asFile.mkdirs()

sourceSets { main { java { srcDir(generatedVersionDir) } } }

// Configure generation of version class

// From https://discuss.kotlinlang.org/t/use-git-hash-as-version-number-in-build-gradle-kts/19818/4
fun String.runCommand(
workingDir: File = File("."),
timeoutAmount: Long = 5,
timeoutUnit: TimeUnit = TimeUnit.SECONDS
): String =
ProcessBuilder(split("\\s(?=(?:[^'\"`]*(['\"`])[^'\"`]*\\1)*[^'\"`]*$)".toRegex()))
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
.apply { waitFor(timeoutAmount, timeoutUnit) }
.run {
val error = errorStream.bufferedReader().readText().trim()
if (error.isNotEmpty()) {
throw IllegalStateException(error)
}
inputStream.bufferedReader().readText().trim()
}

val generateVersionClass =
tasks.register("generateVersionClass") {
dependsOn(project.tasks.processResources)
outputs.dir(generatedVersionDir)

doFirst {
val gitHash = "git rev-parse --short=8 HEAD".runCommand(workingDir = rootDir)
val containingDir = generatedVersionDir.get().dir("dev/restate/sdk/version").asFile
assert(containingDir.exists() || containingDir.mkdirs())

file("$containingDir/Version.java")
.writeText(
"""
package dev.restate.sdk.version;

public final class Version {
private Version() {}

public static final String VERSION = "$version";
public static final String GIT_HASH = "$gitHash";
public static final String X_RESTATE_SERVER = "restate-sdk-java/" + VERSION + "_" + GIT_HASH;
}
"""
.trimIndent())

check(file("${projectDir}/build/version/dev/restate/sdk/version/Version.java").exists()) {
"${projectDir}/build/version/dev/restate/sdk/version/Version.java doesn't exist?!"
}
}
}

tasks {
withType<JavaCompile>().configureEach { dependsOn(generateVersionClass) }
withType<Jar>().configureEach { dependsOn(generateVersionClass) }
}
2 changes: 0 additions & 2 deletions sdk-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import com.google.protobuf.gradle.id

plugins {
`java-library`
`library-publishing-conventions`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import dev.restate.sdk.core.ResolvedEndpointHandler;
import dev.restate.sdk.core.RestateEndpoint;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.version.Version;
import io.netty.util.AsciiString;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.propagation.TextMapGetter;
Expand All @@ -41,6 +42,9 @@ class RequestHttpServerHandler implements Handler<HttpServerRequest> {
private static final Logger LOG = LogManager.getLogger(RequestHttpServerHandler.class);

private static final AsciiString APPLICATION_RESTATE = AsciiString.cached("application/restate");
private static final AsciiString X_RESTATE_SERVER_KEY = AsciiString.cached("x-restate-server");
private static final AsciiString X_RESTATE_SERVER_VALUE =
AsciiString.cached(Version.X_RESTATE_SERVER);
private static final ObjectMapper MANIFEST_OBJECT_MAPPER = new ObjectMapper();

private static final Pattern SLASH = Pattern.compile(Pattern.quote("/"));
Expand Down Expand Up @@ -127,7 +131,9 @@ public void handle(HttpServerRequest request) {
// Vert.x will send them as soon as we send the first write
HttpServerResponse response = request.response();
response.setStatusCode(OK.code());
response.putHeader(CONTENT_TYPE, APPLICATION_RESTATE);
response
.putHeader(CONTENT_TYPE, APPLICATION_RESTATE)
.putHeader(X_RESTATE_SERVER_KEY, X_RESTATE_SERVER_VALUE);
// This is No-op for HTTP2
response.setChunked(true);

Expand Down Expand Up @@ -159,6 +165,7 @@ private void handleDiscoveryRequest(HttpServerRequest request) {
request
.response()
.setStatusCode(OK.code())
.putHeader(X_RESTATE_SERVER_KEY, X_RESTATE_SERVER_VALUE)
.putHeader(CONTENT_TYPE, APPLICATION_JSON)
.end(responseBuffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import dev.restate.sdk.core.ResolvedEndpointHandler;
import dev.restate.sdk.core.RestateEndpoint;
import dev.restate.sdk.core.manifest.DeploymentManifestSchema;
import dev.restate.sdk.version.Version;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.propagation.TextMapGetter;
import java.nio.ByteBuffer;
Expand All @@ -39,9 +40,9 @@ public final class RestateLambdaEndpoint {
private static final String INVOKE_PATH_SEGMENT = "invoke";
private static final String DISCOVER_PATH = "/discover";
private static final Map<String, String> INVOKE_RESPONSE_HEADERS =
Map.of("content-type", "application/restate");
Map.of("content-type", "application/restate", "x-restate-server", Version.X_RESTATE_SERVER);
private static final Map<String, String> DISCOVER_RESPONSE_HEADERS =
Map.of("content-type", "application/json");
Map.of("content-type", "application/json", "x-restate-server", Version.X_RESTATE_SERVER);

private static TextMapGetter<Map<String, String>> OTEL_HEADERS_GETTER =
new TextMapGetter<>() {
Expand Down
Loading