Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Commit 95b5d7b

Browse files
authored
Add OTel semantic error attributes (#23)
Closes undefined
1 parent 108fa0c commit 95b5d7b

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Distributed Tracing open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift Distributed Tracing project
6+
// authors
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Tracing
16+
17+
extension SpanAttributes {
18+
/// Error attributes.
19+
///
20+
/// OpenTelemetry Spec: [Semantic Conventions for Exceptions](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.11.0/specification/trace/semantic_conventions/exceptions.md)
21+
public var error: ErrorAttributes {
22+
get {
23+
.init(attributes: self)
24+
}
25+
set {
26+
self = newValue.attributes
27+
}
28+
}
29+
}
30+
31+
/// Error attributes.
32+
///
33+
/// OpenTelemetry Spec: [Semantic Conventions for Exceptions](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.11.0/specification/trace/semantic_conventions/exceptions.md)
34+
@dynamicMemberLookup
35+
public struct ErrorAttributes: SpanAttributeNamespace {
36+
public var attributes: SpanAttributes
37+
38+
public init(attributes: SpanAttributes) {
39+
self.attributes = attributes
40+
}
41+
42+
public struct NestedSpanAttributes: NestedSpanAttributesProtocol {
43+
public init() {}
44+
45+
/// The type of the error. E.g. `AuthenticationError`.
46+
public var type: Key<String> { "exception.type" }
47+
48+
/// The error message. E.g. `Token expired`.
49+
public var message: Key<String> { "exception.message" }
50+
51+
/// The stacktrace up to the error.
52+
public var stacktrace: Key<String> { "exception.stacktrace" }
53+
54+
/// Whether the error "escaped" the span.
55+
///
56+
/// E.g., if the operation inside a span throws an error to the outside it's considered escaped.
57+
/// If, on the other hand, the error is being handled inside the span it didn't escape.
58+
public var escaped: Key<Bool> { "exception.escaped" }
59+
}
60+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Distributed Tracing open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift Distributed Tracing project
6+
// authors
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Tracing
16+
@testable import TracingOpenTelemetrySemanticConventions
17+
import XCTest
18+
19+
final class ErrorSemanticsTests: XCTestCase {
20+
func test_error() {
21+
var attributes = SpanAttributes()
22+
23+
attributes.error.type = "AuthenticationError"
24+
attributes.error.message = "The provided token already expired."
25+
attributes.error.stacktrace = "test"
26+
attributes.error.escaped = true
27+
28+
XCTAssertSpanAttributesEqual(attributes, [
29+
"exception.type": "AuthenticationError",
30+
"exception.message": "The provided token already expired.",
31+
"exception.stacktrace": "test",
32+
"exception.escaped": true,
33+
])
34+
}
35+
}

0 commit comments

Comments
 (0)