Skip to content

Commit

Permalink
Merge branch 'main' into gcp-histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
ishleenk17 authored Dec 2, 2024
2 parents 6cf8d3d + 0eb63c1 commit aacbfb6
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- Bump aerospike-client-go to version v7.7.1 and add support for basic auth in Aerospike module {pull}41233[41233]
- Only watch metadata for ReplicaSets in metricbeat k8s module {pull}41289[41289]
- Add support for region/zone for Vertex AI service in GCP module {pull}41551[41551]
- Add support for location label as an optional configuration parameter in GCP metrics metricset. {issue}41550[41550] {pull}41626[41626]

*Metricbeat*

Expand Down
9 changes: 1 addition & 8 deletions metricbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ const (
Name = "metricbeat"
)

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

// withECSVersion is a modifier that adds ecs.version to events.
var withECSVersion = processing.WithFields(mapstr.M{
"ecs": mapstr.M{
Expand All @@ -60,7 +57,7 @@ func MetricbeatSettings(moduleNameSpace string) instance.Settings {
if moduleNameSpace == "" {
moduleNameSpace = "module"
}
var runFlags = pflag.NewFlagSet(Name, pflag.ExitOnError)
runFlags := pflag.NewFlagSet(Name, pflag.ExitOnError)
runFlags.AddGoFlag(flag.CommandLine.Lookup("system.hostfs"))
cfgfile.AddAllowedBackwardsCompatibleFlag("system.hostfs")
return instance.Settings{
Expand All @@ -82,7 +79,3 @@ func Initialize(settings instance.Settings) *cmd.BeatsRootCmd {
rootCmd.TestCmd.AddCommand(test.GenTestModulesCmd(Name, "", beater.DefaultTestModulesCreator()))
return rootCmd
}

func init() {
RootCmd = Initialize(MetricbeatSettings(""))
}
1 change: 1 addition & 0 deletions metricbeat/docs/modules/gcp.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ metricbeat.modules:
credentials_file_path: "your JSON credentials file path"
exclude_labels: false
period: 1m
location_label: "resource.labels.zone"
metrics:
- aligner: ALIGN_NONE
service: compute
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

func main() {
if err := cmd.RootCmd.Execute(); err != nil {
if err := cmd.Initialize(cmd.MetricbeatSettings("")).Execute(); err != nil {
os.Exit(1)
}
}
20 changes: 14 additions & 6 deletions metricbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,40 @@ package main

import (
"flag"
"os"
"testing"

"github.com/elastic/beats/v7/libbeat/cfgfile"
cmd "github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
"github.com/elastic/beats/v7/metricbeat/cmd"
mbcmd "github.com/elastic/beats/v7/metricbeat/cmd"
)

var systemTest *bool
var (
systemTest *bool
mbCommand *cmd.BeatsRootCmd
)

func init() {
testing.Init()
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
mbCommand = mbcmd.Initialize(mbcmd.MetricbeatSettings(""))
mbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
mbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile")
}

// Test started when the test binary is started. Only calls main.
func TestSystem(t *testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
main()
if err := mbCommand.Execute(); err != nil {
os.Exit(1)
}
}
}

func TestTemplate(t *testing.T) {
template.TestTemplate(t, cmd.Name, false)
template.TestTemplate(t, mbCommand.Name(), false)
}
2 changes: 1 addition & 1 deletion x-pack/agentbeat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ into a single agentbeat binary.`,
prepareCommand(auditbeat.RootCmd),
prepareCommand(filebeat.Filebeat()),
prepareCommand(heartbeat.RootCmd),
prepareCommand(metricbeat.RootCmd),
prepareCommand(metricbeat.Initialize()),
prepareCommand(osquerybeat.RootCmd),
prepareCommand(packetbeat.RootCmd),
)
Expand Down
7 changes: 4 additions & 3 deletions x-pack/libbeat/management/tests/mbtest/metricbeat_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ var expectedMBStreams = &proto.UnitExpectedConfig{
}

func TestSingleMetricbeatMetricsetWithProcessors(t *testing.T) {
tests.InitBeatsForTest(t, cmd.RootCmd)
var mbStreams = []*proto.Stream{
mbCmd := cmd.Initialize()
tests.InitBeatsForTest(t, mbCmd)
mbStreams := []*proto.Stream{
{
Id: "system/metrics-system.cpu-default-system",
DataStream: &proto.DataStream{
Expand Down Expand Up @@ -79,7 +80,7 @@ func TestSingleMetricbeatMetricsetWithProcessors(t *testing.T) {

go func() {
t.Logf("Running beats...")
err := cmd.RootCmd.Execute()
err := mbCmd.Execute()
require.NoError(t, err)
}()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func TestProcessStatusReporter(t *testing.T) {
unitOutID := mock.NewID()
token := mock.NewID()

tests.InitBeatsForTest(t, cmd.RootCmd)
mbCmd := cmd.Initialize()
tests.InitBeatsForTest(t, mbCmd)

filename := fmt.Sprintf("test-%d", time.Now().Unix())
outPath := filepath.Join(t.TempDir(), filename)
Expand Down Expand Up @@ -122,7 +123,7 @@ func TestProcessStatusReporter(t *testing.T) {

go func() {
t.Logf("Running beats...")
err := cmd.RootCmd.Execute()
err := mbCmd.Execute()
require.NoError(t, err)
}()

Expand Down
14 changes: 6 additions & 8 deletions x-pack/metricbeat/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,28 @@ const (
Name = "metricbeat"
)

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

// withECSVersion is a modifier that adds ecs.version to events.
var withECSVersion = processing.WithFields(mapstr.M{
"ecs": mapstr.M{
"version": ecs.Version,
},
})

func init() {
func Initialize() *cmd.BeatsRootCmd {
globalProcs, err := processors.NewPluginConfigFromList(defaultProcessors())
if err != nil { // these are hard-coded, shouldn't fail
panic(fmt.Errorf("error creating global processors: %w", err))
}
settings := mbcmd.MetricbeatSettings("")
settings.ElasticLicensed = true
settings.Processing = processing.MakeDefaultSupport(true, globalProcs, withECSVersion, processing.WithHost, processing.WithAgentMeta())
RootCmd = cmd.GenRootCmdWithSettings(beater.DefaultCreator(), settings)
RootCmd.AddCommand(cmd.GenModulesCmd(Name, "", mbcmd.BuildModulesManager))
RootCmd.TestCmd.AddCommand(test.GenTestModulesCmd(Name, "", beater.DefaultTestModulesCreator()))
RootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
rootCmd := cmd.GenRootCmdWithSettings(beater.DefaultCreator(), settings)
rootCmd.AddCommand(cmd.GenModulesCmd(Name, "", mbcmd.BuildModulesManager))
rootCmd.TestCmd.AddCommand(test.GenTestModulesCmd(Name, "", beater.DefaultTestModulesCreator()))
rootCmd.PersistentPreRun = func(cmd *cobra.Command, args []string) {
management.ConfigTransform.SetTransform(metricbeatCfg)
}
return rootCmd
}

func defaultProcessors() []mapstr.M {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/metricbeat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

func main() {
if err := cmd.RootCmd.Execute(); err != nil {
if err := cmd.Initialize().Execute(); err != nil {
os.Exit(1)
}
}
20 changes: 14 additions & 6 deletions x-pack/metricbeat/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,40 @@ package main
// This file is mandatory as otherwise the metricbeat.test binary is not generated correctly.
import (
"flag"
"os"
"testing"

"github.com/elastic/beats/v7/libbeat/cfgfile"
cmd "github.com/elastic/beats/v7/libbeat/cmd"
"github.com/elastic/beats/v7/libbeat/tests/system/template"
"github.com/elastic/beats/v7/x-pack/metricbeat/cmd"
mbcmd "github.com/elastic/beats/v7/x-pack/metricbeat/cmd"
)

var systemTest *bool
var (
systemTest *bool
mbCommand *cmd.BeatsRootCmd
)

func init() {
testing.Init()
systemTest = flag.Bool("systemTest", false, "Set to true when running system tests")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
mbCommand = mbcmd.Initialize()
mbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("systemTest"))
cfgfile.AddAllowedBackwardsCompatibleFlag("systemTest")
cmd.RootCmd.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
mbCommand.PersistentFlags().AddGoFlag(flag.CommandLine.Lookup("test.coverprofile"))
cfgfile.AddAllowedBackwardsCompatibleFlag("test.coverprofile")
}

// Test started when the test binary is started. Only calls main.
func TestSystem(t *testing.T) {
cfgfile.ConvertFlagsForBackwardsCompatibility()
if *systemTest {
main()
if err := mbCommand.Execute(); err != nil {
os.Exit(1)
}
}
}

func TestTemplate(t *testing.T) {
template.TestTemplate(t, cmd.Name, true)
template.TestTemplate(t, mbCommand.Name(), true)
}
1 change: 1 addition & 0 deletions x-pack/metricbeat/metricbeat.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,7 @@ metricbeat.modules:
credentials_file_path: "your JSON credentials file path"
exclude_labels: false
period: 1m
location_label: "resource.labels.zone"
metrics:
- aligner: ALIGN_NONE
service: compute
Expand Down
1 change: 1 addition & 0 deletions x-pack/metricbeat/module/gcp/_meta/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
credentials_file_path: "your JSON credentials file path"
exclude_labels: false
period: 1m
location_label: "resource.labels.zone"
metrics:
- aligner: ALIGN_NONE
service: compute
Expand Down
27 changes: 27 additions & 0 deletions x-pack/metricbeat/module/gcp/metrics/_meta/docs.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ services under "Google Cloud metrics", but does not work for other services
(`kubernetes` aka GKE for example).
This option allow to override the default and specify an arbitrary metric prefix.

* *location_label*: Use this option to specify the resource label that identifies the location (such as zone or region) for a Google Cloud service when filtering metrics. For example, labels like `resource.label.location` or `resource.label.zone` are used by Google Cloud to represent the region or zone of a resource. This is an optional configuration for the user.

[float]
=== Example Configuration
* `metrics` metricset is enabled to collect metrics from all zones under
Expand Down Expand Up @@ -134,3 +136,28 @@ metric prefix, as for GKE metrics the required prefix is `kubernetes.io/`
metric_types:
- "container/cpu/core_usage_time"
----

* `metrics` metricset is enabled to collect metrics from region
`us-east4` in `elastic-observability` project. The metric, number of replicas of the prediction model is
collected from a new GCP service `aiplatform`. Since its a new service which is not supported by
default in this metricset, the user provides the servicelabel (resource.label.location), for which
user wants to filter the incoming data

+
[source,yaml]
----
- module: gcp
metricsets:
- metrics
project_id: "elastic-observability"
credentials_json: "your JSON credentials"
exclude_labels: false
period: 1m
location_label: "resource.label.location" # This is an optional configuration
regions:
- us-east4
metrics:
- service: aiplatform
metric_types:
- "prediction/online/replicas"
----
12 changes: 11 additions & 1 deletion x-pack/metricbeat/module/gcp/metrics/metrics_requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (r *metricsRequester) getFilterForMetric(serviceName, m string) string {
// NOTE: some GCP services are global, not regional or zonal. To these services we don't need
// to apply any additional filters.
if locationsConfigsAvailable && !isAGlobalService(serviceName) {
serviceLabel := getServiceLabelFor(serviceName)
serviceLabel := r.getServiceLabel(serviceName)
f = r.buildLocationFilter(serviceLabel, f)
}

Expand All @@ -261,6 +261,16 @@ func (r *metricsRequester) getFilterForMetric(serviceName, m string) string {
return f
}

// getServiceLabel determines the service label to be used for the given service name. If a custom
// location label is configured, it will be used. Otherwise, the default service label for the
// given service name will be returned.
func (r *metricsRequester) getServiceLabel(serviceName string) string {
if r.config.LocationLabel != "" {
return r.config.LocationLabel
}
return getServiceLabelFor(serviceName)
}

// Returns a GCP TimeInterval based on the ingestDelay and samplePeriod from ListMetricDescriptor
func getTimeIntervalAligner(ingestDelay time.Duration, samplePeriod time.Duration, collectionPeriod *durationpb.Duration, inputAligner string) (*monitoringpb.TimeInterval, string) {
var startTime, endTime, currentTime time.Time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ func TestGetFilterForMetric(t *testing.T) {
metricsRequester{config: config{Region: "foobar", Regions: []string{"foo", "bar"}}, logger: logger},
"metric.type=\"dummy\" AND resource.labels.zone = starts_with(\"foobar\")",
},
{
"aiplatform service with configured region and zone",
"aiplatform",
"",
metricsRequester{config: config{Region: "foo", Zone: "bar", LocationLabel: "resource.label.location"}, logger: logger},
"metric.type=\"dummy\" AND resource.label.location = starts_with(\"foo\")",
},
}

for _, c := range cases {
Expand Down
1 change: 1 addition & 0 deletions x-pack/metricbeat/module/gcp/metrics/metricset.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type config struct {
Zone string `config:"zone"`
Region string `config:"region"`
Regions []string `config:"regions"`
LocationLabel string `config:"location_label"`
ProjectID string `config:"project_id" validate:"required"`
ExcludeLabels bool `config:"exclude_labels"`
CredentialsFilePath string `config:"credentials_file_path"`
Expand Down
1 change: 1 addition & 0 deletions x-pack/metricbeat/modules.d/gcp.yml.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
credentials_file_path: "your JSON credentials file path"
exclude_labels: false
period: 1m
location_label: "resource.labels.zone"
metrics:
- aligner: ALIGN_NONE
service: compute
Expand Down

0 comments on commit aacbfb6

Please sign in to comment.