@@ -34,6 +34,8 @@ import (
3434 srv_debug "github.com/arduino/arduino-cli/rpc/debug"
3535 srv_monitor "github.com/arduino/arduino-cli/rpc/monitor"
3636 srv_settings "github.com/arduino/arduino-cli/rpc/settings"
37+ stats "github.com/segmentio/stats/v4"
38+ "github.com/segmentio/stats/v4/prometheus"
3739 "github.com/sirupsen/logrus"
3840 "github.com/spf13/cobra"
3941 "github.com/spf13/viper"
@@ -59,16 +61,40 @@ func NewCommand() *cobra.Command {
5961var daemonize bool
6062
6163func runDaemonCommand (cmd * cobra.Command , args []string ) {
64+
65+ logrus .Infof ("configure telemetry" )
66+
67+ // Configure telemetry engine
68+ ph := prometheus .DefaultHandler
69+
70+ // Register the client so it receives metrics from the default engine.
71+ stats .Register (ph )
72+
73+ // Flush the default stats engine on return to ensure all buffered
74+ // metrics are sent to the server.
75+ defer stats .Flush ()
76+ http .Handle ("/metrics" , ph )
77+ go http .ListenAndServe (":2112" , nil )
78+ logrus .Infof ("Prometheus telemetry is available on /metrics, TCP port 2112" )
79+ // Set a tag on a counter increment.
80+ stats .Incr ("user.login" , stats.Tag {"user" , "luke" })
81+
6282 port := viper .GetString ("daemon.port" )
6383 s := grpc .NewServer ()
6484
65- // register the commands service
66- headers := http.Header {"User-Agent" : []string {
67- fmt .Sprintf ("%s/%s daemon (%s; %s; %s) Commit:%s" ,
68- globals .VersionInfo .Application ,
69- globals .VersionInfo .VersionString ,
70- runtime .GOARCH , runtime .GOOS ,
71- runtime .Version (), globals .VersionInfo .Commit )}}
85+ // Compose user agent header
86+ headers := http.Header {
87+ "User-Agent" : []string {
88+ fmt .Sprintf ("%s/%s daemon (%s; %s; %s) Commit:%s" ,
89+ globals .VersionInfo .Application ,
90+ globals .VersionInfo .VersionString ,
91+ runtime .GOARCH ,
92+ runtime .GOOS ,
93+ runtime .Version (),
94+ globals .VersionInfo .Commit ),
95+ },
96+ }
97+ // Register the commands service
7298 srv_commands .RegisterArduinoCoreServer (s , & daemon.ArduinoCoreServerImpl {
7399 DownloaderHeaders : headers ,
74100 VersionString : globals .VersionInfo .VersionString ,
@@ -121,4 +147,5 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
121147 if err := s .Serve (lis ); err != nil {
122148 logrus .Fatalf ("Failed to serve: %v" , err )
123149 }
150+
124151}
0 commit comments