|
| 1 | +use super::Context; |
| 2 | +use anyhow::{Ok, anyhow}; |
| 3 | +use crossterm::style::Stylize; |
| 4 | +use sift_stream::{ |
| 5 | + ChannelConfig, ChannelDataType, ChannelValue, Credentials, Flow, FlowBuilder, FlowConfig, |
| 6 | + IngestionConfigEncoder, IngestionConfigForm, RecoveryStrategy, RetryPolicy, RunForm, |
| 7 | + SiftStream, SiftStreamBuilder, TimeValue, |
| 8 | +}; |
| 9 | + |
| 10 | +/// Streams metrics to Sift. |
| 11 | +pub struct MetricsStreamingClient { |
| 12 | + ctx: Context, |
| 13 | + asset_name: String, |
| 14 | + sift_stream: Option<SiftStream<IngestionConfigEncoder>>, |
| 15 | +} |
| 16 | + |
| 17 | +impl MetricsStreamingClient { |
| 18 | + pub fn build( |
| 19 | + ctx: Context, |
| 20 | + stream_metrics: &Option<bool>, |
| 21 | + asset_name: &Option<String>, |
| 22 | + ) -> Result<Option<MetricsStreamingClient>, anyhow::Error> { |
| 23 | + if !stream_metrics.unwrap_or(false) { |
| 24 | + return Ok(None); |
| 25 | + } |
| 26 | + |
| 27 | + let Some(asset_name) = asset_name else { |
| 28 | + return Err(anyhow!( |
| 29 | + "must specify {} with streaming enabled", |
| 30 | + "--metrics_asset_name".cyan() |
| 31 | + )); |
| 32 | + }; |
| 33 | + |
| 34 | + Ok(Some(MetricsStreamingClient { |
| 35 | + ctx: ctx, |
| 36 | + asset_name: asset_name.clone(), |
| 37 | + sift_stream: None, |
| 38 | + })) |
| 39 | + } |
| 40 | + |
| 41 | + /// Initialize SiftStream and create ingestion config. |
| 42 | + pub async fn initialize(&mut self) -> Result<(), anyhow::Error> { |
| 43 | + let credentials = Credentials::Config { |
| 44 | + apikey: self.ctx.api_key.clone(), |
| 45 | + uri: self.ctx.grpc_uri.clone(), |
| 46 | + }; |
| 47 | + |
| 48 | + let ingestion_config = IngestionConfigForm { |
| 49 | + asset_name: self.asset_name.to_string(), |
| 50 | + client_key: "stress-test-ingestion-config-test".into(), |
| 51 | + flows: vec![FlowConfig { |
| 52 | + name: "metrics".into(), |
| 53 | + channels: vec![ |
| 54 | + ChannelConfig { |
| 55 | + name: "total_num_streams".into(), |
| 56 | + description: "Total number of streams created".into(), |
| 57 | + data_type: ChannelDataType::Uint32.into(), |
| 58 | + ..Default::default() |
| 59 | + }, |
| 60 | + ChannelConfig { |
| 61 | + name: "total_num_bytes_read".into(), |
| 62 | + description: "Total number of bytes read".into(), |
| 63 | + unit: "B".into(), |
| 64 | + data_type: ChannelDataType::Uint64.into(), |
| 65 | + ..Default::default() |
| 66 | + }, |
| 67 | + ChannelConfig { |
| 68 | + name: "total_num_messages".into(), |
| 69 | + description: "Total number of messages received".into(), |
| 70 | + unit: "message".into(), |
| 71 | + data_type: ChannelDataType::Uint64.into(), |
| 72 | + ..Default::default() |
| 73 | + }, |
| 74 | + ChannelConfig { |
| 75 | + name: "bytes_per_s".into(), |
| 76 | + description: "Number of bytes received per second".into(), |
| 77 | + data_type: ChannelDataType::Double.into(), |
| 78 | + unit: "B/s".into(), |
| 79 | + ..Default::default() |
| 80 | + }, |
| 81 | + ChannelConfig { |
| 82 | + name: "messages_per_s".into(), |
| 83 | + description: "Number of messages received per second".into(), |
| 84 | + unit: "message/s".into(), |
| 85 | + data_type: ChannelDataType::Double.into(), |
| 86 | + ..Default::default() |
| 87 | + }, |
| 88 | + ], |
| 89 | + }], |
| 90 | + }; |
| 91 | + |
| 92 | + let sift_stream = SiftStreamBuilder::new(credentials) |
| 93 | + .ingestion_config(ingestion_config) |
| 94 | + .recovery_strategy(RecoveryStrategy::RetryOnly(RetryPolicy::default())) |
| 95 | + .build() |
| 96 | + .await?; |
| 97 | + |
| 98 | + self.sift_stream = Some(sift_stream); |
| 99 | + |
| 100 | + Ok(()) |
| 101 | + } |
| 102 | + |
| 103 | + /// Send metrics to Sift. |
| 104 | + pub async fn ingest(&mut self, metrics: Metrics) { |
| 105 | + let flow = Flow::new( |
| 106 | + "metrics", |
| 107 | + TimeValue::now(), |
| 108 | + &[ |
| 109 | + ChannelValue::new("total_num_streams", metrics.total_num_streams), |
| 110 | + ChannelValue::new("total_num_bytes_read", metrics.total_num_bytes_read), |
| 111 | + ChannelValue::new("total_num_messages", metrics.total_num_messages), |
| 112 | + ChannelValue::new("bytes_per_s", metrics.bytes_per_s), |
| 113 | + ChannelValue::new("messages_per_s", metrics.messages_per_s), |
| 114 | + ], |
| 115 | + ); |
| 116 | + |
| 117 | + self.sift_stream.as_mut().unwrap().send(flow).await.unwrap(); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +pub struct Metrics { |
| 122 | + pub total_num_streams: u32, |
| 123 | + pub total_num_bytes_read: u64, |
| 124 | + pub total_num_messages: u64, |
| 125 | + pub bytes_per_s: f64, |
| 126 | + pub messages_per_s: f64, |
| 127 | +} |
0 commit comments