apple / swift-distributed-tracing

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

Offer @traced macro when macros gain function body replacement capabilities #125

Open stevapple opened 1 year ago

stevapple commented 1 year ago

Typically we trace at function level by wrapping the whole implementation in a span, as demonstrated by the sample.

func makeDinner() async throws -> Meal {
  try await withSpan("makeDinner") { _ in
    async let veggies = try chopVegetables()
    async let meat = marinateMeat()
    async let oven = preheatOven(temperature: 350)
    // ...
    return try await cook(veggies, meat, oven)
  }
}

It makes me think if we can have an @Traced macro attached to functions, which handles the span automatically for users. Eventually such macro should be configurable (explicit tracer, span name, extracting context from parameters, etc.), but AFAIK Swift macros seem not having such level of expressiveness yet.

@Traced
func makeDinner() async throws -> Meal {
  async let veggies = try chopVegetables()
  async let meat = marinateMeat()
  async let oven = preheatOven(temperature: 350)
  // ...
  return try await cook(veggies, meat, oven)
}
ktoso commented 1 year ago

Yes, this is planned but we're missing macro functionality to pull it off.

We need "function body macros" -- once those ship we'll ship such macro in this library 👍

ktoso commented 1 year ago

rdar://109247127

porglezomp commented 2 months ago

Since function body macros ship with Swift 6.0 (https://github.com/swiftlang/swift/pull/73765) is this unblocked now?