diff --git a/Sources/GRPCInterceptors/Tracing/ClientOTelTracingInterceptor.swift b/Sources/GRPCInterceptors/Tracing/ClientOTelTracingInterceptor.swift index c379c25..488fa69 100644 --- a/Sources/GRPCInterceptors/Tracing/ClientOTelTracingInterceptor.swift +++ b/Sources/GRPCInterceptors/Tracing/ClientOTelTracingInterceptor.swift @@ -15,8 +15,8 @@ */ public import GRPCCore -internal import Tracing internal import Synchronization +internal import Tracing /// A client interceptor that injects tracing information into the request. /// @@ -96,7 +96,8 @@ public struct ClientOTelTracingInterceptor: ClientInterceptor { afterEachWrite: { var event = SpanEvent(name: "rpc.message") event.attributes.rpc.messageType = "SENT" - event.attributes.rpc.messageID = messageSentCounter + event.attributes.rpc.messageID = + messageSentCounter .wrappingAdd(1, ordering: .sequentiallyConsistent) .oldValue span.addEvent(event) @@ -122,7 +123,8 @@ public struct ClientOTelTracingInterceptor: ClientInterceptor { let onEachPartRecordingSequence = success.bodyParts.map { element in var event = SpanEvent(name: "rpc.message") event.attributes.rpc.messageType = "RECEIVED" - event.attributes.rpc.messageID = messageReceivedCounter + event.attributes.rpc.messageID = + messageReceivedCounter .wrappingAdd(1, ordering: .sequentiallyConsistent) .oldValue span.addEvent(event) diff --git a/Sources/GRPCInterceptors/Tracing/SpanAttributes+RPCAttributes.swift b/Sources/GRPCInterceptors/Tracing/SpanAttributes+RPCAttributes.swift index 1f36613..c998ef6 100644 --- a/Sources/GRPCInterceptors/Tracing/SpanAttributes+RPCAttributes.swift +++ b/Sources/GRPCInterceptors/Tracing/SpanAttributes+RPCAttributes.swift @@ -34,7 +34,7 @@ package struct RPCAttributes: SpanAttributeNamespace { var messageType: Key { "rpc.message.type" } var grpcStatusCode: Key { "rpc.grpc.status_code" } - var serverAddress: Key{ "server.address" } + var serverAddress: Key { "server.address" } var serverPort: Key { "server.port" } var clientAddress: Key { "client.address" } @@ -47,9 +47,9 @@ package struct RPCAttributes: SpanAttributeNamespace { } } -package extension SpanAttributes { +extension SpanAttributes { /// Semantic conventions for RPC spans. - var rpc: RPCAttributes { + package var rpc: RPCAttributes { get { .init(attributes: self) } diff --git a/Tests/GRPCInterceptorsTests/TracingInterceptorTests.swift b/Tests/GRPCInterceptorsTests/TracingInterceptorTests.swift index 205a1f0..bb759de 100644 --- a/Tests/GRPCInterceptorsTests/TracingInterceptorTests.swift +++ b/Tests/GRPCInterceptorsTests/TracingInterceptorTests.swift @@ -73,25 +73,31 @@ final class TracingInterceptorTests: XCTestCase { try await AssertStreamContentsEqual([["response"]], response.messages) AssertTestSpanComponents(forMethod: methodDescriptor) { events in - XCTAssertEqual(events.map({ $0.name }), [ - "Request started", - "Request ended", - "Received response start", - "Received response end" - ]) + XCTAssertEqual( + events.map({ $0.name }), + [ + "Request started", + "Request ended", + "Received response start", + "Received response end", + ] + ) } assertAttributes: { attributes in - XCTAssertEqual(attributes, [ - "rpc.system": .string("grpc"), - "rpc.method": .string(methodDescriptor.method), - "rpc.service": .string(methodDescriptor.service.fullyQualifiedService), - "rpc.grpc.status_code": .int(0), - "server.address": .string("someserver.com"), - "server.port": .int(567), - "network.peer.address": .string("10.1.2.80"), - "network.peer.port": .int(567), - "network.transport": .string("tcp"), - "network.type": .string("ipv4") - ]) + XCTAssertEqual( + attributes, + [ + "rpc.system": .string("grpc"), + "rpc.method": .string(methodDescriptor.method), + "rpc.service": .string(methodDescriptor.service.fullyQualifiedService), + "rpc.grpc.status_code": .int(0), + "server.address": .string("someserver.com"), + "server.port": .int(567), + "network.peer.address": .string("10.1.2.80"), + "network.peer.port": .int(567), + "network.transport": .string("tcp"), + "network.type": .string("ipv4"), + ] + ) } assertStatus: { status in XCTAssertNil(status) } assertErrors: { errors in @@ -152,23 +158,29 @@ final class TracingInterceptorTests: XCTestCase { try await AssertStreamContentsEqual([["response"]], response.messages) AssertTestSpanComponents(forMethod: methodDescriptor) { events in - XCTAssertEqual(events.map({ $0.name }), [ - "Request started", - "Request ended", - "Received response start", - "Received response end" - ]) + XCTAssertEqual( + events.map({ $0.name }), + [ + "Request started", + "Request ended", + "Received response start", + "Received response end", + ] + ) } assertAttributes: { attributes in - XCTAssertEqual(attributes, [ - "rpc.system": .string("grpc"), - "rpc.method": .string(methodDescriptor.method), - "rpc.service": .string(methodDescriptor.service.fullyQualifiedService), - "rpc.grpc.status_code": .int(0), - "server.address": .string("someserver.com"), - "network.peer.address": .string("some-path"), - "network.transport": .string("tcp"), - "network.type": .string("unix") - ]) + XCTAssertEqual( + attributes, + [ + "rpc.system": .string("grpc"), + "rpc.method": .string(methodDescriptor.method), + "rpc.service": .string(methodDescriptor.service.fullyQualifiedService), + "rpc.grpc.status_code": .int(0), + "server.address": .string("someserver.com"), + "network.peer.address": .string("some-path"), + "network.transport": .string("tcp"), + "network.type": .string("unix"), + ] + ) } assertStatus: { status in XCTAssertNil(status) } assertErrors: { errors in @@ -226,33 +238,39 @@ final class TracingInterceptorTests: XCTestCase { try await AssertStreamContentsEqual([["response"]], response.messages) AssertTestSpanComponents(forMethod: methodDescriptor) { events in - XCTAssertEqual(events, [ - TestSpanEvent("Request started", [:]), - // Recorded when `request1` is sent - TestSpanEvent("rpc.message", ["rpc.message.type": "SENT", "rpc.message.id": 1]), - // Recorded when `request2` is sent - TestSpanEvent("rpc.message", ["rpc.message.type": "SENT", "rpc.message.id": 2]), - // Recorded after all request parts have been sent - TestSpanEvent("Request ended", [:]), - // Recorded when receiving response part - TestSpanEvent("Received response start", [:]), - TestSpanEvent("rpc.message", ["rpc.message.type": "RECEIVED", "rpc.message.id": 1]), - // Recorded at end of response - TestSpanEvent("Received response end", [:]), - ]) + XCTAssertEqual( + events, + [ + TestSpanEvent("Request started", [:]), + // Recorded when `request1` is sent + TestSpanEvent("rpc.message", ["rpc.message.type": "SENT", "rpc.message.id": 1]), + // Recorded when `request2` is sent + TestSpanEvent("rpc.message", ["rpc.message.type": "SENT", "rpc.message.id": 2]), + // Recorded after all request parts have been sent + TestSpanEvent("Request ended", [:]), + // Recorded when receiving response part + TestSpanEvent("Received response start", [:]), + TestSpanEvent("rpc.message", ["rpc.message.type": "RECEIVED", "rpc.message.id": 1]), + // Recorded at end of response + TestSpanEvent("Received response end", [:]), + ] + ) } assertAttributes: { attributes in - XCTAssertEqual(attributes, [ - "rpc.system": .string("grpc"), - "rpc.method": .string(methodDescriptor.method), - "rpc.service": .string(methodDescriptor.service.fullyQualifiedService), - "rpc.grpc.status_code": .int(0), - "server.address": .string("someserver.com"), - "server.port": .int(567), - "network.peer.address": .string("10.1.2.80"), - "network.peer.port": .int(567), - "network.transport": .string("tcp"), - "network.type": .string("ipv4") - ]) + XCTAssertEqual( + attributes, + [ + "rpc.system": .string("grpc"), + "rpc.method": .string(methodDescriptor.method), + "rpc.service": .string(methodDescriptor.service.fullyQualifiedService), + "rpc.grpc.status_code": .int(0), + "server.address": .string("someserver.com"), + "server.port": .int(567), + "network.peer.address": .string("10.1.2.80"), + "network.peer.port": .int(567), + "network.transport": .string("tcp"), + "network.type": .string("ipv4"), + ] + ) } assertStatus: { status in XCTAssertNil(status) } assertErrors: { errors in @@ -312,17 +330,20 @@ final class TracingInterceptorTests: XCTestCase { XCTAssertEqual(events.map({ $0.name }), ["Request started"]) } assertAttributes: { attributes in // The attributes should not contain a grpc status code, as the request was never even sent. - XCTAssertEqual(attributes, [ - "rpc.system": .string("grpc"), - "rpc.method": .string(methodDescriptor.method), - "rpc.service": .string(methodDescriptor.service.fullyQualifiedService), - "server.address": .string("someserver.com"), - "server.port": .int(567), - "network.peer.address": .string("10.1.2.80"), - "network.peer.port": .int(567), - "network.transport": .string("tcp"), - "network.type": .string("ipv4") - ]) + XCTAssertEqual( + attributes, + [ + "rpc.system": .string("grpc"), + "rpc.method": .string(methodDescriptor.method), + "rpc.service": .string(methodDescriptor.service.fullyQualifiedService), + "server.address": .string("someserver.com"), + "server.port": .int(567), + "network.peer.address": .string("10.1.2.80"), + "network.peer.port": .int(567), + "network.transport": .string("tcp"), + "network.type": .string("ipv4"), + ] + ) } assertStatus: { status in XCTAssertNil(status) } assertErrors: { errors in @@ -378,24 +399,30 @@ final class TracingInterceptorTests: XCTestCase { } AssertTestSpanComponents(forMethod: methodDescriptor) { events in - XCTAssertEqual(events.map({ $0.name }), [ - "Request started", - "Request ended", - "Received error response" - ]) + XCTAssertEqual( + events.map({ $0.name }), + [ + "Request started", + "Request ended", + "Received error response", + ] + ) } assertAttributes: { attributes in - XCTAssertEqual(attributes, [ - "rpc.system": .string("grpc"), - "rpc.method": .string(methodDescriptor.method), - "rpc.service": .string(methodDescriptor.service.fullyQualifiedService), - "rpc.grpc.status_code": .int(14), // this is unavailable's raw code - "server.address": .string("someserver.com"), - "server.port": .int(567), - "network.peer.address": .string("10.1.2.80"), - "network.peer.port": .int(567), - "network.transport": .string("tcp"), - "network.type": .string("ipv4") - ]) + XCTAssertEqual( + attributes, + [ + "rpc.system": .string("grpc"), + "rpc.method": .string(methodDescriptor.method), + "rpc.service": .string(methodDescriptor.service.fullyQualifiedService), + "rpc.grpc.status_code": .int(14), // this is unavailable's raw code + "server.address": .string("someserver.com"), + "server.port": .int(567), + "network.peer.address": .string("10.1.2.80"), + "network.peer.port": .int(567), + "network.transport": .string("tcp"), + "network.type": .string("ipv4"), + ] + ) } assertStatus: { status in XCTAssertEqual(status, .some(.init(code: .error))) } assertErrors: { errors in diff --git a/Tests/GRPCInterceptorsTests/TracingTestsUtilities.swift b/Tests/GRPCInterceptorsTests/TracingTestsUtilities.swift index 6b7c868..a6b64b3 100644 --- a/Tests/GRPCInterceptorsTests/TracingTestsUtilities.swift +++ b/Tests/GRPCInterceptorsTests/TracingTestsUtilities.swift @@ -217,8 +217,8 @@ struct TestSpanEvent: Equatable, CustomDebugStringConvertible { } return """ - (name: \(self.name), attributes: [\(attributesDescription)]) - """ + (name: \(self.name), attributes: [\(attributesDescription)]) + """ } init(_ name: String, _ attributes: SpanAttributes) {