|
| 1 | +// Package main implements mtypes CLI, see README for details. |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "errors" |
| 6 | + "flag" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "net/url" |
| 12 | + "os" |
| 13 | + "strings" |
| 14 | + "text/tabwriter" |
| 15 | + |
| 16 | + dto "github.com/prometheus/client_model/go" |
| 17 | + "github.com/prometheus/common/expfmt" |
| 18 | +) |
| 19 | + |
| 20 | +type stats struct { |
| 21 | + families, series, buckets, objectives int |
| 22 | + |
| 23 | + // adjustedSeries represents series that would result in "series" in Prometheus data model |
| 24 | + // (includes _bucket, _count, _sum, _quantile). |
| 25 | + adjustedSeries int |
| 26 | +} |
| 27 | + |
| 28 | +var metricType_NATIVE_HISTOGRAM dto.MetricType = 999 |
| 29 | + |
| 30 | +func main() { |
| 31 | + resource := flag.String("resource", "", "Path or URL to the resource (file, <url>/metrics) containing Prometheus metric format.") |
| 32 | + avalancheFlagsForTotal := flag.Int("avalanche-flags-for-adjusted-series", 0, "If more than zero, it additionally prints flags for the avalanche 0.6.0 command line to generate metrics for the similar type distribution; to get the total number of adjusted series to the given value.") |
| 33 | + flag.Parse() |
| 34 | + |
| 35 | + var input io.Reader = os.Stdin |
| 36 | + if *resource != "" { |
| 37 | + switch { |
| 38 | + case strings.HasPrefix(*resource, "https://"), strings.HasPrefix(*resource, "http://"): |
| 39 | + if _, err := url.Parse(*resource); err != nil { |
| 40 | + log.Fatalf("error parsing HTTP URL to the resource %v; got %v", *resource, err) |
| 41 | + } |
| 42 | + resp, err := http.Get(*resource) |
| 43 | + if err != nil { |
| 44 | + log.Fatalf("http get against %v failed", err) |
| 45 | + } |
| 46 | + defer resp.Body.Close() |
| 47 | + input = resp.Body |
| 48 | + default: |
| 49 | + // Open the input file. |
| 50 | + file, err := os.Open(*resource) |
| 51 | + if err != nil { |
| 52 | + log.Fatalf("Error opening file: %v", err) //nolint:gocritic |
| 53 | + } |
| 54 | + defer file.Close() |
| 55 | + input = file |
| 56 | + } |
| 57 | + } |
| 58 | + statistics, err := calculateTargetStatistics(input) |
| 59 | + if err != nil { |
| 60 | + log.Fatal(err) |
| 61 | + } |
| 62 | + var total stats |
| 63 | + for _, s := range statistics { |
| 64 | + total.families += s.families |
| 65 | + total.series += s.series |
| 66 | + total.adjustedSeries += s.adjustedSeries |
| 67 | + } |
| 68 | + |
| 69 | + writeStatistics(os.Stdout, total, statistics) |
| 70 | + |
| 71 | + if *avalancheFlagsForTotal > 0 { |
| 72 | + // adjustedGoal is tracking the # of adjusted series we want to generate with avalanche. |
| 73 | + adjustedGoal := float64(*avalancheFlagsForTotal) |
| 74 | + fmt.Println() |
| 75 | + fmt.Println("Avalanche flags for the similar distribution to get to the adjusted series goal of:", adjustedGoal) |
| 76 | + |
| 77 | + adjustedGoal /= 10.0 // Assuming --series-count=10 |
| 78 | + // adjustedSum is tracking the total sum of series so far (at the end hopefully adjustedSum ~= adjustedGoal) |
| 79 | + adjustedSum := 0 |
| 80 | + for _, mtype := range allTypes { |
| 81 | + s := statistics[mtype] |
| 82 | + |
| 83 | + // adjustedSeriesRatio is tracking the ratio of this type in the input file. |
| 84 | + // We try to get similar ratio, but with different absolute counts, given the total sum of series we are aiming for. |
| 85 | + adjustedSeriesRatio := float64(s.adjustedSeries) / float64(total.adjustedSeries) |
| 86 | + |
| 87 | + // adjustedSeriesForType is tracking (per metric type), how many unique series of that |
| 88 | + // metric type avalanche needs to create according to the ratio we got from our input. |
| 89 | + adjustedSeriesForType := int(adjustedGoal * adjustedSeriesRatio) |
| 90 | + |
| 91 | + switch mtype { |
| 92 | + case dto.MetricType_GAUGE: |
| 93 | + fmt.Printf("--gauge-metric-count=%v\n", adjustedSeriesForType) |
| 94 | + adjustedSum += adjustedSeriesForType |
| 95 | + case dto.MetricType_COUNTER: |
| 96 | + fmt.Printf("--counter-metric-count=%v\n", adjustedSeriesForType) |
| 97 | + adjustedSum += adjustedSeriesForType |
| 98 | + case dto.MetricType_HISTOGRAM: |
| 99 | + avgBkts := s.buckets / s.series |
| 100 | + adjustedSeriesForType /= 2 + avgBkts |
| 101 | + fmt.Printf("--histogram-metric-count=%v\n", adjustedSeriesForType) |
| 102 | + fmt.Printf("--histogram-metric-bucket-count=%v\n", avgBkts-1) // -1 is due to caveat of additional +Inf not added by avalanche. |
| 103 | + adjustedSum += adjustedSeriesForType * (2 + avgBkts) |
| 104 | + case metricType_NATIVE_HISTOGRAM: |
| 105 | + fmt.Printf("--native-histogram-metric-count=%v\n", adjustedSeriesForType) |
| 106 | + adjustedSum += adjustedSeriesForType |
| 107 | + case dto.MetricType_SUMMARY: |
| 108 | + avgObjs := s.objectives / s.series |
| 109 | + adjustedSeriesForType /= 2 + avgObjs |
| 110 | + fmt.Printf("--summary-metric-count=%v\n", adjustedSeriesForType) |
| 111 | + fmt.Printf("--summary-metric-objective-count=%v\n", avgObjs) |
| 112 | + adjustedSum += adjustedSeriesForType * (2 + avgObjs) |
| 113 | + default: |
| 114 | + if s.series > 0 { |
| 115 | + log.Fatalf("not supported %v metric in avalanche", mtype) |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + fmt.Printf("--series-count=10\n") |
| 120 | + fmt.Printf("--value-interval=300 # Changes values every 5m.\n") |
| 121 | + fmt.Printf("--series-interval=3600 # 1h series churn.\n") |
| 122 | + fmt.Printf("--metric-interval=0\n") |
| 123 | + |
| 124 | + fmt.Println("This should give the total adjusted series to:", adjustedSum*10) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +var allTypes = []dto.MetricType{dto.MetricType_GAUGE, dto.MetricType_COUNTER, dto.MetricType_HISTOGRAM, metricType_NATIVE_HISTOGRAM, dto.MetricType_GAUGE_HISTOGRAM, dto.MetricType_SUMMARY, dto.MetricType_UNTYPED} |
| 129 | + |
| 130 | +func writeStatistics(writer io.Writer, total stats, statistics map[dto.MetricType]stats) { |
| 131 | + w := tabwriter.NewWriter(writer, 0, 0, 4, ' ', 0) |
| 132 | + fmt.Fprintln(w, "Metric Type\tMetric Families\tSeries (adjusted)\tSeries (adjusted) %\tAverage Buckets/Objectives") |
| 133 | + |
| 134 | + for _, mtype := range allTypes { |
| 135 | + s, ok := statistics[mtype] |
| 136 | + if !ok { |
| 137 | + continue |
| 138 | + } |
| 139 | + |
| 140 | + mtypeStr := mtype.String() |
| 141 | + if mtype == metricType_NATIVE_HISTOGRAM { |
| 142 | + mtypeStr = "HISTOGRAM (native)" |
| 143 | + } |
| 144 | + |
| 145 | + seriesRatio := 100 * float64(s.series) / float64(total.series) |
| 146 | + adjustedSeriesRatio := 100 * float64(s.adjustedSeries) / float64(total.adjustedSeries) |
| 147 | + switch { |
| 148 | + case s.buckets > 0: |
| 149 | + fmt.Fprintf(w, "%s\t%d\t%d (%d)\t%f (%f)\t%f\n", mtypeStr, s.families, s.series, s.adjustedSeries, seriesRatio, adjustedSeriesRatio, float64(s.buckets)/float64(s.series)) |
| 150 | + case s.objectives > 0: |
| 151 | + fmt.Fprintf(w, "%s\t%d\t%d (%d)\t%f (%f)\t%f\n", mtypeStr, s.families, s.series, s.adjustedSeries, seriesRatio, adjustedSeriesRatio, float64(s.objectives)/float64(s.series)) |
| 152 | + default: |
| 153 | + fmt.Fprintf(w, "%s\t%d\t%d (%d)\t%f (%f)\t-\n", mtypeStr, s.families, s.series, s.adjustedSeries, seriesRatio, adjustedSeriesRatio) |
| 154 | + } |
| 155 | + } |
| 156 | + fmt.Fprintf(w, "---\t---\t---\t---\t---\n") |
| 157 | + fmt.Fprintf(w, "*\t%d\t%d (%d)\t%f (%f)\t-\n", total.families, total.series, total.adjustedSeries, 100.0, 100.0) |
| 158 | + _ = w.Flush() |
| 159 | +} |
| 160 | + |
| 161 | +func calculateTargetStatistics(r io.Reader) (statistics map[dto.MetricType]stats, _ error) { |
| 162 | + // Parse the Prometheus Text format. |
| 163 | + parser := expfmt.NewDecoder(r, expfmt.NewFormat(expfmt.TypeProtoText)) |
| 164 | + |
| 165 | + statistics = map[dto.MetricType]stats{} |
| 166 | + nativeS := statistics[metricType_NATIVE_HISTOGRAM] |
| 167 | + for { |
| 168 | + var mf dto.MetricFamily |
| 169 | + if err := parser.Decode(&mf); err != nil { |
| 170 | + if errors.Is(err, io.EOF) { |
| 171 | + break |
| 172 | + } |
| 173 | + return nil, fmt.Errorf("parsing %w", err) |
| 174 | + } |
| 175 | + |
| 176 | + s := statistics[mf.GetType()] |
| 177 | + |
| 178 | + var mfAccounted, mfAccountedNative bool |
| 179 | + switch mf.GetType() { |
| 180 | + case dto.MetricType_GAUGE_HISTOGRAM, dto.MetricType_HISTOGRAM: |
| 181 | + for _, m := range mf.GetMetric() { |
| 182 | + if m.GetHistogram().GetSchema() == 0 { |
| 183 | + // classic one. |
| 184 | + s.series++ |
| 185 | + s.buckets += len(m.GetHistogram().GetBucket()) |
| 186 | + s.adjustedSeries += 2 + len(m.GetHistogram().GetBucket()) |
| 187 | + |
| 188 | + if !mfAccounted { |
| 189 | + s.families++ |
| 190 | + mfAccounted = true |
| 191 | + } |
| 192 | + } else { |
| 193 | + // native one. |
| 194 | + nativeS.series++ |
| 195 | + nativeS.buckets += len(m.GetHistogram().GetNegativeDelta()) |
| 196 | + nativeS.buckets += len(m.GetHistogram().GetNegativeCount()) |
| 197 | + nativeS.buckets += len(m.GetHistogram().GetPositiveDelta()) |
| 198 | + nativeS.buckets += len(m.GetHistogram().GetPositiveCount()) |
| 199 | + nativeS.adjustedSeries++ |
| 200 | + |
| 201 | + if !mfAccountedNative { |
| 202 | + nativeS.families++ |
| 203 | + mfAccountedNative = true |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + case dto.MetricType_SUMMARY: |
| 208 | + s.series += len(mf.GetMetric()) |
| 209 | + s.families++ |
| 210 | + for _, m := range mf.GetMetric() { |
| 211 | + s.objectives += len(m.GetSummary().GetQuantile()) |
| 212 | + s.adjustedSeries += 2 + len(m.GetSummary().GetQuantile()) |
| 213 | + } |
| 214 | + default: |
| 215 | + s.series += len(mf.GetMetric()) |
| 216 | + s.families++ |
| 217 | + s.adjustedSeries += len(mf.GetMetric()) |
| 218 | + } |
| 219 | + statistics[mf.GetType()] = s |
| 220 | + } |
| 221 | + if nativeS.series > 0 { |
| 222 | + statistics[metricType_NATIVE_HISTOGRAM] = nativeS |
| 223 | + } |
| 224 | + return statistics, nil |
| 225 | +} |
0 commit comments