Skip to content

Commit 6a1ebce

Browse files
committed
Add HTTP attributes on server span
1 parent 806b633 commit 6a1ebce

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

Sources/GRPC/GRPCServerRequestRoutingHandler.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ extension GRPCServerRequestRoutingHandler: ChannelInboundHandler, RemovableChann
131131
var span: Span
132132

133133
let requestPart = self.unwrapInboundIn(data)
134-
switch self.unwrapInboundIn(data) {
134+
switch requestPart {
135135
case let .head(requestHead):
136136
precondition(self.state == .notConfigured)
137137

@@ -141,6 +141,12 @@ extension GRPCServerRequestRoutingHandler: ChannelInboundHandler, RemovableChann
141141
ofKind: .server
142142
)
143143

144+
if let userAgent = requestHead.headers.first(name: "User-Agent") {
145+
span.attributes[SpanAttributeName.HTTP.userAgent] = .string(userAgent)
146+
}
147+
span.attributes[SpanAttributeName.HTTP.method] = .string(requestHead.method.rawValue)
148+
span.attributes[SpanAttributeName.HTTP.flavor] = "\(requestHead.version.major).\(requestHead.version.minor)"
149+
span.attributes[SpanAttributeName.HTTP.target] = .string(requestHead.uri)
144150
span.attributes[SpanAttributeName.RPC.system] = "grpc"
145151

146152
InstrumentationSystem.instrument.extract(
@@ -245,11 +251,6 @@ extension GRPCServerRequestRoutingHandler: ChannelInboundHandler, RemovableChann
245251
}
246252

247253
private func makeCallHandler(channel: Channel, requestInfo: GRPCRequestInfo) -> GRPCCallHandler? {
248-
// URI format: "/package.Servicename/MethodName", resulting in the following components separated by a slash:
249-
// - uriComponents[0]: empty
250-
// - uriComponents[1]: service name (including the package name);
251-
// `CallHandlerProvider`s should provide the service name including the package name.
252-
// - uriComponents[2]: method name.
253254
self.logger.debug("making call handler", metadata: ["path": "\(requestInfo.uri)"])
254255

255256
let context = CallHandlerContext(

Sources/GRPC/HTTP1ToGRPCServerCodec.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import NIOFoundationCompat
2020
import NIOHTTP1
2121
import SwiftProtobuf
2222
import TracingInstrumentation
23+
import OpenTelemetryInstrumentationSupport
2324

2425
/// Incoming gRPC package with a fixed message type.
2526
///
@@ -350,15 +351,18 @@ extension HTTP1ToGRPCServerCodec: ChannelOutboundHandler {
350351
headers.add(name: GRPCHeaderName.acceptEncoding, value: acceptEncoding)
351352
}
352353

354+
let responseStatus = HTTPResponseStatus.ok
353355
context.write(
354356
self
355357
.wrapOutboundOut(.head(HTTPResponseHead(
356358
version: version,
357-
status: .ok,
359+
status: responseStatus,
358360
headers: headers
359361
))),
360362
promise: promise
361363
)
364+
self.span.attributes[SpanAttributeName.HTTP.statusCode] = .int(Int(responseStatus.code))
365+
self.span.attributes[SpanAttributeName.HTTP.statusText] = .string(responseStatus.reasonPhrase)
362366
self.outboundState = .expectingBodyOrStatus
363367

364368
case let .message(messageContext):
@@ -392,6 +396,7 @@ extension HTTP1ToGRPCServerCodec: ChannelOutboundHandler {
392396
compressed: messageContext.compressed
393397
)
394398
context.write(self.wrapOutboundOut(.body(.byteBuffer(messageBuffer))), promise: promise)
399+
self.span.attributes[SpanAttributeName.HTTP.responseContentLength] = .int(messageBuffer.readableBytes)
395400
}
396401
} catch {
397402
let error = GRPCError.SerializationFailure().captureContext()
@@ -462,6 +467,7 @@ extension HTTP1ToGRPCServerCodec: ChannelOutboundHandler {
462467
responseTextBuffer.clear(minimumCapacity: UInt32(encodedData.utf8.count))
463468
responseTextBuffer.writeString(encodedData)
464469

470+
self.span.attributes[SpanAttributeName.HTTP.responseContentLength] = .int(encodedData.utf8.count)
465471
// After collecting all response for gRPC Web connections, send one final aggregated
466472
// response.
467473
context.write(

0 commit comments

Comments
 (0)