Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gjcairo committed Jan 17, 2025
1 parent 5aa8dbb commit 2929500
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ package struct RPCAttributes: SpanAttributeNamespace {
var messageType: Key<String> { "rpc.message.type" }
var grpcStatusCode: Key<Int> { "rpc.grpc.status_code" }

var serverAddress: Key<String>{ "server.address" }
var serverAddress: Key<String> { "server.address" }
var serverPort: Key<Int> { "server.port" }

var clientAddress: Key<String> { "client.address" }
Expand All @@ -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)
}
Expand Down
203 changes: 115 additions & 88 deletions Tests/GRPCInterceptorsTests/TracingInterceptorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Tests/GRPCInterceptorsTests/TracingTestsUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ struct TestSpanEvent: Equatable, CustomDebugStringConvertible {
}

return """
(name: \(self.name), attributes: [\(attributesDescription)])
"""
(name: \(self.name), attributes: [\(attributesDescription)])
"""
}

init(_ name: String, _ attributes: SpanAttributes) {
Expand Down

0 comments on commit 2929500

Please sign in to comment.