Skip to content

Commit

Permalink
Merge pull request tikv#8 from quodlibetor/upstream
Browse files Browse the repository at this point in the history
Merge upstream into master
  • Loading branch information
quodlibetor committed Jun 30, 2020
2 parents 64a16e2 + 7810817 commit 535d5af
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 141 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fnv = "1.0"
lazy_static = "1.4"
libc = { version = "0.2", optional = true }
protobuf = { version = "2.0", optional = true }
regex = "1.3"
reqwest = { version = "0.10", features = ["blocking"], optional = true }
thiserror = "1.0"
parking_lot = "0.10.2"
Expand Down
63 changes: 18 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,44 @@
# Prometheus Rust client library

[![Build Status](https://travis-ci.org/pingcap/rust-prometheus.svg?branch=master)](https://travis-ci.org/pingcap/rust-prometheus)
[![Build Status](https://travis-ci.org/tikv/rust-prometheus.svg?branch=master)](https://travis-ci.org/pingcap/rust-prometheus)
[![docs.rs](https://docs.rs/prometheus/badge.svg)](https://docs.rs/prometheus)
[![crates.io](http://meritbadge.herokuapp.com/prometheus)](https://crates.io/crates/prometheus)
[![Dependency Status](https://deps.rs/repo/github/tikv/rust-prometheus/status.svg)](https://deps.rs/repo/github/tikv/rust-prometheus)

This is the [Rust](https://www.rust-lang.org) client library for [Prometheus](http://prometheus.io).
The main Structures and APIs are ported from [Go client](https://github.com/prometheus/client_golang).
This is the [Rust](https://www.rust-lang.org) client library for
[Prometheus](http://prometheus.io). The main data structures and APIs are ported
from [Go client](https://github.com/prometheus/client_golang).

## Usage

- Add dependency to your `Cargo.toml`:
## Documentation

```toml
prometheus = "0.8"
```
Find the latest documentation at https://docs.rs/prometheus

- Optional: Better performance for Rust nightly.

```toml
prometheus = { version = "0.8", features = ["nightly"] }
```

### Note

The crate has a pre-generated protobuf binding file for `protobuf` v2.0, if you need use the latest version of `protobuf`, you can generate the binding file on building with the `gen` feature.

```toml
prometheus = { version = "0.8", features = ["gen"] }
```

## Example

```rust
use prometheus::{Opts, Registry, Counter, TextEncoder, Encoder};
## Advanced

// Create a Counter.
let counter_opts = Opts::new("test_counter", "test counter help");
let counter = Counter::with_opts(counter_opts).unwrap();
### Features

// Create a Registry and register Counter.
let r = Registry::new();
r.register(Box::new(counter.clone())).unwrap();
This library supports four features:

// Inc.
counter.inc();
- `gen`: To generate protobuf client with the latest protobuf version instead of
using the pre-generated client.

// Gather the metrics.
let mut buffer = vec![];
let encoder = TextEncoder::new();
let metric_families = r.gather();
encoder.encode(&metric_families, &mut buffer).unwrap();
- `nightly`: Enable nightly only features.

// Output to the standard output.
println!("{}", String::from_utf8(buffer).unwrap());
```
- `process`: For collecting process info.

[More Examples](./examples)
- `push`: Enable push support.

## Advanced

### Static Metric

Static metric helps you make metric vectors faster.
When using a `MetricVec` with label values known at compile time
prometheus-static-metric reduces the overhead of retrieving the concrete
`Metric` from a `MetricVec`.

See [static-metric](./static-metric) directory for details.


## Thanks

- [brian-brazil](https://github.com/brian-brazil)
Expand Down
79 changes: 79 additions & 0 deletions benches/text_encoder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

#![feature(test)]

extern crate test;

use prometheus::{CounterVec, Encoder, HistogramOpts, HistogramVec, Opts, Registry, TextEncoder};
use test::Bencher;

#[bench]
fn bench_text_encoder_without_escaping(b: &mut Bencher) {
let registry = registry_with_test_metrics(false);
run_text_encoder(registry, b)
}

#[bench]
fn bench_text_encoder_with_escaping(b: &mut Bencher) {
let registry = registry_with_test_metrics(true);
run_text_encoder(registry, b)
}

fn registry_with_test_metrics(with_escaping: bool) -> Registry {
let registry = Registry::new();

for i in 0..100 {
let counter = CounterVec::new(
Opts::new(
format!("benchmark_counter_{}", i),
"A counter to benchmark it.",
),
&["one", "two", "three"],
)
.unwrap();
registry.register(Box::new(counter.clone())).unwrap();

let histogram = HistogramVec::new(
HistogramOpts::new(
format!("benchmark_histogram_{}", i),
"A histogram to benchmark it.",
),
&["one", "two", "three"],
)
.unwrap();
registry.register(Box::new(histogram.clone())).unwrap();

for j in 0..100 {
let j_string = j.to_string();
let label_values = if with_escaping {
["ei\\ns\n", "zw\"e\"i", &j_string]
} else {
["eins", "zwei", &j_string]
};

counter.with_label_values(&label_values).inc();
histogram.with_label_values(&label_values).observe(j.into());
}
}

registry
}

fn run_text_encoder(registry: Registry, b: &mut Bencher) {
let mut buffer = vec![];
let encoder = TextEncoder::new();
let metric_families = registry.gather();

b.iter(|| encoder.encode(&metric_families, &mut buffer).unwrap());
}
Loading

0 comments on commit 535d5af

Please sign in to comment.