Skip to content

Commit

Permalink
diagcfg : Read from global logging config if data dir not provided (#…
Browse files Browse the repository at this point in the history
…1844)

When a datadir is not provided, this will have diagcfg read from the default logging config in the global config path. It will then generate a default config if that file does not exist
  • Loading branch information
bricerisingalgorand authored and onetechnical committed Jan 20, 2021
1 parent 6b62f79 commit 3c07756
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/diagcfg/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func maybeUpdateDataDirFromEnv() {

func readTelemetryConfigOrExit() logging.TelemetryConfig {
maybeUpdateDataDirFromEnv()
cfg, err := logging.ReadTelemetryConfigOrDefault(&dataDir, "")
cfg, err := logging.ReadTelemetryConfigOrDefault(dataDir, "")
if err != nil {
fmt.Fprintf(os.Stderr, telemetryConfigReadError, err)
os.Exit(1)
Expand Down Expand Up @@ -112,7 +112,7 @@ var telemetryStatusCmd = &cobra.Command{
Long: `Print the node's telemetry status`,
Run: func(cmd *cobra.Command, args []string) {
maybeUpdateDataDirFromEnv()
cfg, err := logging.ReadTelemetryConfigOrDefault(&dataDir, "")
cfg, err := logging.ReadTelemetryConfigOrDefault(dataDir, "")

// If error loading config, can't disable / no need to disable
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions logging/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@ func makeTelemetryState(cfg TelemetryConfig, hookFactory hookFactory) (*telemetr
}

// ReadTelemetryConfigOrDefault reads telemetry config from file or defaults if no config file found.
func ReadTelemetryConfigOrDefault(dataDir *string, genesisID string) (cfg TelemetryConfig, err error) {
func ReadTelemetryConfigOrDefault(dataDir string, genesisID string) (cfg TelemetryConfig, err error) {
err = nil
if dataDir != nil && *dataDir != "" {
configPath := filepath.Join(*dataDir, TelemetryConfigFilename)
dataDirProvided := dataDir != ""
if dataDirProvided {
configPath := filepath.Join(dataDir, TelemetryConfigFilename)
cfg, err = LoadTelemetryConfig(configPath)
}
if err != nil && os.IsNotExist(err) {
if (err != nil && os.IsNotExist(err)) || !dataDirProvided {
var configPath string
configPath, err = config.GetConfigFilePath(TelemetryConfigFilename)
if err != nil {
Expand Down
23 changes: 22 additions & 1 deletion logging/telemetry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ package logging

import (
"fmt"
"os"
"testing"
"time"

"github.com/algorand/go-deadlock"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"

"github.com/algorand/go-deadlock"

"github.com/algorand/go-algorand/config"
"github.com/algorand/go-algorand/logging/telemetryspec"
)

Expand Down Expand Up @@ -315,3 +318,21 @@ func TestLogHistoryLevels(t *testing.T) {
a.Nil(data[5]["log"]) // Panic - this is stack trace
a.NotNil(data[6]["log"]) // Panic
}

func TestReadTelemetryConfigOrDefaultNoDataDir(t *testing.T) {
a := require.New(t)
tempDir := os.TempDir()
originalGlobalConfigFileRoot, _ := config.GetGlobalConfigFileRoot()
config.SetGlobalConfigFileRoot(tempDir)

cfg, err := ReadTelemetryConfigOrDefault("", "")
defaultCfgSettings := createTelemetryConfig()
config.SetGlobalConfigFileRoot(originalGlobalConfigFileRoot)

a.Nil(err)
a.NotNil(cfg)
a.NotEqual(TelemetryConfig{}, cfg)
a.Equal(defaultCfgSettings.UserName, cfg.UserName)
a.Equal(defaultCfgSettings.Password, cfg.Password)
a.Equal(len(defaultCfgSettings.GUID), len(cfg.GUID))
}

0 comments on commit 3c07756

Please sign in to comment.