Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support text format #12

Merged
merged 17 commits into from
Aug 23, 2016
Merged

support text format #12

merged 17 commits into from
Aug 23, 2016

Conversation

overvenus
Copy link
Member

#7 #9

  • Support text format.
  • Add a simple demo.

PTAL @siddontang @BusyJay

 * add Encoder
 * add TextEncoder
 * add HttpHandler
}
}

fn scarp(registry: &Registry, writer: &mut Write, encoder: &Encoder) -> Result<usize> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is scarp?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scrap is frequently used in doc

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this was supposed to be scrap.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, my poor english.

fn on_response_writable(&mut self, encoder: &mut HyperEncoder<HttpStream>) -> Next {
match encoder.try_write(&self.buffer.0[self.write_pos..]) {
Ok(Some(n)) => {
if n == self.buffer.0.len() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bug here, fix later.

colloctors_by_id: HashMap::new(),
dim_hashes_by_name: HashMap::new(),
};
pub fn scrap_registry(registry: &Registry, writer: &mut Write, encoder: &Encoder) -> Result<usize> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why using scrap here? confuse me.

@@ -8,8 +8,12 @@ default = []
dev = ["clippy"]

[[bin]]
name = "example"
path = "example/simple_example.rs"
name = "example_hyper"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please follow http://doc.crates.io/guide.html#project-layout, maybe we don't need bin for examples.


let c2 = counter.clone();
thread::spawn(move || {
loop {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't loop forever, some counters is ok enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not create new clones, just increases count.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I mean is don't run this forever, run some times and then terminate the program.

thread::spawn(move || {
for _ in 0..10 {
thread::sleep(Duration::from_millis(500));
c2.inc_by(3.14159265358979323846264338327).unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use std::f64::consts::PI?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use simple value like 1.0, 2.0, 3.0 in counter example.
Do go client use PI in counter?


// TODO: check type.
// TODO: check consistency.
for metric in mf.take_metric().into_iter() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use extend instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

existent_metrics is a protobuf::RepeatedField, unfortunately it does not support extend.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it


written += try!(writer.write(name.as_bytes()));

written += try!(label_pairs_to_text(mc.get_label(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seem that here can only support text format, how to support protocbuf later?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not, only for TextEncoder.

/// perform checks on the content of the metric and label names,
/// i.e. invalid metric or label names will result in invalid text format
/// output.
fn encode(&self, &[MetricFamily], &mut Write) -> Result<usize>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to return the written bytes here? Can it be figured out by the caller?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it returns the number of written bytes. Why? Because Content-Length in HTTP.

It is difficult for out caller to compute precise number of written bytes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, header should be written before body. Then a buffer is needed to cache the output. We can figure out the body length by the buffer length's diff.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you suggest return Result<()>?


@siddontang

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both are ok, but may Result<()> is more commonly used.
Prom go client encoder doesn't return size too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If return Result<()>, then write_writer is not needed, we can just use write!. The latter doesn't need to create another temporary string.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, but encoder in go client counts number internally.
Maybe I should change Result<usize> to Result<()>, but let TextEncoder counts number internally too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need, don't add it now.


let help = mf.get_help();
if !help.is_empty() {
try!(writer.write_all(format!("# HELP {} {}\n", name, escape_string(help, false))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use write!?

pub struct HttpHandler<'a> {
registry: Registry,
encoder: &'a (Encoder + 'a),
buffer: Vec<u8>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice, we should use VecDeque.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is only example, I think ok here.

@BusyJay
Copy link
Member

BusyJay commented Aug 23, 2016

rest LGTM

@BusyJay
Copy link
Member

BusyJay commented Aug 23, 2016

LGTM

@overvenus overvenus merged commit aff9075 into tikv:master Aug 23, 2016
@overvenus
Copy link
Member Author

Ref #8

@overvenus overvenus mentioned this pull request Aug 30, 2016
3 tasks
@overvenus overvenus deleted the text-format branch January 18, 2017 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants