Skip to content
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
15 changes: 15 additions & 0 deletions collectors/monitoring_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ type MetricFilter struct {
FilterQuery string
}

func ParseMetricExtraFilters(raw []string) []MetricFilter {
out := make([]MetricFilter, 0, len(raw))
for _, entry := range raw {
prefix, filter := utils.SplitExtraFilter(entry, ":")
if prefix == "" {
continue
}
out = append(out, MetricFilter{
TargetedMetricPrefix: strings.ToLower(prefix),
FilterQuery: filter,
})
}
return out
}

type MonitoringCollector struct {
projectID string
metricsTypePrefixes []string
Expand Down
29 changes: 28 additions & 1 deletion collectors/monitoring_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

package collectors

import "testing"
import (
"reflect"
"testing"
)

func TestIsGoogleMetric(t *testing.T) {
good := []string{
Expand All @@ -37,3 +40,27 @@ func TestIsGoogleMetric(t *testing.T) {
}
}
}

func TestParseMetricExtraFilters(t *testing.T) {
input := []string{
"pubsub.googleapis.com/subscription:resource.labels.subscription_id=monitoring.regex.full_match(\"my-subs-prefix.*\")",
"missing-separator",
"compute.googleapis.com/instance:metric.labels.instance_name=\"example:vm\"",
}

got := ParseMetricExtraFilters(input)
want := []MetricFilter{
{
TargetedMetricPrefix: "pubsub.googleapis.com/subscription",
FilterQuery: "resource.labels.subscription_id=monitoring.regex.full_match(\"my-subs-prefix.*\")",
},
{
TargetedMetricPrefix: "compute.googleapis.com/instance",
FilterQuery: "metric.labels.instance_name=\"example:vm\"",
},
}

if !reflect.DeepEqual(got, want) {
t.Fatalf("ParseMetricExtraFilters() = %#v, want %#v", got, want)
}
}
172 changes: 172 additions & 0 deletions otelcollector/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Copyright 2020 The Prometheus Authors
// Licensed 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 otelcollector

import (
"fmt"
"net/http"
"time"

prombridge "github.com/ArthurSens/prometheus-collector-bridge"
)

// Config maps stackdriver_exporter runtime settings into exporter_config.
type Config struct {
Comment on lines +24 to +25
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would love to find a way that both sides shared this configuration. This way if we add a new CLI flag someone is forced to add it here and same with someone adding a config field to the OTel config.

ProjectIDs []string `mapstructure:"project_ids"`
ProjectsFilter string `mapstructure:"projects_filter"`
UniverseDomain string `mapstructure:"universe_domain"`
MaxRetries int `mapstructure:"max_retries"`
HTTPTimeout string `mapstructure:"http_timeout"`
MaxBackoff string `mapstructure:"max_backoff"`
BackoffJitter string `mapstructure:"backoff_jitter"`
RetryStatuses []int `mapstructure:"retry_statuses"`
MetricsPrefixes []string `mapstructure:"metrics_prefixes"`
MetricsInterval string `mapstructure:"metrics_interval"`
MetricsOffset string `mapstructure:"metrics_offset"`
MetricsIngest bool `mapstructure:"metrics_ingest_delay"`
FillMissing bool `mapstructure:"fill_missing_labels"`
DropDelegated bool `mapstructure:"drop_delegated_projects"`
Filters []string `mapstructure:"filters"`
AggregateDeltas bool `mapstructure:"aggregate_deltas"`
DeltasTTL string `mapstructure:"aggregate_deltas_ttl"`
DescriptorTTL string `mapstructure:"descriptor_cache_ttl"`
DescriptorGoogleOnly bool `mapstructure:"descriptor_cache_only_google"`
}

var _ prombridge.Config = (*Config)(nil)

func defaultConfig() *Config {
return &Config{
UniverseDomain: "googleapis.com",
MaxRetries: 0,
HTTPTimeout: "10s",
MaxBackoff: "5s",
BackoffJitter: "1s",
RetryStatuses: []int{503},
MetricsInterval: "5m",
MetricsOffset: "0s",
MetricsIngest: false,
FillMissing: true,
DropDelegated: false,
AggregateDeltas: false,
DeltasTTL: "30m",
DescriptorTTL: "0s",
DescriptorGoogleOnly: true,
}
}

func defaultComponentDefaults() map[string]interface{} {
cfg := defaultConfig()
return map[string]interface{}{
"max_retries": cfg.MaxRetries,
"http_timeout": cfg.HTTPTimeout,
"max_backoff": cfg.MaxBackoff,
"backoff_jitter": cfg.BackoffJitter,
"retry_statuses": cfg.RetryStatuses,
"universe_domain": cfg.UniverseDomain,
"metrics_interval": cfg.MetricsInterval,
"metrics_offset": cfg.MetricsOffset,
"metrics_ingest_delay": cfg.MetricsIngest,
"fill_missing_labels": cfg.FillMissing,
"drop_delegated_projects": cfg.DropDelegated,
"aggregate_deltas": cfg.AggregateDeltas,
"aggregate_deltas_ttl": cfg.DeltasTTL,
"descriptor_cache_ttl": cfg.DescriptorTTL,
"descriptor_cache_only_google": cfg.DescriptorGoogleOnly,
}
}

func (c *Config) Validate() error {
if len(c.MetricsPrefixes) == 0 {
return fmt.Errorf("metrics_prefixes must have at least one entry")
}

_, err := c.parsedDurations()
if err != nil {
return err
}

for _, code := range c.RetryStatuses {
if code < http.StatusContinue || code > 599 {
return fmt.Errorf("retry status %d is not a valid HTTP status code", code)
}
}

return nil
}

type parsedConfig struct {
HTTPTimeout time.Duration
MaxBackoff time.Duration
BackoffJitter time.Duration
MetricsInterval time.Duration
MetricsOffset time.Duration
DeltasTTL time.Duration
DescriptorTTL time.Duration
}

func (c *Config) parsedDurations() (parsedConfig, error) {
parse := func(name, raw string) (time.Duration, error) {
d, err := time.ParseDuration(raw)
if err != nil {
return 0, fmt.Errorf("%s: invalid duration %q: %w", name, raw, err)
}
return d, nil
}

httpTimeout, err := parse("http_timeout", c.HTTPTimeout)
if err != nil {
return parsedConfig{}, err
}
maxBackoff, err := parse("max_backoff", c.MaxBackoff)
if err != nil {
return parsedConfig{}, err
}
backoffJitter, err := parse("backoff_jitter", c.BackoffJitter)
if err != nil {
return parsedConfig{}, err
}
metricsInterval, err := parse("metrics_interval", c.MetricsInterval)
if err != nil {
return parsedConfig{}, err
}
metricsOffset, err := parse("metrics_offset", c.MetricsOffset)
if err != nil {
return parsedConfig{}, err
}
deltasTTL, err := parse("aggregate_deltas_ttl", c.DeltasTTL)
if err != nil {
return parsedConfig{}, err
}
descriptorTTL, err := parse("descriptor_cache_ttl", c.DescriptorTTL)
if err != nil {
return parsedConfig{}, err
}

return parsedConfig{
HTTPTimeout: httpTimeout,
MaxBackoff: maxBackoff,
BackoffJitter: backoffJitter,
MetricsInterval: metricsInterval,
MetricsOffset: metricsOffset,
DeltasTTL: deltasTTL,
DescriptorTTL: descriptorTTL,
}, nil
}

type configUnmarshaler struct{}

func (configUnmarshaler) GetConfigStruct() prombridge.Config {
return defaultConfig()
}
Loading
Loading