Skip to content

Commit 0829d20

Browse files
waahm7Steven YuanTingDaoK
authored
feat: Add fetch_metrics() for HTTP connection managers (#273)
Co-authored-by: Steven Yuan <yuasteve@amazon.com> Co-authored-by: Dengke Tang <815825145@qq.com>
1 parent 9d6d175 commit 0829d20

9 files changed

Lines changed: 65 additions & 7 deletions

File tree

Package.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ awsCCommonPlatformExcludes.append("source/arch/arm")
3838
#if !os(Windows)
3939
awsCCommonPlatformExcludes.append("source/windows")
4040
#endif
41+
let cSettingsCommon: [CSetting] = [
42+
.headerSearchPath("source/external/libcbor"),
43+
.define("DEBUG_BUILD", .when(configuration: .debug))
44+
]
4145

4246
//////////////////////////////////////////////////////////////////////
4347
/// aws-c-cal
@@ -198,7 +202,7 @@ packageTargets.append(contentsOf: [
198202
dependencies: ["AwsCPlatformConfig"],
199203
path: "aws-common-runtime/aws-c-common",
200204
exclude: awsCCommonPlatformExcludes,
201-
cSettings: cSettings
205+
cSettings: cSettingsCommon
202206
),
203207
.target(
204208
name: "AwsCSdkUtils",

Source/AwsCommonRuntimeKit/http/HTTP2StreamManager.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ public class HTTP2StreamManager {
4242
})
4343
}
4444

45+
/// Fetch the current manager metrics from connection manager.
46+
public func fetchMetrics() -> HTTPClientConnectionManagerMetrics {
47+
var cManagerMetrics = aws_http_manager_metrics()
48+
aws_http2_stream_manager_fetch_metrics(rawValue, &cManagerMetrics)
49+
return HTTPClientConnectionManagerMetrics(
50+
availableConcurrency: cManagerMetrics.available_concurrency,
51+
pendingConcurrencyAcquires: cManagerMetrics.pending_concurrency_acquires,
52+
leasedConcurrency: cManagerMetrics.leased_concurrency
53+
)
54+
}
55+
4556
deinit {
4657
aws_http2_stream_manager_release(rawValue)
4758
}

Source/AwsCommonRuntimeKit/http/HTTPClientConnectionManager.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ public class HTTPClientConnectionManager {
3939
}
4040
}
4141

42+
/// Fetch the current manager metrics from connection manager.
43+
public func fetchMetrics() -> HTTPClientConnectionManagerMetrics {
44+
var cManagerMetrics = aws_http_manager_metrics()
45+
aws_http_connection_manager_fetch_metrics(rawValue, &cManagerMetrics)
46+
return HTTPClientConnectionManagerMetrics(
47+
availableConcurrency: cManagerMetrics.available_concurrency,
48+
pendingConcurrencyAcquires: cManagerMetrics.pending_concurrency_acquires,
49+
leasedConcurrency: cManagerMetrics.leased_concurrency
50+
)
51+
}
52+
4253
deinit {
4354
aws_http_connection_manager_release(rawValue)
4455
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0.
3+
4+
public struct HTTPClientConnectionManagerMetrics {
5+
/// The number of additional concurrent requests that can be supported by the HTTP manager without needing to
6+
/// establish additional connections to the target server.
7+
///
8+
/// For connection manager, it equals to connections that's idle.
9+
/// For stream manager, it equals to the number of streams that are possible to be made without creating new
10+
/// connection, although the implementation can create new connection without fully filling it.
11+
public var availableConcurrency: Int
12+
/// The number of requests that are awaiting concurrency to be made available from the HTTP manager.
13+
public var pendingConcurrencyAcquires: Int
14+
/// The number of connections (HTTP/1.1) or streams (for HTTP/2 via. stream manager) currently vended to user.
15+
public var leasedConcurrency: Int
16+
17+
public init(
18+
availableConcurrency: Int,
19+
pendingConcurrencyAcquires: Int,
20+
leasedConcurrency: Int
21+
) {
22+
self.availableConcurrency = availableConcurrency
23+
self.pendingConcurrencyAcquires = pendingConcurrencyAcquires
24+
self.leasedConcurrency = leasedConcurrency
25+
}
26+
}

Test/AwsCommonRuntimeKitTests/http/HTTP2StreamManagerTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ class HTT2StreamManagerTests: HTTPClientTestFixture {
125125

126126
let stream = try await streamManager.acquireStream(requestOptions: http2RequestOptions)
127127
XCTAssertFalse(onCompleteCalled)
128+
let metrics = streamManager.fetchMetrics()
129+
XCTAssertTrue(metrics.availableConcurrency > 0)
130+
XCTAssertTrue(metrics.leasedConcurrency > 0)
128131
let data = TEST_DOC_LINE.data(using: .utf8)!
129132
for chunk in data.chunked(into: 5) {
130133
try await stream.writeChunk(chunk: chunk, endOfStream: false)

Test/AwsCommonRuntimeKitTests/http/HTTPTests.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class HTTPTests: HTTPClientTestFixture {
1515
_ = try await sendHTTPRequest(method: "GET", endpoint: host, path: getPath, connectionManager: connectionManager)
1616
_ = try await sendHTTPRequest(method: "GET", endpoint: host, path: "/delete", expectedStatus: 404, connectionManager: connectionManager)
1717
}
18-
18+
1919
func testGetHTTPSRequestWithUtf8Header() async throws {
2020
let connectionManager = try await getHttpConnectionManager(endpoint: host, ssh: true, port: 443)
2121
let utf8Header = HTTPHeader(name: "TestHeader", value: "TestValueWithEmoji🤯")
@@ -64,6 +64,9 @@ class HTTPTests: HTTPClientTestFixture {
6464
let streamBase = try connection.makeRequest(requestOptions: httpRequestOptions)
6565
try streamBase.activate()
6666
XCTAssertFalse(onCompleteCalled)
67+
let metrics = connectionManager.fetchMetrics()
68+
XCTAssertTrue(metrics.leasedConcurrency > 0)
69+
6770
let data = TEST_DOC_LINE.data(using: .utf8)!
6871
for chunk in data.chunked(into: 5) {
6972
try await streamBase.writeChunk(chunk: chunk, endOfStream: false)
@@ -118,7 +121,7 @@ class HTTPTests: HTTPClientTestFixture {
118121
// Sleep for 5 seconds to make sure onComplete is not triggerred
119122
try await Task.sleep(nanoseconds: 5_000_000_000)
120123
XCTAssertFalse(onCompleteCalled)
121-
124+
122125
let lastChunkData = Data("last chunk data".utf8)
123126
try await streamBase.writeChunk(chunk: lastChunkData, endOfStream: true)
124127
semaphore.wait()
@@ -135,7 +138,7 @@ class HTTPTests: HTTPClientTestFixture {
135138
XCTAssertEqual(body.data, TEST_DOC_LINE + String(decoding: lastChunkData, as: UTF8.self))
136139
}
137140

138-
141+
139142
func testHTTPStreamIsReleasedIfNotActivated() async throws {
140143
do {
141144
let httpRequestOptions = try getHTTPRequestOptions(method: "GET", endpoint: host, path: getPath)

aws-common-runtime/aws-c-common

Submodule aws-c-common updated 83 files

aws-common-runtime/s2n

Submodule s2n updated 343 files

0 commit comments

Comments
 (0)