Skip to content

Commit f8a6b84

Browse files
ethanalee-workgopherbot
authored andcommitted
internal/server: add instrumentation to track gopls command usage
- Add source field to args in server/code_lens - Interpet and log source field in executeComand Change-Id: Ia5cbe8176f97c69c2ed0e0e72024c5c898846e37 Reviewed-on: https://go-review.googlesource.com/c/tools/+/709956 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ethan Lee <ethanalee@google.com> Reviewed-by: Hongxiang Jiang <hxjiang@golang.org>
1 parent 70d65ec commit f8a6b84

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

gopls/internal/server/code_lens.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package server
66

77
import (
88
"context"
9+
"encoding/json"
910
"fmt"
1011
"sort"
1112

@@ -19,6 +20,10 @@ import (
1920
"golang.org/x/tools/internal/event"
2021
)
2122

23+
type codeLensMetadata struct {
24+
Source string `json:"source"`
25+
}
26+
2227
// CodeLens reports the set of available CodeLenses
2328
// (range-associated commands) in the given file.
2429
func (s *server) CodeLens(ctx context.Context, params *protocol.CodeLensParams) ([]protocol.CodeLens, error) {
@@ -55,6 +60,18 @@ func (s *server) CodeLens(ctx context.Context, params *protocol.CodeLensParams)
5560
}
5661
lenses = append(lenses, added...)
5762
}
63+
for i := range lenses {
64+
if lenses[i].Command == nil {
65+
continue
66+
}
67+
meta := codeLensMetadata{Source: "codelens"}
68+
metaBytes, err := json.Marshal(meta)
69+
if err != nil {
70+
event.Error(ctx, "failed to marshal codelens metadata", err)
71+
continue
72+
}
73+
lenses[i].Command.Arguments = append(lenses[i].Command.Arguments, json.RawMessage(metaBytes))
74+
}
5875
sort.Slice(lenses, func(i, j int) bool {
5976
a, b := lenses[i], lenses[j]
6077
if cmp := protocol.CompareRange(a.Range, b.Range); cmp != 0 {

gopls/internal/server/command.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ func (s *server) ExecuteCommand(ctx context.Context, params *protocol.ExecuteCom
6565
return nil, fmt.Errorf("%s is not a supported command", params.Command)
6666
}
6767

68+
if len(params.Arguments) > 0 {
69+
last := params.Arguments[len(params.Arguments)-1]
70+
var meta struct {
71+
Source string `json:"source"`
72+
}
73+
if err := json.Unmarshal(last, &meta); err == nil && meta.Source != "" {
74+
commandName := strings.TrimPrefix(params.Command, "gopls.")
75+
counterName := fmt.Sprintf("gopls/cmd/source:%s-%s", commandName, meta.Source)
76+
counter.New(counterName).Inc()
77+
params.Arguments = params.Arguments[:len(params.Arguments)-1]
78+
}
79+
}
80+
6881
handler := &commandHandler{
6982
s: s,
7083
params: params,

0 commit comments

Comments
 (0)