Skip to content

Recording an error may carry attributes #90

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 4 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 Sources/Tracing/NoOpTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public struct NoOpTracer: Tracer {

public func addEvent(_ event: SpanEvent) {}

public func recordError(_ error: Error) {}
public func recordError(_ error: Error, attributes: SpanAttributes) {}

public var attributes: SpanAttributes {
get {
Expand Down
8 changes: 7 additions & 1 deletion Sources/Tracing/Span.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public protocol Span: AnyObject, _SwiftTracingSendableSpan {
///
/// - Parameters:
/// - error: The error to be recorded.
func recordError(_ error: Error)
func recordError(_ error: Error, attributes: SpanAttributes)

/// The attributes describing this `Span`.
var attributes: SpanAttributes { get set }
Expand Down Expand Up @@ -97,6 +97,12 @@ extension Span {
}
}

extension Span {
public func recordError(_ error: Error) {
self.recordError(error, attributes: [:])
}
}

// ==== ----------------------------------------------------------------------------------------------------------------
// MARK: Span Event

Expand Down
2 changes: 1 addition & 1 deletion Tests/TracingTests/DynamicTracepointTracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ extension DynamicTracepointTestTracer {
// nothing
}

func recordError(_ error: Error) {
func recordError(_ error: Error, attributes: SpanAttributes) {
print("")
}

Expand Down
12 changes: 8 additions & 4 deletions Tests/TracingTests/TestTracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Tracing
/// Only intended to be used in single-threaded testing.
final class TestTracer: Tracer {
private(set) var spans = [TestSpan]()
var onEndSpan: (Span) -> Void = { _ in }
var onEndSpan: (TestSpan) -> Void = { _ in }

func startSpan(
_ operationName: String,
Expand Down Expand Up @@ -104,6 +104,8 @@ final class TestSpan: Span {
private let startTime: DispatchWallTime
private(set) var endTime: DispatchWallTime?

private(set) var recordedErrors: [(Error, SpanAttributes)] = []

let baggage: Baggage

private(set) var events = [SpanEvent]() {
Expand All @@ -122,14 +124,14 @@ final class TestSpan: Span {

private(set) var isRecording = false

let onEnd: (Span) -> Void
let onEnd: (TestSpan) -> Void

init(
operationName: String,
startTime: DispatchWallTime,
baggage: Baggage,
kind: SpanKind,
onEnd: @escaping (Span) -> Void
onEnd: @escaping (TestSpan) -> Void
) {
self.operationName = operationName
self.startTime = startTime
Expand All @@ -151,7 +153,9 @@ final class TestSpan: Span {
self.events.append(event)
}

func recordError(_ error: Error) {}
func recordError(_ error: Error, attributes: SpanAttributes) {
self.recordedErrors.append((error, attributes))
}

func end(at time: DispatchWallTime) {
self.endTime = time
Expand Down
2 changes: 1 addition & 1 deletion Tests/TracingTests/TracedLockTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private final class TracedLockPrintlnTracer: Tracer {
self.events.append(event)
}

func recordError(_ error: Error) {}
func recordError(_ error: Error, attributes: SpanAttributes) {}

func end(at time: DispatchWallTime) {
self.endTime = time
Expand Down
1 change: 1 addition & 0 deletions Tests/TracingTests/TracerTests+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extension TracerTests {
("testWithSpan_automaticBaggagePropagation_async", testWithSpan_automaticBaggagePropagation_async),
("testWithSpan_enterFromNonAsyncCode_passBaggage_asyncOperation", testWithSpan_enterFromNonAsyncCode_passBaggage_asyncOperation),
("testWithSpan_automaticBaggagePropagation_async_throws", testWithSpan_automaticBaggagePropagation_async_throws),
("testWithSpan_recordErrorWithAttributes", testWithSpan_recordErrorWithAttributes),
]
}
}
Expand Down
31 changes: 31 additions & 0 deletions Tests/TracingTests/TracerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,37 @@ final class TracerTests: XCTestCase {
#endif
}

func testWithSpan_recordErrorWithAttributes() throws {
#if swift(>=5.5) && canImport(_Concurrency)
guard #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) else {
throw XCTSkip("Task locals are not supported on this platform.")
}

let tracer = TestTracer()
InstrumentationSystem.bootstrapInternal(tracer)
defer {
InstrumentationSystem.bootstrapInternal(nil)
}

var endedSpan: TestSpan?
tracer.onEndSpan = { span in endedSpan = span }

let errorToThrow = ExampleSpanError()
let attrsForError: SpanAttributes = ["attr": "value"]

tracer.withSpan("hello") { span in
span.recordError(errorToThrow, attributes: attrsForError)
}

XCTAssertTrue(endedSpan != nil)
XCTAssertEqual(endedSpan!.recordedErrors.count, 1)
let error = endedSpan!.recordedErrors.first!.0
XCTAssertEqual(error as! ExampleSpanError, errorToThrow)
let attrs = endedSpan!.recordedErrors.first!.1
XCTAssertEqual(attrs, attrsForError)
Comment on lines +278 to +283
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want to use XCTUnwrap here instead of the force unwraps?

Copy link
Member Author

Choose a reason for hiding this comment

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

Meh, tbh I don't mind those !, thank you for the review!

#endif
}

#if swift(>=5.5) && canImport(_Concurrency)
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
/// Helper method to execute async operations until we can use async tests (currently incompatible with the generated LinuxMain file).
Expand Down