Skip to content

Commit b762d66

Browse files
authored
Add metrics for alias and command invocations (#3351)
1 parent c422a3b commit b762d66

File tree

28 files changed

+107
-39
lines changed

28 files changed

+107
-39
lines changed

ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Version 17.15.0
2+
---------------
3+
* Add metrics for alias and command invocations (#3351)
4+
15
Version 17.14.0
26
---------------
37
* Add a `--notrim` flag to `plz export` that avoids trying to clean out unused build targerts

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
17.14.0
1+
17.15.0

src/BUILD.plz

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go_binary(
44
name = "please",
55
srcs = ["please.go"],
66
definitions = {
7-
"github.com/thought-machine/please/src/core.PleaseVersion": VERSION,
7+
"github.com/thought-machine/please/src/version.PleaseVersion": VERSION,
88
},
99
visibility = ["PUBLIC"],
1010
deps = [
@@ -26,6 +26,7 @@ go_binary(
2626
"//src/generate",
2727
"//src/hashes",
2828
"//src/help",
29+
"//src/metrics",
2930
"//src/output",
3031
"//src/plz",
3132
"//src/plzinit",
@@ -37,6 +38,7 @@ go_binary(
3738
"//src/test",
3839
"//src/tool",
3940
"//src/update",
41+
"//src/version",
4042
"//src/watch",
4143
],
4244
)

src/build/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ go_library(
1717
"//src/generate",
1818
"//src/metrics",
1919
"//src/process",
20+
"//src/version",
2021
],
2122
)
2223

src/build/build_step.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/thought-machine/please/src/generate"
2929
"github.com/thought-machine/please/src/metrics"
3030
"github.com/thought-machine/please/src/process"
31+
"github.com/thought-machine/please/src/version"
3132
)
3233

3334
var log = logging.Log
@@ -1134,7 +1135,7 @@ func fetchOneRemoteFile(state *core.BuildState, target *core.BuildTarget, url st
11341135
// setHeaders sets up all the headers we should send on remote_file() requests, including User-Agent and any user
11351136
// defined ones.
11361137
func setHeaders(req *http.Request, target *core.BuildTarget, env core.BuildEnv) error {
1137-
req.Header.Set("User-Agent", "please.build/"+core.PleaseVersion)
1138+
req.Header.Set("User-Agent", "please.build/"+version.PleaseVersion)
11381139

11391140
param := func(str string) (string, string) {
11401141
if !strings.HasPrefix(str, "remote_file:") {

src/core/BUILD

+2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ go_library(
2525
"//src/cli/logging",
2626
"//src/cmap",
2727
"//src/fs",
28+
"//src/metrics",
2829
"//src/process",
2930
"//src/scm",
31+
"//src/version",
3032
],
3133
)
3234

src/core/config.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727

2828
"github.com/thought-machine/please/src/cli"
2929
"github.com/thought-machine/please/src/fs"
30+
"github.com/thought-machine/please/src/metrics"
31+
"github.com/thought-machine/please/src/version"
3032
)
3133

3234
// OsArch is the os/arch pair, like linux_amd64 etc.
@@ -108,7 +110,7 @@ func readConfigFile(fs iofs.FS, config *Configuration, filename string, subrepo
108110

109111
func checkPluginVersionRequirements(config *Configuration) {
110112
if config.PluginDefinition.Name != "" {
111-
currentPlzVersion := *semver.New(PleaseVersion)
113+
currentPlzVersion := *semver.New(version.PleaseVersion)
112114
// Get plugin config version requirement which may or may not exist
113115
pluginVerReq := config.Please.Version.Version
114116

@@ -692,11 +694,7 @@ type Configuration struct {
692694

693695
FeatureFlags struct {
694696
} `help:"Flags controlling preview features for the next release. Typically these config options gate breaking changes and only have a lifetime of one major release."`
695-
Metrics struct {
696-
PrometheusGatewayURL string `help:"The gateway URL to push prometheus updates to."`
697-
Timeout cli.Duration `help:"timeout for pushing to the gateway. Defaults to 2 seconds." `
698-
PushHostInfo bool `help:"Whether to push host info"`
699-
} `help:"Settings for collecting metrics."`
697+
Metrics metrics.Config `help:"Settings for collecting metrics."`
700698
}
701699

702700
// An Alias represents aliases in the config.
@@ -1027,6 +1025,13 @@ func (config *Configuration) Completions(prefix string) []flags.Completion {
10271025
return ret
10281026
}
10291027

1028+
var aliasInvocationCount = metrics.NewCounterVec(
1029+
"alias",
1030+
"invocation",
1031+
"How many times each alias is used",
1032+
[]string{"alias"},
1033+
)
1034+
10301035
// UpdateArgsWithAliases applies the aliases in this config to the given set of arguments.
10311036
func (config *Configuration) UpdateArgsWithAliases(args []string) []string {
10321037
for idx, arg := range args[1:] {
@@ -1036,6 +1041,7 @@ func (config *Configuration) UpdateArgsWithAliases(args []string) []string {
10361041
}
10371042
for k, v := range config.Alias {
10381043
if arg == k {
1044+
aliasInvocationCount.WithLabelValues(k).Inc()
10391045
// We could insert every token in v into os.Args at this point and then we could have
10401046
// aliases defined in terms of other aliases but that seems rather like overkill so just
10411047
// stick the replacement in wholesale instead.

src/help/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ go_library(
2020
"//src/fs",
2121
"//src/parse/asp",
2222
"//src/plz",
23+
"//src/version",
2324
],
2425
)
2526

src/help/config.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
"strconv"
88
"strings"
99

10-
"github.com/peterebden/go-deferred-regex"
10+
deferredregex "github.com/peterebden/go-deferred-regex"
1111

1212
"github.com/thought-machine/please/src/core"
13+
"github.com/thought-machine/please/src/version"
1314
)
1415

1516
var urlRegex = deferredregex.DeferredRegex{Re: "https?://[^ ]+[^.]"}
@@ -23,7 +24,7 @@ func ExampleValue(f reflect.Value, name string, t reflect.Type, example, options
2324
} else if options != "" {
2425
return strings.ReplaceAll(options, ",", " | ")
2526
} else if name == "version" {
26-
return core.PleaseVersion // keep it up to date!
27+
return version.PleaseVersion
2728
} else if t.Kind() == reflect.String {
2829
if f.String() != "" {
2930
return f.String()

src/metrics/BUILD

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ go_library(
77
"///third_party/go/github.com_prometheus_client_golang//prometheus",
88
"///third_party/go/github.com_prometheus_client_golang//prometheus/push",
99
"///third_party/go/github.com_prometheus_common//expfmt",
10+
"//src/cli",
1011
"//src/cli/logging",
11-
"//src/core",
12+
"//src/version",
1213
],
1314
)

src/metrics/prometheus.go

+27-8
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ import (
1111
"github.com/prometheus/client_golang/prometheus/push"
1212
"github.com/prometheus/common/expfmt"
1313

14+
"github.com/thought-machine/please/src/cli"
1415
"github.com/thought-machine/please/src/cli/logging"
15-
"github.com/thought-machine/please/src/core"
16+
"github.com/thought-machine/please/src/version"
1617
)
1718

1819
var log = logging.Log
1920

2021
var registerer = prometheus.WrapRegistererWith(prometheus.Labels{
21-
"version": core.PleaseVersion,
22+
"version": version.PleaseVersion,
2223
}, prometheus.DefaultRegisterer)
2324

25+
type Config struct {
26+
PrometheusGatewayURL string `help:"The aggregation gateway URL to push prometheus updates to."`
27+
Timeout cli.Duration `help:"timeout for pushing to the gateway. Defaults to 2 seconds." `
28+
PushHostInfo bool `help:"Whether to push host info"`
29+
}
30+
2431
// Push performs a single push of all registered metrics to the pushgateway (if configured).
25-
func Push(config *core.Configuration) {
32+
func Push(config Config, isRemoteExecution bool) {
2633
if family, err := prometheus.DefaultGatherer.Gather(); err == nil {
2734
var buf strings.Builder
2835
for _, fam := range family {
@@ -37,28 +44,28 @@ func Push(config *core.Configuration) {
3744
}
3845
}
3946

40-
if config.Metrics.PrometheusGatewayURL == "" {
47+
if config.PrometheusGatewayURL == "" {
4148
return
4249
}
4350

44-
if config.Metrics.PushHostInfo {
51+
if config.PushHostInfo {
4552
name, _ := os.Hostname()
4653
counter := prometheus.NewCounter(prometheus.CounterOpts{
4754
Namespace: "plz",
4855
Subsystem: "metrics",
4956
Name: "hostinfo",
5057
Help: "Please host running info",
5158
ConstLabels: prometheus.Labels{
52-
"remote": strconv.FormatBool(config.IsRemoteExecution()),
59+
"remote": strconv.FormatBool(isRemoteExecution),
5360
"hostname": name,
5461
},
5562
})
5663
MustRegister(counter)
5764
counter.Inc()
5865
}
5966

60-
if err := push.New(config.Metrics.PrometheusGatewayURL, "please").
61-
Client(&http.Client{Timeout: time.Duration(config.Metrics.Timeout)}).
67+
if err := push.New(config.PrometheusGatewayURL, "please").
68+
Client(&http.Client{Timeout: time.Duration(config.Timeout)}).
6269
Gatherer(prometheus.DefaultGatherer).Format(expfmt.NewFormat(expfmt.TypeTextPlain)).
6370
Push(); err != nil {
6471
log.Warning("Error pushing Prometheus metrics: %s", err)
@@ -83,6 +90,18 @@ func NewCounter(subsystem, name, help string) prometheus.Counter {
8390
return counter
8491
}
8592

93+
// NewCounter creates & registers a new counter.
94+
func NewCounterVec(subsystem, name, help string, labelNames []string) *prometheus.CounterVec {
95+
counter := prometheus.NewCounterVec(prometheus.CounterOpts{
96+
Namespace: "plz",
97+
Subsystem: subsystem,
98+
Name: name,
99+
Help: help,
100+
}, labelNames)
101+
MustRegister(counter)
102+
return counter
103+
}
104+
86105
// NewHistogram creates & registers a new histogram.
87106
func NewHistogram(subsystem, name, help string, buckets []float64) prometheus.Histogram {
88107
histogram := prometheus.NewHistogram(prometheus.HistogramOpts{

src/parse/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ go_library(
1616
"//src/core",
1717
"//src/fs",
1818
"//src/parse/asp",
19+
"//src/version",
1920
],
2021
)
2122

src/parse/internal_package.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"text/template"
99

1010
"github.com/thought-machine/please/src/core"
11+
"github.com/thought-machine/please/src/version"
1112
)
1213

1314
const InternalPackageName = "_please"
@@ -23,7 +24,7 @@ func GetInternalPackage(config *core.Configuration) (string, error) {
2324

2425
url := config.Please.ToolsURL.String()
2526
if url == "" {
26-
url = fmt.Sprintf("%s/%s_%s/%s/please_tools_%s.tar.xz", config.Please.DownloadLocation, runtime.GOOS, runtime.GOARCH, core.PleaseVersion, core.PleaseVersion)
27+
url = fmt.Sprintf("%s/%s_%s/%s/please_tools_%s.tar.xz", config.Please.DownloadLocation, runtime.GOOS, runtime.GOARCH, version.PleaseVersion, version.PleaseVersion)
2728
}
2829

2930
data := struct {

src/please.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/thought-machine/please/src/generate"
3535
"github.com/thought-machine/please/src/hashes"
3636
"github.com/thought-machine/please/src/help"
37+
"github.com/thought-machine/please/src/metrics"
3738
"github.com/thought-machine/please/src/output"
3839
"github.com/thought-machine/please/src/plz"
3940
"github.com/thought-machine/please/src/plzinit"
@@ -45,6 +46,7 @@ import (
4546
"github.com/thought-machine/please/src/test"
4647
"github.com/thought-machine/please/src/tool"
4748
"github.com/thought-machine/please/src/update"
49+
"github.com/thought-machine/please/src/version"
4850
"github.com/thought-machine/please/src/watch"
4951
)
5052

@@ -684,7 +686,7 @@ var buildFunctions = map[string]func() int{
684686
return 1
685687
},
686688
"update": func() int {
687-
fmt.Printf("Up to date (version %s).\n", core.PleaseVersion)
689+
fmt.Printf("Up to date (version %s).\n", version.PleaseVersion)
688690
return 0 // We'd have died already if something was wrong.
689691
},
690692
"op": func() int {
@@ -1428,7 +1430,7 @@ func initBuild(args []string) string {
14281430
parser, extraArgs, flagsErr := cli.ParseFlags("Please", &opts, args, flags.PassDoubleDash, handleCompletions, additionalUsageInfo)
14291431
// Note that we must leave flagsErr for later, because it may be affected by aliases.
14301432
if opts.HelpFlags.Version {
1431-
fmt.Printf("Please version %s\n", core.PleaseVersion)
1433+
fmt.Printf("Please version %s\n", version.PleaseVersion)
14321434
os.Exit(0) // Ignore other flags if --version was passed.
14331435
} else if opts.HelpFlags.Help {
14341436
parser.WriteHelp(os.Stderr)
@@ -1544,6 +1546,13 @@ func toExitCode(success bool, state *core.BuildState) int {
15441546
return 1
15451547
}
15461548

1549+
var commandInvocationCount = metrics.NewCounterVec(
1550+
"command",
1551+
"invocation",
1552+
"How many times each command is used",
1553+
[]string{"command"},
1554+
)
1555+
15471556
func execute(command string) int {
15481557
if opts.Profile != "" {
15491558
f, err := os.Create(opts.Profile)
@@ -1583,6 +1592,7 @@ func execute(command string) int {
15831592
}
15841593

15851594
log.Debugf("plz %v", command)
1595+
commandInvocationCount.WithLabelValues(command).Inc()
15861596
return buildFunctions[command]()
15871597
}
15881598

src/plz/plz.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func Run(targets, preTargets []core.BuildLabel, state *core.BuildState, config *
120120
log.Info("Total remote RPC data in: %d out: %d", in, out)
121121
}
122122
state.CloseResults()
123-
metrics.Push(config)
123+
metrics.Push(config.Metrics, config.IsRemoteExecution())
124124
}
125125

126126
// RunHost is a convenience function that uses the host architecture, the given state's

src/plzinit/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ go_library(
1818
"//src/core",
1919
"//src/fs",
2020
"//src/scm",
21+
"//src/version",
2122
],
2223
)
2324

src/plzinit/init.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/thought-machine/please/src/core"
1313
"github.com/thought-machine/please/src/fs"
1414
"github.com/thought-machine/please/src/scm"
15+
"github.com/thought-machine/please/src/version"
1516
)
1617

1718
const configTemplate = `; Please config file
@@ -59,7 +60,7 @@ func InitConfig(dir string, bazelCompatibility bool, noPrompt bool) {
5960
log.Warning("Can't determine absolute directory: %s", err)
6061
}
6162
config := filepath.Join(dir, core.ConfigFileName)
62-
contents := fmt.Sprintf(configTemplate, core.PleaseVersion)
63+
contents := fmt.Sprintf(configTemplate, version.PleaseVersion)
6364

6465
if bazelCompatibility {
6566
contents += bazelCompatibilityConfig

src/remote/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ go_library(
4141
"//src/process",
4242
"//src/remote/fs",
4343
"//src/remote/fs/cache",
44+
"//src/version",
4445
],
4546
)
4647

src/remote/utils.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/thought-machine/please/src/fs"
3232
"github.com/thought-machine/please/src/metrics"
3333
remotefs "github.com/thought-machine/please/src/remote/fs"
34+
"github.com/thought-machine/please/src/version"
3435
)
3536

3637
var downloadErrors = metrics.NewCounter(
@@ -658,7 +659,7 @@ func (c *Client) contextWithMetadata(target *core.BuildTarget) context.Context {
658659
CorrelatedInvocationsId: c.buildID,
659660
ToolDetails: &pb.ToolDetails{
660661
ToolName: "please",
661-
ToolVersion: core.PleaseVersion,
662+
ToolVersion: version.PleaseVersion,
662663
},
663664
})
664665
return metadata.NewOutgoingContext(context.Background(), metadata.Pairs(key, string(b)))

0 commit comments

Comments
 (0)