|
| 1 | +use chrono::{DateTime, Duration, Utc}; |
| 2 | +use futures::stream; |
| 3 | +use vector_core::event::{Event, Metric, MetricKind, MetricValue}; |
| 4 | +use vector_core::metric_tags; |
| 5 | + |
| 6 | +use crate::sinks::util::test::load_sink; |
| 7 | +use crate::{ |
| 8 | + config::{SinkConfig, SinkContext}, |
| 9 | + test_util::{ |
| 10 | + components::{run_and_assert_sink_compliance, SINK_TAGS}, |
| 11 | + trace_init, |
| 12 | + }, |
| 13 | +}; |
| 14 | + |
| 15 | +use super::GreptimeDBConfig; |
| 16 | + |
| 17 | +#[tokio::test] |
| 18 | +async fn test_greptimedb_sink() { |
| 19 | + trace_init(); |
| 20 | + let cfg = format!( |
| 21 | + r#"endpoint= "{}" |
| 22 | +"#, |
| 23 | + std::env::var("GREPTIMEDB_ENDPOINT").unwrap_or_else(|_| "localhost:4001".to_owned()) |
| 24 | + ); |
| 25 | + |
| 26 | + let (config, _) = load_sink::<GreptimeDBConfig>(&cfg).unwrap(); |
| 27 | + let (sink, _hc) = config.build(SinkContext::default()).await.unwrap(); |
| 28 | + |
| 29 | + let query_client = query_client(); |
| 30 | + |
| 31 | + // Drop the table and data inside |
| 32 | + let _ = query_client |
| 33 | + .get(&format!( |
| 34 | + "{}/v1/sql", |
| 35 | + std::env::var("GREPTIMEDB_HTTP").unwrap_or_else(|_| "http://localhost:4000".to_owned()) |
| 36 | + )) |
| 37 | + .query(&[("sql", "DROP TABLE ns_my_counter")]) |
| 38 | + .send() |
| 39 | + .await |
| 40 | + .unwrap(); |
| 41 | + |
| 42 | + let base_time = Utc::now(); |
| 43 | + let events: Vec<_> = (0..10).map(|idx| create_event(idx, base_time)).collect(); |
| 44 | + run_and_assert_sink_compliance(sink, stream::iter(events), &SINK_TAGS).await; |
| 45 | + |
| 46 | + let query_response = query_client |
| 47 | + .get(&format!( |
| 48 | + "{}/v1/sql", |
| 49 | + std::env::var("GREPTIMEDB_HTTP").unwrap_or_else(|_| "http://localhost:4000".to_owned()) |
| 50 | + )) |
| 51 | + .query(&[("sql", "SELECT region, val FROM ns_my_counter")]) |
| 52 | + .send() |
| 53 | + .await |
| 54 | + .unwrap() |
| 55 | + .text() |
| 56 | + .await |
| 57 | + .expect("Fetch json from greptimedb failed"); |
| 58 | + let result: serde_json::Value = |
| 59 | + serde_json::from_str(&query_response).expect("Invalid json returned from greptimedb query"); |
| 60 | + assert_eq!( |
| 61 | + result |
| 62 | + .pointer("/output/0/records/rows") |
| 63 | + .and_then(|v| v.as_array()) |
| 64 | + .expect("Error getting greptimedb response array") |
| 65 | + .len(), |
| 66 | + 10 |
| 67 | + ) |
| 68 | +} |
| 69 | + |
| 70 | +fn query_client() -> reqwest::Client { |
| 71 | + reqwest::Client::builder().build().unwrap() |
| 72 | +} |
| 73 | + |
| 74 | +fn create_event(i: i32, base_time: DateTime<Utc>) -> Event { |
| 75 | + Event::Metric( |
| 76 | + Metric::new( |
| 77 | + "my_counter".to_owned(), |
| 78 | + MetricKind::Incremental, |
| 79 | + MetricValue::Counter { value: i as f64 }, |
| 80 | + ) |
| 81 | + .with_namespace(Some("ns")) |
| 82 | + .with_tags(Some(metric_tags!( |
| 83 | + "region" => "us-west-1", |
| 84 | + "production" => "true", |
| 85 | + ))) |
| 86 | + .with_timestamp(Some(base_time + Duration::seconds(i as i64))), |
| 87 | + ) |
| 88 | +} |
0 commit comments