|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +import protocol SmithyHTTPAPI.HTTPClient |
| 7 | +import struct Smithy.Attributes |
| 8 | +import struct Smithy.AttributeKey |
| 9 | + |
| 10 | +/// Container for HTTPClient telemetry, including configurable attributes and names. |
| 11 | +/// |
| 12 | +/// Note: This is intended to be used within generated code, not directly. |
| 13 | +public class HttpTelemetry { |
| 14 | + private static var idleConnectionAttributes: Attributes { |
| 15 | + var attributes = Attributes() |
| 16 | + attributes.set(key: HttpMetricsAttributesKeys.state, value: ConnectionState.idle) |
| 17 | + return attributes |
| 18 | + } |
| 19 | + private static var acquiredConnectionAttributes: Attributes { |
| 20 | + var attributes = Attributes() |
| 21 | + attributes.set(key: HttpMetricsAttributesKeys.state, value: ConnectionState.acquired) |
| 22 | + return attributes |
| 23 | + } |
| 24 | + private static var inflightRequestsAttributes: Attributes { |
| 25 | + var attributes = Attributes() |
| 26 | + attributes.set(key: HttpMetricsAttributesKeys.state, value: RequestState.inflight) |
| 27 | + return attributes |
| 28 | + } |
| 29 | + private static var queuedRequestsAttributes: Attributes { |
| 30 | + var attributes = Attributes() |
| 31 | + attributes.set(key: HttpMetricsAttributesKeys.state, value: RequestState.queued) |
| 32 | + return attributes |
| 33 | + } |
| 34 | + |
| 35 | + internal let contextManager: any TelemetryContextManager |
| 36 | + internal let tracerProvider: any TracerProvider |
| 37 | + internal let loggerProvider: any LoggerProvider |
| 38 | + |
| 39 | + internal let tracerScope: String |
| 40 | + internal let tracerAttributes: Attributes? |
| 41 | + internal let spanName: String |
| 42 | + internal let spanAttributes: Attributes? |
| 43 | + |
| 44 | + internal let connectionsAcquireDuration: any Histogram |
| 45 | + private let connectionsLimit: any AsyncMeasurementHandle |
| 46 | + private let connectionsUsage: any AsyncMeasurementHandle |
| 47 | + internal var httpMetricsUsage: HttpMetricsUsage |
| 48 | + internal let connectionsUptime: any Histogram |
| 49 | + private let requestsUsage: any AsyncMeasurementHandle |
| 50 | + internal let requestsQueuedDuration: any Histogram |
| 51 | + internal let bytesSent: any MonotonicCounter |
| 52 | + internal let bytesReceived: any MonotonicCounter |
| 53 | + |
| 54 | + public init( |
| 55 | + httpScope: String, |
| 56 | + telemetryProvider: any TelemetryProvider = DefaultTelemetry.provider, |
| 57 | + meterScope: String? = nil, |
| 58 | + meterAttributes: Attributes? = nil, |
| 59 | + tracerScope: String? = nil, |
| 60 | + tracerAttributes: Attributes? = nil, |
| 61 | + spanName: String? = nil, |
| 62 | + spanAttributes: Attributes? = nil |
| 63 | + ) { |
| 64 | + self.contextManager = telemetryProvider.contextManager |
| 65 | + self.tracerProvider = telemetryProvider.tracerProvider |
| 66 | + self.loggerProvider = telemetryProvider.loggerProvider |
| 67 | + let meterScope: String = meterScope != nil ? meterScope! : httpScope |
| 68 | + self.tracerScope = tracerScope != nil ? tracerScope! : httpScope |
| 69 | + self.tracerAttributes = tracerAttributes |
| 70 | + self.spanName = spanName != nil ? spanName! : "HTTP" |
| 71 | + self.spanAttributes = spanAttributes |
| 72 | + let httpMetricsUsage = HttpMetricsUsage() |
| 73 | + self.httpMetricsUsage = httpMetricsUsage |
| 74 | + |
| 75 | + let meter = telemetryProvider.meterProvider.getMeter(scope: meterScope, attributes: meterAttributes) |
| 76 | + self.connectionsAcquireDuration = meter.createHistogram( |
| 77 | + name: "smithy.client.http.connections.acquire_duration", |
| 78 | + units: "s", |
| 79 | + description: "The time it takes a request to acquire a connection") |
| 80 | + self.connectionsLimit = meter.createAsyncUpDownCounter( |
| 81 | + name: "smithy.client.http.connections.limit", |
| 82 | + callback: { handle in |
| 83 | + handle.record( |
| 84 | + value: httpMetricsUsage.connectionsLimit, |
| 85 | + attributes: Attributes(), |
| 86 | + context: telemetryProvider.contextManager.current() |
| 87 | + ) |
| 88 | + }, |
| 89 | + units: "{connection}", |
| 90 | + description: "The maximum open connections allowed/configured for the HTTP client") |
| 91 | + self.connectionsUsage = meter.createAsyncUpDownCounter( |
| 92 | + name: "smithy.client.http.connections.usage", |
| 93 | + callback: { handle in |
| 94 | + handle.record( |
| 95 | + value: httpMetricsUsage.idleConnections, |
| 96 | + attributes: HttpTelemetry.idleConnectionAttributes, |
| 97 | + context: telemetryProvider.contextManager.current() |
| 98 | + ) |
| 99 | + handle.record( |
| 100 | + value: httpMetricsUsage.acquiredConnections, |
| 101 | + attributes: HttpTelemetry.acquiredConnectionAttributes, |
| 102 | + context: telemetryProvider.contextManager.current() |
| 103 | + ) |
| 104 | + }, |
| 105 | + units: "{connection}", |
| 106 | + description: "Current state of connections pool") |
| 107 | + self.connectionsUptime = meter.createHistogram( |
| 108 | + name: "smithy.client.http.connections.uptime", |
| 109 | + units: "s", |
| 110 | + description: "The amount of time a connection has been open") |
| 111 | + self.requestsUsage = meter.createAsyncUpDownCounter( |
| 112 | + name: "smithy.client.http.requests.usage", |
| 113 | + callback: { handle in |
| 114 | + handle.record( |
| 115 | + value: httpMetricsUsage.idleConnections, |
| 116 | + attributes: HttpTelemetry.idleConnectionAttributes, |
| 117 | + context: telemetryProvider.contextManager.current() |
| 118 | + ) |
| 119 | + handle.record( |
| 120 | + value: httpMetricsUsage.acquiredConnections, |
| 121 | + attributes: HttpTelemetry.acquiredConnectionAttributes, |
| 122 | + context: telemetryProvider.contextManager.current() |
| 123 | + ) |
| 124 | + }, |
| 125 | + units: "{request}", |
| 126 | + description: "The current state of HTTP client request concurrency") |
| 127 | + self.requestsQueuedDuration = meter.createHistogram( |
| 128 | + name: "smithy.client.http.requests.queued_duration", |
| 129 | + units: "s", |
| 130 | + description: "The amount of time a request spent queued waiting to be executed by the HTTP client") |
| 131 | + self.bytesSent = meter.createCounter( |
| 132 | + name: "smithy.client.http.bytes_sent", |
| 133 | + units: "By", |
| 134 | + description: "The total number of bytes sent by the HTTP client") |
| 135 | + self.bytesReceived = meter.createCounter( |
| 136 | + name: "smithy.client.http.bytes_received", |
| 137 | + units: "By", |
| 138 | + description: "The total number of bytes received by the HTTP client") |
| 139 | + } |
| 140 | + |
| 141 | + deinit { |
| 142 | + self.connectionsLimit.stop() |
| 143 | + self.connectionsUsage.stop() |
| 144 | + self.requestsUsage.stop() |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +private enum ConnectionState { |
| 149 | + fileprivate static let idle = "idle" |
| 150 | + fileprivate static let acquired = "acquired" |
| 151 | +} |
| 152 | + |
| 153 | +private enum RequestState { |
| 154 | + fileprivate static let inflight = "inflight" |
| 155 | + fileprivate static let queued = "queued" |
| 156 | +} |
| 157 | + |
| 158 | +internal enum HttpMetricsAttributesKeys { |
| 159 | + fileprivate static let state = AttributeKey<String>(name: "state") |
| 160 | + internal static let serverAddress = AttributeKey<String>(name: "server.address") |
| 161 | +} |
| 162 | + |
| 163 | +internal struct HttpMetricsUsage { |
| 164 | + public var connectionsLimit: Int = 0 |
| 165 | + public var idleConnections: Int = 0 |
| 166 | + public var acquiredConnections: Int = 0 |
| 167 | + public var inflightRequests: Int = 0 |
| 168 | + public var queuedRequests: Int = 0 |
| 169 | +} |
0 commit comments