Skip to content

Commit

Permalink
Improve reading config file (#38)
Browse files Browse the repository at this point in the history
* Fix config file reading for GCF

* Allow specifying config path via env variable

* Automatically fall back to other file paths if config file can't be found
  • Loading branch information
Kwintenvdb authored Dec 21, 2023
1 parent fe1d3a6 commit bf40949
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
44 changes: 41 additions & 3 deletions core/configuration/dt_config_file_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package configuration
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"os"
)

type fileConfig struct {
Expand Down Expand Up @@ -55,10 +57,46 @@ type configFileReader interface {
type jsonConfigFileReader struct {
}

// ReadConfigFromFile looks for a config file "dtconfig.json" in the current directory and attempts to parse it.
// readConfigFromFile looks for a config file "dtconfig.json" and attempts to parse it.
// Returns an error if the file can't be read or the parsing fails.
func (j *jsonConfigFileReader) readConfigFromFile() (fileConfig, error) {
return j.readConfigFromFileByPath("./dtconfig.json")
func (j *jsonConfigFileReader) readConfigFromFile() (cfg fileConfig, err error) {
filePaths := j.configFilePaths()

if len(filePaths) == 0 {
return fileConfig{}, errors.New("could not determine any file paths to read config file from")
}

for _, filePath := range filePaths {
cfg, err = j.readConfigFromFileByPath(filePath)
if err == nil {
return cfg, nil
}
}

return cfg, err
}

// configFilePaths returns all possible file paths to look for the config file in.
func (j *jsonConfigFileReader) configFilePaths() []string {
var filePaths []string

if configFilePathFromEnv := os.Getenv("DT_CONFIG_FILE_PATH"); configFilePathFromEnv != "" {
filePaths = append(filePaths, configFilePathFromEnv)
}

// When running in a Google Cloud Functions Go runtime, we need to find the config file at a different path.
// For reference on the K_SERVICE environment variable, see:
// https://cloud.google.com/functions/docs/configuring/env-var#runtime_environment_variables_set_automatically
_, inGcf := os.LookupEnv("K_SERVICE")
if inGcf {
// In the GCF Go runtime, the root directory of your function source code is
// beneath the current working directory at ./serverless_function_source_code
// See https://cloud.google.com/functions/docs/concepts/execution-environment#memory-file-system
filePaths = append(filePaths, "./serverless_function_source_code/dtconfig.json")
}

filePaths = append(filePaths, "./dtconfig.json")
return filePaths
}

func (j *jsonConfigFileReader) readConfigFromFileByPath(filePath string) (fileConfig, error) {
Expand Down
11 changes: 11 additions & 0 deletions core/configuration/dt_config_file_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package configuration

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -26,6 +27,16 @@ func TestJsonConfigFileReader_NoErrorForValidFile(t *testing.T) {
assert.NoError(t, err)
}

func TestJsonConfigFileReader_CustomPath(t *testing.T) {
os.Setenv("DT_CONFIG_FILE_PATH", "./testdata/subfolder/dtconfig_test_valid.json")
defer os.Unsetenv("DT_CONFIG_FILE_PATH")

reader := jsonConfigFileReader{}
cfg, err := reader.readConfigFromFile()
assert.NoError(t, err)
assert.Equal(t, "subfolder_config", cfg.Tenant)
}

func TestJsonConfigFileReader_ErrorForInvalidFile(t *testing.T) {
reader := jsonConfigFileReader{}
_, err := reader.readConfigFromFileByPath("./testdata/dtconfig_test_invalid.json")
Expand Down
29 changes: 29 additions & 0 deletions core/configuration/testdata/subfolder/dtconfig_test_valid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"AgentActive": true,
"ClusterID": 12345,
"Tenant": "subfolder_config",
"Connection": {
"AuthToken": "dt0a01.schnitzel.xsdffdedr",
"BaseUrl": "https://ag.xyz.com"
},
"RUM": {
"ClientIpHeaders": [
"x-forwarded-for"
]
},
"Testability": {
"SpanProcessingIntervalMs": 3000,
"KeepAliveIntervalMs": 30000,
"MetricCollectionIntervalMs": 10000,
"MetricCollectionsPerExport": 6
},
"Logging": {
"Destination": "stderr",
"Go": {
"Flags": "Exporter=true,Propagator=false"
}
},
"Debug": {
"AddStackOnStart": true
}
}

0 comments on commit bf40949

Please sign in to comment.