Skip to content

Commit

Permalink
telemetry recorded locally as info log (algorand#666)
Browse files Browse the repository at this point in the history
config.json: {"TelemetryToLog":true}
logging.config: {"Enable":false,"SendToLog":true}
  • Loading branch information
algobolson authored Jan 8, 2020
1 parent 95b412d commit 0d56cc1
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 24 deletions.
16 changes: 9 additions & 7 deletions cmd/algod/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ func main() {
}
defer fileLock.Unlock()

cfg, err := config.LoadConfigFromDisk(absolutePath)
if err != nil && !os.IsNotExist(err) {
// log is not setup yet, this will log to stderr
log.Fatalf("Cannot load config: %v", err)
}

// Enable telemetry hook in daemon to send logs to cloud
// If ALGOTEST env variable is set, telemetry is disabled - allows disabling telemetry for tests
isTest := os.Getenv("ALGOTEST") != ""
Expand All @@ -166,10 +172,12 @@ func main() {
os.Exit(1)
}

telemetryConfig.SendToLog = telemetryConfig.SendToLog || cfg.TelemetryToLog

// Apply telemetry override.
telemetryConfig.Enable = logging.TelemetryOverride(*telemetryOverride)

if telemetryConfig.Enable {
if telemetryConfig.Enable || telemetryConfig.SendToLog {
// If session GUID specified, use it.
if *sessionGUID != "" {
if len(*sessionGUID) == 36 {
Expand All @@ -188,12 +196,6 @@ func main() {
Genesis: genesis,
}

cfg, err := config.LoadConfigFromDisk(s.RootPath)
if err != nil && !os.IsNotExist(err) {
// log is not setup yet, this will log to stderr
log.Fatalf("Cannot load config: %v", err)
}

// Generate a REST API token if one was not provided
apiToken, wroteNewToken, err := tokens.ValidateOrGenerateAPIToken(s.RootPath, tokens.AlgodTokenFilename)

Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,9 @@ type Local struct {
// PeerConnectionsUpdateInterval defines the interval at which the peer connections information is being sent to the
// telemetry ( when enabled ). Defined in seconds.
PeerConnectionsUpdateInterval int

// TelemetryToLog records messages to node.log that are normally sent to remote event monitoring
TelemetryToLog bool
}

// Filenames of config files within the configdir (e.g. ~/.algorand)
Expand Down
1 change: 1 addition & 0 deletions config/local_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ var defaultLocalV5 = Local{
RunHosted: false,
SuggestedFeeBlockHistory: 3,
SuggestedFeeSlidingWindowSize: 50,
TelemetryToLog: true,
TxPoolExponentialIncreaseFactor: 2,
TxPoolSize: 15000,
TxSyncIntervalSeconds: 60,
Expand Down
1 change: 1 addition & 0 deletions installer/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"RunHosted": false,
"SuggestedFeeBlockHistory": 3,
"SuggestedFeeSlidingWindowSize": 50,
"TelemetryToLog": true,
"TxPoolExponentialIncreaseFactor": 2,
"TxPoolSize": 15000,
"TxSyncIntervalSeconds": 60,
Expand Down
2 changes: 1 addition & 1 deletion logging/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func NewLogger() Logger {
}

func (l logger) EnableTelemetry(cfg TelemetryConfig) (err error) {
if l.loggerState.telemetry != nil || !cfg.Enable {
if l.loggerState.telemetry != nil || (!cfg.Enable && !cfg.SendToLog) {
return nil
}
return EnableTelemetry(cfg, &l)
Expand Down
35 changes: 21 additions & 14 deletions logging/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func EnableTelemetry(cfg TelemetryConfig, l *logger) (err error) {
func enableTelemetryState(telemetry *telemetryState, l *logger) {
l.loggerState.telemetry = telemetry
// Hook our normal logging to send desired types to telemetry
l.AddHook(telemetry.hook)
if telemetry.hook != nil {
l.AddHook(telemetry.hook)
}
// Wrap current logger Output writer to capture history
l.setOutput(telemetry.wrapOutput(l.getOutput()))
}
Expand All @@ -71,19 +73,19 @@ func makeLevels(min logrus.Level) []logrus.Level {
}

func makeTelemetryState(cfg TelemetryConfig, hookFactory hookFactory) (*telemetryState, error) {
history := createLogBuffer(logBufferDepth)
if cfg.SessionGUID == "" {
cfg.SessionGUID = uuid.NewV4().String()
}
hook, err := createTelemetryHook(cfg, history, hookFactory)
if err != nil {
return nil, err
}

telemetry := &telemetryState{
history,
createAsyncHookLevels(hook, 32, 100, makeLevels(cfg.MinLogLevel)),
telemetry := &telemetryState{}
telemetry.history = createLogBuffer(logBufferDepth)
if cfg.Enable {
if cfg.SessionGUID == "" {
cfg.SessionGUID = uuid.NewV4().String()
}
hook, err := createTelemetryHook(cfg, telemetry.history, hookFactory)
if err != nil {
return nil, err
}
telemetry.hook = createAsyncHookLevels(hook, 32, 100, makeLevels(cfg.MinLogLevel))
}
telemetry.sendToLog = cfg.SendToLog
return telemetry, nil
}

Expand Down Expand Up @@ -222,7 +224,12 @@ func (t *telemetryState) logTelemetry(l logger, message string, details interfac
entry.Level = logrus.InfoLevel
entry.Message = message

t.hook.Fire(entry)
if t.sendToLog {
entry.Info(message)
}
if t.hook != nil {
t.hook.Fire(entry)
}
}

func (t *telemetryState) Close() {
Expand Down
6 changes: 4 additions & 2 deletions logging/telemetryCommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@ type TelemetryOperation struct {
}

type telemetryState struct {
history *logBuffer
hook *asyncTelemetryHook
history *logBuffer
hook *asyncTelemetryHook
sendToLog bool
}

// TelemetryConfig represents the configuration of Telemetry logging
type TelemetryConfig struct {
Enable bool
SendToLog bool
URI string
Name string
GUID string
Expand Down
1 change: 1 addition & 0 deletions test/testdata/configs/config-v5.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"RestWriteTimeoutSeconds": 120,
"RunHosted": false,
"SuggestedFeeBlockHistory": 3,
"TelemetryToLog": true,
"TxPoolExponentialIncreaseFactor": 2,
"TxPoolSize": 15000,
"TxSyncIntervalSeconds": 60,
Expand Down

0 comments on commit 0d56cc1

Please sign in to comment.