Skip to content

Commit ec21caa

Browse files
author
Roberto Sora
committed
Implement configuration via Viper
1 parent 599a174 commit ec21caa

File tree

6 files changed

+49
-15
lines changed

6 files changed

+49
-15
lines changed

cli/daemon/daemon.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ var daemonize bool
5656

5757
func runDaemonCommand(cmd *cobra.Command, args []string) {
5858

59-
telemetry.Activate("daemon")
60-
defer telemetry.Engine.Flush()
59+
if viper.GetBool("telemetry.enabled") {
60+
telemetry.Activate("daemon", viper.GetString("installation.id"))
61+
defer telemetry.Engine.Flush()
62+
}
6163

6264
port := viper.GetString("daemon.port")
6365
s := grpc.NewServer()
@@ -91,7 +93,6 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
9193
go func() {
9294
// stdin is closed when the controlling parent process ends
9395
_, _ = io.Copy(ioutil.Discard, os.Stdin)
94-
telemetry.Engine.Flush()
9596
os.Exit(0)
9697
}()
9798
}

commands/compile/compile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
4747

4848
tags := map[string]string{
4949
"fqbn": req.Fqbn,
50-
"sketchPath": req.SketchPath,
50+
"sketchPath": telemetry.SanitizeSketchPath(req.SketchPath),
5151
"showProperties": strconv.FormatBool(req.ShowProperties),
5252
"preprocess": strconv.FormatBool(req.Preprocess),
5353
"buildProperties": strings.Join(req.BuildProperties, ","),

configuration/defaults.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package configuration
1818
import (
1919
"path/filepath"
2020

21+
"github.com/gofrs/uuid"
2122
"github.com/spf13/viper"
2223
)
2324

@@ -35,4 +36,17 @@ func setDefaults(dataDir, userDir string) {
3536

3637
// daemon settings
3738
viper.SetDefault("daemon.port", "50051")
39+
40+
//telemetry settings
41+
viper.SetDefault("telemetry.enabled", true)
42+
viper.SetDefault("telemetry.addr", ":2112")
43+
viper.SetDefault("telemetry.pattern", "/metrics")
44+
45+
//Installation ID
46+
// FIXME: how should I treat this error?
47+
installationID, _ := uuid.NewV4()
48+
installationSecret, _ := uuid.NewV4()
49+
viper.SetDefault("installation.id", installationID.String())
50+
viper.SetDefault("installation.secret", installationSecret.String())
51+
3852
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/fluxio/multierror v0.0.0-20160419044231-9c68d39025e5 // indirect
1818
github.com/fsnotify/fsnotify v1.4.7
1919
github.com/go-errors/errors v1.0.1
20+
github.com/gofrs/uuid v3.2.0+incompatible
2021
github.com/golang/protobuf v1.3.2
2122
github.com/h2non/filetype v1.0.8 // indirect
2223
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV
4141
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
4242
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
4343
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
44+
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
45+
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
4446
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
4547
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
4648
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=

telemetry/telemetry.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
package telemetry
22

33
import (
4+
"crypto/hmac"
5+
"crypto/sha256"
6+
"encoding/hex"
47
"net/http"
8+
"path/filepath"
59

610
"github.com/segmentio/stats/v4"
711
"github.com/segmentio/stats/v4/prometheus"
812
"github.com/sirupsen/logrus"
13+
"github.com/spf13/viper"
914
)
1015

1116
// Engine is the engine used by global helper functions for this module.
1217
var Engine = stats.DefaultEngine
1318

14-
var serverAddr = ":2112"
15-
var serverPattern = "/metrics"
16-
17-
//Activate configure and starts the telemetry server exposing a Prometheus resource
18-
func Activate(metricPrefix string) {
19+
// Activate configure and starts the telemetry server exposing a Prometheus resource
20+
func Activate(metricPrefix, installationID string) {
1921
// Configure telemetry engine
2022
// Create a Prometheus default handler
2123
ph := prometheus.DefaultHandler
22-
// Replace the default stats engine with an engine that prepends the "daemon" prefix to all metrics
23-
Engine = stats.WithPrefix(metricPrefix)
24+
// Create a new stats engine with an engine that prepends the "daemon" prefix to all metrics
25+
// and includes the installationID as a tag
26+
Engine = stats.WithPrefix(metricPrefix, stats.T("installationID", installationID))
2427
// Register the handler so it receives metrics from the default engine.
2528
Engine.Register(ph)
26-
// Flush the default stats engine on return to ensure all buffered
27-
// metrics are sent to the server.
28-
defer Engine.Flush()
29-
// move everything inside commands and search for setting up a common prefix for all metrics sent!
29+
30+
// Configure using viper settings
31+
serverAddr := viper.GetString("telemetry.addr")
32+
serverPattern := viper.GetString("telemetry.pattern")
3033
logrus.Infof("Setting up Prometheus telemetry on %s%s", serverAddr, serverPattern)
3134
go func() {
3235
http.Handle(serverPattern, ph)
3336
logrus.Error(http.ListenAndServe(serverAddr, nil))
3437
}()
3538

3639
}
40+
41+
// SanitizeSketchPath uses config generated UUID (installation.secret) as an HMAC secret to sanitize and anonymize the sketch
42+
// name maintaining it distinguishable from a different sketch from the same Installation
43+
func SanitizeSketchPath(sketchPath string) string {
44+
installationSecret := viper.GetString("installation.secret")
45+
sketchName := filepath.Base(sketchPath)
46+
// Create a new HMAC by defining the hash type and the key (as byte array)
47+
h := hmac.New(sha256.New, []byte(installationSecret))
48+
// Write Data to it
49+
h.Write([]byte(sketchName))
50+
// Get result and encode as hexadecimal string
51+
return hex.EncodeToString(h.Sum(nil))
52+
}

0 commit comments

Comments
 (0)