Skip to content

Commit

Permalink
Restore backwards compatibility with 2.0.6 by placing ECS fields behi…
Browse files Browse the repository at this point in the history
…nd a configuration, to be enabled in 3.0

Resolve go generate calling go run without -mod=vendor resulting in non-vendor access during packaging
  • Loading branch information
driskell committed Feb 7, 2021
1 parent f5d4d2b commit fe23125
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 28 deletions.
2 changes: 1 addition & 1 deletion fact-courier/fact-courier.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

// Generate platform-specific default configuration values
//go:generate go run ../lc-lib/config/generate/platform.go platform main config.DefaultConfigurationFile DefaultMuninConfigBase
//go:generate go run -mod=vendor ../lc-lib/config/generate/platform.go platform main config.DefaultConfigurationFile DefaultMuninConfigBase

var (
log *logging.Logger
Expand Down
2 changes: 1 addition & 1 deletion lc-admin/lc-admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

// Generate platform-specific default configuration values
//go:generate go run ../lc-lib/config/generate/platform.go platform main config.DefaultConfigurationFile prospector.DefaultGeneralPersistDir admin.DefaultAdminBind
//go:generate go run -mod=vendor ../lc-lib/config/generate/platform.go platform main config.DefaultConfigurationFile prospector.DefaultGeneralPersistDir admin.DefaultAdminBind

type commandProcessor interface {
ProcessCommand(string) bool
Expand Down
7 changes: 5 additions & 2 deletions lc-lib/codecs/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,15 @@ func (cs *Stream) ProcessEvent(startOffset int64, endOffset int64, text string)
func (cs *Stream) eventCallback(startOffset int64, endOffset int64, text string) {
data := map[string]interface{}{
"message": text,
"log": map[string]interface{}{"offset": startOffset},
}

// TODO: Deprecate
if cs.streamConfig.AddOffsetField {
data["offset"] = startOffset
if cs.streamConfig.EnableECS {
data["log"] = map[string]interface{}{"offset": startOffset}
} else {
data["offset"] = startOffset
}
}

cs.eventFunc(startOffset, endOffset, data)
Expand Down
2 changes: 1 addition & 1 deletion lc-lib/config/generate/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const platformHeader = `// THIS IS A GO GENERATED FILE
// Useful for package maintainers
func main() {
if len(os.Args) < 3 {
log.Fatalf("Usage: go run <path-to-lc-lib>/config/generate.go -- <target> <package-name> <configs>...")
log.Fatalf("Usage: go run -mod=vendor <path-to-lc-lib>/config/generate.go -- <target> <package-name> <configs>...")
}

targetFile := os.Args[1] + ".go"
Expand Down
2 changes: 1 addition & 1 deletion lc-lib/core/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
package core

// LogCourierVersion is the library version number
const LogCourierVersion string = "2.5.0"
const LogCourierVersion string = "2.5.1"
43 changes: 28 additions & 15 deletions lc-lib/event/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ import (
)

const (
defaultStreamAddHostField bool = true
defaultStreamAddTimezoneField bool = false
defaultEnableECS bool = false
)

// StreamConfig holds the configuration for a log stream
type StreamConfig struct {
AddHostField bool `config:"add host field"`
AddTimezoneField bool `config:"add timezone field"`
EnableECS bool `config:"enable ecs"`
Fields map[string]interface{} `config:"fields"`

genConfig *config.General
Expand All @@ -40,7 +44,9 @@ type StreamConfig struct {

// Defaults initialises the default configuration for a log stream
func (sc *StreamConfig) Defaults() {
sc.AddHostField = defaultStreamAddHostField
sc.AddTimezoneField = defaultStreamAddTimezoneField
sc.EnableECS = defaultEnableECS

sc.timezone = time.Now().Format("-0700 MST")
}
Expand All @@ -62,24 +68,31 @@ func (sc *StreamConfig) Validate(p *config.Parser, path string) (err error) {
// to the data that will eventually become an event
func (sc *StreamConfig) Decorate(data map[string]interface{}) map[string]interface{} {
data["@timestamp"] = time.Now()
data["host"] = map[string]interface{}{
"name": sc.genConfig.Host,
"hostname": sc.genConfig.Host,
if sc.AddHostField {
if sc.EnableECS {
data["host"] = map[string]interface{}{
"name": sc.genConfig.Host,
"hostname": sc.genConfig.Host,
}
} else {
data["host"] = sc.genConfig.Host
}
}

var (
eventEntry map[string]interface{}
ok bool
)
if eventEntry, ok = data["event"].(map[string]interface{}); !ok {
eventEntry = map[string]interface{}{}
data["event"] = eventEntry
}
eventEntry["timezone"] = sc.timezone

// TODO: Deprecate
if sc.AddTimezoneField {
data["timezone"] = sc.timezone
if sc.EnableECS {
var (
eventEntry map[string]interface{}
ok bool
)
if eventEntry, ok = data["event"].(map[string]interface{}); !ok {
eventEntry = map[string]interface{}{}
data["event"] = eventEntry
}
eventEntry["timezone"] = sc.timezone
} else {
data["timezone"] = sc.timezone
}
}

for k := range sc.genConfig.GlobalFields {
Expand Down
14 changes: 9 additions & 5 deletions lc-lib/harvester/harvester.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,16 @@ func (h *Harvester) statCheck() error {

// eventCallback receives events from the final codec and ships them to the output
func (h *Harvester) eventCallback(startOffset int64, endOffset int64, data map[string]interface{}) {
// data["log"] is provided by codecs StreamConfig
data["log"].(map[string]interface{})["file"] = map[string]interface{}{"path": h.path}

// TODO: Deprecate
if h.streamConfig.AddPathField {
data["path"] = h.path
if h.streamConfig.EnableECS {
if !h.streamConfig.AddOffsetField {

}
// data["log"] is provided by codecs StreamConfig
data["log"].(map[string]interface{})["file"] = map[string]interface{}{"path": h.path}
} else {
data["path"] = h.path
}
}

// If we split any of the line data, tag it
Expand Down
2 changes: 1 addition & 1 deletion log-carver/log-carver.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
)

// Generate platform-specific default configuration values
//go:generate go run ../lc-lib/config/generate/platform.go platform main config.DefaultConfigurationFile admin.DefaultAdminBind processor.DefaultGeoIPActionDatabase
//go:generate go run -mod=vendor ../lc-lib/config/generate/platform.go platform main config.DefaultConfigurationFile admin.DefaultAdminBind processor.DefaultGeoIPActionDatabase

var (
app *core.App
Expand Down
2 changes: 1 addition & 1 deletion log-courier.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
)

// Generate platform-specific default configuration values
//go:generate go run lc-lib/config/generate/platform.go platform main config.DefaultConfigurationFile prospector.DefaultGeneralPersistDir admin.DefaultAdminBind
//go:generate go run -mod=vendor lc-lib/config/generate/platform.go platform main config.DefaultConfigurationFile prospector.DefaultGeneralPersistDir admin.DefaultAdminBind

var (
app *core.App
Expand Down

0 comments on commit fe23125

Please sign in to comment.