Skip to content

Commit 013afa2

Browse files
author
Roberto Sora
committed
Added prefix and moved Insrumentation inside the command package
1 parent 561416d commit 013afa2

File tree

3 files changed

+10
-13
lines changed

3 files changed

+10
-13
lines changed

cli/daemon/daemon.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,22 @@ var daemonize bool
6363
func runDaemonCommand(cmd *cobra.Command, args []string) {
6464

6565
// Configure telemetry engine
66+
// Create a Prometheus default handler
6667
ph := prometheus.DefaultHandler
67-
68-
// Register the client so it receives metrics from the default engine.
69-
engine := stats.WithPrefix("daemon")
70-
engine.Register(ph)
71-
68+
// Replace the default stats engine with an engine that prepends the "daemon" prefix to all metrics
69+
stats.DefaultEngine = stats.WithPrefix("daemon")
70+
// Register the handler so it receives metrics from the default engine.
71+
stats.Register(ph)
7272
// Flush the default stats engine on return to ensure all buffered
7373
// metrics are sent to the server.
7474
defer stats.Flush()
75-
7675
// move everything inside commands and search for setting up a common prefix for all metrics sent!
7776
logrus.Infof("Setting up Prometheus telemetry on /metrics, TCP port 2112")
7877
go func() {
7978
http.Handle("/metrics", ph)
8079
logrus.Error(http.ListenAndServe(":2112", nil))
8180
}()
8281

83-
engine.Incr("board.yes", stats.Tag{"success", "false"})
84-
stats.Flush()
85-
engine.Flush()
8682
port := viper.GetString("daemon.port")
8783
s := grpc.NewServer()
8884

commands/board/list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package board
1818
import (
1919
"encoding/json"
2020
"fmt"
21+
"github.com/segmentio/stats/v4"
2122
"io/ioutil"
2223
"net/http"
2324
"regexp"
@@ -106,11 +107,13 @@ func List(instanceID int32) ([]*rpc.DetectedPort, error) {
106107

107108
pm := commands.GetPackageManager(instanceID)
108109
if pm == nil {
110+
stats.Incr("board.list", stats.Tag{"success", "false"})
109111
return nil, errors.New("invalid instance")
110112
}
111113

112114
ports, err := commands.ListBoards(pm)
113115
if err != nil {
116+
stats.Incr("board.list", stats.Tag{"success", "false"})
114117
return nil, errors.Wrap(err, "error getting port list from serial-discovery")
115118
}
116119

@@ -136,6 +139,7 @@ func List(instanceID int32) ([]*rpc.DetectedPort, error) {
136139
logrus.Debug("Board not recognized")
137140
} else if err != nil {
138141
// this is bad, bail out
142+
stats.Incr("board.list", stats.Tag{"success", "false"})
139143
return nil, errors.Wrap(err, "error getting board info from Arduino Cloud")
140144
}
141145

@@ -156,5 +160,6 @@ func List(instanceID int32) ([]*rpc.DetectedPort, error) {
156160
retVal = append(retVal, p)
157161
}
158162

163+
stats.Incr("board.list", stats.Tag{"success", "true"})
159164
return retVal, nil
160165
}

commands/daemon/daemon.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/arduino/arduino-cli/commands/lib"
3030
"github.com/arduino/arduino-cli/commands/upload"
3131
rpc "github.com/arduino/arduino-cli/rpc/commands"
32-
"github.com/segmentio/stats/v4"
3332
)
3433

3534
// ArduinoCoreServerImpl FIXMEDOC
@@ -47,12 +46,9 @@ func (s *ArduinoCoreServerImpl) BoardDetails(ctx context.Context, req *rpc.Board
4746
func (s *ArduinoCoreServerImpl) BoardList(ctx context.Context, req *rpc.BoardListReq) (*rpc.BoardListResp, error) {
4847
ports, err := board.List(req.GetInstance().GetId())
4948
if err != nil {
50-
// helper function from grpc req to tags
51-
stats.Incr("board.list", stats.Tag{"success", "false"})
5249
return nil, err
5350
}
5451

55-
stats.Incr("board.list", stats.Tag{"success", "true"})
5652
return &rpc.BoardListResp{
5753
Ports: ports,
5854
}, nil

0 commit comments

Comments
 (0)