Skip to content

Commit 8b5fc5d

Browse files
author
Roberto Sora
committed
Added prefix and moved Insrumentation inside the command package
1 parent 16a6204 commit 8b5fc5d

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
@@ -58,26 +58,22 @@ var daemonize bool
5858
func runDaemonCommand(cmd *cobra.Command, args []string) {
5959

6060
// Configure telemetry engine
61+
// Create a Prometheus default handler
6162
ph := prometheus.DefaultHandler
62-
63-
// Register the client so it receives metrics from the default engine.
64-
engine := stats.WithPrefix("daemon")
65-
engine.Register(ph)
66-
63+
// Replace the default stats engine with an engine that prepends the "daemon" prefix to all metrics
64+
stats.DefaultEngine = stats.WithPrefix("daemon")
65+
// Register the handler so it receives metrics from the default engine.
66+
stats.Register(ph)
6767
// Flush the default stats engine on return to ensure all buffered
6868
// metrics are sent to the server.
6969
defer stats.Flush()
70-
7170
// move everything inside commands and search for setting up a common prefix for all metrics sent!
7271
logrus.Infof("Setting up Prometheus telemetry on /metrics, TCP port 2112")
7372
go func() {
7473
http.Handle("/metrics", ph)
7574
logrus.Error(http.ListenAndServe(":2112", nil))
7675
}()
7776

78-
engine.Incr("board.yes", stats.Tag{"success", "false"})
79-
stats.Flush()
80-
engine.Flush()
8177
port := viper.GetString("daemon.port")
8278
s := grpc.NewServer()
8379

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)