diff --git a/pkg/trace/README.md b/pkg/trace/README.md index 9a421e5bdc..dfccaa0544 100644 --- a/pkg/trace/README.md +++ b/pkg/trace/README.md @@ -62,8 +62,10 @@ This stores the data locally in the specified directory. ### Push Based Event Collection Push based event collection is where the consensus node pushes trace data to an -external server. At the moment, this is just an S3 bucket. To use this, add the -following to the config.toml file: +external server. At the moment, this is just an S3 bucket. To use this, two options are available: +#### Using push config file + +Add the following to the config.toml file: ```toml # TracePushConfig is the relative path of the push config. @@ -73,7 +75,7 @@ following to the config.toml file: trace_push_config = "{{ .Instrumentation.TracePushConfig }}" ``` -The push config file should look like this: +The push config file is a JSON file that should look like this: ```json { @@ -84,3 +86,17 @@ The push config file should look like this: "push_delay": 60 // number of seconds to wait between intervals of pushing all files } ``` + +#### Using environment variables for s3 bucket + +Alternatively, you can set the following environment variables: + +```bash +export TRACE_PUSH_BUCKET_NAME=bucket-name +export TRACE_PUSH_REGION=region +export TRACE_PUSH_ACCESS_KEY=access-key +export TRACE_PUSH_SECRET_KEY=secret-key +export TRACE_PUSH_DELAY=push-delay +``` + +`bucket_name` , `region`, `access_key`, `secret_key` and `push_delay` are the s3 bucket name, region, access key, secret key and the delay between pushes respectively. diff --git a/pkg/trace/local_tracer.go b/pkg/trace/local_tracer.go index 3a2fe19125..d8e85b634f 100644 --- a/pkg/trace/local_tracer.go +++ b/pkg/trace/local_tracer.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path" + "strconv" "strings" "time" @@ -89,6 +90,7 @@ func NewLocalTracer(cfg *config.Config, logger log.Logger, chainID, nodeID strin if cfg.Instrumentation.TracePullAddress != "" { go lt.servePullData() } + if cfg.Instrumentation.TracePushConfig != "" { s3Config, err := readS3Config(path.Join(cfg.RootDir, "config", cfg.Instrumentation.TracePushConfig)) if err != nil { @@ -96,11 +98,37 @@ func NewLocalTracer(cfg *config.Config, logger log.Logger, chainID, nodeID strin } lt.s3Config = s3Config go lt.pushLoop() + } else if s3Config, err := GetPushConfigFromEnv(); err == nil { + lt.s3Config = s3Config + go lt.pushLoop() } return lt, nil } +// GetPushConfigFromEnv reads the required environment variables to push trace +func GetPushConfigFromEnv() (S3Config, error) { + bucketName := os.Getenv("TRACE_PUSH_BUCKET_NAME") + region := os.Getenv("TRACE_PUSH_REGION") + accessKey := os.Getenv("TRACE_PUSH_ACCESS_KEY") + secretKey := os.Getenv("TRACE_PUSH_SECRET_KEY") + pushDelay, err := strconv.ParseInt(os.Getenv("TRACE_PUSH_DELAY"), 10, 64) + if err != nil { + return S3Config{}, err + } + if bucketName == "" || region == "" || accessKey == "" || secretKey == "" { + return S3Config{}, fmt.Errorf("missing required environment variables") + } + var s3Config = S3Config{ + BucketName: bucketName, + Region: region, + AccessKey: accessKey, + SecretKey: secretKey, + PushDelay: pushDelay, + } + return s3Config, nil +} + func (lt *LocalTracer) Write(e Entry) { if !lt.IsCollecting(e.Table()) { return diff --git a/pkg/trace/local_tracer_test.go b/pkg/trace/local_tracer_test.go index da8d9437d8..571e11e6bd 100644 --- a/pkg/trace/local_tracer_test.go +++ b/pkg/trace/local_tracer_test.go @@ -121,6 +121,21 @@ func TestLocalTracerServerPull(t *testing.T) { } } +// TestReadPushConfigFromConfigFile tests reading the push config from the environment variables. +func TestReadPushConfigFromEnvVars(t *testing.T) { + os.Setenv("TRACE_PUSH_BUCKET_NAME", "bucket") + os.Setenv("TRACE_PUSH_REGION", "region") + os.Setenv("TRACE_PUSH_ACCESS_KEY", "access") + os.Setenv("TRACE_PUSH_SECRET_KEY", "secret") + os.Setenv("TRACE_PUSH_DELAY", "10") + + lt := setupLocalTracer(t, 0) + require.Equal(t, "bucket", lt.s3Config.BucketName) + require.Equal(t, "region", lt.s3Config.Region) + require.Equal(t, "access", lt.s3Config.AccessKey) + require.Equal(t, "secret", lt.s3Config.SecretKey) + require.Equal(t, int64(10), lt.s3Config.PushDelay) +} func setupLocalTracer(t *testing.T, port int) *LocalTracer { logger := log.NewNopLogger() cfg := config.DefaultConfig()