Skip to content

Commit

Permalink
feat: adds the feature to read push config from env vars (#1318)
Browse files Browse the repository at this point in the history
Closes #1317
  • Loading branch information
staheri14 committed Apr 24, 2024
1 parent 9ba3fac commit f624a58
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pkg/trace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
{
Expand All @@ -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.
28 changes: 28 additions & 0 deletions pkg/trace/local_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -89,18 +90,45 @@ 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 {
return nil, fmt.Errorf("failed to read s3 config: %w", err)
}
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
Expand Down
15 changes: 15 additions & 0 deletions pkg/trace/local_tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f624a58

Please sign in to comment.