This repository was archived by the owner on Aug 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
scope.yml
config file support
#187
Open
tonyredondo
wants to merge
12
commits into
master
Choose a base branch
from
config-file
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3e5b0d3
Type definition for config file
tonyredondo 407db18
Vars file with loader structure
tonyredondo 8bb0630
Import Go-Env fork to handle env vars and default values.
tonyredondo 2ee08f6
Configuration loader form yaml->env->defaults
tonyredondo 73894d8
Update go env package
tonyredondo 4a2e415
Removes old env package and changed to use the new config package
tonyredondo a833a3c
Imports sorting and go mod tidy
tonyredondo 634f2fb
update payload sizes to load from config
tonyredondo 232701c
go fmt
tonyredondo d9dd0fc
go mod tidy
tonyredondo d675cf1
go mod tidy
tonyredondo 95f9c95
Rebase fixes
tonyredondo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package config | ||
|
||
type ( | ||
ScopeConfig struct { | ||
Dsn *string `env:"SCOPE_DSN"` | ||
ApiKey *string `env:"SCOPE_APIKEY"` | ||
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"` | ||
Branch *string `yaml:"branch" env:"SCOPE_BRANCH"` | ||
SourceRoot *string `yaml:"source_root" env:"SCOPE_SOURCE_ROOT"` | ||
Logger LoggerConfig `yaml:"logger"` | ||
Metadata map[string]interface{} `yaml:"metadata" env:"SCOPE_METADATA"` | ||
Configuration []string `yaml:"configuration" env:"SCOPE_CONFIGURATION" default:"platform.name, platform.architecture, go.version"` | ||
TestingMode *bool `yaml:"testing_mode" env:"SCOPE_TESTING_MODE" default:"false"` | ||
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 { | ||
DiffSummary *bool `yaml:"diff_summary" env:"SCOPE_INSTRUMENTATION_DIFF_SUMMARY" default:"true"` | ||
TestsFrameworks InstrumentationTestsFrameworksConfig `yaml:"tests_frameworks"` | ||
DB InstrumentationDatabaseConfig `yaml:"db"` | ||
Http InstrumentationHttpConfig `yaml:"http"` | ||
Logger InstrumentationLoggerConfig `yaml:"logger"` | ||
} | ||
InstrumentationTestsFrameworksConfig struct { | ||
FailRetries *int `yaml:"fail_retries" env:"SCOPE_INSTRUMENTATION_TESTS_FRAMEWORKS_FAIL_RETRIES" default:"0"` | ||
PanicAsFail *bool `yaml:"panic_as_fail" env:"SCOPE_INSTRUMENTATION_TESTS_FRAMEWORKS_PANIC_AS_FAIL" default:"false"` | ||
} | ||
InstrumentationDatabaseConfig struct { | ||
StatementValues *bool `yaml:"statement_values" env:"SCOPE_INSTRUMENTATION_DB_STATEMENT_VALUES" default:"false"` | ||
Stacktrace *bool `yaml:"stacktrace" env:"SCOPE_INSTRUMENTATION_DB_STACKTRACE" default:"false"` | ||
} | ||
InstrumentationHttpConfig struct { | ||
Client *bool `yaml:"client" env:"SCOPE_INSTRUMENTATION_HTTP_CLIENT" default:"true"` | ||
Server *bool `yaml:"server" env:"SCOPE_INSTRUMENTATION_HTTP_SERVER" default:"true"` | ||
Payloads *bool `yaml:"payloads" env:"SCOPE_INSTRUMENTATION_HTTP_PAYLOADS" default:"false"` | ||
Stacktrace *bool `yaml:"stacktrace" env:"SCOPE_INSTRUMENTATION_HTTP_STACKTRACE" default:"false"` | ||
Headers []string `yaml:"headers" env:"SCOPE_INSTRUMENTATION_HTTP_HEADERS"` | ||
} | ||
InstrumentationLoggerConfig struct { | ||
StandardLogger *bool `yaml:"standard_logger" env:"SCOPE_INSTRUMENTATION_LOGGER_STANDARD_LOGGER" default:"true"` | ||
StandardOutput *bool `yaml:"standard_output" env:"SCOPE_INSTRUMENTATION_LOGGER_STANDARD_OUTPUT" default:"false"` | ||
StandardError *bool `yaml:"standard_error" env:"SCOPE_INSTRUMENTATION_LOGGER_STANDARD_ERROR" default:"false"` | ||
} | ||
TracerConfig struct { | ||
Global *bool `yaml:"global" env:"SCOPE_TRACER_GLOBAL, SCOPE_SET_GLOBAL_TRACER" default:"false"` | ||
Dispatcher TracerDispatcherConfig `yaml:"dispatcher"` | ||
} | ||
TracerDispatcherConfig struct { | ||
HealthCheckFrequency *int `yaml:"healthcheck_frecuency" env:"SCOPE_TRACER_DISPATCHER_HEALTHCHECK_FRECUENCY" default:"1000"` | ||
HealthCheckFrequencyInTestMode *int `yaml:"healthcheck_frecuency_in_testmode" env:"SCOPE_TRACER_DISPATCHER_HEALTHCHECK_FRECUENCY_IN_TESTMODE" default:"60000"` | ||
ConcurrencyLevel *int `yaml:"concurrency_level" env:"SCOPE_TRACER_DISPATCHER_CONCURRENCY_LEVEL" default:"1"` | ||
Spans TracerDispatcherSpansConfig `yaml:"spans"` | ||
Events TracerDispatcherEventsConfig `yaml:"events"` | ||
} | ||
TracerDispatcherSpansConfig struct { | ||
MaxPayloadSize *int `yaml:"max_payload_size" env:"SCOPE_TRACER_DISPATCHER_SPANS_MAX_PAYLOAD_SIZE" default:"1000"` | ||
} | ||
TracerDispatcherEventsConfig struct { | ||
MaxPayloadSize *int `yaml:"max_payload_size" env:"SCOPE_TRACER_DISPATCHER_EVENTS_MAX_PAYLOAD_SIZE" default:"1000"` | ||
} | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
||
"gopkg.in/yaml.v2" | ||
|
||
"github.com/undefinedlabs/go-env" | ||
) | ||
|
||
var ( | ||
current *ScopeConfig | ||
m sync.RWMutex | ||
) | ||
|
||
func Get() *ScopeConfig { | ||
// We check is already loaded with a reader lock | ||
m.RLock() | ||
if current != nil { | ||
defer m.RUnlock() | ||
return current | ||
} | ||
m.RUnlock() | ||
|
||
// Is not loaded we block to load it | ||
m.Lock() | ||
tonyredondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defer m.Unlock() | ||
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 { | ||
config.LoadError = err | ||
} | ||
current = &config | ||
return current | ||
} | ||
|
||
func readConfigurationFile() ([]byte, *string, error) { | ||
dir, err := os.Getwd() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
for { | ||
rel, _ := filepath.Rel("/", dir) | ||
// Exit the loop once we reach the basePath. | ||
if rel == "." { | ||
break | ||
} | ||
|
||
path := fmt.Sprintf("%v/scope.yml", dir) | ||
dat, err := ioutil.ReadFile(path) | ||
if err == nil { | ||
return dat, &path, nil | ||
} | ||
|
||
// Going up! | ||
dir += "/.." | ||
} | ||
return nil, nil, errors.New("configuration not found") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.