1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//! Janus-Kafka
//!
//! Kafka adapter for Janus.
#![warn(
    missing_debug_implementations,
    missing_docs,
    rust_2018_idioms,
    unreachable_pub
)]
#![cfg_attr(doc_cfg, feature(doc_cfg))]

mod error;
mod publisher;
#[cfg(feature = "serde1")]
mod serde;
mod subscriber;

use std::future::Future;

use futures_util::stream::TryStreamExt;
use janus::{AckHandler, AckMessage, Message};

pub use crate::error::{KafkaError, OffsetError};
pub use crate::publisher::{
    KafkaPublisher, KafkaPublisherStatus, PublisherAcker, PublisherConfig, PublisherMessage,
};
pub use crate::subscriber::{
    KafkaSubscriber, KafkaSubscriberStatus, Offset, SubscriberAcker, SubscriberConfig,
};

#[cfg(feature = "tokio-rt")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "tokio-rt")))]
pub use rdkafka::util::TokioRuntime;

#[cfg(feature = "instrumented")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "instrumented")))]
pub use prometheus::{opts, Opts};

/// Message extension methods for Kafka messages
pub trait MessageExt: Message {
    /// The topic of the message.
    fn topic(&self) -> &str;

    /// The key the message is partitioned against.
    fn key(&self) -> Option<&str>;
}

impl<M: MessageExt> MessageExt for AckMessage<M> {
    fn topic(&self) -> &str {
        self.message().topic()
    }

    fn key(&self) -> Option<&str> {
        self.message().key()
    }
}

/// Awaits each future returned by the publisher ack handler.
pub async fn noop_publisher_ack_handler<M, A>(mut handler: A) -> Result<(), A::Error>
where
    M: MessageExt,
    A: AckHandler,
    <A as janus::AckHandler>::Output: Future<Output = Result<M, (M, A::Error)>>,
{
    while let Some(fut) = handler.try_next().await? {
        fut.await.map(|_| ()).map_err(|(_, e)| e)?;
    }
    Ok(())
}

/// Acknowledges each ack, if the result variant is an error, the error is propagated.
pub async fn noop_subscriber_ack_handler<M, A>(mut handler: A) -> Result<(), KafkaError>
where
    M: MessageExt,
    A: AckHandler<Output = Result<M, (M, KafkaError)>, Error = KafkaError>,
{
    while let Some(res) = handler.try_next().await? {
        res.map(|_| ()).map_err(|(_, e)| e)?;
    }
    Ok(())
}

/// Awaits each future returned by the ack handler and increments a metric.
/// A convenience function to continuously processes acks until an error is
/// encountered.
#[cfg(feature = "instrumented")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "instrumented")))]
pub async fn instrumented_publisher_ack_handler<M, A>(
    mut handler: A,
    opts: prometheus::Opts,
) -> Result<(), A::Error>
where
    M: MessageExt,
    A: AckHandler,
    <A as janus::AckHandler>::Output: Future<Output = Result<M, (M, A::Error)>>,
{
    let counter = prometheus::register_counter_vec!(opts, &["status", "topic"]).unwrap();

    while let Some(fut) = handler.try_next().await? {
        fut.await
            .map(|m| {
                counter.with_label_values(&["success", m.topic()]).inc();
                ()
            })
            .map_err(|(m, e)| {
                counter.with_label_values(&["error", m.topic()]).inc();
                e
            })?;
    }
    Ok(())
}

/// For each ack increments a metric depending on the variant in the result.
/// A convenience function to continuously processes acks until an error is
/// encountered.
#[cfg(feature = "instrumented")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "instrumented")))]
pub async fn instrumented_subscriber_ack_handler<M, A>(
    mut handler: A,
    opts: prometheus::Opts,
) -> Result<(), KafkaError>
where
    M: MessageExt,
    A: AckHandler<Output = Result<M, (M, KafkaError)>, Error = KafkaError>,
{
    let counter = prometheus::register_counter_vec!(opts, &["status", "topic"]).unwrap();

    while let Some(res) = handler.try_next().await? {
        res.map(|m| {
            counter.with_label_values(&["success", m.topic()]).inc();
            ()
        })
        .map_err(|(m, e)| {
            counter.with_label_values(&["error", m.topic()]).inc();
            e
        })?
    }
    Ok(())
}