Skip to content
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

[Generator] Fix metricbeat-style custom beats #13293

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions generator/metricbeat/{beat}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ setup: copy-vendor git-init
# Copy beats into vendor directory
.PHONY: copy-vendor
copy-vendor:
mkdir -p vendor/github.com/elastic
cp -R ${GOPATH}/src/github.com/elastic/beats vendor/github.com/elastic/
mkdir -p vendor/github.com/elastic/beats
git archive --remote ${BEAT_GOPATH}/src/github.com/elastic/beats HEAD | tar -x --exclude=x-pack -C vendor/github.com/elastic/beats
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

ln -sf ${PWD}/vendor/github.com/elastic/beats/metricbeat/scripts/generate_imports_helper.py ${PWD}/vendor/github.com/elastic/beats/script/generate_imports_helper.py
rm -rf vendor/github.com/elastic/beats/.git vendor/github.com/elastic/beats/x-pack
mkdir -p vendor/github.com/magefile
cp -R ${BEAT_GOPATH}/src/github.com/elastic/beats/vendor/github.com/magefile/mage vendor/github.com/magefile
cp -R ${BEAT_GOPATH}/src/github.com/elastic/beats/vendor/github.com/pkg vendor/github.com/

.PHONY: git-init
git-init:
Expand Down
4 changes: 4 additions & 0 deletions generator/metricbeat/{beat}/_meta/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{.BeatName}}.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good if metricbeat would use the same files, so we remember to maintain them. Maybe a way to do it is to generate a custom beat called "metricbeat", and check that these files are the same as the ones for the real Metricbeat.

But let's forget about this by now.

54 changes: 54 additions & 0 deletions generator/metricbeat/{beat}/_meta/reference.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
########################## {{.BeatName}} Configuration ###########################

# This file is a full configuration example documenting all non-deprecated
# options in comments. For a shorter configuration example, that contains only
# the most common options, please see metricbeat.yml in the same directory.
#
# You can find the full configuration reference here:
# https://www.elastic.co/guide/en/beats/metricbeat/index.html

#============================ Config Reloading ===============================

# Config reloading allows to dynamically load modules. Each file which is
# monitored must contain one or multiple modules as a list.
{{.BeatName}}.config.modules:

