13
13
//===----------------------------------------------------------------------===//
14
14
15
15
import Dispatch
16
+ import Distributed
16
17
@_exported import Instrumentation
17
18
@_exported import ServiceContextModule
18
19
@@ -382,7 +383,7 @@ public func withSpan<T>(
382
383
function: String = #function,
383
384
file fileID: String = #fileID,
384
385
line: UInt = #line,
385
- _ operation: @ Sendable ( any Span ) async throws -> T
386
+ _ operation: ( any Span ) async throws -> T
386
387
) async rethrows -> T {
387
388
try await InstrumentationSystem . legacyTracer. withAnySpan (
388
389
operationName,
@@ -477,7 +478,7 @@ public func withSpan<T>(
477
478
function: String = #function,
478
479
file fileID: String = #fileID,
479
480
line: UInt = #line,
480
- _ operation: @ Sendable ( any Span ) async throws -> T
481
+ _ operation: ( any Span ) async throws -> T
481
482
) async rethrows -> T {
482
483
try await InstrumentationSystem . legacyTracer. withAnySpan (
483
484
operationName,
@@ -491,4 +492,114 @@ public func withSpan<T>(
491
492
try await operation ( anySpan)
492
493
}
493
494
}
495
+
496
+ // FIXME: type checker does not understand that AnyActor shall suffice for checking isolation domains,
497
+ // thus we can't declare the method once, but have to duplicate it for every actor kind:
498
+ //@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
499
+ //extension AnyActor {
500
+ // public func withSpanWorkaround<T>(
501
+ // _ operationName: String,
502
+ // context: @autoclosure () -> ServiceContext = .current ?? .topLevel,
503
+ // ofKind kind: SpanKind = .internal,
504
+ // at instant: @autoclosure () -> some TracerInstant = DefaultTracerClock.now,
505
+ // function: String = #function,
506
+ // file fileID: String = #fileID,
507
+ // line: UInt = #line,
508
+ // _ operation: (any Span) async throws -> T
509
+ // ) async rethrows -> T {
510
+ // fatalError("Can't use this, must extend both types separately")
511
+ // }
512
+ //}
513
+
514
+ @available ( macOS 10 . 15 , iOS 13 , tvOS 13 , watchOS 6 , * )
515
+ extension Actor {
516
+
517
+ public func withSpanWorkaround< T> (
518
+ _ operationName: String ,
519
+ context: @autoclosure ( ) -> ServiceContext = . current ?? . topLevel,
520
+ ofKind kind: SpanKind = . internal,
521
+ at instant: @autoclosure ( ) -> some TracerInstant = DefaultTracerClock . now,
522
+ function: String = #function,
523
+ file fileID: String = #fileID,
524
+ line: UInt = #line,
525
+ _ operation: ( any Span ) async throws -> T
526
+ ) async rethrows -> T {
527
+ let span = InstrumentationSystem . legacyTracer. startAnySpan (
528
+ operationName,
529
+ context: context ( ) ,
530
+ ofKind: kind,
531
+ at: instant ( ) ,
532
+ function: function,
533
+ file: fileID,
534
+ line: line
535
+ )
536
+ defer { span. end ( ) }
537
+ do {
538
+ return try await ServiceContext . $current. withValue ( span. context) {
539
+ try await operation ( span)
540
+ }
541
+ } catch {
542
+ span. recordError ( error)
543
+ throw error // rethrow
544
+ }
545
+ // // FIXME: can't use withSpan to implement this... must use startSpan and duplicate much code
546
+ // try await InstrumentationSystem.legacyTracer.withAnySpan(
547
+ // operationName,
548
+ // at: instant(),
549
+ // context: context(),
550
+ // ofKind: kind,
551
+ // function: function,
552
+ // file: fileID,
553
+ // line: line
554
+ // ) { anySpan in
555
+ // try await operation(anySpan)
556
+ // }
557
+ }
558
+ }
559
+
560
+ @available ( macOS 13 . 0 , iOS 16 . 0 , watchOS 9 . 0 , tvOS 16 . 0 , * )
561
+ extension DistributedActor {
562
+
563
+ public func withSpanWorkaround< T> (
564
+ _ operationName: String ,
565
+ context: @autoclosure ( ) -> ServiceContext = . current ?? . topLevel,
566
+ ofKind kind: SpanKind = . internal,
567
+ at instant: @autoclosure ( ) -> some TracerInstant = DefaultTracerClock . now,
568
+ function: String = #function,
569
+ file fileID: String = #fileID,
570
+ line: UInt = #line,
571
+ _ operation: ( any Span ) async throws -> T
572
+ ) async rethrows -> T {
573
+ let span = InstrumentationSystem . legacyTracer. startAnySpan (
574
+ operationName,
575
+ context: context ( ) ,
576
+ ofKind: kind,
577
+ at: instant ( ) ,
578
+ function: function,
579
+ file: fileID,
580
+ line: line
581
+ )
582
+ defer { span. end ( ) }
583
+ do {
584
+ return try await ServiceContext . $current. withValue ( span. context) {
585
+ try await operation ( span)
586
+ }
587
+ } catch {
588
+ span. recordError ( error)
589
+ throw error // rethrow
590
+ }
591
+ // // FIXME: can't use withSpan to implement this... must use startSpan and duplicate much code
592
+ // try await InstrumentationSystem.legacyTracer.withAnySpan(
593
+ // operationName,
594
+ // at: instant(),
595
+ // context: context(),
596
+ // ofKind: kind,
597
+ // function: function,
598
+ // file: fileID,
599
+ // line: line
600
+ // ) { anySpan in
601
+ // try await operation(anySpan)
602
+ // }
603
+ }
604
+ }
494
605
#endif
0 commit comments