It seems people like discussion in the forum, instead of externally at Google Docs. Just copy my proposal draft here to increase visibility.
@ktoso Let me know if you have any further comments/suggestions.
Distributed Tracing to AsyncHTTPClient and Vapor
Synopsis
This project aims at providing an out-of-box distributed tracing instrumentation for async-http-client and Vapor:
-
async-http-client is the recommended HTTP client for server applications in Swift
-
Vapor is a widely used HTTP web framework for Swift. With powerful built-in modules including intelligent request routing, RESTful support and middlewares for intercepting HTTP requests. Vapor makes it very convenient to build a web application from scratch.
This proposal aims at instrumenting async-http-client and Vapor with the swift-distributed-tracing library.
Benefits to Community
With tracing instrumentation, swift developers can add (or switch) tracing implementations/backends with an O(1) configuration change. More importantly, when Tracing is instrumented in async-http-client and Vapor, a low enough level in the software stack, even large applications could be traced without additional annotations. Based on our contributions, the application programmers even do not need to be aware of the tracing system.
Deliverables
Client side instrumentation
A PR to async-http-client will be raised to add the tracing instrumentation. Unit test will be provided to verify that the expected tracing HTTP header has been added to HTTP request if tracing is enabled.
Server side instrumentation
A PR to Vapor will be raised to add the tracing instrumentation. It will be a Vapor Middleware, and implement the following Middleware protocol.
@preconcurrency
public protocol Middleware: Sendable {
func respond(to request: Request, chainingTo next: Responder) -> EventLoopFuture<Response>
}
Inside the respond() function, it will:
- Mutate the Request object by adding tracing information. If the request is already attached with a Span created by client side, reuse it, otherwise create a new one and record the tracing metrics.
- Mutate the Response object by adding tracing information
Please refer to Middleware.swift for more details about the Vapor Middleware protocol.
Demo App
A Demo app will be provided to showcase the instrumentation works
Implementation
Information to be emitted
Tentatively the following information will be emitted
- component name
- operation name
- HTTP method
- HTTP URL
- HTTP status code
Tracing specification
We will adopt OpenTelemetry as the tracing specification/protocol. Please refer to the opentelemetry-specification for more details.
Demo App
Demo App client side
Client side will send an async http request to server side using the instrumented async-http-client. It will be a simple HTTP GET request to http://127.0.0.1:8080
Demo App server side
Server side will use the Vapor framework and enable the newly added Tracing Middleware. The server will listen to 8080 port
It will use the swift-otel library to create a OTel span exporter, exporting the metrics/spans info via port 4317
Demo App Tracing backend
We will launch a Jaeger as the tracing backend, which collects the metrics/spans info exposed at port 4317 by the OTel span exporter. The jaeger-ui will be used to visualize all the information being emitted.
Demo App bundle
We will provide a Dockerfile for the demo app client side and server side respectively, then use Docker Compose to bundle the demo app as well as the Jaeger and jaeger-ui, so that developers can quickly try out the functionality.
Alternatives considered
Opentracing
While the Opentracing project used to be actively developed and maintained, the project is archived now, see: OpenTracing has been Archived. We have decided to adopt OpenTelemetry as the tracing specification
opentelemetry-swift
While OpenTelemetry community provide a opentelemetry-swift library, we have decide to use the swift-distributed-tracing library to to the instrumentation, as โโswift-distributed-tracing library is more suitable for instrumenting multithreaded and distributed systems with Distributed Traces
Related Work