Skip to content

Commit a49eced

Browse files
committed
Add W3C.TraceContext
1 parent d79edd3 commit a49eced

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Sources/W3CTraceContext/TraceContext.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,20 @@
1010
// SPDX-License-Identifier: Apache-2.0
1111
//
1212
//===----------------------------------------------------------------------===//
13+
1314
public enum W3C {}
15+
16+
extension W3C {
17+
public struct TraceContext: Equatable {
18+
public let parent: TraceParent
19+
20+
public init(parent: TraceParent) {
21+
self.parent = parent
22+
}
23+
24+
public init?(parent parentRawValue: String) {
25+
guard let parent = TraceParent(rawValue: parentRawValue) else { return nil }
26+
self.parent = parent
27+
}
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift W3C Trace Context open source project
4+
//
5+
// Copyright (c) 2020 Moritz Lang and the Swift W3C Trace Context project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
import W3CTraceContext
15+
import XCTest
16+
17+
final class TraceContextTests: XCTestCase {
18+
func testInitWithValidRawValues() {
19+
let traceParentRawValue = "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"
20+
21+
guard let traceContext = W3C.TraceContext(parent: traceParentRawValue) else {
22+
XCTFail("Could not decode valid trace context")
23+
return
24+
}
25+
26+
XCTAssertEqual(
27+
traceContext,
28+
W3C.TraceContext(
29+
parent: W3C.TraceParent(
30+
traceID: "0af7651916cd43dd8448eb211c80319c",
31+
parentID: "b7ad6b7169203331",
32+
traceFlags: "01"
33+
)
34+
)
35+
)
36+
}
37+
38+
func testInitWithInvalidTraceParent() {
39+
XCTAssertNil(W3C.TraceContext(parent: "invalid"))
40+
}
41+
}

0 commit comments

Comments
 (0)