| 1 |
// Copyright 2021 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 |
//go:build go1.17 |
| 15 |
// +build go1.17 |
| 16 |
|
| 17 |
package internal |
| 18 |
|
| 19 |
import ( |
| 20 |
"runtime/metrics" |
| 21 |
"testing" |
| 22 |
) |
| 23 |
|
| 24 |
func TestRuntimeMetricsToProm(t *testing.T) { |
| 25 |
tests := []struct { |
| 26 |
got metrics.Description |
| 27 |
expect string |
| 28 |
}{ |
| 29 |
{ |
| 30 |
metrics.Description{ |
| 31 |
Name: "/memory/live:bytes", |
| 32 |
Kind: metrics.KindUint64, |
| 33 |
}, |
| 34 |
"go_memory_live_bytes", |
| 35 |
}, |
| 36 |
{ |
| 37 |
metrics.Description{ |
| 38 |
Name: "/memory/allocs:bytes", |
| 39 |
Kind: metrics.KindUint64, |
| 40 |
Cumulative: true, |
| 41 |
}, |
| 42 |
"go_memory_allocs_bytes_total", |
| 43 |
}, |
| 44 |
{ |
| 45 |
metrics.Description{ |
| 46 |
Name: "/memory/alloc-rate:bytes/second", |
| 47 |
Kind: metrics.KindFloat64, |
| 48 |
}, |
| 49 |
"go_memory_alloc_rate_bytes_per_second", |
| 50 |
}, |
| 51 |
{ |
| 52 |
metrics.Description{ |
| 53 |
Name: "/gc/time:cpu*seconds", |
| 54 |
Kind: metrics.KindFloat64, |
| 55 |
Cumulative: true, |
| 56 |
}, |
| 57 |
"go_gc_time_cpu_seconds_total", |
| 58 |
}, |
| 59 |
{ |
| 60 |
metrics.Description{ |
| 61 |
Name: "/this/is/a/very/deep/metric:metrics", |
| 62 |
Kind: metrics.KindFloat64, |
| 63 |
}, |
| 64 |
"go_this_is_a_very_deep_metric_metrics", |
| 65 |
}, |
| 66 |
{ |
| 67 |
metrics.Description{ |
| 68 |
Name: "/this*is*an*invalid...:µname", |
| 69 |
Kind: metrics.KindUint64, |
| 70 |
}, |
| 71 |
"", |
| 72 |
}, |
| 73 |
{ |
| 74 |
metrics.Description{ |
| 75 |
Name: "/this/is/a/valid/name:objects", |
| 76 |
Kind: metrics.KindBad, |
| 77 |
}, |
| 78 |
"", |
| 79 |
}, |
| 80 |
} |
| 81 |
for _, test := range tests { |
| 82 |
ns, ss, n, ok := RuntimeMetricsToProm(&test.got) |
| 83 |
name := ns + "_" + ss + "_" + n |
| 84 |
if test.expect == "" && ok { |
| 85 |
t.Errorf("bad input expected a bad output: input %s, got %s", test.got.Name, name) |
| 86 |
continue |
| 87 |
} |
| 88 |
if test.expect != "" && !ok { |
| 89 |
t.Errorf("unexpected bad output on good input: input %s", test.got.Name) |
| 90 |
continue |
| 91 |
} |
| 92 |
if test.expect != "" && name != test.expect { |
| 93 |
t.Errorf("expected %s, got %s", test.expect, name) |
| 94 |
continue |
| 95 |
} |
| 96 |
} |
| 97 |
} |