| 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 |
"math" |
| 21 |
"path" |
| 22 |
"runtime/metrics" |
| 23 |
"strings" |
| 24 |
|
| 25 |
"github.com/prometheus/common/model" |
| 26 |
) |
| 27 |
|
| 28 |
// RuntimeMetricsToProm produces a Prometheus metric name from a runtime/metrics |
| 29 |
// metric description and validates whether the metric is suitable for integration |
| 30 |
// with Prometheus. |
| 31 |
// |
| 32 |
// Returns false if a name could not be produced, or if Prometheus does not understand |
| 33 |
// the runtime/metrics Kind. |
| 34 |
// |
| 35 |
// Note that the main reason a name couldn't be produced is if the runtime/metrics |
| 36 |
// package exports a name with characters outside the valid Prometheus metric name |
| 37 |
// character set. This is theoretically possible, but should never happen in practice. |
| 38 |
// Still, don't rely on it. |
| 39 |
func RuntimeMetricsToProm(d *metrics.Description) (string, string, string, bool) { |
| 40 |
namespace := "go" |
| 41 |
|
| 42 |
comp := strings.SplitN(d.Name, ":", 2) |
| 43 |
key := comp[0] |
| 44 |
unit := comp[1] |
| 45 |
|
| 46 |
// The last path element in the key is the name, |
| 47 |
// the rest is the subsystem. |
| 48 |
subsystem := path.Dir(key[1:] /* remove leading / */) |
| 49 |
name := path.Base(key) |
| 50 |
|
| 51 |
// subsystem is translated by replacing all / and - with _. |
| 52 |
subsystem = strings.ReplaceAll(subsystem, "/", "_") |
| 53 |
subsystem = strings.ReplaceAll(subsystem, "-", "_") |
| 54 |
|
| 55 |
// unit is translated assuming that the unit contains no |
| 56 |
// non-ASCII characters. |
| 57 |
unit = strings.ReplaceAll(unit, "-", "_") |
| 58 |
unit = strings.ReplaceAll(unit, "*", "_") |
| 59 |
unit = strings.ReplaceAll(unit, "/", "_per_") |
| 60 |
|
| 61 |
// name has - replaced with _ and is concatenated with the unit and |
| 62 |
// other data. |
| 63 |
name = strings.ReplaceAll(name, "-", "_") |
| 64 |
name += "_" + unit |
| 65 |
if d.Cumulative && d.Kind != metrics.KindFloat64Histogram { |
| 66 |
name += "_total" |
| 67 |
} |
| 68 |
|
| 69 |
// Our current conversion moves to legacy naming, so use legacy validation. |
| 70 |
valid := model.LegacyValidation.IsValidMetricName(namespace + "_" + subsystem + "_" + name) |
| 71 |
switch d.Kind { |
| 72 |
case metrics.KindUint64: |
| 73 |
case metrics.KindFloat64: |
| 74 |
case metrics.KindFloat64Histogram: |
| 75 |
default: |
| 76 |
valid = false |
| 77 |
} |
| 78 |
return namespace, subsystem, name, valid |
| 79 |
} |
| 80 |
|
| 81 |
// RuntimeMetricsBucketsForUnit takes a set of buckets obtained for a runtime/metrics histogram |
| 82 |
// type (so, lower-bound inclusive) and a unit from a runtime/metrics name, and produces |
| 83 |
// a reduced set of buckets. This function always removes any -Inf bucket as it's represented |
| 84 |
// as the bottom-most upper-bound inclusive bucket in Prometheus. |
| 85 |
func RuntimeMetricsBucketsForUnit(buckets []float64, unit string) []float64 { |
| 86 |
switch unit { |
| 87 |
case "bytes": |
| 88 |
// Re-bucket as powers of 2. |
| 89 |
return reBucketExp(buckets, 2) |
| 90 |
case "seconds": |
| 91 |
// Re-bucket as powers of 10 and then merge all buckets greater |
| 92 |
// than 1 second into the +Inf bucket. |
| 93 |
b := reBucketExp(buckets, 10) |
| 94 |
for i := range b { |
| 95 |
if b[i] <= 1 { |
| 96 |
continue |
| 97 |
} |
| 98 |
b[i] = math.Inf(1) |
| 99 |
b = b[:i+1] |
| 100 |
break |
| 101 |
} |
| 102 |
return b |
| 103 |
} |
| 104 |
return buckets |
| 105 |
} |
| 106 |
|
| 107 |
// reBucketExp takes a list of bucket boundaries (lower bound inclusive) and |
| 108 |
// downsamples the buckets to those a multiple of base apart. The end result |
| 109 |
// is a roughly exponential (in many cases, perfectly exponential) bucketing |
| 110 |
// scheme. |
| 111 |
func reBucketExp(buckets []float64, base float64) []float64 { |
| 112 |
bucket := buckets[0] |
| 113 |
var newBuckets []float64 |
| 114 |
// We may see a -Inf here, in which case, add it and skip it |
| 115 |
// since we risk producing NaNs otherwise. |
| 116 |
// |
| 117 |
// We need to preserve -Inf values to maintain runtime/metrics |
| 118 |
// conventions. We'll strip it out later. |
| 119 |
if bucket == math.Inf(-1) { |
| 120 |
newBuckets = append(newBuckets, bucket) |
| 121 |
buckets = buckets[1:] |
| 122 |
bucket = buckets[0] |
| 123 |
} |
| 124 |
// From now on, bucket should always have a non-Inf value because |
| 125 |
// Infs are only ever at the ends of the bucket lists, so |
| 126 |
// arithmetic operations on it are non-NaN. |
| 127 |
for i := 1; i < len(buckets); i++ { |
| 128 |
if bucket >= 0 && buckets[i] < bucket*base { |
| 129 |
// The next bucket we want to include is at least bucket*base. |
| 130 |
continue |
| 131 |
} else if bucket < 0 && buckets[i] < bucket/base { |
| 132 |
// In this case the bucket we're targeting is negative, and since |
| 133 |
// we're ascending through buckets here, we need to divide to get |
| 134 |
// closer to zero exponentially. |
| 135 |
continue |
| 136 |
} |
| 137 |
// The +Inf bucket will always be the last one, and we'll always |
| 138 |
// end up including it here because bucket |
| 139 |
newBuckets = append(newBuckets, bucket) |
| 140 |
bucket = buckets[i] |
| 141 |
} |
| 142 |
return append(newBuckets, bucket) |
| 143 |
} |