Skip to content
This repository has been archived by the owner on Aug 17, 2020. It is now read-only.

Commit

Permalink
Removes old env package and changed to use the new config package
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyredondo committed Mar 2, 2020
1 parent 7b1ad4c commit a2a0dc3
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 250 deletions.
53 changes: 29 additions & 24 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/opentracing/opentracing-go"

"go.undefinedlabs.com/scopeagent/config"
"go.undefinedlabs.com/scopeagent/env"
scopeError "go.undefinedlabs.com/scopeagent/errors"
"go.undefinedlabs.com/scopeagent/instrumentation"
"go.undefinedlabs.com/scopeagent/runner"
Expand Down Expand Up @@ -64,6 +63,8 @@ var (

testingModeFrequency = time.Second
nonTestingModeFrequency = time.Minute

cfg = config.Get()
)

func WithApiKey(apiKey string) Option {
Expand Down Expand Up @@ -194,13 +195,13 @@ func NewAgent(options ...Option) (*Agent, error) {
agent.logger = log.New(ioutil.Discard, "", 0)
}

agent.debugMode = agent.debugMode || env.ScopeDebug.Value
agent.debugMode = agent.debugMode || *cfg.Debug

configProfile := GetConfigCurrentProfile()

if agent.apiKey == "" || agent.apiEndpoint == "" {
if dsn, set := env.ScopeDsn.Tuple(); set && dsn != "" {
dsnApiKey, dsnApiEndpoint, dsnErr := parseDSN(dsn)
if cfg.Dsn != nil && *cfg.Dsn != "" {
dsnApiKey, dsnApiEndpoint, dsnErr := parseDSN(*cfg.Dsn)
if dsnErr != nil {
agent.logger.Printf("Error parsing dsn value: %v\n", dsnErr)
} else {
Expand All @@ -213,8 +214,8 @@ func NewAgent(options ...Option) (*Agent, error) {
}

if agent.apiKey == "" {
if apiKey, set := env.ScopeApiKey.Tuple(); set && apiKey != "" {
agent.apiKey = apiKey
if cfg.ApiKey != nil && *cfg.ApiKey != "" {
agent.apiKey = *cfg.ApiKey
} else if configProfile != nil {
agent.logger.Println("API key found in the native app configuration")
agent.apiKey = configProfile.ApiKey
Expand All @@ -226,12 +227,13 @@ func NewAgent(options ...Option) (*Agent, error) {
}

if agent.apiEndpoint == "" {
if endpoint, set := env.ScopeApiEndpoint.Tuple(); set && endpoint != "" {
agent.apiEndpoint = endpoint
if cfg.ApiEndpoint != nil && *cfg.ApiEndpoint != "" {
agent.apiEndpoint = *cfg.ApiEndpoint
} else if configProfile != nil {
agent.logger.Println("API endpoint found in the native app configuration")
agent.apiEndpoint = configProfile.ApiEndpoint
} else {
endpoint := "https://app.scope.dev"
agent.logger.Printf("using default endpoint: %v\n", endpoint)
agent.apiEndpoint = endpoint
}
Expand Down Expand Up @@ -271,13 +273,19 @@ func NewAgent(options ...Option) (*Agent, error) {
agent.metadata[tags.GoVersion] = runtime.Version()

// Service name
addElementToMapIfEmpty(agent.metadata, tags.Service, env.ScopeService.Value)
if cfg.Service != nil {
addElementToMapIfEmpty(agent.metadata, tags.Service, *cfg.Service)
}

// Configurations
addElementToMapIfEmpty(agent.metadata, tags.ConfigurationKeys, env.ScopeConfiguration.Value)
if cfg.Configuration != nil {
addElementToMapIfEmpty(agent.metadata, tags.ConfigurationKeys, cfg.Configuration)
}

// Metadata
addToMapIfEmpty(agent.metadata, env.ScopeMetadata.Value)
if cfg.Metadata != nil {
addToMapIfEmpty(agent.metadata, cfg.Metadata)
}

// Git data
addToMapIfEmpty(agent.metadata, getGitInfoFromEnv())
Expand All @@ -292,26 +300,26 @@ func NewAgent(options ...Option) (*Agent, error) {
agent.metadata[tags.Dependencies] = getDependencyMap()

// Expand '~' in source root
var sourceRoot string
if sRoot, ok := agent.metadata[tags.SourceRoot]; ok {
if sRootEx, err := homedir.Expand(sRoot.(string)); err == nil {
sourceRoot = sRootEx
agent.metadata[tags.SourceRoot] = sRootEx
}
}

if !agent.testingMode {
if env.ScopeTestingMode.IsSet {
agent.testingMode = env.ScopeTestingMode.Value
if cfg.TestingMode != nil {
agent.testingMode = *cfg.TestingMode
} else {
agent.testingMode = agent.metadata[tags.CI].(bool)
}
}

if agent.failRetriesCount == 0 {
agent.failRetriesCount = env.ScopeTestingFailRetries.Value
if agent.failRetriesCount == 0 && cfg.Instrumentation.TestsFrameworks.FailRetries != nil {
agent.failRetriesCount = *cfg.Instrumentation.TestsFrameworks.FailRetries
}
if cfg.Instrumentation.TestsFrameworks.PanicAsFail != nil {
agent.panicAsFail = agent.panicAsFail || *cfg.Instrumentation.TestsFrameworks.PanicAsFail
}
agent.panicAsFail = agent.panicAsFail || env.ScopeTestingPanicAsFail.Value

if agent.debugMode {
agent.logMetadata()
Expand Down Expand Up @@ -339,10 +347,7 @@ func NewAgent(options ...Option) (*Agent, error) {
})
instrumentation.SetTracer(agent.tracer)
instrumentation.SetLogger(agent.logger)
if err := config.Load(path.Join(sourceRoot, "scope.yml")); err != nil {
agent.logger.Println(err)
}
if agent.setGlobalTracer || env.ScopeTracerGlobal.Value {
if agent.setGlobalTracer || (cfg.Tracer.Global != nil && *cfg.Tracer.Global) {
opentracing.SetGlobalTracer(agent.Tracer())
}

Expand Down Expand Up @@ -408,8 +413,8 @@ func generateAgentID() string {
}

func getLogPath() (string, error) {
if env.ScopeLoggerRoot.IsSet {
return env.ScopeLoggerRoot.Value, nil
if cfg.Logger.Root != nil {
return *cfg.Logger.Root, nil
}

logFolder := ""
Expand Down
6 changes: 4 additions & 2 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
"time"

"go.undefinedlabs.com/scopeagent/env"
"go.undefinedlabs.com/scopeagent/config"
"go.undefinedlabs.com/scopeagent/tags"
)

Expand Down Expand Up @@ -133,7 +133,9 @@ func sameElements(a, b []string) bool {
}

func TestTildeExpandRaceMetadata(t *testing.T) {
env.ScopeSourceRoot.Value = "~/scope"
cfg := config.Get()
sroot := "~/scope"
cfg.SourceRoot = &sroot
agent, err := NewAgent(WithApiKey("123"), WithTestingModeEnabled())
if err != nil {
t.Fatal(err)
Expand Down
21 changes: 12 additions & 9 deletions agent/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/google/uuid"

"go.undefinedlabs.com/scopeagent/env"
"go.undefinedlabs.com/scopeagent/tags"
)

Expand Down Expand Up @@ -62,6 +61,10 @@ func getGitData() *GitData {
}

func getGitDiff() *GitDiff {
if cfg.Instrumentation.DiffSummary != nil && !*cfg.Instrumentation.DiffSummary {
return nil
}

var diff string
if diffBytes, err := exec.Command("git", "diff", "--numstat").Output(); err == nil {
diff = string(diffBytes)
Expand Down Expand Up @@ -128,17 +131,17 @@ func getGitInfoFromGitFolder() map[string]interface{} {
func getGitInfoFromEnv() map[string]interface{} {
gitInfo := map[string]interface{}{}

if repository, set := env.ScopeRepository.Tuple(); set && repository != "" {
gitInfo[tags.Repository] = repository
if cfg.Repository != nil && *cfg.Repository != "" {
gitInfo[tags.Repository] = *cfg.Repository
}
if commit, set := env.ScopeCommitSha.Tuple(); set && commit != "" {
gitInfo[tags.Commit] = commit
if cfg.CommitSha != nil && *cfg.CommitSha != "" {
gitInfo[tags.Commit] = *cfg.CommitSha
}
if sourceRoot, set := env.ScopeSourceRoot.Tuple(); set && sourceRoot != "" {
gitInfo[tags.SourceRoot] = sourceRoot
if cfg.SourceRoot != nil && *cfg.SourceRoot != "" {
gitInfo[tags.SourceRoot] = *cfg.SourceRoot
}
if branch, set := env.ScopeBranch.Tuple(); set && branch != "" {
gitInfo[tags.Branch] = branch
if cfg.Branch != nil && *cfg.Branch != "" {
gitInfo[tags.Branch] = *cfg.Branch
}

return gitInfo
Expand Down
5 changes: 3 additions & 2 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type (
ScopeConfig struct {
Dsn *string `env:"SCOPE_DSN"`
ApiKey *string `env:"SCOPE_APIKEY"`
ApiEndpoint *string `env:"SCOPE_API_ENDPOINT" default:"https://app.scope.dev"`
ApiEndpoint *string `env:"SCOPE_API_ENDPOINT"`
Service *string `yaml:"service" env:"SCOPE_SERVICE" default:"default"`
Repository *string `yaml:"repository" env:"SCOPE_REPOSITORY"`
CommitSha *string `yaml:"commit_sha" env:"SCOPE_COMMIT_SHA"`
Expand All @@ -17,12 +17,13 @@ type (
Instrumentation InstrumentationConfig `yaml:"instrumentation"`
Tracer TracerConfig `yaml:"tracer"`
Debug *bool `env:"SCOPE_DEBUG" default:"false"`
ConfigPath *string
LoadError error
}
LoggerConfig struct {
Root *string `yaml:"root" env:"SCOPE_LOGGER_ROOT, SCOPE_LOG_ROOT_PATH"`
}
InstrumentationConfig struct {
Enabled *bool `yaml:"enabled" env:"SCOPE_INSTRUMENTATION_ENABLED" default:"true"`
DiffSummary *bool `yaml:"diff_summary" env:"SCOPE_INSTRUMENTATION_DIFF_SUMMARY" default:"true"`
TestsFrameworks InstrumentationTestsFrameworksConfig `yaml:"tests_frameworks"`
DB InstrumentationDatabaseConfig `yaml:"db"`
Expand Down
74 changes: 54 additions & 20 deletions config/vars.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package config

import (
"errors"
"fmt"
"github.com/undefinedlabs/go-env"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"
"sync"

env "github.com/undefinedlabs/go-env"
"gopkg.in/yaml.v2"
)

var (
Expand All @@ -15,31 +17,63 @@ var (
)

func Get() *ScopeConfig {
// We check is already loaded with a reader lock
m.RLock()
defer m.RUnlock()

return current
}
if current != nil {
defer m.RUnlock()
return current
}
m.RUnlock()

func Load(filePath string) error {
// Is not loaded we block to load it
m.Lock()
defer m.Unlock()

file, err := os.Open(filePath)
if current != nil {
return current
}
var config ScopeConfig
content, path, err := readConfigurationFile()
if err == nil {
config.ConfigPath = path
_ = yaml.Unmarshal(content, &config)
if config.Metadata != nil {
for k, v := range config.Metadata {
if str, ok := v.(string); ok {
config.Metadata[k] = os.ExpandEnv(str)
}
}
}
} else {
config.LoadError = err
}
_, err = env.UnmarshalFromEnviron(&config)
if err != nil {
return err
config.LoadError = err
}
content, err := ioutil.ReadAll(file)
current = &config
return current
}

func readConfigurationFile() ([]byte, *string, error) {
dir, err := os.Getwd()
if err != nil {
return err
return nil, nil, err
}
for {
rel, _ := filepath.Rel("/", dir)
// Exit the loop once we reach the basePath.
if rel == "." {
break
}

var config ScopeConfig
yamlErr := yaml.Unmarshal(content, &config)
_, envErr := env.UnmarshalFromEnviron(&config)
if yamlErr != nil && envErr != nil {
return envErr
path := fmt.Sprintf("%v/scope.yml", dir)
dat, err := ioutil.ReadFile(path)
if err == nil {
return dat, &path, nil
}

// Going up!
dir += "/.."
}
current = &config
return nil
return nil, nil, errors.New("configuration not found")
}
Loading

0 comments on commit a2a0dc3

Please sign in to comment.