Skip to content

Commit 913ee3d

Browse files
committed
Forward all read events in extracting NIO handler
1 parent 3b2c3c9 commit 913ee3d

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

Sources/NIOInstrumentation/HTTPHeadersExtractingHandler.swift renamed to Sources/NIOInstrumentation/HeaderExtractingHTTPServerHandler.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,24 @@ import Instrumentation
1616
import NIO
1717
import NIOHTTP1
1818

19-
public final class HTTPHeadersExtractingHandler: ChannelInboundHandler {
19+
import Baggage
20+
import Instrumentation
21+
import NIO
22+
import NIOHTTP1
23+
24+
public final class HeaderExtractingHTTPServerHandler: ChannelInboundHandler {
2025
public typealias InboundIn = HTTPServerRequestPart
2126
public typealias InboundOut = HTTPServerRequestPart
2227

2328
public init() {}
2429

2530
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
26-
guard case .head(let head) = unwrapInboundIn(data) else { return }
27-
var baggage = Baggage.topLevel
28-
InstrumentationSystem.instrument.extract(head.headers, into: &baggage, using: HTTPHeadersExtractor())
29-
context.baggage = baggage
31+
if case .head(let head) = self.unwrapInboundIn(data) {
32+
var baggage = Baggage.topLevel
33+
InstrumentationSystem.instrument.extract(head.headers, into: &baggage, using: HTTPHeadersExtractor())
34+
context.baggage = baggage
35+
}
36+
3037
context.fireChannelRead(data)
3138
}
3239
}

Tests/LinuxMain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class LinuxMainRunnerImpl: LinuxMainRunner {
3737
XCTMain([
3838
testCase(HTTPHeadersCarrierTests.allTests),
3939
testCase(HTTPHeadersExtractInjectTests.allTests),
40-
testCase(HTTPHeadersExtractingHandlerTests.allTests),
4140
testCase(HTTPHeadersInjectingHandlerTests.allTests),
41+
testCase(HeaderExtractingHTTPServerHandlerTests.allTests),
4242
testCase(InstrumentTests.allTests),
4343
testCase(InstrumentationSystemTests.allTests),
4444
testCase(SpanAttributeSemanticsTests.allTests),

Tests/NIOInstrumentationTests/HTTPHeadersExtractInjectTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class HTTPHeadersExtractInjectTests: XCTestCase {
2828

2929
let outboundHandler = HTTPHeadersInjectingHandler()
3030
let requestHandler = MockRequestHandler()
31-
let inboundHandler = HTTPHeadersExtractingHandler()
31+
let inboundHandler = HeaderExtractingHTTPServerHandler()
3232

3333
let channel = EmbeddedChannel(loop: EmbeddedEventLoop())
3434
XCTAssertNoThrow(try channel.pipeline.addHandlers([outboundHandler, requestHandler, inboundHandler]).wait())

Tests/NIOInstrumentationTests/HTTPHeadersExtractingHandlerTests+XCTest.swift renamed to Tests/NIOInstrumentationTests/HeaderExtractingHTTPServerHandlerTests+XCTest.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313
//
14-
// HTTPHeadersExtractingHandlerTests+XCTest.swift
14+
// HeaderExtractingHTTPServerHandlerTests+XCTest.swift
1515
//
1616
import XCTest
1717
///
@@ -20,12 +20,13 @@ import XCTest
2020
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
2121
///
2222

23-
extension HTTPHeadersExtractingHandlerTests {
23+
extension HeaderExtractingHTTPServerHandlerTests {
2424

2525
@available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings")
26-
static var allTests : [(String, (HTTPHeadersExtractingHandlerTests) -> () throws -> Void)] {
26+
static var allTests : [(String, (HeaderExtractingHTTPServerHandlerTests) -> () throws -> Void)] {
2727
return [
2828
("test_extracts_http_request_headers_into_baggage", test_extracts_http_request_headers_into_baggage),
29+
("test_forwards_all_read_events", test_forwards_all_read_events),
2930
]
3031
}
3132
}

Tests/NIOInstrumentationTests/HTTPHeadersExtractingHandlerTests.swift renamed to Tests/NIOInstrumentationTests/HeaderExtractingHTTPServerHandlerTests.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import NIOHTTP1
1818
import NIOInstrumentation
1919
import XCTest
2020

21-
final class HTTPHeadersExtractingHandlerTests: XCTestCase {
21+
final class HeaderExtractingHTTPServerHandlerTests: XCTestCase {
2222
override class func tearDown() {
2323
super.tearDown()
2424
InstrumentationSystem.bootstrapInternal(nil)
@@ -28,7 +28,7 @@ final class HTTPHeadersExtractingHandlerTests: XCTestCase {
2828
InstrumentationSystem.bootstrapInternal(FakeTracer())
2929

3030
let traceID = "abc"
31-
let handler = HTTPHeadersExtractingHandler()
31+
let handler = HeaderExtractingHTTPServerHandler()
3232
let loop = EmbeddedEventLoop()
3333
let channel = EmbeddedChannel(handler: handler, loop: loop)
3434

@@ -41,4 +41,21 @@ final class HTTPHeadersExtractingHandlerTests: XCTestCase {
4141

4242
XCTAssertEqual(channel._channelCore.baggage[FakeTracer.TraceIDKey.self], traceID)
4343
}
44+
45+
func test_forwards_all_read_events() throws {
46+
InstrumentationSystem.bootstrapInternal(FakeTracer())
47+
48+
let channel = EmbeddedChannel(loop: EmbeddedEventLoop())
49+
try channel.pipeline.addHandlers(HeaderExtractingHTTPServerHandler()).wait()
50+
51+
let requestHead = HTTPRequestHead(version: .init(major: 1, minor: 1), method: .GET, uri: "/")
52+
try channel.writeInbound(HTTPServerRequestPart.head(requestHead))
53+
XCTAssertNotNil(try channel.readInbound(as: HTTPServerRequestPart.self))
54+
55+
try channel.writeInbound(HTTPServerRequestPart.body(channel.allocator.buffer(string: "Test")))
56+
XCTAssertNotNil(try channel.readInbound(as: HTTPServerRequestPart.self))
57+
58+
try channel.writeInbound(HTTPServerRequestPart.end(nil))
59+
XCTAssertNotNil(try channel.readInbound(as: HTTPServerRequestPart.self))
60+
}
4461
}

0 commit comments

Comments
 (0)