-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Introduce dynamic index patterns #10450
Changes from all commits
6a5a327
590728e
aa01503
4195e6b
b8bdb62
50a35ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,13 @@ import ( | |
"context" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"path/filepath" | ||
|
||
"github.com/elastic/beats/libbeat/beat" | ||
|
||
"github.com/elastic/beats/libbeat/kibana" | ||
|
||
errw "github.com/pkg/errors" | ||
|
||
"github.com/elastic/beats/libbeat/common" | ||
|
@@ -31,17 +36,17 @@ import ( | |
// ImportDashboards tries to import the kibana dashboards. | ||
func ImportDashboards( | ||
ctx context.Context, | ||
beatName, hostname, homePath string, | ||
beatInfo beat.Info, homePath string, | ||
kibanaConfig, dashboardsConfig *common.Config, | ||
msgOutputter MessageOutputter, | ||
msgOutputter MessageOutputter, migration bool, fields []byte, | ||
) error { | ||
if dashboardsConfig == nil || !dashboardsConfig.Enabled() { | ||
return nil | ||
} | ||
|
||
// unpack dashboard config | ||
dashConfig := defaultConfig | ||
dashConfig.Beat = beatName | ||
dashConfig.Beat = beatInfo.Beat | ||
dashConfig.Dir = filepath.Join(homePath, defaultDirectory) | ||
err := dashboardsConfig.Unpack(&dashConfig) | ||
if err != nil { | ||
|
@@ -57,14 +62,28 @@ func ImportDashboards( | |
return errors.New("kibana configuration missing for loading dashboards.") | ||
} | ||
|
||
return setupAndImportDashboardsViaKibana(ctx, hostname, kibanaConfig, &dashConfig, msgOutputter) | ||
// Generate index pattern | ||
version, _ := common.NewVersion("7.0.0") // TODO: dynamic version | ||
//indexP, _ := esConfig.String("index", 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @urso I think this also ties into your current work around indexing changes. Where should the config which index pattern should be used go for Kibana setup part? |
||
// TODO: What should we do about the index pattern. Kind of strange that ES configs are needed here. | ||
indexPattern, err := kibana.NewGenerator(beatInfo.Beat+"-*", beatInfo.Beat, fields, dashConfig.Dir, beatInfo.Version, *version, migration) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
pattern, err := indexPattern.Generate() | ||
if err != nil { | ||
log.Fatalf("ERROR: %s", err) | ||
} | ||
|
||
return setupAndImportDashboardsViaKibana(ctx, beatInfo.Hostname, kibanaConfig, &dashConfig, msgOutputter, pattern) | ||
|
||
} | ||
|
||
func setupAndImportDashboardsViaKibana(ctx context.Context, hostname string, kibanaConfig *common.Config, | ||
dashboardsConfig *Config, msgOutputter MessageOutputter) error { | ||
dashboardsConfig *Config, msgOutputter MessageOutputter, fields common.MapStr) error { | ||
|
||
kibanaLoader, err := NewKibanaLoader(ctx, kibanaConfig, dashboardsConfig, hostname, msgOutputter) | ||
kibanaLoader, err := NewKibanaLoader(ctx, kibanaConfig, dashboardsConfig, hostname, msgOutputter, fields) | ||
if err != nil { | ||
return fmt.Errorf("fail to create the Kibana loader: %v", err) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,18 +53,10 @@ type MessageOutputter func(msg string, a ...interface{}) | |
type Importer struct { | ||
cfg *Config | ||
version common.Version | ||
|
||
loader Loader | ||
} | ||
|
||
type Loader interface { | ||
ImportIndex(file string) error | ||
ImportDashboard(file string) error | ||
statusMsg(msg string, a ...interface{}) | ||
Close() error | ||
loader *KibanaLoader | ||
} | ||
|
||
func NewImporter(version common.Version, cfg *Config, loader Loader) (*Importer, error) { | ||
func NewImporter(version common.Version, cfg *Config, loader *KibanaLoader) (*Importer, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported function NewImporter should have comment or be unexported |
||
|
||
// Current max version is 7 | ||
if version.Major > 6 { | ||
|
@@ -106,7 +98,7 @@ func (imp Importer) ImportFile(fileType string, file string) error { | |
if fileType == "dashboard" { | ||
return imp.loader.ImportDashboard(file) | ||
} else if fileType == "index-pattern" { | ||
return imp.loader.ImportIndex(file) | ||
return imp.loader.ImportIndex() | ||
} | ||
return fmt.Errorf("Unexpected file type %s", fileType) | ||
} | ||
|
@@ -312,7 +304,7 @@ func (imp Importer) ImportKibanaDir(dir string) error { | |
|
||
check := []string{} | ||
if !imp.cfg.OnlyDashboards { | ||
check = append(check, "index-pattern") | ||
imp.loader.ImportIndex() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This breaks current behaviour in that it makes it impossible to load an index pattern from a file and requires the one from Beats. |
||
} | ||
wantDashboards := false | ||
if !imp.cfg.OnlyIndex { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,9 +38,10 @@ type KibanaLoader struct { | |
version common.Version | ||
hostname string | ||
msgOutputter MessageOutputter | ||
fields common.MapStr | ||
} | ||
|
||
func NewKibanaLoader(ctx context.Context, cfg *common.Config, dashboardsConfig *Config, hostname string, msgOutputter MessageOutputter) (*KibanaLoader, error) { | ||
func NewKibanaLoader(ctx context.Context, cfg *common.Config, dashboardsConfig *Config, hostname string, msgOutputter MessageOutputter, fields common.MapStr) (*KibanaLoader, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported function NewKibanaLoader should have comment or be unexported |
||
|
||
if cfg == nil || !cfg.Enabled() { | ||
return nil, fmt.Errorf("Kibana is not configured or enabled") | ||
|
@@ -57,6 +58,7 @@ func NewKibanaLoader(ctx context.Context, cfg *common.Config, dashboardsConfig * | |
version: client.GetVersion(), | ||
hostname: hostname, | ||
msgOutputter: msgOutputter, | ||
fields: fields, | ||
} | ||
|
||
version := client.GetVersion() | ||
|
@@ -81,24 +83,11 @@ func getKibanaClient(ctx context.Context, cfg *common.Config, retryCfg *Retry, r | |
return client, nil | ||
} | ||
|
||
func (loader KibanaLoader) ImportIndex(file string) error { | ||
func (loader KibanaLoader) ImportIndex() error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exported method KibanaLoader.ImportIndex should have comment or be unexported |
||
params := url.Values{} | ||
params.Set("force", "true") //overwrite the existing dashboards | ||
|
||
// read json file | ||
reader, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
return fmt.Errorf("fail to read index-pattern from file %s: %v", file, err) | ||
} | ||
|
||
var indexContent common.MapStr | ||
err = json.Unmarshal(reader, &indexContent) | ||
if err != nil { | ||
return fmt.Errorf("fail to unmarshal the index content from file %s: %v", file, err) | ||
} | ||
|
||
indexContent = ReplaceIndexInIndexPattern(loader.config.Index, indexContent) | ||
|
||
indexContent := ReplaceIndexInIndexPattern(loader.config.Index, loader.fields) | ||
return loader.client.ImportJSON(importAPI, params, indexContent) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taken over for 10451 as it was missing there.