Skip to content

Commit

Permalink
Derives serde::Serialize on Statistics (#616)
Browse files Browse the repository at this point in the history
I realize that there is a `stats_raw` trait method, but it would be better to not have to re-type out the statistics fields myself. This came about because I would like to act on many of the individual fields locally via some aggregations, but then output them back out (along with other fields in a larger status struct) as a JSON-encoded string, e.g.:

```rs
#[derive(Serialize)]
pub struct Status {
  pub sample_field: u64,
  pub sample_field_2: boolean,
  pub statistics: Statistics,
}
```
  • Loading branch information
icdevin authored Nov 7, 2023
1 parent b3f3201 commit c4c1d8c
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
use std::collections::HashMap;

use serde::Deserialize;
use serde::{Deserialize, Serialize};

/// Overall statistics.
#[derive(Deserialize, Debug, Default, Clone)]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Statistics {
/// The name of the librdkafka handle.
pub name: String,
Expand Down Expand Up @@ -73,7 +73,7 @@ pub struct Statistics {
}

/// Per-broker statistics.
#[derive(Deserialize, Debug, Default, Clone)]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Broker {
/// The broker hostname, port, and ID, in the form `HOSTNAME:PORT/ID`.
pub name: String,
Expand Down Expand Up @@ -168,7 +168,7 @@ pub struct Broker {
///
/// These values are not exact; they are sampled estimates maintained by an
/// HDR histogram in librdkafka.
#[derive(Deserialize, Debug, Default, Clone)]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Window {
/// The smallest value.
pub min: i64,
Expand Down Expand Up @@ -202,7 +202,7 @@ pub struct Window {
}

/// A topic and partition specifier.
#[derive(Deserialize, Debug, Default, Clone)]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct TopicPartition {
/// The name of the topic.
pub topic: String,
Expand All @@ -211,7 +211,7 @@ pub struct TopicPartition {
}

/// Per-topic statistics.
#[derive(Deserialize, Debug, Default, Clone)]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Topic {
/// The name of the topic.
pub topic: String,
Expand All @@ -226,7 +226,7 @@ pub struct Topic {
}

/// Per-partition statistics.
#[derive(Deserialize, Debug, Default, Clone)]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Partition {
/// The partition ID.
pub partition: i32,
Expand Down Expand Up @@ -299,7 +299,7 @@ pub struct Partition {
}

/// Consumer group manager statistics.
#[derive(Deserialize, Debug, Default, Clone)]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct ConsumerGroup {
/// The local consumer group handler's state.
pub state: String,
Expand All @@ -321,7 +321,7 @@ pub struct ConsumerGroup {
}

/// Exactly-once semantics statistics.
#[derive(Deserialize, Debug, Default, Clone)]
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct ExactlyOnceSemantics {
/// The current idempotent producer state.
pub idemp_state: String,
Expand Down

0 comments on commit c4c1d8c

Please sign in to comment.