-
Notifications
You must be signed in to change notification settings - Fork 40
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
Changes from all commits
bf2f8c9
b42d1db
1803401
12d7326
8ed9307
1775c76
7ea80f7
24dd200
f96ed04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
function: String, | ||
file fileID: String, | ||
line: UInt | ||
) -> Span | ||
|
||
/// Export all ended spans to the configured backend that have not yet been exported. | ||
|
@@ -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". | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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) | ||
|
@@ -92,6 +198,7 @@ extension Tracer { | |
throw error // rethrow | ||
} | ||
} | ||
#endif | ||
} | ||
|
||
// ==== ---------------------------------------------------------------------------------------------------------------- | ||
|
@@ -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) | ||
} | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
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), | ||
] | ||
} | ||
} | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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 👍
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#71