Skip to content

Commit 3ae3807

Browse files
author
Roberto Sora
committed
Implement configuration via Viper
1 parent 0004520 commit 3ae3807

File tree

6 files changed

+52
-16
lines changed

6 files changed

+52
-16
lines changed

cli/daemon/daemon.go

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

6262
func runDaemonCommand(cmd *cobra.Command, args []string) {
6363

64-
telemetry.Activate("daemon")
65-
defer telemetry.Engine.Flush()
64+
if viper.GetBool("telemetry.enabled") {
65+
telemetry.Activate("daemon", viper.GetString("installation.id"))
66+
defer telemetry.Engine.Flush()
67+
}
6668

6769
port := viper.GetString("daemon.port")
6870
s := grpc.NewServer()
@@ -99,7 +101,6 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
99101
go func() {
100102
// Stdin is closed when the controlling parent process ends
101103
_, _ = io.Copy(ioutil.Discard, os.Stdin)
102-
telemetry.Engine.Flush()
103104
os.Exit(0)
104105
}()
105106
}

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.3
2122
github.com/h2non/filetype v1.0.8 // indirect
2223
github.com/juju/errors v0.0.0-20181118221551-089d3ea4e4d5 // indirect

go.sum

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV
4444
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
4545
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
4646
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
47+
github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE=
48+
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
4749
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
4850
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
4951
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
@@ -113,8 +115,8 @@ github.com/schollz/closestmatch v2.1.0+incompatible h1:Uel2GXEpJqOWBrlyI+oY9LTiy
113115
github.com/schollz/closestmatch v2.1.0+incompatible/go.mod h1:RtP1ddjLong6gTkbtmuhtR2uUrrJOpYzYRvbcPAid+g=
114116
github.com/segmentio/fasthash v0.0.0-20180216231524-a72b379d632e h1:uO75wNGioszjmIzcY/tvdDYKRLVvzggtAmmJkn9j4GQ=
115117
github.com/segmentio/fasthash v0.0.0-20180216231524-a72b379d632e/go.mod h1:tm/wZFQ8e24NYaBGIlnO2WGCAi67re4HHuOm0sftE/M=
118+
github.com/segmentio/objconv v1.0.1 h1:QjfLzwriJj40JibCV3MGSEiAoXixbp4ybhwfTB8RXOM=
116119
github.com/segmentio/objconv v1.0.1/go.mod h1:auayaH5k3137Cl4SoXTgrzQcuQDmvuVtZgS0fb1Ahys=
117-
github.com/segmentio/stats v4.1.0+incompatible h1:Sz7ypAORgiXoR8x8VNIOKYoVpJUIeHGt/2UDMjjRFuo=
118120
github.com/segmentio/stats/v4 v4.5.3 h1:Y/DSUWZ4c8ICgqJ9rQohzKvGqGWbLPWad5zmxVoKN+Y=
119121
github.com/segmentio/stats/v4 v4.5.3/go.mod h1:LsaahUJR7iiSs8mnkvQvdQ/RLHAS5adGLxuntg0ydGo=
120122
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
@@ -167,6 +169,7 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r
167169
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
168170
golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628=
169171
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
172+
golang.org/x/net v0.0.0-20190313220215-9f648a60d977/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
170173
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
171174
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
172175
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@@ -213,6 +216,7 @@ gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogR
213216
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
214217
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce h1:xcEWjVhvbDy+nHP67nPDDpbYrY+ILlfndk4bRioVHaU=
215218
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
219+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
216220
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
217221
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
218222
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

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)