Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions expfmt/openmetrics_2_0_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ func writeOpenMetrics20Sample(w enhancedWriter, name string, metric *dto.Metric,
}
}

if exemplar != nil && exemplar.Timestamp != nil {
if exemplar != nil {
n, err = writeExemplar20(w, exemplar)
written += n
if err != nil {
Expand All @@ -280,13 +280,14 @@ func writeOpenMetrics20Sample(w enhancedWriter, name string, metric *dto.Metric,
}

// writeExemplar20 writes the provided exemplar in OpenMetrics 2.0 format to w.
// In OpenMetrics 2.0, exemplars without a timestamp are dropped.
// In OpenMetrics 2.0, invalid exemplars or exemplars without a timestamp are dropped.
func writeExemplar20(w enhancedWriter, e *dto.Exemplar) (int, error) {
if e == nil || e.Timestamp == nil {
if e == nil {
return 0, nil
}
// In OpenMetrics 2.0, invalid exemplars are dropped rather than failing the entire exposition.
if err := validateExemplar20(e); err != nil {
return 0, err
return 0, nil
Comment thread
bwplotka marked this conversation as resolved.
}
written := 0
n, err := w.WriteString(" # ")
Expand Down Expand Up @@ -318,10 +319,6 @@ func writeExemplar20(w enhancedWriter, e *dto.Exemplar) (int, error) {
if err != nil {
return written, err
}
err = e.Timestamp.CheckValid()
if err != nil {
return written, err
}
ts := e.Timestamp
n, err = writeProtoTimestamp(w, ts)
written += n
Expand Down Expand Up @@ -387,6 +384,9 @@ func containsRawNewline(s string) bool {
}

func validateExemplar20(e *dto.Exemplar) error {
if e.Timestamp == nil {
return errors.New("exemplar timestamp is required")
}
if err := e.Timestamp.CheckValid(); err != nil {
return err
}
Expand Down
120 changes: 96 additions & 24 deletions expfmt/openmetrics_2_0_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,102 @@ http_requests_total 1027.0 # {} 1.0 1234567890.5
},
out: `# TYPE http_requests_total counter
http_requests_total 1027.0
`,
},
{
name: "CounterWithInvalidExemplarTimestamp",
in: &dto.MetricFamily{
Name: proto.String("http_requests_total"),
Type: dto.MetricType_COUNTER.Enum(),
Metric: []*dto.Metric{
{
Counter: &dto.Counter{
Value: proto.Float64(1027),
Exemplar: &dto.Exemplar{
Label: []*dto.LabelPair{
{Name: proto.String("trace_id"), Value: proto.String("1234")},
},
Value: proto.Float64(1),
Timestamp: &timestamppb.Timestamp{
Nanos: -1,
},
},
},
},
},
},
out: `# TYPE http_requests_total counter
http_requests_total 1027.0
`,
},
{
name: "CounterWithInvalidExemplarLabel",
in: &dto.MetricFamily{
Name: proto.String("http_requests_total"),
Type: dto.MetricType_COUNTER.Enum(),
Metric: []*dto.Metric{
{
Counter: &dto.Counter{
Value: proto.Float64(1027),
Exemplar: &dto.Exemplar{
Label: []*dto.LabelPair{
{Name: proto.String(""), Value: proto.String("1234")},
},
Value: proto.Float64(1),
Timestamp: &timestamppb.Timestamp{Seconds: 1234567890},
},
},
},
},
},
out: `# TYPE http_requests_total counter
http_requests_total 1027.0
`,
},
{
name: "CounterWithNewlineInExemplarLabelName",
in: &dto.MetricFamily{
Name: proto.String("http_requests_total"),
Type: dto.MetricType_COUNTER.Enum(),
Metric: []*dto.Metric{
{
Counter: &dto.Counter{
Value: proto.Float64(1027),
Exemplar: &dto.Exemplar{
Label: []*dto.LabelPair{
{Name: proto.String("trace\nid"), Value: proto.String("1234")},
},
Value: proto.Float64(1),
Timestamp: &timestamppb.Timestamp{Seconds: 1234567890},
},
},
},
},
},
out: `# TYPE http_requests_total counter
http_requests_total 1027.0
`,
},
{
name: "CounterWithNilExemplarLabelPair",
in: &dto.MetricFamily{
Name: proto.String("http_requests_total"),
Type: dto.MetricType_COUNTER.Enum(),
Metric: []*dto.Metric{
{
Counter: &dto.Counter{
Value: proto.Float64(1027),
Exemplar: &dto.Exemplar{
Label: []*dto.LabelPair{nil},
Value: proto.Float64(1),
Timestamp: &timestamppb.Timestamp{Seconds: 1234567890},
},
},
},
},
},
out: `# TYPE http_requests_total counter
http_requests_total 1027.0
`,
},
{
Expand Down Expand Up @@ -556,30 +652,6 @@ func TestCreateOpenMetrics20_Errors(t *testing.T) {
},
expectedErr: "invalid created timestamp in metric test_counter_total",
},
{
name: "ExemplarInvalidTimestamp",
in: &dto.MetricFamily{
Name: proto.String("test_counter_total"),
Type: dto.MetricType_COUNTER.Enum(),
Metric: []*dto.Metric{
{
Counter: &dto.Counter{
Value: proto.Float64(1.0),
Exemplar: &dto.Exemplar{
Label: []*dto.LabelPair{
{Name: proto.String("trace_id"), Value: proto.String("1234")},
},
Value: proto.Float64(1.0),
Timestamp: &timestamppb.Timestamp{
Nanos: -1,
},
},
},
},
},
},
expectedErr: "has out-of-range nanos",
},
}

for _, tc := range tests {
Expand Down
Loading