# Glob pattern for configuration reloading
path: ${path.config}/modules.d/*.yml

# Period on which files under path should be checked for changes
reload.period: 10s

# Set to true to enable config reloading
reload.enabled: false

# Maximum amount of time to randomly delay the start of a metricset. Use 0 to
# disable startup delay.
{{.BeatName}}.max_start_delay: 10s

#============================== Autodiscover ===================================

# Autodiscover allows you to detect changes in the system and spawn new modules
# as they happen.

#{{.BeatName}}.autodiscover:
# List of enabled autodiscover providers
# providers:
# - type: docker
# templates:
# - condition:
# equals.docker.container.image: etcd
# config:
# - module: etcd
# metricsets: ["leader", "self", "store"]
# period: 10s
# hosts: ["${host}:2379"]

#=========================== Timeseries instance ===============================

# Enabling this will add a `timeseries.instance` keyword field to all metric
# events. For a given metricset, this field will be unique for every single item
# being monitored.
# This setting is experimental.

#timeseries.enabled: false
19 changes: 19 additions & 0 deletions generator/metricbeat/{beat}/_meta/short.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

#========================== Modules configuration ============================

{{.BeatName}}.config.modules:
# Glob pattern for configuration loading
path: ${path.config}/modules.d/*.yml

# Set to true to enable config reloading
reload.enabled: false

# Period on which files under path should be checked for changes
#reload.period: 10s

#==================== Elasticsearch template setting ==========================

setup.template.settings:
index.number_of_shards: 1
index.codec: best_compression
#_source.enabled: false
48 changes: 48 additions & 0 deletions generator/metricbeat/{beat}/cmd/modules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package cmd

import (
"strings"

"github.com/pkg/errors"

"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/cfgfile"
"github.com/elastic/beats/libbeat/cmd"
)

// BuildModulesManager adds support for modules management to a beat
func BuildModulesManager(beat *beat.Beat) (cmd.ModulesManager, error) {
config := beat.BeatConfig

glob, err := config.String("config.modules.path", -1)
if err != nil {
return nil, errors.Errorf("modules management requires '%s.config.modules.path' setting", Name)
}

if !strings.HasSuffix(glob, "*.yml") {
return nil, errors.Errorf("wrong settings for config.modules.path, it is expected to end with *.yml. Got: %s", glob)
}

modulesManager, err := cfgfile.NewGlobManager(glob, ".yml", ".disabled")
if err != nil {
return nil, errors.Wrap(err, "initialization error")
}
return modulesManager, nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should move the one in github.com/elastic/beats/metricbeat/cmd to some reusable place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I can try that. Might not be a bad idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this. We'd need to separate out the ModulesManager and root code in metricbeat, since both the user beats and metricbeat share an init() that would fight with each other.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is what I meant with moving to some reusable place 😬 But ok, we can leave this for a future change dedicated only to this.

50 changes: 50 additions & 0 deletions generator/metricbeat/{beat}/cmd/root.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package cmd

import (
cmd "github.com/elastic/beats/libbeat/cmd"
"github.com/elastic/beats/libbeat/cmd/instance"
"github.com/elastic/beats/metricbeat/beater"
"github.com/elastic/beats/metricbeat/cmd/test"
"github.com/elastic/beats/metricbeat/mb/module"
)

// Name of this beat
var Name = "{beat}"

// RootCmd to handle beats cli
var RootCmd *cmd.BeatsRootCmd

var (
// Use a customized instance of Metricbeat where startup delay has
// been disabled to workaround the fact that Modules() will return
// the static modules (not the dynamic ones) with a start delay.
testModulesCreator = beater.Creator(
beater.WithModuleOptions(
module.WithMetricSetInfo(),
module.WithMaxStartDelay(0),
),
)
)

func init() {
RootCmd = cmd.GenRootCmdWithSettings(beater.DefaultCreator(), instance.Settings{Name: Name})
RootCmd.AddCommand(cmd.GenModulesCmd(Name, "", BuildModulesManager))
RootCmd.TestCmd.AddCommand(test.GenTestModulesCmd(Name, "", testModulesCreator))
}
8 changes: 7 additions & 1 deletion generator/metricbeat/{beat}/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,11 @@ func GoTestIntegration(ctx context.Context) error {

// Config generates both the short/reference/docker configs.
func Config() error {
return devtools.Config(devtools.AllConfigTypes, devtools.ConfigFileParams{}, ".")
customDeps := devtools.ConfigFileParams{
ShortParts: []string{"_meta/short.yml", devtools.LibbeatDir("_meta/config.yml.tmpl")},
ReferenceParts: []string{"_meta/reference.yml", devtools.LibbeatDir("_meta/config.reference.yml.tmpl")},
DockerParts: []string{"_meta/docker.yml", devtools.LibbeatDir("_meta/config.docker.yml")},
ExtraVars: map[string]interface{}{"BeatName": devtools.BeatName},
}
return devtools.Config(devtools.AllConfigTypes, customDeps, ".")
}
11 changes: 2 additions & 9 deletions generator/metricbeat/{beat}/main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,15 @@ package main
import (
"os"

"github.com/elastic/beats/libbeat/cmd/instance"
"github.com/elastic/beats/metricbeat/beater"

// Comment out the following line to exclude all official metricbeat modules and metricsets
_ "github.com/elastic/beats/metricbeat/include"
"{beat_path}/cmd"

// Make sure all your modules and metricsets are linked in this file
_ "{beat_path}/include"
)

var settings = instance.Settings{
Name: "{beat}",
}

func main() {
if err := instance.Run(settings, beater.DefaultCreator()); err != nil {
if err := cmd.RootCmd.Execute(); err != nil {
os.Exit(1)
}
}