Skip to content

Commit d7dc26d

Browse files
support for grafana cloud which is snappy default and no suffix in url
1 parent 36acf47 commit d7dc26d

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ default = ["protobuf"]
2222
gen = ["protobuf-codegen-pure"]
2323
nightly = ["libc"]
2424
process = ["libc", "procfs"]
25-
push = ["reqwest", "libc", "protobuf"]
25+
push = ["reqwest", "libc", "protobuf", "snap"]
2626

2727
[dependencies]
2828
cfg-if = "^1.0"
@@ -31,6 +31,7 @@ lazy_static = "^1.4"
3131
libc = { version = "^0.2", optional = true }
3232
parking_lot = "^0.12"
3333
protobuf = { version = "^2.0", optional = true }
34+
snap = { version = "^1.1", optional = true }
3435
memchr = "^2.3"
3536
reqwest = { version = "^0.11", features = ["blocking"], optional = true }
3637
thiserror = "^1.0"

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ pub use self::pulling_gauge::PullingGauge;
224224
#[cfg(feature = "push")]
225225
pub use self::push::{
226226
hostname_grouping_key, push_add_collector, push_add_metrics, push_collector, push_metrics,
227-
BasicAuthentication,
227+
push_raw, BasicAuthentication,
228228
};
229229
pub use self::registry::Registry;
230230
pub use self::registry::{default_registry, gather, register, unregister};

src/push.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,60 @@ fn push<S: BuildHasher>(
173173
}
174174
}
175175

176+
pub fn push_raw<S: BuildHasher>(
177+
url: &str,
178+
mfs: Vec<proto::MetricFamily>,
179+
method: &str,
180+
basic_auth: Option<BasicAuthentication>,
181+
) -> Result<()> {
182+
let mut push_url = if url.contains("://") {
183+
url.to_owned()
184+
} else {
185+
format!("http://{}", url)
186+
};
187+
188+
if push_url.ends_with('/') {
189+
push_url.pop();
190+
}
191+
192+
let encoder = ProtobufEncoder::new();
193+
let mut proto_buf = Vec::new();
194+
195+
for mf in mfs {
196+
// Ignore error, `no metrics` and `no name`.
197+
let _ = encoder.encode(&[mf], &mut proto_buf);
198+
}
199+
200+
let buf = snap::raw::Encoder::new()
201+
.compress_vec(&proto_buf)
202+
.expect("msg");
203+
204+
let mut builder = HTTP_CLIENT
205+
.request(
206+
Method::from_str(method).unwrap(),
207+
Url::from_str(&push_url).unwrap(),
208+
)
209+
.header(CONTENT_TYPE, encoder.format_type())
210+
.header("Content-Encoding", "snappy")
211+
.body(buf);
212+
213+
if let Some(BasicAuthentication { username, password }) = basic_auth {
214+
builder = builder.basic_auth(username, Some(password));
215+
}
216+
217+
let response = builder.send().map_err(|e| Error::Msg(format!("{}", e)))?;
218+
219+
match response.status() {
220+
StatusCode::ACCEPTED => Ok(()),
221+
StatusCode::OK => Ok(()),
222+
_ => Err(Error::Msg(format!(
223+
"unexpected status code {} while pushing to {}",
224+
response.status(),
225+
push_url
226+
))),
227+
}
228+
}
229+
176230
fn push_from_collector<S: BuildHasher>(
177231
job: &str,
178232
grouping: HashMap<String, String, S>,

0 commit comments

Comments
 (0)