Last active
July 18, 2017 22:32
-
-
Save stuartnelson3/88b70ffa1aa8d8c0e0d703d0574acc09 to your computer and use it in GitHub Desktop.
borrowing + lifetime issues
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "rust_learning" | |
version = "0.1.0" | |
authors = ["Stuart Nelson <[email protected]>"] | |
[dependencies] | |
prometheus = "0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate prometheus; | |
use prometheus::{Opts, Registry, Counter, TextEncoder, Encoder}; | |
use std::net::{TcpListener, TcpStream}; | |
use std::io::Write; | |
use std::thread; | |
fn handle_client(stream: TcpStream) { | |
// Turn this into a closure that accepts the prometheus::Registry | |
// I'll make a different function or something that increments the prometheus::Counter. | |
} | |
fn main() { | |
let listener = TcpListener::bind("0.0.0.0:5000").unwrap(); | |
// Create a Counter. | |
let counter_opts = Opts::new("test_counter", "test counter help"); | |
let counter = Counter::with_opts(counter_opts).unwrap(); | |
// Create a Registry and register Counter. | |
let r = Registry::new(); | |
r.register(Box::new(counter.clone())).unwrap(); | |
// accept connections and process them serially | |
for stream in listener.incoming() { | |
match stream { | |
Ok(mut stream) => { | |
thread::spawn(|| { | |
// handle_client(stream); | |
counter.inc(); | |
let mut buffer = vec![]; | |
let encoder = TextEncoder::new(); | |
let metric_familys = r.gather(); | |
encoder.encode(&metric_familys, &mut buffer).unwrap(); | |
stream.write(&buffer); | |
}); | |
} | |
Err(e) => { /* connection failed */ } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment