| 1 |
// Copyright 2018 The Prometheus Authors |
| 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 |
package prometheus |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
"time" |
| 19 |
|
| 20 |
dto "github.com/prometheus/client_model/go" |
| 21 |
"google.golang.org/protobuf/types/known/timestamppb" |
| 22 |
) |
| 23 |
|
| 24 |
func TestNewConstMetricInvalidLabelValues(t *testing.T) { |
| 25 |
testCases := []struct { |
| 26 |
desc string |
| 27 |
labels Labels |
| 28 |
}{ |
| 29 |
{ |
| 30 |
desc: "non utf8 label value", |
| 31 |
labels: Labels{"a": "\xFF"}, |
| 32 |
}, |
| 33 |
{ |
| 34 |
desc: "not enough label values", |
| 35 |
labels: Labels{}, |
| 36 |
}, |
| 37 |
{ |
| 38 |
desc: "too many label values", |
| 39 |
labels: Labels{"a": "1", "b": "2"}, |
| 40 |
}, |
| 41 |
} |
| 42 |
|
| 43 |
for _, test := range testCases { |
| 44 |
metricDesc := NewDesc( |
| 45 |
"sample_value", |
| 46 |
"sample value", |
| 47 |
[]string{"a"}, |
| 48 |
Labels{}, |
| 49 |
) |
| 50 |
|
| 51 |
expectPanic(t, func() { |
| 52 |
MustNewConstMetric(metricDesc, CounterValue, 0.3, "\xFF") |
| 53 |
}, "WithLabelValues: expected panic because: "+test.desc) |
| 54 |
|
| 55 |
if _, err := NewConstMetric(metricDesc, CounterValue, 0.3, "\xFF"); err == nil { |
| 56 |
t.Errorf("NewConstMetric: expected error because: %s", test.desc) |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestNewConstMetricWithCreatedTimestamp(t *testing.T) { |
| 62 |
now := time.Now() |
| 63 |
|
| 64 |
for _, tcase := range []struct { |
| 65 |
desc string |
| 66 |
metricType ValueType |
| 67 |
createdTimestamp time.Time |
| 68 |
expecErr bool |
| 69 |
expectedCt *timestamppb.Timestamp |
| 70 |
}{ |
| 71 |
{ |
| 72 |
desc: "gauge with CT", |
| 73 |
metricType: GaugeValue, |
| 74 |
createdTimestamp: now, |
| 75 |
expecErr: true, |
| 76 |
expectedCt: nil, |
| 77 |
}, |
| 78 |
{ |
| 79 |
desc: "counter with CT", |
| 80 |
metricType: CounterValue, |
| 81 |
createdTimestamp: now, |
| 82 |
expecErr: false, |
| 83 |
expectedCt: timestamppb.New(now), |
| 84 |
}, |
| 85 |
} { |
| 86 |
t.Run(tcase.desc, func(t *testing.T) { |
| 87 |
metricDesc := NewDesc( |
| 88 |
"sample_value", |
| 89 |
"sample value", |
| 90 |
nil, |
| 91 |
nil, |
| 92 |
) |
| 93 |
m, err := NewConstMetricWithCreatedTimestamp(metricDesc, tcase.metricType, float64(1), tcase.createdTimestamp) |
| 94 |
if tcase.expecErr && err == nil { |
| 95 |
t.Errorf("Expected error is test %s, got no err", tcase.desc) |
| 96 |
} |
| 97 |
if !tcase.expecErr && err != nil { |
| 98 |
t.Errorf("Didn't expect error in test %s, got %s", tcase.desc, err.Error()) |
| 99 |
} |
| 100 |
|
| 101 |
if tcase.expectedCt != nil { |
| 102 |
var metric dto.Metric |
| 103 |
m.Write(&metric) |
| 104 |
if metric.Counter.CreatedTimestamp.AsTime() != tcase.expectedCt.AsTime() { |
| 105 |
t.Errorf("Expected timestamp %v, got %v", tcase.expectedCt, &metric.Counter.CreatedTimestamp) |
| 106 |
} |
| 107 |
} |
| 108 |
}) |
| 109 |
} |
| 110 |
} |