initial import of metrics-exporter-http v0.1.0

This commit is contained in:
Toby Lawrence 2019-05-05 20:42:46 -04:00
parent 75a38b7bd8
commit 5ff6ec980e
No known key found for this signature in database
GPG Key ID: 3BB201B0EEE9212E
6 changed files with 186 additions and 0 deletions

3
metrics-exporter-http/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
**/*.rs.bk
Cargo.lock

View File

@ -0,0 +1,30 @@
# The Code of Conduct
This document is based on the [Rust Code of Conduct](https://www.rust-lang.org/conduct.html) and outlines the standard of conduct which is both expected and enforced as part of this project.
## Conduct
* We are committed to providing a friendly, safe and welcoming environment for all, regardless of level of experience, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, nationality, or other similar characteristic.
* Avoid using overtly sexual nicknames or other nicknames that might detract from a friendly, safe and welcoming environment for all.
* Please be kind and courteous. There's no need to be mean or rude.
* Respect that people have differences of opinion and that every design or implementation choice carries a trade-off and numerous costs. There is seldom a right answer.
* Please keep unstructured critique to a minimum. If you have solid ideas you want to experiment with, make a fork and see how it works.
* We will exclude you from interaction if you insult, demean or harass anyone. That is not welcome behaviour. We interpret the term "harassment" as including the definition in the [Citizen Code of Conduct](http://citizencodeofconduct.org/); if you have any lack of clarity about what might be included in that concept, please read their definition. In particular, we don't tolerate behavior that excludes people in socially marginalized groups.
* Private harassment is also unacceptable. No matter who you are, if you feel you have been or are being harassed or made uncomfortable by a community member, please contact one of the repository Owners immediately. Whether you're a regular contributor or a newcomer, we care about making this community a safe place for you and we've got your back.
* Likewise any spamming, trolling, flaming, baiting or other attention-stealing behaviour is not welcome.
## Moderation
These are the policies for upholding our community's standards of conduct. If you feel that a thread needs moderation, please use the contact information above, or mention @tobz or @LucioFranco in the thread.
1. Remarks that violate this Code of Conduct, including hateful, hurtful, oppressive, or exclusionary remarks, are not allowed. (Cursing is allowed, but never targeting another user, and never in a hateful manner.)
2. Remarks that moderators find inappropriate, whether listed in the code of conduct or not, are also not allowed.
In the Rust community we strive to go the extra step to look out for each other. Don't just aim to be technically unimpeachable, try to be your best self. In particular, avoid flirting with offensive or sensitive issues, particularly if they're off-topic; this all too often leads to unnecessary fights, hurt feelings, and damaged trust; worse, it can drive people away from the community entirely.
And if someone takes issue with something you said or did, resist the urge to be defensive. Just stop doing what it was they complained about and apologize. Even if you feel you were misinterpreted or unfairly accused, chances are good there was something you could've communicated better — remember that it's your responsibility to make your fellow Rustaceans comfortable. Everyone wants to get along and we are all here first and foremost because we want to talk about cool technology. You will find that people will be eager to assume good intent and forgive as long as you earn their trust.
## Contacts:
- Toby Lawrence ([toby@nuclearfurnace.com](mailto:toby@nuclearfurnace.com))
- Lucio Franco ([luciofranco14@gmail.com](mailto:luciofranco14@gmail.com))

View File

@ -0,0 +1,18 @@
[package]
name = "metrics-exporter-http"
version = "0.1.0"
authors = ["Toby Lawrence <toby@nuclearfurnace.com>"]
edition = "2018"
license = "MIT"
description = "metric exporter for serving metrics over HTTP"
homepage = "https://github.com/metrics-rs/metrics"
repository = "https://github.com/metrics-rs/metrics-exporter-http"
documentation = "https://docs.rs/metrics-exporter-http"
[dependencies]
metrics-core = { path = "../metrics-core", version = "^0.3" }
hyper = "^0.12"
log = "^0.4"

View File

@ -0,0 +1,17 @@
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

View File

@ -0,0 +1,18 @@
# metrics-exporter-http
[![conduct-badge][]][conduct] [![downloads-badge][] ![release-badge][]][crate] [![docs-badge][]][docs] [![license-badge][]](#license)
[conduct-badge]: https://img.shields.io/badge/%E2%9D%A4-code%20of%20conduct-blue.svg
[downloads-badge]: https://img.shields.io/crates/d/metrics-exporter-http.svg
[release-badge]: https://img.shields.io/crates/v/metrics-exporter-http.svg
[license-badge]: https://img.shields.io/crates/l/metrics-exporter-http.svg
[docs-badge]: https://docs.rs/metrics-exporter-http/badge.svg
[conduct]: https://github.com/metrics-rs/metrics-exporter-http/blob/master/CODE_OF_CONDUCT.md
[crate]: https://crates.io/crates/metrics-exporter-http
[docs]: https://docs.rs/metrics-exporter-http
__metrics-exporter-http__ is a metric exporter that outputs metrics over HTTP.
## code of conduct
**NOTE**: All conversations and contributions to this project shall adhere to the [Code of Conduct][conduct].

View File

@ -0,0 +1,100 @@
//! Exports metrics over HTTP.
//!
//! This exporter can utilize recorders that are able to be converted to a textual representation
//! via [`Into`]. It will respond to any requests, regardless of the method or path.
//!
//! # Run Modes
//! - `run` can be used to block the current thread, running the HTTP server on the configured
//! address
//! - `into_future` will return a [`Future`] that when driven will run the HTTP server on the
//! configured address
#[macro_use]
extern crate log;
use metrics_core::{AsyncSnapshotProvider, Recorder, Snapshot};
use std::error::Error;
use std::net::SocketAddr;
use hyper::{Body, Response, Server};
use hyper::service::service_fn;
use hyper::rt::run as hyper_run;
use hyper::rt::Future;
/// Exports metrics over HTTP.
pub struct HttpExporter<C, R> {
controller: C,
recorder: R,
address: SocketAddr,
}
impl<C, R> HttpExporter<C, R>
where
C: AsyncSnapshotProvider + Clone + Send + 'static,
C::SnapshotFuture: Send + 'static,
C::SnapshotError: Error + Send + Sync + 'static,
R: Recorder + Clone + Into<String> + Send + 'static,
{
/// Creates a new [`HttpExporter`] that listens on the given `address`.
///
/// Recorders expose their output by being converted into strings.
pub fn new(controller: C, recorder: R, address: SocketAddr) -> Self {
HttpExporter {
controller,
recorder,
address,
}
}
/// Run the exporter on the current thread.
///
/// This starts an HTTP server on the `address` the exporter was originally configured with,
/// responding to any request with the output of the configured recorder.
pub fn run(self) {
let server = self.into_future();
hyper_run(server);
}
/// Converts this exporter into a future which can be driven externally.
/// logs output on the given interval.
///
/// This starts an HTTP server on the `address` the exporter was originally configured with,
/// responding to any request with the output of the configured recorder.
pub fn into_future(self) -> impl Future<Item = (), Error = ()> {
let controller = self.controller;
let recorder = self.recorder;
let address = self.address;
build_hyper_server(controller, recorder, address)
}
}
fn build_hyper_server<C, R>(controller: C, recorder: R, address: SocketAddr) -> impl Future<Item = (), Error = ()>
where
C: AsyncSnapshotProvider + Clone + Send + 'static,
C::SnapshotFuture: Send + 'static,
C::SnapshotError: Error + Send + Sync + 'static,
R: Recorder + Clone + Into<String> + Send + 'static,
{
let service = move || {
let controller2 = controller.clone();
let recorder2 = recorder.clone();
service_fn(move |_| {
let recorder3 = recorder2.clone();
controller2.get_snapshot_async()
.then(move |result| match result {
Ok(snapshot) => {
let mut r = recorder3.clone();
snapshot.record(&mut r);
let output = r.into();
Ok(Response::new(Body::from(output)))
},
Err(e) => Err(e),
})
})
};
Server::bind(&address)
.serve(service)
.map_err(|e| error!("http exporter server error: {}", e))
}