Skip to content

Commit e507ffb

Browse files
committed
Add prometheus protobuf
1 parent 9c7878e commit e507ffb

6 files changed

Lines changed: 520 additions & 657 deletions

File tree

build.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
use std::io::Result;
22

33
fn main() -> Result<()> {
4-
let proto_files = ["src/encoding/proto/openmetrics_data_model.proto"];
4+
let proto_files = ["src/encoding/proto/metrics.proto"];
55
let include_dirs = ["src/encoding/proto/"];
66
#[cfg(feature = "protobuf")]
7-
prost_build::compile_protos(
8-
&proto_files,
9-
&include_dirs,
10-
)?;
7+
prost_build::compile_protos(&proto_files, &include_dirs)?;
118

12-
for path in &proto_files {
13-
println!("cargo:rerun-if-changed={}", path);
14-
}
15-
for path in &include_dirs {
16-
println!("cargo:rerun-if-changed={}", path);
17-
}
9+
for path in &proto_files {
10+
println!("cargo:rerun-if-changed={}", path);
11+
}
12+
for path in &include_dirs {
13+
println!("cargo:rerun-if-changed={}", path);
14+
}
1815
Ok(())
1916
}

derive-encode/tests/lib.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn basic_flow() {
4848
mod protobuf {
4949
use crate::{Labels, Method};
5050
use prometheus_client::encoding::protobuf::encode;
51-
use prometheus_client::encoding::protobuf::openmetrics_data_model;
51+
use prometheus_client::encoding::protobuf::prometheus_data_model;
5252
use prometheus_client::metrics::counter::Counter;
5353
use prometheus_client::metrics::family::Family;
5454
use prometheus_client::registry::Registry;
@@ -67,19 +67,17 @@ mod protobuf {
6767
})
6868
.inc();
6969

70-
// Encode all metrics in the registry in the OpenMetrics protobuf format.
71-
let mut metric_set = encode(&registry).unwrap();
72-
let mut family: openmetrics_data_model::MetricFamily =
73-
metric_set.metric_families.pop().unwrap();
74-
let metric: openmetrics_data_model::Metric = family.metrics.pop().unwrap();
70+
let mut metric_families = encode(&registry).unwrap();
71+
let mut family: prometheus_data_model::MetricFamily = metric_families.pop().unwrap();
72+
let metric: prometheus_data_model::Metric = family.metric.pop().unwrap();
7573

76-
let method = &metric.labels[0];
77-
assert_eq!("method", method.name);
78-
assert_eq!("Get", method.value);
74+
let method = &metric.label[0];
75+
assert_eq!(Some("method".to_string()), method.name.clone());
76+
assert_eq!(Some("Get".to_string()), method.value.clone());
7977

80-
let path = &metric.labels[1];
81-
assert_eq!("path", path.name);
82-
assert_eq!("/metrics", path.value);
78+
let path = &metric.label[1];
79+
assert_eq!(Some("path".to_string()), path.name.clone());
80+
assert_eq!(Some("/metrics".to_string()), path.value.clone());
8381
}
8482

8583
#[test]
@@ -96,15 +94,13 @@ mod protobuf {
9694
})
9795
.inc();
9896

99-
// Encode all metrics in the registry in the OpenMetrics protobuf format.
100-
let mut metric_set = encode(&registry).unwrap();
101-
let mut family: openmetrics_data_model::MetricFamily =
102-
metric_set.metric_families.pop().unwrap();
103-
let metric: openmetrics_data_model::Metric = family.metrics.pop().unwrap();
97+
let mut metric_families = encode(&registry).unwrap();
98+
let mut family: prometheus_data_model::MetricFamily = metric_families.pop().unwrap();
99+
let metric: prometheus_data_model::Metric = family.metric.pop().unwrap();
104100

105-
let label = &metric.labels[0];
106-
assert_eq!("method", label.name);
107-
assert_eq!("Get", label.value);
101+
let label = &metric.label[0];
102+
assert_eq!(Some("method".to_string()), label.name.clone());
103+
assert_eq!(Some("Get".to_string()), label.value.clone());
108104
}
109105
}
110106

