Skip to content

Feature: expose librdkafka statistics as swift metrics #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c870864
introduce statistics for producer
blindspotbounty Jul 26, 2023
633773f
Merge remote-tracking branch 'origin/main' into feature/sc-1976/gsoc-…
blindspotbounty Jul 31, 2023
d9887b9
add statistics to new consumer with events
blindspotbounty Jul 31, 2023
e5f0483
Merge remote-tracking branch 'origin/main' into feature/sc-1976/gsoc-…
blindspotbounty Aug 10, 2023
d55a7fd
fix some artefacts
blindspotbounty Aug 10, 2023
8b4525b
adjust to KeyRefreshAttempts
blindspotbounty Aug 10, 2023
23e08fc
draft: statistics with metrics
blindspotbounty Aug 17, 2023
612a3c4
make structures internal
blindspotbounty Aug 17, 2023
5c10435
Update Sources/Kafka/Configuration/KafkaConfiguration+Metrics.swift
blindspotbounty Aug 21, 2023
2be2bd9
Update Sources/Kafka/Configuration/KafkaConsumerConfiguration.swift
blindspotbounty Aug 21, 2023
2cd0f8b
Update Sources/Kafka/Configuration/KafkaConfiguration+Metrics.swift
blindspotbounty Aug 21, 2023
abd97de
Update Sources/Kafka/Configuration/KafkaConfiguration+Metrics.swift
blindspotbounty Aug 21, 2023
15284e1
address review comments
blindspotbounty Aug 21, 2023
a4ee678
formatting
blindspotbounty Aug 21, 2023
0a0f1b8
map gauges in one place
blindspotbounty Aug 24, 2023
dcdbe21
Merge remote-tracking branch 'origin/main' into feature/sc-1976/gsoc-…
blindspotbounty Aug 29, 2023
5448eb4
move json mode as rd kafka statistics, misc renaming + docc
blindspotbounty Aug 30, 2023
900cb38
Merge branch 'main' into feature/sc-1976/gsoc-expose-librdkafka-stati…
blindspotbounty Sep 5, 2023
a83c970
address review comments
blindspotbounty Oct 10, 2023
4ebdf9d
remove import Metrics
blindspotbounty Oct 11, 2023
05cf1b9
divide producer/consumer configuration
blindspotbounty Oct 20, 2023
3febfcd
apply swiftformat
blindspotbounty Oct 20, 2023
455be80
Merge branch 'main' into feature/sc-1976/gsoc-expose-librdkafka-stati…
blindspotbounty Oct 20, 2023
a96edf7
Merge branch 'main' into feature/sc-1976/gsoc-expose-librdkafka-stati…
blindspotbounty Nov 3, 2023
af05f5b
fix code after conflicts
blindspotbounty Nov 3, 2023
8a3caf3
fix formatting
blindspotbounty Nov 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ let package = Package(
.package(url: "https://github.com/apple/swift-nio.git", from: "2.55.0"),
.package(url: "https://github.com/swift-server/swift-service-lifecycle.git", from: "2.0.0-alpha.1"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-metrics", from: "2.4.1"),
// The zstd Swift package produces warnings that we cannot resolve:
// https://github.com/facebook/zstd/issues/3328
.package(url: "https://github.com/facebook/zstd.git", from: "1.5.0"),
.package(url: "https://github.com/swift-extras/swift-extras-json.git", .upToNextMajor(from: "0.6.0")),
],
targets: [
.target(
Expand Down Expand Up @@ -80,6 +82,8 @@ let package = Package(
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
.product(name: "Logging", package: "swift-log"),
.product(name: "Metrics", package: "swift-metrics"),
.product(name: "ExtrasJSON", package: "swift-extras-json"),
]
),
.target(
Expand Down
65 changes: 65 additions & 0 deletions Sources/Kafka/Configuration/KafkaConfiguration+Metrics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-kafka-client open source project
//
// Copyright (c) 2023 Apple Inc. and the swift-kafka-client project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of swift-kafka-client project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Metrics

extension KafkaConfiguration {
// MARK: - Metrics

/// Use to configure metrics.
public struct MetricsOptions: Sendable {
/// librdkafka's internal monotonic clock (microseconds)
public var ts: Gauge?
/// Wall clock time in seconds since the epoch
public var time: Timer?
/// Time since this client instance was created
public var age: Timer?
/// Number of ops (callbacks, events, etc) waiting in queue for application to serve
public var replyQueue: Gauge?
/// Current number of messages in producer queues
public var msgCount: Gauge?
/// Current total size of messages in producer queues
public var msgSize: Gauge?
/// Threshold: maximum number of messages allowed allowed on the producer queues
public var msgMax: Gauge?
/// Threshold: maximum total size of messages allowed on the producer queues
public var msgSizeMax: Gauge?

/// Total number of requests sent to Kafka brokers
public var tx: Gauge?
/// Total number of bytes transmitted to Kafka brokers
public var txBytex: Gauge?
/// Total number of responses received from Kafka brokers
public var rx: Gauge?
/// Total number of bytes received from Kafka brokers
public var rxBytex: Gauge?

/// Total number of messages transmitted (produced) to Kafka brokers
public var txMessages: Gauge?
/// Total number of message bytes (including framing, such as per-Message framing and MessageSet/batch framing) transmitted to Kafka brokers
public var txMessagesBytex: Gauge?
/// Total number of messages consumed, not including ignored messages (due to offset, etc), from Kafka brokers.
public var rxMessages: Gauge?
/// Total number of message bytes (including framing) received from Kafka brokers
public var rxMessagesBytex: Gauge?

/// Number of topics in the metadata cache.
public var metadataCacheCount: Gauge?
}

public enum Metrics: Sendable {
case disable
case enable(updateInterval: KafkaConfiguration.KeyRefreshAttempts, options: MetricsOptions)
}
}
18 changes: 18 additions & 0 deletions Sources/Kafka/Configuration/KafkaConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,22 @@ public enum KafkaConfiguration {
/// Use the IPv6 address family.
public static let v6 = IPAddressFamily(description: "v6")
}

/// Minimum time between key refresh attempts.
public struct KeyRefreshAttempts: Sendable, Hashable {
internal let rawValue: UInt

private init(rawValue: UInt) {
self.rawValue = rawValue
}

/// (Lowest granularity is milliseconds)
public static func value(_ value: Duration) -> KeyRefreshAttempts {
precondition(
value.canBeRepresentedAsMilliseconds,
"Lowest granularity is milliseconds"
)
return .init(rawValue: UInt(value.inMilliseconds))
}
}
}
7 changes: 7 additions & 0 deletions Sources/Kafka/Configuration/KafkaConsumerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ public struct KafkaConsumerConfiguration {
/// Reconnect options.
public var reconnect: KafkaConfiguration.ReconnectOptions = .init()

/// Options for librdkafka metrics updates
public var metrics: KafkaConfiguration.Metrics = .disable

/// Security protocol to use (plaintext, ssl, sasl_plaintext, sasl_ssl).
/// Default: `.plaintext`
public var securityProtocol: KafkaConfiguration.SecurityProtocol = .plaintext
Expand Down Expand Up @@ -270,6 +273,10 @@ extension KafkaConsumerConfiguration {
resultDict["broker.address.family"] = broker.addressFamily.description
resultDict["reconnect.backoff.ms"] = String(reconnect.backoff.rawValue)
resultDict["reconnect.backoff.max.ms"] = String(reconnect.maximumBackoff.inMilliseconds)

if case .enable(let interval, _) = metrics {
resultDict["statistics.interval.ms"] = String(interval.rawValue)
}

// Merge with SecurityProtocol configuration dictionary
resultDict.merge(securityProtocol.dictionary) { _, _ in
Expand Down
7 changes: 7 additions & 0 deletions Sources/Kafka/Configuration/KafkaProducerConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public struct KafkaProducerConfiguration {
/// Reconnect options.
public var reconnect: KafkaConfiguration.ReconnectOptions = .init()

/// Options for librdkafka metrics updates
public var metrics: KafkaConfiguration.Metrics = .disable

/// Security protocol to use (plaintext, ssl, sasl_plaintext, sasl_ssl).
/// Default: `.plaintext`
public var securityProtocol: KafkaConfiguration.SecurityProtocol = .plaintext
Expand Down Expand Up @@ -211,6 +214,10 @@ extension KafkaProducerConfiguration {
resultDict["broker.address.family"] = self.broker.addressFamily.description
resultDict["reconnect.backoff.ms"] = String(self.reconnect.backoff.rawValue)
resultDict["reconnect.backoff.max.ms"] = String(self.reconnect.maximumBackoff.inMilliseconds)

if case .enable(let interval, _) = metrics {
resultDict["statistics.interval.ms"] = String(interval.rawValue)
}

// Merge with SecurityProtocol configuration dictionary
resultDict.merge(self.securityProtocol.dictionary) { _, _ in
Expand Down
28 changes: 21 additions & 7 deletions Sources/Kafka/KafkaConsumer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ public final class KafkaConsumer: Sendable, Service {
NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure,
KafkaConsumerCloseOnTerminate
>
typealias ProducerEvents = NIOAsyncSequenceProducer<
KafkaConsumerEvent,
NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure,
KafkaConsumerCloseOnTerminate
>

/// The configuration object of the consumer client.
private let configuration: KafkaConsumerConfiguration
/// A logger.
Expand Down Expand Up @@ -241,6 +247,9 @@ public final class KafkaConsumer: Sendable, Service {
if configuration.isAutoCommitEnabled == false {
subscribedEvents.append(.offsetCommit)
}
if case .enable = configuration.metrics {
subscribedEvents.append(.statistics)
}

let client = try RDKafkaClient.makeClient(
type: .consumer,
Expand All @@ -249,20 +258,21 @@ public final class KafkaConsumer: Sendable, Service {
logger: logger
)

let consumer = try KafkaConsumer(
client: client,
stateMachine: stateMachine,
configuration: configuration,
logger: logger
)

let sourceAndSequence = NIOAsyncSequenceProducer.makeSequence(
elementType: KafkaConsumerEvent.self,
backPressureStrategy: NIOAsyncSequenceProducerBackPressureStrategies.NoBackPressure(),
delegate: KafkaConsumerCloseOnTerminate(stateMachine: stateMachine)
)

let eventsSequence = KafkaConsumerEvents(wrappedSequence: sourceAndSequence.sequence)

let consumer = try KafkaConsumer(
client: client,
stateMachine: stateMachine,
configuration: configuration,
logger: logger
)

return (consumer, eventsSequence)
}

Expand Down Expand Up @@ -333,6 +343,10 @@ public final class KafkaConsumer: Sendable, Service {
source.finish()
throw error
}
case .statistics(let statistics):
if case let .enable(_, options) = self.configuration.metrics {
statistics.fill(options)
}
default:
break // Ignore
}
Expand Down
5 changes: 5 additions & 0 deletions Sources/Kafka/KafkaConsumerEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@

/// An enumeration representing events that can be received through the ``KafkaConsumerEvents`` asynchronous sequence.
public enum KafkaConsumerEvent: Sendable, Hashable {
// /// Statistics from librdkafka
// case statistics(KafkaStatistics)
/// - Important: Always provide a `default` case when switiching over this `enum`.
case DO_NOT_SWITCH_OVER_THIS_EXHAUSITVELY

internal init(_ event: RDKafkaClient.KafkaEvent) {
switch event {
case .statistics(let stat):
fatalError("Cannot cast \(event) to KafkaConsumerEvent")
// self = .statistics(stat)
case .deliveryReport:
fatalError("Cannot cast \(event) to KafkaConsumerEvent")
case .consumerMessages:
Expand Down
14 changes: 13 additions & 1 deletion Sources/Kafka/KafkaProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,17 @@ public final class KafkaProducer: Service, Sendable {
delegate: KafkaProducerCloseOnTerminate(stateMachine: stateMachine)
)
let source = sourceAndSequence.source

var subscribedEvents: [RDKafkaEvent] = [.log, .deliveryReport]
// Listen to statistics events when statistics enabled
if case .enable = configuration.metrics {
subscribedEvents.append(.statistics)
}

let client = try RDKafkaClient.makeClient(
type: .producer,
configDictionary: configuration.dictionary,
events: [.log, .deliveryReport],
events: subscribedEvents,
logger: logger
)

Expand Down Expand Up @@ -208,6 +214,12 @@ public final class KafkaProducer: Service, Sendable {
case .pollAndYield(let client, let source):
let events = client.eventPoll()
for event in events {
if case let .statistics(kafkaStatistics) = event {
if case let .enable(_, options) = self.configuration.metrics {
kafkaStatistics.fill(options)
}
continue
}
let producerEvent = KafkaProducerEvent(event)
// Ignore YieldResult as we don't support back pressure in KafkaProducer
_ = source?.yield(producerEvent)
Expand Down
5 changes: 5 additions & 0 deletions Sources/Kafka/KafkaProducerEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
public enum KafkaProducerEvent: Sendable, Hashable {
/// A collection of delivery reports received from the Kafka cluster indicating the status of produced messages.
case deliveryReports([KafkaDeliveryReport])
// /// Statistics from librdkafka
// case statistics(KafkaStatistics)
/// - Important: Always provide a `default` case when switching over this `enum`.
case DO_NOT_SWITCH_OVER_THIS_EXHAUSITVELY

internal init(_ event: RDKafkaClient.KafkaEvent) {
switch event {
case .deliveryReport(results: let results):
self = .deliveryReports(results)
case .statistics(let stat):
// self = .statistics(stat)
fatalError("Cannot cast \(event) to KafkaProducerEvent")
case .consumerMessages:
fatalError("Cannot cast \(event) to KafkaProducerEvent")
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/Kafka/RDKafka/RDKafkaClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ final class RDKafkaClient: Sendable {
enum KafkaEvent {
case deliveryReport(results: [KafkaDeliveryReport])
case consumerMessages(result: Result<KafkaConsumerMessage, Error>)
case statistics(KafkaStatistics)
}

/// Poll the event `rd_kafka_queue_t` for new events.
Expand Down Expand Up @@ -199,6 +200,8 @@ final class RDKafkaClient: Sendable {
self.handleLogEvent(event)
case .offsetCommit:
self.handleOffsetCommitEvent(event)
case .statistics:
events.append(self.handleStatistics(event))
case .none:
// Finished reading events, return early
return events
Expand Down Expand Up @@ -250,6 +253,11 @@ final class RDKafkaClient: Sendable {
// The returned message(s) MUST NOT be freed with rd_kafka_message_destroy().
}

private func handleStatistics(_ event: OpaquePointer?) -> KafkaEvent {
let jsonStr = String(cString: rd_kafka_event_stats(event))
return .statistics(KafkaStatistics(jsonString: jsonStr))
}

/// Handle event of type `RDKafkaEvent.log`.
///
/// - Parameter event: Pointer to underlying `rd_kafka_event_t`.
Expand Down
33 changes: 33 additions & 0 deletions Sources/Kafka/Utilities/KafkaStatistics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-kafka-gsoc open source project
//
// Copyright (c) 2023 Apple Inc. and the swift-kafka-gsoc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of swift-kafka-gsoc project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import ExtrasJSON

struct KafkaStatistics: Sendable, Hashable {
let jsonString: String

func fill(_ options: KafkaConfiguration.MetricsOptions) {
do {
let json = try XJSONDecoder().decode(KafkaStatisticsJson.self, from: self.jsonString.utf8)
if let age = options.age,
let jsonAge = json.age {
age.recordMicroseconds(jsonAge)
}

// TODO: other metrics
} catch {
fatalError("Statistics json decode error \(error)")
}
}
}
Loading