servers/prom_remote_write/
mod.rs1pub mod decode;
21pub(crate) mod row_builder;
22pub(crate) mod types;
23#[cfg(any(test, feature = "testing"))]
24pub mod v2;
25#[cfg(not(any(test, feature = "testing")))]
26pub(crate) mod v2;
27pub mod validation;
28
29use bytes::Bytes;
30use lazy_static::lazy_static;
31use object_pool::Pool;
32use snafu::ResultExt;
33
34use crate::error;
35use crate::prom_remote_write::decode::{PromSeriesProcessor, PromWriteRequest};
36use crate::prom_remote_write::row_builder::TablesBuilder;
37use crate::prom_remote_write::validation::PromValidationMode;
38use crate::prom_store::{snappy_decompress, zstd_decompress};
39
40pub const REMOTE_WRITE_V1_VERSION: &str = "1.0";
43pub const REMOTE_WRITE_V2_VERSION: &str = "2.0";
44
45lazy_static! {
46 static ref PROM_WRITE_REQUEST_POOL: Pool<PromWriteRequest<'static>> =
47 Pool::new(256, PromWriteRequest::default);
48}
49
50pub fn try_decompress(is_zstd: bool, body: &[u8]) -> crate::error::Result<Vec<u8>> {
51 if is_zstd {
52 zstd_decompress(body)
53 } else {
54 snappy_decompress(body)
55 }
56}
57
58pub fn decode_remote_write_request(
59 is_zstd: bool,
60 body: Bytes,
61 prom_validation_mode: PromValidationMode,
62 processor: &mut PromSeriesProcessor,
63) -> crate::error::Result<TablesBuilder<'static>> {
64 let _timer = crate::metrics::METRIC_HTTP_PROM_STORE_CODEC_ELAPSED
65 .with_label_values(&["decode", REMOTE_WRITE_V1_VERSION])
66 .start_timer();
67
68 let buf = if let Ok(buf) = try_decompress(is_zstd, &body[..]) {
75 buf
76 } else {
77 try_decompress(!is_zstd, &body[..])?
79 };
80
81 let mut request = PROM_WRITE_REQUEST_POOL.pull(PromWriteRequest::default);
82
83 request
84 .decode(buf, prom_validation_mode, processor)
85 .context(error::DecodePromRemoteRequestSnafu)?;
86 Ok(std::mem::take(&mut request.table_data))
87}