|
| 1 | +// Copyright 2023 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// package upload provides methods for loading and querying a |
| 6 | +// telemetry upload config file. |
| 7 | +package upload |
| 8 | + |
| 9 | +import ( |
| 10 | + "encoding/json" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "golang.org/x/telemetry" |
| 15 | +) |
| 16 | + |
| 17 | +// Config is a wrapper around telemetry.UploadConfig that provides some |
| 18 | +// convenience methods for checking the contents of a report. |
| 19 | +type Config struct { |
| 20 | + *telemetry.UploadConfig |
| 21 | + program map[string]bool |
| 22 | + goos map[string]bool |
| 23 | + goarch map[string]bool |
| 24 | + goversion map[string]bool |
| 25 | + pgversion map[pgkey]bool |
| 26 | + pgcounter map[pgkey]bool |
| 27 | + pgstack map[pgkey]bool |
| 28 | +} |
| 29 | + |
| 30 | +type pgkey struct { |
| 31 | + program, key string |
| 32 | +} |
| 33 | + |
| 34 | +func ReadConfig(file string) (*Config, error) { |
| 35 | + data, err := os.ReadFile(file) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + var cfg telemetry.UploadConfig |
| 40 | + if err := json.Unmarshal(data, &cfg); err != nil { |
| 41 | + return nil, err |
| 42 | + } |
| 43 | + return NewConfig(&cfg), nil |
| 44 | +} |
| 45 | + |
| 46 | +func NewConfig(cfg *telemetry.UploadConfig) *Config { |
| 47 | + ucfg := Config{UploadConfig: cfg} |
| 48 | + ucfg.goos = set(ucfg.GOOS) |
| 49 | + ucfg.goarch = set(ucfg.GOARCH) |
| 50 | + ucfg.goversion = set(ucfg.GoVersion) |
| 51 | + ucfg.program = make(map[string]bool, len(ucfg.Programs)) |
| 52 | + ucfg.pgversion = make(map[pgkey]bool, len(ucfg.Programs)) |
| 53 | + ucfg.pgcounter = make(map[pgkey]bool, len(ucfg.Programs)) |
| 54 | + ucfg.pgstack = make(map[pgkey]bool, len(ucfg.Programs)) |
| 55 | + for _, p := range ucfg.Programs { |
| 56 | + ucfg.program[p.Name] = true |
| 57 | + for _, v := range p.Versions { |
| 58 | + ucfg.pgversion[pgkey{p.Name, v}] = true |
| 59 | + } |
| 60 | + for _, c := range p.Counters { |
| 61 | + for _, e := range expand(c.Name) { |
| 62 | + ucfg.pgcounter[pgkey{p.Name, e}] = true |
| 63 | + } |
| 64 | + } |
| 65 | + for _, s := range p.Stacks { |
| 66 | + ucfg.pgstack[pgkey{p.Name, s.Name}] = true |
| 67 | + } |
| 68 | + } |
| 69 | + return &ucfg |
| 70 | +} |
| 71 | + |
| 72 | +func (r *Config) HasProgram(s string) bool { |
| 73 | + return r.program[s] |
| 74 | +} |
| 75 | + |
| 76 | +func (r *Config) HasGOOS(s string) bool { |
| 77 | + return r.goos[s] |
| 78 | +} |
| 79 | + |
| 80 | +func (r *Config) HasGOARCH(s string) bool { |
| 81 | + return r.goarch[s] |
| 82 | +} |
| 83 | + |
| 84 | +func (r *Config) HasGoVersion(s string) bool { |
| 85 | + return r.goversion[s] |
| 86 | +} |
| 87 | + |
| 88 | +func (r *Config) HasVersion(program, version string) bool { |
| 89 | + return r.pgversion[pgkey{program, version}] |
| 90 | +} |
| 91 | + |
| 92 | +func (r *Config) HasCounter(program, counter string) bool { |
| 93 | + return r.pgcounter[pgkey{program, counter}] |
| 94 | +} |
| 95 | + |
| 96 | +func (r *Config) HasStack(program, stack string) bool { |
| 97 | + return r.pgstack[pgkey{program, stack}] |
| 98 | +} |
| 99 | + |
| 100 | +func set(slice []string) map[string]bool { |
| 101 | + s := make(map[string]bool, len(slice)) |
| 102 | + for _, v := range slice { |
| 103 | + s[v] = true |
| 104 | + } |
| 105 | + return s |
| 106 | +} |
| 107 | + |
| 108 | +// expand takes a counter defined with buckets and expands it into distinct |
| 109 | +// strings for each bucket |
| 110 | +func expand(counter string) []string { |
| 111 | + prefix, rest, hasBuckets := strings.Cut(counter, "{") |
| 112 | + var counters []string |
| 113 | + if hasBuckets { |
| 114 | + buckets := strings.Split(strings.TrimSuffix(rest, "}"), ",") |
| 115 | + for _, b := range buckets { |
| 116 | + counters = append(counters, prefix+b) |
| 117 | + } |
| 118 | + } else { |
| 119 | + counters = append(counters, prefix) |
| 120 | + } |
| 121 | + return counters |
| 122 | +} |
0 commit comments