Skip to content

Commit

Permalink
parse the duration string specified in an event file config into a ti…
Browse files Browse the repository at this point in the history
…me.Duration (#1446)
  • Loading branch information
jimlambrt authored Aug 4, 2021
1 parent 8496093 commit acc3dda
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
9 changes: 9 additions & 0 deletions internal/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,15 @@ func parseEventing(eventObj *ast.ObjectItem) (*event.EventerConfig, error) {
s.StderrConfig = new(event.StderrSinkTypeConfig)
}

// parse the duration string specified in a file config into a time.Duration
if s.FileConfig != nil && s.FileConfig.RotateDurationHCL != "" {
var err error
s.FileConfig.RotateDuration, err = time.ParseDuration(s.FileConfig.RotateDurationHCL)
if err != nil {
return nil, fmt.Errorf("can't parse rotation duration %s", s.FileConfig.RotateDurationHCL)
}
}

if err := s.Validate(); err != nil {
return nil, err
}
Expand Down
11 changes: 9 additions & 2 deletions internal/cmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"testing"
"time"

"github.com/hashicorp/boundary/internal/observability/event"
"github.com/hashicorp/go-secure-stdlib/configutil"
Expand Down Expand Up @@ -394,6 +395,7 @@ func TestController_EventingConfig(t *testing.T) {
event_types = [ "audit", "observation" ]
file {
file_name = "file-name"
rotate_duration = "2m"
}
}
sink {
Expand All @@ -412,6 +414,7 @@ func TestController_EventingConfig(t *testing.T) {
event_types = [ "audit", "observation" ]
file {
file_name = "file-name"
rotate_duration = "2m"
}
}
sink "stderr" {
Expand All @@ -429,6 +432,7 @@ func TestController_EventingConfig(t *testing.T) {
event_types = [ "audit", "observation" ]
file {
file_name = "file-name"
rotate_duration = "2m"
}
}
sink {
Expand All @@ -449,7 +453,8 @@ func TestController_EventingConfig(t *testing.T) {
"name": "configured-sink",
"event_types": ["audit", "observation"],
"file": {
"file_name": "file-name"
"file_name": "file-name",
"rotate_duration": "2m"
}
},
{
Expand All @@ -472,7 +477,9 @@ func TestController_EventingConfig(t *testing.T) {
Format: "cloudevents-json",
EventTypes: []event.Type{"audit", "observation"},
FileConfig: &event.FileSinkTypeConfig{
FileName: "file-name",
FileName: "file-name",
RotateDurationHCL: "2m",
RotateDuration: 2 * time.Minute,
},
},
{
Expand Down
11 changes: 6 additions & 5 deletions internal/observability/event/sink_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ type StderrSinkTypeConfig struct{}

// FileSinkTypeConfig contains configuration structures for file sink types
type FileSinkTypeConfig struct {
Path string `hcl:"path" mapstructure:"path"` // Path defines the file path for the sink
FileName string `hcl:"file_name" mapstructure:"file_name"` // FileName defines the file name for the sink
RotateBytes int `hcl:"rotate_bytes" mapstructure:"rotate_bytes"` // RotateByes defines the number of bytes that should trigger rotation of a FileSink
RotateDuration time.Duration `hcl:"rotate_duration" mapstructure:"rotate_duration"` // RotateDuration defines how often a FileSink should be rotated
RotateMaxFiles int `hcl:"rotate_max_files" mapstructure:"rotate_max_files"` // RotateMaxFiles defines how may historical rotated files should be kept for a FileSink
Path string `hcl:"path" mapstructure:"path"` // Path defines the file path for the sink
FileName string `hcl:"file_name" mapstructure:"file_name"` // FileName defines the file name for the sink
RotateBytes int `hcl:"rotate_bytes" mapstructure:"rotate_bytes"` // RotateBytes defines the number of bytes that should trigger rotation of a FileSink
RotateDuration time.Duration `mapstructure:"rotate_duration"` // RotateDuration defines how often a FileSink should be rotated
RotateDurationHCL string `hcl:"rotate_duration" json:"-"` // RotateDurationHCL defines hcl string version of RotateDuration
RotateMaxFiles int `hcl:"rotate_max_files" mapstructure:"rotate_max_files"` // RotateMaxFiles defines how may historical rotated files should be kept for a FileSink
}

// FilterType defines a type for filters (allow or deny)
Expand Down

0 comments on commit acc3dda

Please sign in to comment.