src/encoding.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,7 @@ impl EncodeGaugeValue for i64 {
642642

643643
impl EncodeGaugeValue for u64 {
644644
fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> {
645-
// Between forcing end users to do endless as i64 for things that are
646-
// clearly valid i64 and having one error case for rarely used protobuf when
647-
// a gauge is set to >i64::MAX, the latter seems like the right choice.
648-
encoder.encode_i64(i64::try_from(*self).map_err(|_err| std::fmt::Error)?)
645+
encoder.encode_u64(*self)
649646
}
650647
}
651648

@@ -683,6 +680,10 @@ impl GaugeValueEncoder<'_> {
683680
for_both_mut!(self, GaugeValueEncoderInner, e, e.encode_u32(v))
684681
}
685682

683+
fn encode_u64(&mut self, v: u64) -> Result<(), std::fmt::Error> {
684+
for_both_mut!(self, GaugeValueEncoderInner, e, e.encode_u64(v))
685+
}
686+
686687
fn encode_i64(&mut self, v: i64) -> Result<(), std::fmt::Error> {
687688
for_both_mut!(self, GaugeValueEncoderInner, e, e.encode_i64(v))
688689
}

src/encoding/proto/metrics.proto

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright 2013 Prometheus Team
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
syntax = "proto2";
15+
16+
package io.prometheus.client;
17+
18+
option java_package = "io.prometheus.client";
19+
option go_package = "github.com/prometheus/client_model/go;io_prometheus_client";
20+
21+
import "google/protobuf/timestamp.proto";
22+
23+
message LabelPair {
24+
optional string name = 1;
25+
optional string value = 2;
26+
}
27+
28+
enum MetricType {
29+
COUNTER = 0;
30+
GAUGE = 1;
31+
SUMMARY = 2;
32+
UNTYPED = 3;
33+
HISTOGRAM = 4;
34+
GAUGE_HISTOGRAM = 5;
35+
}
36+
37+
message Gauge {
38+
optional double value = 1;
39+
}
40+
41+
message Counter {
42+
optional double value = 1;
43+
optional Exemplar exemplar = 2;
44+
optional google.protobuf.Timestamp created_timestamp = 3;
45+
}
46+
47+
message Quantile {
48+
optional double quantile = 1;
49+
optional double value = 2;
50+
}
51+
52+
message Summary {
53+
optional uint64 sample_count = 1;
54+
optional double sample_sum = 2;
55+
repeated Quantile quantile = 3;
56+
optional google.protobuf.Timestamp created_timestamp = 4;
57+
}
58+
59+
message Untyped {
60+
optional double value = 1;
61+
}
62+
63+
message Histogram {
64+
optional uint64 sample_count = 1;
65+
optional double sample_count_float = 4;
66+
optional double sample_sum = 2;
67+
repeated Bucket bucket = 3;
68+
optional google.protobuf.Timestamp created_timestamp = 15;
69+
70+
optional sint32 schema = 5;
71+
optional double zero_threshold = 6;
72+
optional uint64 zero_count = 7;
73+
optional double zero_count_float = 8;
74+
75+
repeated BucketSpan negative_span = 9;
76+
repeated sint64 negative_delta = 10;
77+
repeated double negative_count = 11;
78+
79+
repeated BucketSpan positive_span = 12;
80+
repeated sint64 positive_delta = 13;
81+
repeated double positive_count = 14;
82+
83+
repeated Exemplar exemplars = 16;
84+
}
85+
86+
message Bucket {
87+
optional uint64 cumulative_count = 1;
88+
optional double cumulative_count_float = 4;
89+
optional double upper_bound = 2;
90+
optional Exemplar exemplar = 3;
91+
}
92+
93+
message BucketSpan {
94+
optional sint32 offset = 1;
95+
optional uint32 length = 2;
96+
}
97+
98+
message Exemplar {
99+
repeated LabelPair label = 1;
100+
optional double value = 2;
101+
optional google.protobuf.Timestamp timestamp = 3;
102+
}
103+
104+
message Metric {
105+
repeated LabelPair label = 1;
106+
optional Gauge gauge = 2;
107+
optional Counter counter = 3;
108+
optional Summary summary = 4;
109+
optional Untyped untyped = 5;
110+
optional int64 timestamp_ms = 6;
111+
optional Histogram histogram = 7;
112+
}
113+
114+
message MetricFamily {
115+
optional string name = 1;
116+
optional string help = 2;
117+
optional MetricType type = 3;
118+
repeated Metric metric = 4;
119+
optional string unit = 5;
120+
}

0 commit comments

Comments
 (0)