Skip to content

Commit a3c4291

Browse files
Propagate otelContext in Request and in thread-local/coroutine context.
1 parent ce8cf10 commit a3c4291

File tree

9 files changed

+36
-26
lines changed

9 files changed

+36
-26
lines changed

sdk-api-kotlin/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ dependencies {
1515
implementation(kotlinLibs.kotlinx.serialization.json)
1616

1717
implementation(coreLibs.log4j.api)
18+
implementation(platform(coreLibs.opentelemetry.bom))
19+
implementation(coreLibs.opentelemetry.kotlin)
1820

1921
testImplementation(project(":sdk-core"))
2022
testImplementation(testingLibs.junit.jupiter)

sdk-api-kotlin/src/main/kotlin/dev/restate/sdk/kotlin/Service.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package dev.restate.sdk.kotlin
1111
import com.google.protobuf.ByteString
1212
import dev.restate.sdk.common.*
1313
import dev.restate.sdk.common.syscalls.*
14+
import io.opentelemetry.extension.kotlin.asContextElement
1415
import kotlin.coroutines.CoroutineContext
1516
import kotlinx.coroutines.CoroutineScope
1617
import kotlinx.coroutines.Dispatchers
@@ -138,7 +139,8 @@ private constructor(
138139
val scope =
139140
CoroutineScope(
140141
options.coroutineContext +
141-
InvocationHandler.SYSCALLS_THREAD_LOCAL.asContextElement(syscalls))
142+
InvocationHandler.SYSCALLS_THREAD_LOCAL.asContextElement(syscalls) +
143+
syscalls.request().otelContext()!!.asContextElement())
142144
scope.launch {
143145
val serializedResult: ByteString
144146

sdk-api/src/main/java/dev/restate/sdk/Service.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import dev.restate.sdk.common.*;
1313
import dev.restate.sdk.common.ServiceType;
1414
import dev.restate.sdk.common.syscalls.*;
15+
import io.opentelemetry.context.Scope;
1516
import java.util.*;
1617
import java.util.concurrent.Executor;
1718
import java.util.concurrent.Executors;
@@ -105,7 +106,6 @@ public Service build(Service.Options options) {
105106
}
106107
}
107108

108-
@SuppressWarnings("unchecked")
109109
public static class Handler<REQ, RES> implements InvocationHandler<Service.Options> {
110110
private final HandlerSignature<REQ, RES> handlerSignature;
111111
private final HandlerType handlerType;
@@ -119,6 +119,7 @@ public Handler(
119119
BiFunction<? extends Context, REQ, RES> runner) {
120120
this.handlerSignature = handlerSignature;
121121
this.handlerType = handlerType;
122+
//noinspection unchecked
122123
this.runner = (BiFunction<Context, REQ, RES>) runner;
123124
}
124125

@@ -148,7 +149,7 @@ public void handle(
148149
options.executor.execute(
149150
() -> {
150151
SYSCALLS_THREAD_LOCAL.set(syscalls);
151-
try {
152+
try (Scope ignored = syscalls.request().otelContext().makeCurrent()) {
152153
runnable.run();
153154
} finally {
154155
SYSCALLS_THREAD_LOCAL.remove();

sdk-common/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ dependencies {
99
compileOnly(coreLibs.jspecify)
1010

1111
api(coreLibs.protobuf.java)
12+
api(platform(coreLibs.opentelemetry.bom))
13+
api(coreLibs.opentelemetry.api)
1214

1315
implementation(platform(jacksonLibs.jackson.bom))
1416
implementation(jacksonLibs.jackson.core)

sdk-common/src/main/java/dev/restate/sdk/common/Request.java

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,24 @@
99
package dev.restate.sdk.common;
1010

1111
import com.google.protobuf.ByteString;
12+
import io.opentelemetry.context.Context;
1213
import java.util.Map;
1314
import java.util.Objects;
1415

1516
public final class Request {
1617

1718
private final InvocationId invocationId;
19+
private final Context otelContext;
1820
private final ByteString body;
1921
private final Map<String, String> headers;
2022

21-
public Request(InvocationId invocationId, ByteString body, Map<String, String> headers) {
23+
public Request(
24+
InvocationId invocationId,
25+
Context otelContext,
26+
ByteString body,
27+
Map<String, String> headers) {
2228
this.invocationId = invocationId;
29+
this.otelContext = otelContext;
2330
this.body = body;
2431
this.headers = headers;
2532
}
@@ -28,6 +35,10 @@ public InvocationId invocationId() {
2835
return invocationId;
2936
}
3037

38+
public Context otelContext() {
39+
return otelContext;
40+
}
41+
3142
public byte[] body() {
3243
return body.toByteArray();
3344
}
@@ -46,29 +57,23 @@ public boolean equals(Object o) {
4657
if (o == null || getClass() != o.getClass()) return false;
4758

4859
Request request = (Request) o;
49-
50-
if (!Objects.equals(invocationId, request.invocationId)) return false;
51-
if (!Objects.equals(body, request.body)) return false;
52-
return Objects.equals(headers, request.headers);
60+
return Objects.equals(invocationId, request.invocationId)
61+
&& Objects.equals(otelContext, request.otelContext)
62+
&& Objects.equals(body, request.body)
63+
&& Objects.equals(headers, request.headers);
5364
}
5465

5566
@Override
5667
public int hashCode() {
57-
int result = invocationId != null ? invocationId.hashCode() : 0;
58-
result = 31 * result + (body != null ? body.hashCode() : 0);
59-
result = 31 * result + (headers != null ? headers.hashCode() : 0);
68+
int result = Objects.hashCode(invocationId);
69+
result = 31 * result + Objects.hashCode(otelContext);
70+
result = 31 * result + Objects.hashCode(body);
71+
result = 31 * result + Objects.hashCode(headers);
6072
return result;
6173
}
6274

6375
@Override
6476
public String toString() {
65-
return "Request{"
66-
+ "invocationId="
67-
+ invocationId
68-
+ ", body="
69-
+ body
70-
+ ", headers="
71-
+ headers
72-
+ '}';
77+
return "Request{" + "invocationId=" + invocationId + ", headers=" + headers + '}';
7378
}
7479
}

sdk-core/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ dependencies {
2424

2525
implementation(platform(coreLibs.opentelemetry.bom))
2626
implementation(coreLibs.opentelemetry.api)
27-
implementation(coreLibs.opentelemetry.semconv)
2827

2928
testCompileOnly(coreLibs.jspecify)
3029
testImplementation(testingLibs.junit.jupiter)

sdk-core/src/main/java/dev/restate/sdk/core/InvocationStateMachine.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import dev.restate.sdk.common.syscalls.*;
2020
import io.opentelemetry.api.common.Attributes;
2121
import io.opentelemetry.api.trace.Span;
22+
import io.opentelemetry.context.Context;
2223
import java.util.*;
2324
import java.util.concurrent.Flow;
2425
import java.util.function.Consumer;
@@ -227,6 +228,7 @@ void onStartMessage(MessageLite msg) {
227228
Request request =
228229
new Request(
229230
invocationId,
231+
Context.root().with(span),
230232
inputEntry.getValue(),
231233
inputEntry.getHeadersList().stream()
232234
.collect(

sdk-core/src/main/java/dev/restate/sdk/core/RestateEndpoint.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import io.opentelemetry.api.trace.Span;
1818
import io.opentelemetry.api.trace.SpanKind;
1919
import io.opentelemetry.api.trace.Tracer;
20-
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
2120
import java.util.*;
2221
import java.util.concurrent.Executor;
2322
import java.util.function.Function;
@@ -72,9 +71,6 @@ public ResolvedEndpointHandler resolve(
7271
.spanBuilder("Invoke method")
7372
.setSpanKind(SpanKind.SERVER)
7473
.setParent(otelContext)
75-
.setAttribute(SemanticAttributes.RPC_SYSTEM, "restate")
76-
.setAttribute(SemanticAttributes.RPC_SERVICE, componentName)
77-
.setAttribute(SemanticAttributes.RPC_METHOD, handlerName)
7874
.startSpan();
7975

8076
// Setup logging context

settings.gradle.kts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependencyResolutionManagement {
3636
create("coreLibs") {
3737
version("protobuf", "3.24.3")
3838
version("log4j", "2.22.0")
39-
version("opentelemetry", "1.30.1")
39+
version("opentelemetry", "1.37.0")
4040

4141
library("protoc", "com.google.protobuf", "protoc").versionRef("protobuf")
4242
library("protobuf-java", "com.google.protobuf", "protobuf-java").versionRef("protobuf")
@@ -48,7 +48,8 @@ dependencyResolutionManagement {
4848
library("opentelemetry-bom", "io.opentelemetry", "opentelemetry-bom")
4949
.versionRef("opentelemetry")
5050
library("opentelemetry-api", "io.opentelemetry", "opentelemetry-api").withoutVersion()
51-
library("opentelemetry-semconv", "io.opentelemetry:opentelemetry-semconv:1.19.0-alpha")
51+
library("opentelemetry-kotlin", "io.opentelemetry", "opentelemetry-extension-kotlin")
52+
.withoutVersion()
5253

5354
library("jspecify", "org.jspecify", "jspecify").version("0.3.0")
5455
}

0 commit comments

Comments
 (0)