Skip to content

Commit 56e7e60

Browse files
committed
Merge branch 'master' of github.com:tikv/rust-prometheus into hashing
Signed-off-by: Liam Gray <[email protected]>
2 parents 95ad518 + e17c5ce commit 56e7e60

25 files changed

+1827
-1642
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ on:
99
env:
1010
CARGO_TERM_COLOR: always
1111
# Pinned toolchain for linting and benchmarks
12-
ACTIONS_LINTS_TOOLCHAIN: 1.78.0
12+
ACTIONS_LINTS_TOOLCHAIN: 1.81.0
1313
# Minimum supported Rust version (MSRV)
14-
ACTION_MSRV_TOOLCHAIN: 1.74.0
14+
ACTION_MSRV_TOOLCHAIN: 1.81.0
1515
EXTRA_FEATURES: "protobuf push process"
1616

1717
jobs:

Cargo.toml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ license = "Apache-2.0"
99
name = "prometheus"
1010
readme = "README.md"
1111
repository = "https://github.com/tikv/rust-prometheus"
12+
rust-version = "1.81"
1213
version = "0.13.4"
1314

1415
[package.metadata.docs.rs]
1516
features = ["nightly"]
1617

1718
[features]
1819
default = ["protobuf"]
19-
gen = ["protobuf-codegen-pure"]
20+
gen = ["protobuf-codegen"]
2021
nightly = ["libc"]
2122
process = ["libc", "procfs"]
2223
push = ["reqwest", "libc", "protobuf"]
@@ -27,22 +28,23 @@ fnv = "^1.0"
2728
lazy_static = "^1.4"
2829
libc = { version = "^0.2", optional = true }
2930
parking_lot = "^0.12"
30-
protobuf = { version = "^2.0", optional = true }
31+
protobuf = { version = "^3.7.2", optional = true }
3132
memchr = "^2.3"
3233
reqwest = { version = "^0.12", features = ["blocking"], optional = true }
33-
thiserror = "^1.0"
34+
thiserror = "^2.0"
3435

3536
[target.'cfg(target_os = "linux")'.dependencies]
36-
procfs = { version = "^0.16", optional = true, default-features = false }
37+
procfs = { version = "^0.17", optional = true, default-features = false }
3738

3839
[dev-dependencies]
3940
criterion = "0.5"
4041
getopts = "^0.2"
41-
hyper = { version = "^0.14", features = ["server", "http1", "tcp"] }
42-
tokio = { version = "^1.0", features = ["macros", "rt-multi-thread"] }
42+
hyper = { version = "^1.6", features = ["http1", "server"] }
43+
hyper-util = { version = "^0.1", features = ["http1", "server", "tokio"] }
44+
tokio = { version = "^1.0", features = ["macros", "net", "rt-multi-thread"] }
4345

4446
[build-dependencies]
45-
protobuf-codegen-pure = { version = "^2.0", optional = true }
47+
protobuf-codegen = { version = "^3.7.2", optional = true }
4648

4749
[workspace]
4850
members = ["static-metric"]

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Find the latest documentation at <https://docs.rs/prometheus>.
1818

1919
This crate provides several optional components which can be enabled via [Cargo `[features]`](https://doc.rust-lang.org/cargo/reference/features.html):
2020

21+
- `protobuf`: Protobuf support, enabled by default.
22+
2123
- `gen`: To generate protobuf client with the latest protobuf version instead of
2224
using the pre-generated client.
2325

build.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
#[cfg(feature = "gen")]
44
fn generate_protobuf_binding_file() {
5-
protobuf_codegen_pure::run(protobuf_codegen_pure::Args {
6-
out_dir: "proto",
7-
input: &["proto/proto_model.proto"],
8-
includes: &["proto"],
9-
..Default::default()
10-
})
11-
.unwrap();
5+
protobuf_codegen::Codegen::new()
6+
.out_dir("proto")
7+
.inputs(["proto/proto_model.proto"])
8+
.includes(["proto"])
9+
.run()
10+
.expect("Protobuf codegen failed");
1211
}
1312

1413
#[cfg(not(feature = "gen"))]

examples/example_hyper.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
// Copyright 2019 TiKV Project Authors. Licensed under Apache-2.0.
22

3-
use hyper::{
4-
header::CONTENT_TYPE,
5-
service::{make_service_fn, service_fn},
6-
Body, Request, Response, Server,
7-
};
8-
use prometheus::{Counter, Encoder, Gauge, HistogramVec, TextEncoder};
3+
use std::net::SocketAddr;
94

5+
use hyper::body::Incoming;
6+
use hyper::header::CONTENT_TYPE;
7+
use hyper::server::conn::http1;
8+
use hyper::service::service_fn;
9+
use hyper::Request;
10+
use hyper::Response;
11+
use hyper_util::rt::TokioIo;
1012
use lazy_static::lazy_static;
1113
use prometheus::{labels, opts, register_counter, register_gauge, register_histogram_vec};
14+
use prometheus::{Counter, Encoder, Gauge, HistogramVec, TextEncoder};
15+
use tokio::net::TcpListener;
16+
17+
type BoxedErr = Box<dyn std::error::Error + Send + Sync + 'static>;
1218

1319
lazy_static! {
1420
static ref HTTP_COUNTER: Counter = register_counter!(opts!(
@@ -31,38 +37,39 @@ lazy_static! {
3137
.unwrap();
3238
}
3339

34-
async fn serve_req(_req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
40+
async fn serve_req(_req: Request<Incoming>) -> Result<Response<String>, BoxedErr> {
3541
let encoder = TextEncoder::new();
3642

3743
HTTP_COUNTER.inc();
3844
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["all"]).start_timer();
3945

4046
let metric_families = prometheus::gather();
41-
let mut buffer = vec![];
42-
encoder.encode(&metric_families, &mut buffer).unwrap();
43-
HTTP_BODY_GAUGE.set(buffer.len() as f64);
47+
let body = encoder.encode_to_string(&metric_families)?;
48+
HTTP_BODY_GAUGE.set(body.len() as f64);
4449

4550
let response = Response::builder()
4651
.status(200)
4752
.header(CONTENT_TYPE, encoder.format_type())
48-
.body(Body::from(buffer))
49-
.unwrap();
53+
.body(body)?;
5054

5155
timer.observe_duration();
5256

5357
Ok(response)
5458
}
5559

5660
#[tokio::main]
57-
async fn main() {
58-
let addr = ([127, 0, 0, 1], 9898).into();
61+
async fn main() -> Result<(), BoxedErr> {
62+
let addr: SocketAddr = ([127, 0, 0, 1], 9898).into();
5963
println!("Listening on http://{}", addr);
64+
let listener = TcpListener::bind(addr).await?;
6065

61-
let serve_future = Server::bind(&addr).serve(make_service_fn(|_| async {
62-
Ok::<_, hyper::Error>(service_fn(serve_req))
63-
}));
66+
loop {
67+
let (stream, _) = listener.accept().await?;
68+
let io = TokioIo::new(stream);
6469

65-
if let Err(err) = serve_future.await {
66-
eprintln!("server error: {}", err);
70+
let service = service_fn(serve_req);
71+
if let Err(err) = http1::Builder::new().serve_connection(io, service).await {
72+
eprintln!("server error: {:?}", err);
73+
};
6774
}
6875
}

proto/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// @generated
2+
3+
pub mod proto_model;

0 commit comments

Comments
 (0)