{"action":"created","comment":{"url":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/comments/75570145","id":75570145,"diff_hunk":"@@ -0,0 +1,249 @@\n+// Copyright 2016 PingCAP, Inc.\n+//\n+// Licensed under the Apache License, Version 2.0 (the \"License\");\n+// you may not use this file except in compliance with the License.\n+// You may obtain a copy of the License at\n+//\n+//     http://www.apache.org/licenses/LICENSE-2.0\n+//\n+// Unless required by applicable law or agreed to in writing, software\n+// distributed under the License is distributed on an \"AS IS\" BASIS,\n+// See the License for the specific language governing permissions and\n+// limitations under the License.\n+\n+use std::io::Write;\n+\n+use errors::{Result, Error};\n+use proto::MetricFamily;\n+use proto::{self, MetricType};\n+\n+pub trait Encoder {\n+    fn encode(&self, &MetricFamily, &mut Write) -> Result<usize>;\n+    fn get_format(&self) -> String;\n+}\n+\n+pub type Format = &'static str;\n+\n+pub const TEXT_FORMAT: Format = \"text/plain; version=0.0.4\";\n+\n+#[derive(Debug, Default)]\n+pub struct TextEncoder(String);\n+\n+impl TextEncoder {\n+    pub fn new() -> TextEncoder {\n+        TextEncoder(TEXT_FORMAT.to_owned())\n+    }\n+}\n+\n+impl Encoder for TextEncoder {\n+    /// encode converts a MetricFamily proto message into text format and\n+    /// writes the resulting lines to 'out'. It returns the number of bytes written\n+    /// and any error encountered.  This function does not perform checks on the\n+    /// content of the metric and label names, i.e. invalid metric or label names\n+    /// will result in invalid text format output.\n+    fn encode(&self, metric_family: &MetricFamily, writer: &mut Write) -> Result<usize> {\n+        if metric_family.get_metric().len() == 0 {\n+            return Err(Error::Msg(\"MetricFamily has no metrics\".to_owned()));\n+        }\n+\n+        let name = metric_family.get_name();\n+        if name == \"\" {\n+            return Err(Error::Msg(\"MetricFamily has no name\".to_owned()));\n+        }\n+\n+        let mut written = 0;\n+        let help = metric_family.get_help();\n+        if !help.is_empty() {\n+            written += try!(\n+                writer.write((format!(\"# HELP {} {}\\n\", name, escape_string(help, false)))\n+                        .as_bytes()).or_else(|e| Err(Error::Io(e))));\n+        }\n+\n+        let metric_type = metric_family.get_field_type();\n+\n+        let lowercase_type = format!(\"{:?}\", metric_type).to_owned().to_lowercase();\n+        written += try!(writer.write((format!(\"# TYPE {} {}\\n\", name, lowercase_type)).as_bytes())\n+            .or_else(|e| Err(Error::Io(e))));\n+\n+        for m in metric_family.get_metric() {\n+            match metric_type {\n+                MetricType::COUNTER => {\n+                    written +=\n+                        try!(write_sample(name, m, \"\", \"\", m.get_counter().get_value(), writer));\n+                }\n+                MetricType::GAUGE => {\n+                    written +=\n+                        try!(write_sample(name, m, \"\", \"\", m.get_gauge().get_value(), writer));\n+                }\n+                MetricType::SUMMARY | MetricType::HISTOGRAM | MetricType::UNTYPED => {}","path":"src/encoder.rs","position":78,"original_position":78,"commit_id":"1630a4e3ebd049184187e8f0be960acd1542480b","original_commit_id":"1630a4e3ebd049184187e8f0be960acd1542480b","user":{"login":"siddontang","id":1080370,"avatar_url":"https://avatars.githubusercontent.com/u/1080370?v=3","gravatar_id":"","url":"https://api.github.com/users/siddontang","html_url":"https://github.com/siddontang","followers_url":"https://api.github.com/users/siddontang/followers","following_url":"https://api.github.com/users/siddontang/following{/other_user}","gists_url":"https://api.github.com/users/siddontang/gists{/gist_id}","starred_url":"https://api.github.com/users/siddontang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/siddontang/subscriptions","organizations_url":"https://api.github.com/users/siddontang/orgs","repos_url":"https://api.github.com/users/siddontang/repos","events_url":"https://api.github.com/users/siddontang/events{/privacy}","received_events_url":"https://api.github.com/users/siddontang/received_events","type":"User","site_admin":false},"body":"do we support gauge now? ","created_at":"2016-08-20T01:36:46Z","updated_at":"2016-08-20T01:36:46Z","html_url":"https://github.com/pingcap/rust-prometheus/pull/12#discussion_r75570145","pull_request_url":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/12","_links":{"self":{"href":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/comments/75570145"},"html":{"href":"https://github.com/pingcap/rust-prometheus/pull/12#discussion_r75570145"},"pull_request":{"href":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/12"}}},"pull_request":{"url":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/12","id":81933317,"html_url":"https://github.com/pingcap/rust-prometheus/pull/12","diff_url":"https://github.com/pingcap/rust-prometheus/pull/12.diff","patch_url":"https://github.com/pingcap/rust-prometheus/pull/12.patch","issue_url":"https://api.github.com/repos/pingcap/rust-prometheus/issues/12","number":12,"state":"open","locked":false,"title":"support text format","user":{"login":"overvenus","id":2150711,"avatar_url":"https://avatars.githubusercontent.com/u/2150711?v=3","gravatar_id":"","url":"https://api.github.com/users/overvenus","html_url":"https://github.com/overvenus","followers_url":"https://api.github.com/users/overvenus/followers","following_url":"https://api.github.com/users/overvenus/following{/other_user}","gists_url":"https://api.github.com/users/overvenus/gists{/gist_id}","starred_url":"https://api.github.com/users/overvenus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/overvenus/subscriptions","organizations_url":"https://api.github.com/users/overvenus/orgs","repos_url":"https://api.github.com/users/overvenus/repos","events_url":"https://api.github.com/users/overvenus/events{/privacy}","received_events_url":"https://api.github.com/users/overvenus/received_events","type":"User","site_admin":false},"body":"#7 #9 \r\n\r\n - Support text format.\r\n - Add a simple demo.\r\n\r\nPTAL @siddontang @BusyJay ","created_at":"2016-08-19T06:40:44Z","updated_at":"2016-08-20T01:36:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ea84db5837f419ea9e384820e0beeb94ead2036e","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/12/commits","review_comments_url":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/12/comments","review_comment_url":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/comments{/number}","comments_url":"https://api.github.com/repos/pingcap/rust-prometheus/issues/12/comments","statuses_url":"https://api.github.com/repos/pingcap/rust-prometheus/statuses/1630a4e3ebd049184187e8f0be960acd1542480b","head":{"label":"overvenus:text-format","ref":"text-format","sha":"1630a4e3ebd049184187e8f0be960acd1542480b","user":{"login":"overvenus","id":2150711,"avatar_url":"https://avatars.githubusercontent.com/u/2150711?v=3","gravatar_id":"","url":"https://api.github.com/users/overvenus","html_url":"https://github.com/overvenus","followers_url":"https://api.github.com/users/overvenus/followers","following_url":"https://api.github.com/users/overvenus/following{/other_user}","gists_url":"https://api.github.com/users/overvenus/gists{/gist_id}","starred_url":"https://api.github.com/users/overvenus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/overvenus/subscriptions","organizations_url":"https://api.github.com/users/overvenus/orgs","repos_url":"https://api.github.com/users/overvenus/repos","events_url":"https://api.github.com/users/overvenus/events{/privacy}","received_events_url":"https://api.github.com/users/overvenus/received_events","type":"User","site_admin":false},"repo":{"id":66003047,"name":"rust-prometheus","full_name":"overvenus/rust-prometheus","owner":{"login":"overvenus","id":2150711,"avatar_url":"https://avatars.githubusercontent.com/u/2150711?v=3","gravatar_id":"","url":"https://api.github.com/users/overvenus","html_url":"https://github.com/overvenus","followers_url":"https://api.github.com/users/overvenus/followers","following_url":"https://api.github.com/users/overvenus/following{/other_user}","gists_url":"https://api.github.com/users/overvenus/gists{/gist_id}","starred_url":"https://api.github.com/users/overvenus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/overvenus/subscriptions","organizations_url":"https://api.github.com/users/overvenus/orgs","repos_url":"https://api.github.com/users/overvenus/repos","events_url":"https://api.github.com/users/overvenus/events{/privacy}","received_events_url":"https://api.github.com/users/overvenus/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/overvenus/rust-prometheus","description":"Prometheus instrumentation library for Rust applications","fork":true,"url":"https://api.github.com/repos/overvenus/rust-prometheus","forks_url":"https://api.github.com/repos/overvenus/rust-prometheus/forks","keys_url":"https://api.github.com/repos/overvenus/rust-prometheus/keys{/key_id}","collaborators_url":"https://api.github.com/repos/overvenus/rust-prometheus/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/overvenus/rust-prometheus/teams","hooks_url":"https://api.github.com/repos/overvenus/rust-prometheus/hooks","issue_events_url":"https://api.github.com/repos/overvenus/rust-prometheus/issues/events{/number}","events_url":"https://api.github.com/repos/overvenus/rust-prometheus/events","assignees_url":"https://api.github.com/repos/overvenus/rust-prometheus/assignees{/user}","branches_url":"https://api.github.com/repos/overvenus/rust-prometheus/branches{/branch}","tags_url":"https://api.github.com/repos/overvenus/rust-prometheus/tags","blobs_url":"https://api.github.com/repos/overvenus/rust-prometheus/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/overvenus/rust-prometheus/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/overvenus/rust-prometheus/git/refs{/sha}","trees_url":"https://api.github.com/repos/overvenus/rust-prometheus/git/trees{/sha}","statuses_url":"https://api.github.com/repos/overvenus/rust-prometheus/statuses/{sha}","languages_url":"https://api.github.com/repos/overvenus/rust-prometheus/languages","stargazers_url":"https://api.github.com/repos/overvenus/rust-prometheus/stargazers","contributors_url":"https://api.github.com/repos/overvenus/rust-prometheus/contributors","subscribers_url":"https://api.github.com/repos/overvenus/rust-prometheus/subscribers","subscription_url":"https://api.github.com/repos/overvenus/rust-prometheus/subscription","commits_url":"https://api.github.com/repos/overvenus/rust-prometheus/commits{/sha}","git_commits_url":"https://api.github.com/repos/overvenus/rust-prometheus/git/commits{/sha}","comments_url":"https://api.github.com/repos/overvenus/rust-prometheus/comments{/number}","issue_comment_url":"https://api.github.com/repos/overvenus/rust-prometheus/issues/comments{/number}","contents_url":"https://api.github.com/repos/overvenus/rust-prometheus/contents/{+path}","compare_url":"https://api.github.com/repos/overvenus/rust-prometheus/compare/{base}...{head}","merges_url":"https://api.github.com/repos/overvenus/rust-prometheus/merges","archive_url":"https://api.github.com/repos/overvenus/rust-prometheus/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/overvenus/rust-prometheus/downloads","issues_url":"https://api.github.com/repos/overvenus/rust-prometheus/issues{/number}","pulls_url":"https://api.github.com/repos/overvenus/rust-prometheus/pulls{/number}","milestones_url":"https://api.github.com/repos/overvenus/rust-prometheus/milestones{/number}","notifications_url":"https://api.github.com/repos/overvenus/rust-prometheus/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/overvenus/rust-prometheus/labels{/name}","releases_url":"https://api.github.com/repos/overvenus/rust-prometheus/releases{/id}","deployments_url":"https://api.github.com/repos/overvenus/rust-prometheus/deployments","created_at":"2016-08-18T14:17:28Z","updated_at":"2016-08-18T14:17:30Z","pushed_at":"2016-08-19T11:17:28Z","git_url":"git://github.com/overvenus/rust-prometheus.git","ssh_url":"git@github.com:overvenus/rust-prometheus.git","clone_url":"https://github.com/overvenus/rust-prometheus.git","svn_url":"https://github.com/overvenus/rust-prometheus","homepage":null,"size":41,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"pingcap:master","ref":"master","sha":"9b908f2a6aa3113a39978861b3aa8a31a71bcbc1","user":{"login":"pingcap","id":11855343,"avatar_url":"https://avatars.githubusercontent.com/u/11855343?v=3","gravatar_id":"","url":"https://api.github.com/users/pingcap","html_url":"https://github.com/pingcap","followers_url":"https://api.github.com/users/pingcap/followers","following_url":"https://api.github.com/users/pingcap/following{/other_user}","gists_url":"https://api.github.com/users/pingcap/gists{/gist_id}","starred_url":"https://api.github.com/users/pingcap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pingcap/subscriptions","organizations_url":"https://api.github.com/users/pingcap/orgs","repos_url":"https://api.github.com/users/pingcap/repos","events_url":"https://api.github.com/users/pingcap/events{/privacy}","received_events_url":"https://api.github.com/users/pingcap/received_events","type":"Organization","site_admin":false},"repo":{"id":65430363,"name":"rust-prometheus","full_name":"pingcap/rust-prometheus","owner":{"login":"pingcap","id":11855343,"avatar_url":"https://avatars.githubusercontent.com/u/11855343?v=3","gravatar_id":"","url":"https://api.github.com/users/pingcap","html_url":"https://github.com/pingcap","followers_url":"https://api.github.com/users/pingcap/followers","following_url":"https://api.github.com/users/pingcap/following{/other_user}","gists_url":"https://api.github.com/users/pingcap/gists{/gist_id}","starred_url":"https://api.github.com/users/pingcap/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pingcap/subscriptions","organizations_url":"https://api.github.com/users/pingcap/orgs","repos_url":"https://api.github.com/users/pingcap/repos","events_url":"https://api.github.com/users/pingcap/events{/privacy}","received_events_url":"https://api.github.com/users/pingcap/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/pingcap/rust-prometheus","description":"Prometheus instrumentation library for Rust applications","fork":false,"url":"https://api.github.com/repos/pingcap/rust-prometheus","forks_url":"https://api.github.com/repos/pingcap/rust-prometheus/forks","keys_url":"https://api.github.com/repos/pingcap/rust-prometheus/keys{/key_id}","collaborators_url":"https://api.github.com/repos/pingcap/rust-prometheus/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/pingcap/rust-prometheus/teams","hooks_url":"https://api.github.com/repos/pingcap/rust-prometheus/hooks","issue_events_url":"https://api.github.com/repos/pingcap/rust-prometheus/issues/events{/number}","events_url":"https://api.github.com/repos/pingcap/rust-prometheus/events","assignees_url":"https://api.github.com/repos/pingcap/rust-prometheus/assignees{/user}","branches_url":"https://api.github.com/repos/pingcap/rust-prometheus/branches{/branch}","tags_url":"https://api.github.com/repos/pingcap/rust-prometheus/tags","blobs_url":"https://api.github.com/repos/pingcap/rust-prometheus/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/pingcap/rust-prometheus/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/pingcap/rust-prometheus/git/refs{/sha}","trees_url":"https://api.github.com/repos/pingcap/rust-prometheus/git/trees{/sha}","statuses_url":"https://api.github.com/repos/pingcap/rust-prometheus/statuses/{sha}","languages_url":"https://api.github.com/repos/pingcap/rust-prometheus/languages","stargazers_url":"https://api.github.com/repos/pingcap/rust-prometheus/stargazers","contributors_url":"https://api.github.com/repos/pingcap/rust-prometheus/contributors","subscribers_url":"https://api.github.com/repos/pingcap/rust-prometheus/subscribers","subscription_url":"https://api.github.com/repos/pingcap/rust-prometheus/subscription","commits_url":"https://api.github.com/repos/pingcap/rust-prometheus/commits{/sha}","git_commits_url":"https://api.github.com/repos/pingcap/rust-prometheus/git/commits{/sha}","comments_url":"https://api.github.com/repos/pingcap/rust-prometheus/comments{/number}","issue_comment_url":"https://api.github.com/repos/pingcap/rust-prometheus/issues/comments{/number}","contents_url":"https://api.github.com/repos/pingcap/rust-prometheus/contents/{+path}","compare_url":"https://api.github.com/repos/pingcap/rust-prometheus/compare/{base}...{head}","merges_url":"https://api.github.com/repos/pingcap/rust-prometheus/merges","archive_url":"https://api.github.com/repos/pingcap/rust-prometheus/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/pingcap/rust-prometheus/downloads","issues_url":"https://api.github.com/repos/pingcap/rust-prometheus/issues{/number}","pulls_url":"https://api.github.com/repos/pingcap/rust-prometheus/pulls{/number}","milestones_url":"https://api.github.com/repos/pingcap/rust-prometheus/milestones{/number}","notifications_url":"https://api.github.com/repos/pingcap/rust-prometheus/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/pingcap/rust-prometheus/labels{/name}","releases_url":"https://api.github.com/repos/pingcap/rust-prometheus/releases{/id}","deployments_url":"https://api.github.com/repos/pingcap/rust-prometheus/deployments","created_at":"2016-08-11T02:06:25Z","updated_at":"2016-08-17T10:17:43Z","pushed_at":"2016-08-19T11:17:28Z","git_url":"git://github.com/pingcap/rust-prometheus.git","ssh_url":"git@github.com:pingcap/rust-prometheus.git","clone_url":"https://github.com/pingcap/rust-prometheus.git","svn_url":"https://github.com/pingcap/rust-prometheus","homepage":null,"size":42,"stargazers_count":4,"watchers_count":4,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":11,"forks":1,"open_issues":11,"watchers":4,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/12"},"html":{"href":"https://github.com/pingcap/rust-prometheus/pull/12"},"issue":{"href":"https://api.github.com/repos/pingcap/rust-prometheus/issues/12"},"comments":{"href":"https://api.github.com/repos/pingcap/rust-prometheus/issues/12/comments"},"review_comments":{"href":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/12/comments"},"review_comment":{"href":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/pingcap/rust-prometheus/pulls/12/commits"},"statuses":{"href":"https://api.github.com/repos/pingcap/rust-prometheus/statuses/1630a4e3ebd049184187e8f0be960acd1542480b"}}}}
{"actor":{"display_login":"siddontang"}}