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

[Metricbeat] support credentials_json in gcp module #29584

Merged
merged 5 commits into from
Jan 26, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ The list below covers the major changes between 7.0.0-rc2 and master only.
- Introduce `libbeat/beat.Beat.OutputConfigReloader` {pull}28048[28048]
- Update Go version to 1.17.1. {pull}27543[27543]
- Whitelist `GCP_*` environment variables in dev tools {pull}28364[28364]
- Add support for `credentials_json` in `gcp` module, all metricsets {pull}29584[29584]

==== Deprecated

Expand Down
22 changes: 19 additions & 3 deletions x-pack/metricbeat/module/gcp/billing/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -49,14 +50,19 @@ type MetricSet struct {
type config struct {
Period time.Duration `config:"period" validate:"required"`
ProjectID string `config:"project_id" validate:"required"`
CredentialsFilePath string `config:"credentials_file_path" validate:"required"`
CredentialsFilePath string `config:"credentials_file_path"`
CredentialsJSON string `config:"credentials_json"`
DatasetID string `config:"dataset_id" validate:"required"`
TablePattern string `config:"table_pattern"`
CostType string `config:"cost_type"`
}

// Validate checks for deprecated config options
func (c config) Validate() error {
if c.CredentialsFilePath == "" && c.CredentialsJSON == "" {
return errors.New("no credentials_file_path or credentials_json specified")
}

if c.CostType != "" {
// cost_type can only be regular, tax, adjustment, or rounding error
costTypes := []string{"regular", "tax", "adjustment", "rounding error"}
Expand Down Expand Up @@ -106,7 +112,17 @@ func (m *MetricSet) Fetch(ctx context.Context, reporter mb.ReporterV2) (err erro
// find current month
month := getCurrentMonth()

opt := []option.ClientOption{option.WithCredentialsFile(m.config.CredentialsFilePath)}
var opt []option.ClientOption
if m.config.CredentialsFilePath != "" && m.config.CredentialsJSON != "" {
return errors.New("both credentials_file_path and credentials_json specified, you must use only one of them")
} else if m.config.CredentialsFilePath != "" {
opt = []option.ClientOption{option.WithCredentialsFile(m.config.CredentialsFilePath)}
} else if m.config.CredentialsJSON != "" {
opt = []option.ClientOption{option.WithCredentialsJSON([]byte(m.config.CredentialsJSON))}
} else {
return errors.New("no credentials_file_path or credentials_json specified")
}

client, err := bigquery.NewClient(ctx, m.config.ProjectID, opt...)
if err != nil {
return fmt.Errorf("gerror creating bigquery client: %w", err)
Expand Down Expand Up @@ -286,7 +302,7 @@ func getCurrentDate() string {
}

func generateEventID(currentDate string, rowItems []bigquery.Value) string {
// create eventID using hash of current_date + invoice_month + project_id + cost_type
// create eventID using hash of current_date + invoice.month + project.id + project.name
// This will prevent more than one billing metric getting collected in the same day.
eventID := currentDate + rowItems[0].(string) + rowItems[1].(string) + rowItems[2].(string)
h := sha256.New()
Expand Down
13 changes: 12 additions & 1 deletion x-pack/metricbeat/module/gcp/metrics/metricset.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type config struct {
ProjectID string `config:"project_id" validate:"required"`
ExcludeLabels bool `config:"exclude_labels"`
CredentialsFilePath string `config:"credentials_file_path"`
CredentialsJSON string `config:"credentials_json"`

opt []option.ClientOption
period *duration.Duration
Expand All @@ -129,7 +130,17 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
}

m.MetricsConfig = metricsConfigs.Metrics
m.config.opt = []option.ClientOption{option.WithCredentialsFile(m.config.CredentialsFilePath)}

if m.config.CredentialsFilePath != "" && m.config.CredentialsJSON != "" {
return m, errors.New("both credentials_file_path and credentials_json specified, you must use only one of them")
} else if m.config.CredentialsFilePath != "" {
m.config.opt = []option.ClientOption{option.WithCredentialsFile(m.config.CredentialsFilePath)}
} else if m.config.CredentialsJSON != "" {
m.config.opt = []option.ClientOption{option.WithCredentialsJSON([]byte(m.config.CredentialsJSON))}
} else {
return m, errors.New("no credentials_file_path or credentials_json specified")
}

m.config.period = &duration.Duration{
Seconds: int64(m.Module().Config().Period.Seconds()),
}
Expand Down