Skip to content

startSpan should take tracepoint location #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
.library(name: "Tracing", targets: ["Tracing"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")),
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [
Expand Down
2 changes: 1 addition & 1 deletion Package@swift-5.2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
.library(name: "Tracing", targets: ["Tracing"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")),
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
],
targets: [
// ==== --------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Package@swift-5.3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
.library(name: "Tracing", targets: ["Tracing"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")),
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
],
targets: [
// ==== --------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Package@swift-5.4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
.library(name: "Tracing", targets: ["Tracing"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")),
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
],
targets: [
// ==== --------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Package@swift-5.5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let package = Package(
.library(name: "Tracing", targets: ["Tracing"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.3.0")),
.package(url: "https://github.com/apple/swift-distributed-tracing-baggage.git", .upToNextMinor(from: "0.4.1")),
],
targets: [
// ==== --------------------------------------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion Sources/Tracing/NoOpTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public struct NoOpTracer: Tracer {
_ operationName: String,
baggage: Baggage,
ofKind kind: SpanKind,
at time: DispatchWallTime
at time: DispatchWallTime,
function: String,
file fileID: String,
line: UInt
) -> Span {
NoOpSpan(baggage: baggage)
}
Expand Down
155 changes: 147 additions & 8 deletions Sources/Tracing/Tracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ public protocol Tracer: Instrument {
/// - baggage: The `Baggage` providing information on where to start the new ``Span``.
/// - kind: The ``SpanKind`` of the new ``Span``.
/// - time: The `DispatchTime` at which to start the new ``Span``.
/// - function: The function name in which the span was started
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
func startSpan(
_ operationName: String,
baggage: Baggage,
ofKind kind: SpanKind,
at time: DispatchWallTime
at time: DispatchWallTime,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not the new Clock/Duration/... API?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah just noticed this too tbh...

I'll need to think about this, it'd force users to require 5.7+ so we may not want that... we'd want vapor to adopt without having to suddenly bump their required swift versions. Probably we can add an public API accepting Instant but I guess implementors may have to implement against dispatch time or something else... to keep the swift requirement low :-/

I'll think about it more and ticketify. AHC we'd also not want to force to require 5.7 suddenly 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add an overload though that delegates to the dispatch time one 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add overload in separate PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#71

function: String,
file fileID: String,
line: UInt
) -> Span

/// Export all ended spans to the configured backend that have not yet been exported.
Expand All @@ -47,25 +53,111 @@ public protocol Tracer: Instrument {
}

extension Tracer {
/// Start a new ``Span`` with the given `Baggage` starting at `DispatchWallTime.now()`.
#if swift(>=5.3.0)
/// Start a new ``Span`` with the given `Baggage` starting "now".
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good simplification 👍

///
/// - Parameters:
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
/// - function: The function name in which the span was started.
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
public func startSpan(
_ operationName: String,
baggage: Baggage,
ofKind kind: SpanKind = .internal
ofKind kind: SpanKind = .internal,
function: String = #function,
file fileID: String = #fileID,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm very much looking forward to a time where we can start to only support #fileID in server-side Swift projects 😬

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh NIO requires 5.5+ already, so most projects can. I'm not even sure we should keep trying with 5.1+ here... but for now we do.

line: UInt = #line
) -> Span {
self.startSpan(operationName, baggage: baggage, ofKind: kind, at: .now())
self.startSpan(
operationName,
baggage: baggage,
ofKind: kind,
at: .now(),
function: function,
file: fileID,
line: line
)
}
#else
/// Start a new ``Span`` with the given `Baggage` starting "now".
///
/// - Parameters:
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
/// - function: The function name in which the span was started.
/// - file: The `file` where the span was started.
/// - line: The file line where the span was started.
public func startSpan(
_ operationName: String,
baggage: Baggage,
ofKind kind: SpanKind = .internal,
function: String = #function,
file: String = #file,
line: UInt = #line
) -> Span {
self.startSpan(
operationName,
baggage: baggage,
ofKind: kind,
at: .now(),
function: function,
file: file,
line: line
)
}
#endif
}

// ==== ----------------------------------------------------------------------------------------------------------------
// MARK: Starting spans: `withSpan`

extension Tracer {
#if swift(>=5.3.0)
/// Execute a specific task within a newly created ``Span``.
///
/// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns.
///
/// - Parameters:
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
/// - operation: operation to wrap in a span start/end and execute immediately
/// - function: The function name in which the span was started.
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
public func withSpan<T>(
_ operationName: String,
baggage: Baggage,
ofKind kind: SpanKind = .internal,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
_ operation: (Span) throws -> T
) rethrows -> T {
let span = self.startSpan(
operationName,
baggage: baggage,
ofKind: kind,
at: .now(),
function: function,
file: fileID,
line: line
)
defer { span.end() }
do {
return try operation(span)
} catch {
span.recordError(error)
throw error // rethrow
}
}
#else
/// Execute a specific task within a newly created ``Span``.
///
/// DO NOT `end()` the passed in span manually. It will be ended automatically when the `operation` returns.
Expand All @@ -75,15 +167,29 @@ extension Tracer {
/// - baggage: Baggage potentially containing trace identifiers of a parent ``Span``.
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
/// - operation: operation to wrap in a span start/end and execute immediately
/// - function: The function name in which the span was started.
/// - file: The `#file` where the span was started.
/// - line: The file line where the span was started.
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
public func withSpan<T>(
_ operationName: String,
baggage: Baggage,
ofKind kind: SpanKind = .internal,
function: String = #function,
file: String = #file,
line: UInt = #line,
_ operation: (Span) throws -> T
) rethrows -> T {
let span = self.startSpan(operationName, baggage: baggage, ofKind: kind)
let span = self.startSpan(
operationName,
baggage: baggage,
ofKind: kind,
at: .now(),
function: function,
file: file,
line: line
)
defer { span.end() }
do {
return try operation(span)
Expand All @@ -92,6 +198,7 @@ extension Tracer {
throw error // rethrow
}
}
#endif
}

// ==== ----------------------------------------------------------------------------------------------------------------
Expand All @@ -109,14 +216,27 @@ extension Tracer {
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
/// - operation: operation to wrap in a span start/end and execute immediately
/// - function: The function name in which the span was started.
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
public func withSpan<T>(
_ operationName: String,
ofKind kind: SpanKind = .internal,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
_ operation: (Span) throws -> T
) rethrows -> T {
try self.withSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind) { span in
try self.withSpan(
operationName,
baggage: .current ?? .topLevel,
ofKind: kind,
function: function,
file: fileID,
line: line
) { span in
try Baggage.$current.withValue(span.baggage) {
try operation(span)
}
Expand All @@ -132,14 +252,27 @@ extension Tracer {
/// - operationName: The name of the operation being traced. This may be a handler function, database call, ...
/// - kind: The ``SpanKind`` of the ``Span`` to be created. Defaults to ``SpanKind/internal``.
/// - operation: operation to wrap in a span start/end and execute immediately
/// - function: The function name in which the span was started.
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
public func withSpan<T>(
_ operationName: String,
ofKind kind: SpanKind = .internal,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
_ operation: (Span) async throws -> T
) async rethrows -> T {
let span = self.startSpan(operationName, baggage: .current ?? .topLevel, ofKind: kind)
let span = self.startSpan(
operationName,
baggage: .current ?? .topLevel,
ofKind: kind,
function: function,
file: fileID,
line: line
)
defer { span.end() }
do {
return try await Baggage.$current.withValue(span.baggage) {
Expand All @@ -162,15 +295,21 @@ extension Tracer {
// task local and modified before passing into this function. The baggage will be made the current task-local baggage for the duration of the `operation`.
/// - kind: The `SpanKind` of the `Span` to be created. Defaults to `.internal`.
/// - operation: operation to wrap in a span start/end and execute immediately
/// - function: The function name in which the span was started.
/// - fileID: The `fileID` where the span was started.
/// - line: The file line where the span was started.
/// - Returns: the value returned by `operation`
/// - Throws: the error the `operation` has thrown (if any)
public func withSpan<T>(
_ operationName: String,
baggage: Baggage,
ofKind kind: SpanKind = .internal,
function: String = #function,
file fileID: String = #fileID,
line: UInt = #line,
_ operation: (Span) async throws -> T
) async rethrows -> T {
let span = self.startSpan(operationName, baggage: baggage, ofKind: kind)
let span = self.startSpan(operationName, baggage: baggage, ofKind: kind, function: function, file: fileID, line: line)
defer { span.end() }
do {
return try await Baggage.$current.withValue(span.baggage) {
Expand Down
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class LinuxMainRunnerImpl: LinuxMainRunner {
@available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings")
func run() {
XCTMain([
testCase(DynamicTracepointTracerTests.allTests),
testCase(InstrumentTests.allTests),
testCase(InstrumentationSystemTests.allTests),
testCase(SpanTests.allTests),
Expand Down
34 changes: 34 additions & 0 deletions Tests/TracingTests/DynamicTracepointTracerTests+XCTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Distributed Tracing open source project
//
// Copyright (c) 2020-2021 Apple Inc. and the Swift Distributed Tracing project
// authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
//
// DynamicTracepointTracerTests+XCTest.swift
//
import XCTest
///
/// NOTE: This file was generated by generate_linux_tests.rb
///
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
///

extension DynamicTracepointTracerTests {

@available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings")
static var allTests : [(String, (DynamicTracepointTracerTests) -> () throws -> Void)] {
return [
("test_adhoc_enableBySourceLoc", test_adhoc_enableBySourceLoc),
("test_adhoc_enableByFunction", test_adhoc_enableByFunction),
]
}
}

Loading