kafka_protocol/messages/
push_telemetry_request.rs

1//! PushTelemetryRequest
2//!
3//! See the schema for this message [here](https://github.com/apache/kafka/blob/trunk/clients/src/main/resources/common/message/PushTelemetryRequest.json).
4// WARNING: the items of this module are generated and should not be edited directly
5#![allow(unused)]
6
7use std::borrow::Borrow;
8use std::collections::BTreeMap;
9
10use anyhow::{bail, Result};
11use bytes::Bytes;
12use uuid::Uuid;
13
14use crate::protocol::{
15    buf::{ByteBuf, ByteBufMut},
16    compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
17    Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
18};
19
20/// Valid versions: 0
21#[non_exhaustive]
22#[derive(Debug, Clone, PartialEq)]
23pub struct PushTelemetryRequest {
24    /// Unique id for this client instance.
25    ///
26    /// Supported API versions: 0
27    pub client_instance_id: Uuid,
28
29    /// Unique identifier for the current subscription.
30    ///
31    /// Supported API versions: 0
32    pub subscription_id: i32,
33
34    /// Client is terminating the connection.
35    ///
36    /// Supported API versions: 0
37    pub terminating: bool,
38
39    /// Compression codec used to compress the metrics.
40    ///
41    /// Supported API versions: 0
42    pub compression_type: i8,
43
44    /// Metrics encoded in OpenTelemetry MetricsData v1 protobuf format.
45    ///
46    /// Supported API versions: 0
47    pub metrics: Bytes,
48
49    /// Other tagged fields
50    pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
51}
52
53impl PushTelemetryRequest {
54    /// Sets `client_instance_id` to the passed value.
55    ///
56    /// Unique id for this client instance.
57    ///
58    /// Supported API versions: 0
59    pub fn with_client_instance_id(mut self, value: Uuid) -> Self {
60        self.client_instance_id = value;
61        self
62    }
63    /// Sets `subscription_id` to the passed value.
64    ///
65    /// Unique identifier for the current subscription.
66    ///
67    /// Supported API versions: 0
68    pub fn with_subscription_id(mut self, value: i32) -> Self {
69        self.subscription_id = value;
70        self
71    }
72    /// Sets `terminating` to the passed value.
73    ///
74    /// Client is terminating the connection.
75    ///
76    /// Supported API versions: 0
77    pub fn with_terminating(mut self, value: bool) -> Self {
78        self.terminating = value;
79        self
80    }
81    /// Sets `compression_type` to the passed value.
82    ///
83    /// Compression codec used to compress the metrics.
84    ///
85    /// Supported API versions: 0
86    pub fn with_compression_type(mut self, value: i8) -> Self {
87        self.compression_type = value;
88        self
89    }
90    /// Sets `metrics` to the passed value.
91    ///
92    /// Metrics encoded in OpenTelemetry MetricsData v1 protobuf format.
93    ///
94    /// Supported API versions: 0
95    pub fn with_metrics(mut self, value: Bytes) -> Self {
96        self.metrics = value;
97        self
98    }
99    /// Sets unknown_tagged_fields to the passed value.
100    pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
101        self.unknown_tagged_fields = value;
102        self
103    }
104    /// Inserts an entry into unknown_tagged_fields.
105    pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
106        self.unknown_tagged_fields.insert(key, value);
107        self
108    }
109}
110
111#[cfg(feature = "client")]
112impl Encodable for PushTelemetryRequest {
113    fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
114        types::Uuid.encode(buf, &self.client_instance_id)?;
115        types::Int32.encode(buf, &self.subscription_id)?;
116        types::Boolean.encode(buf, &self.terminating)?;
117        types::Int8.encode(buf, &self.compression_type)?;
118        types::CompactBytes.encode(buf, &self.metrics)?;
119        let num_tagged_fields = self.unknown_tagged_fields.len();
120        if num_tagged_fields > std::u32::MAX as usize {
121            bail!(
122                "Too many tagged fields to encode ({} fields)",
123                num_tagged_fields
124            );
125        }
126        types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
127
128        write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
129        Ok(())
130    }
131    fn compute_size(&self, version: i16) -> Result<usize> {
132        let mut total_size = 0;
133        total_size += types::Uuid.compute_size(&self.client_instance_id)?;
134        total_size += types::Int32.compute_size(&self.subscription_id)?;
135        total_size += types::Boolean.compute_size(&self.terminating)?;
136        total_size += types::Int8.compute_size(&self.compression_type)?;
137        total_size += types::CompactBytes.compute_size(&self.metrics)?;
138        let num_tagged_fields = self.unknown_tagged_fields.len();
139        if num_tagged_fields > std::u32::MAX as usize {
140            bail!(
141                "Too many tagged fields to encode ({} fields)",
142                num_tagged_fields
143            );
144        }
145        total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
146
147        total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
148        Ok(total_size)
149    }
150}
151
152#[cfg(feature = "broker")]
153impl Decodable for PushTelemetryRequest {
154    fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
155        let client_instance_id = types::Uuid.decode(buf)?;
156        let subscription_id = types::Int32.decode(buf)?;
157        let terminating = types::Boolean.decode(buf)?;
158        let compression_type = types::Int8.decode(buf)?;
159        let metrics = types::CompactBytes.decode(buf)?;
160        let mut unknown_tagged_fields = BTreeMap::new();
161        let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
162        for _ in 0..num_tagged_fields {
163            let tag: u32 = types::UnsignedVarInt.decode(buf)?;
164            let size: u32 = types::UnsignedVarInt.decode(buf)?;
165            let unknown_value = buf.try_get_bytes(size as usize)?;
166            unknown_tagged_fields.insert(tag as i32, unknown_value);
167        }
168        Ok(Self {
169            client_instance_id,
170            subscription_id,
171            terminating,
172            compression_type,
173            metrics,
174            unknown_tagged_fields,
175        })
176    }
177}
178
179impl Default for PushTelemetryRequest {
180    fn default() -> Self {
181        Self {
182            client_instance_id: Uuid::nil(),
183            subscription_id: 0,
184            terminating: false,
185            compression_type: 0,
186            metrics: Default::default(),
187            unknown_tagged_fields: BTreeMap::new(),
188        }
189    }
190}
191
192impl Message for PushTelemetryRequest {
193    const VERSIONS: VersionRange = VersionRange { min: 0, max: 0 };
194    const DEPRECATED_VERSIONS: Option<VersionRange> = None;
195}
196
197impl HeaderVersion for PushTelemetryRequest {
198    fn header_version(version: i16) -> i16 {
199        2
200    }
201}