@@ -21,6 +21,7 @@ import DistributedActorsConcurrencyHelpers
2121import Foundation // for UUID
2222import Logging
2323import NIO
24+ import Tracing
2425
2526/// A `ClusterSystem` is a confined space which runs and manages Actors.
2627///
@@ -1148,28 +1149,38 @@ extension ClusterSystem {
11481149 let recipient = _RemoteClusterActorPersonality < InvocationMessage > ( shell: clusterShell, id: actor . id. _asRemote, system: self )
11491150 let arguments = invocation. arguments
11501151
1151- let reply : RemoteCallReply < Res > = try await self . withCallID ( on: actor . id, target: target) { callID in
1152- let invocation = InvocationMessage (
1153- callID: callID,
1154- targetIdentifier: target. identifier,
1155- genericSubstitutions: invocation. genericSubstitutions,
1156- arguments: arguments
1157- )
1152+ // -- Pick up the distributed-tracing baggage; It will be injected into the message inside the withCallID body.
1153+ let baggage = Baggage . current ?? . topLevel
1154+ // TODO: we can enrich this with actor and system information here if not already present.
1155+
1156+ return try await InstrumentationSystem . tracer. withSpan ( " \( target) " , baggage: baggage, ofKind: . client) { span in
1157+ let reply : RemoteCallReply < Res > = try await self . withCallID ( on: actor . id, target: target) { callID in
1158+ var invocation = InvocationMessage (
1159+ callID: callID,
1160+ targetIdentifier: target. identifier,
1161+ genericSubstitutions: invocation. genericSubstitutions,
1162+ arguments: arguments
1163+ )
11581164
1159- recipient. sendInvocation ( invocation)
1160- }
1165+ if let baggage {
1166+ InstrumentationSystem . instrument. inject ( baggage, into: & invocation, using: . invocationMessage)
1167+ }
11611168
1162- if let error = reply. thrownError {
1163- throw error
1164- }
1165- guard let value = reply. value else {
1166- throw RemoteCallError (
1167- . invalidReply( reply. callID) ,
1168- on: actor . id,
1169- target: target
1170- )
1169+ recipient. sendInvocation ( invocation)
1170+ }
1171+
1172+ if let error = reply. thrownError {
1173+ throw error
1174+ }
1175+ guard let value = reply. value else {
1176+ throw RemoteCallError (
1177+ . invalidReply( reply. callID) ,
1178+ on: actor . id,
1179+ target: target
1180+ )
1181+ }
1182+ return value
11711183 }
1172- return value
11731184 }
11741185
11751186 public func remoteCallVoid< Act, Err> (
@@ -1211,18 +1222,29 @@ extension ClusterSystem {
12111222 let recipient = _RemoteClusterActorPersonality < InvocationMessage > ( shell: clusterShell, id: actor . id. _asRemote, system: self )
12121223 let arguments = invocation. arguments
12131224
1214- let reply : RemoteCallReply < _Done > = try await self . withCallID ( on: actor . id, target: target) { callID in
1215- let invocation = InvocationMessage (
1216- callID: callID,
1217- targetIdentifier: target. identifier,
1218- genericSubstitutions: invocation. genericSubstitutions,
1219- arguments: arguments
1220- )
1221- recipient. sendInvocation ( invocation)
1222- }
1225+ // -- Pick up the distributed-tracing baggage; It will be injected into the message inside the withCallID body.
1226+ let baggage = Baggage . current ?? . topLevel
1227+ // TODO: we can enrich this with actor and system information here if not already present.
1228+
1229+ return try await InstrumentationSystem . tracer. withSpan ( " \( target) " , baggage: baggage, ofKind: . client) { span in
1230+ let reply : RemoteCallReply < _Done > = try await self . withCallID ( on: actor . id, target: target) { callID in
1231+ var invocation = InvocationMessage (
1232+ callID: callID,
1233+ targetIdentifier: target. identifier,
1234+ genericSubstitutions: invocation. genericSubstitutions,
1235+ arguments: arguments
1236+ )
12231237
1224- if let error = reply. thrownError {
1225- throw error
1238+ if let baggage {
1239+ InstrumentationSystem . instrument. inject ( baggage, into: & invocation, using: . invocationMessage)
1240+ }
1241+
1242+ recipient. sendInvocation ( invocation)
1243+ }
1244+
1245+ if let error = reply. thrownError {
1246+ throw error
1247+ }
12261248 }
12271249 }
12281250
@@ -1403,6 +1425,9 @@ extension ClusterSystem {
14031425 return
14041426 }
14051427
1428+ var baggage : Baggage = . topLevel
1429+ InstrumentationSystem . instrument. extract ( invocation, into: & baggage, using: . invocationMessage)
1430+
14061431 Task {
14071432 var decoder = ClusterInvocationDecoder ( system: self , message: invocation)
14081433
@@ -1420,12 +1445,14 @@ extension ClusterSystem {
14201445 throw DeadLetterError ( recipient: recipient)
14211446 }
14221447
1423- try await executeDistributedTarget (
1424- on: actor ,
1425- target: target,
1426- invocationDecoder: & decoder,
1427- handler: resultHandler
1428- )
1448+ try await InstrumentationSystem . tracer. withSpan ( " \( target) " , baggage: baggage, ofKind: . server) { span in
1449+ try await executeDistributedTarget (
1450+ on: actor ,
1451+ target: target,
1452+ invocationDecoder: & decoder,
1453+ handler: resultHandler
1454+ )
1455+ }
14291456 } catch {
14301457 // FIXME(distributed): is this right?
14311458 do {
0 commit comments