apple / swift-distributed-tracing

Instrumentation library for Swift server applications
https://swiftpackageindex.com/apple/swift-distributed-tracing/main/documentation/tracing
Apache License 2.0
224 stars 34 forks source link

Consider `Span` to be an `associatedType` on `Tracer` #78

Closed fabianfett closed 1 year ago

fabianfett commented 1 year ago

Currently the Tracer protocol has the startSpan requirement. The return of startSpan is a Span which is a protocol. In other words we return an existential here. Instead we might want to consider making the Span an associated type and returning the concrete type:

public protocol Tracer: Instrument {
    associatedType Span: SpanProtocol // the current Span protocol maybe needs a rename to SpanProtocol for this?

    func startSpan(
        _ operationName: String,
        baggage: Baggage,
        ofKind kind: SpanKind,
        at time: DispatchWallTime,
        function: String,
        file fileID: String,
        line: UInt
    ) -> Span

    func forceFlush()
}

I can see no reasons, why a Tracer would return Spans of different types. If that was a requirement adopters could work around this, by using internal enums.

Result

We preserve more type information if we work with a concrete Tracer.

slashmo commented 1 year ago

Sounds good to me 👍 The rename to SpanProtocol also sounds good and actually fits nicely with @ktoso's rename of Tracer and Instrument to *Protocol: #69

ktoso commented 1 year ago

Yeah, sounds like a good idea -- it does seem like we'll indeed adopt more ...Protocol types, so this sounds good as well.

ktoso commented 1 year ago

Done, I'll do one more rename though -- we'll keep protocol Span