Skip to content
Open
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
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 {
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()
}
166 changes: 166 additions & 0 deletions otelcollector/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// 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 (
"testing"
"time"
)

func TestConfig_Validate(t *testing.T) {
t.Parallel()

tests := []struct {
name string
cfg Config
wantErr bool
}{
{
name: "valid with project IDs",
cfg: Config{
ProjectIDs: []string{"my-project"},
MetricsPrefixes: []string{"compute.googleapis.com/instance"},
HTTPTimeout: "10s",
MaxBackoff: "5s",
BackoffJitter: "1s",
MetricsInterval: "5m",
MetricsOffset: "0s",
DeltasTTL: "30m",
DescriptorTTL: "0s",
},
},
{
name: "valid with projects filter",
cfg: Config{
ProjectsFilter: "parent.type:folder parent.id:123",
MetricsPrefixes: []string{"pubsub.googleapis.com/subscription"},
HTTPTimeout: "10s",
MaxBackoff: "5s",
BackoffJitter: "1s",
MetricsInterval: "5m",
MetricsOffset: "0s",
DeltasTTL: "30m",
DescriptorTTL: "0s",
},
},
{
name: "valid with implicit default project discovery",
cfg: Config{
MetricsPrefixes: []string{"compute.googleapis.com/instance"},
HTTPTimeout: "10s",
MaxBackoff: "5s",
BackoffJitter: "1s",
MetricsInterval: "5m",
MetricsOffset: "0s",
DeltasTTL: "30m",
DescriptorTTL: "0s",
},
},
{
name: "invalid without metrics prefixes",
cfg: Config{
ProjectIDs: []string{"my-project"},
HTTPTimeout: "10s",
MaxBackoff: "5s",
BackoffJitter: "1s",
},
wantErr: true,
},
{
name: "invalid timeout",
cfg: Config{
ProjectIDs: []string{"my-project"},
MetricsPrefixes: []string{"compute.googleapis.com/instance"},
HTTPTimeout: "not-a-duration",
MaxBackoff: "5s",
BackoffJitter: "1s",
MetricsInterval: "5m",
MetricsOffset: "0s",
DeltasTTL: "30m",
DescriptorTTL: "0s",
},
wantErr: true,
},
{
name: "invalid retry status",
cfg: Config{
ProjectIDs: []string{"my-project"},
MetricsPrefixes: []string{"compute.googleapis.com/instance"},
HTTPTimeout: "10s",
MaxBackoff: "5s",
BackoffJitter: "1s",
MetricsInterval: "5m",
MetricsOffset: "0s",
DeltasTTL: "30m",
DescriptorTTL: "0s",
RetryStatuses: []int{99},
},
wantErr: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := tt.cfg.Validate()
if (err != nil) != tt.wantErr {
t.Fatalf("Validate() error = %v, wantErr = %v", err, tt.wantErr)
}
})
}
}

func TestConfig_Durations(t *testing.T) {
t.Parallel()

cfg := Config{
ProjectIDs: []string{"my-project"},
MetricsPrefixes: []string{"compute.googleapis.com/instance"},
HTTPTimeout: "10s",
MaxBackoff: "5s",
BackoffJitter: "1s",
MetricsInterval: "5m",
MetricsOffset: "2m",
DeltasTTL: "30m",
DescriptorTTL: "0s",
}

parsed, err := cfg.parsedDurations()
if err != nil {
t.Fatalf("parsedDurations() error = %v", err)
}

if parsed.HTTPTimeout != 10*time.Second {
t.Fatalf("HTTPTimeout = %v, want %v", parsed.HTTPTimeout, 10*time.Second)
}
if parsed.MaxBackoff != 5*time.Second {
t.Fatalf("MaxBackoff = %v, want %v", parsed.MaxBackoff, 5*time.Second)
}
if parsed.BackoffJitter != 1*time.Second {
t.Fatalf("BackoffJitter = %v, want %v", parsed.BackoffJitter, 1*time.Second)
}
if parsed.MetricsInterval != 5*time.Minute {
t.Fatalf("MetricsInterval = %v, want %v", parsed.MetricsInterval, 5*time.Minute)
}
if parsed.MetricsOffset != 2*time.Minute {
t.Fatalf("MetricsOffset = %v, want %v", parsed.MetricsOffset, 2*time.Minute)
}
if parsed.DeltasTTL != 30*time.Minute {
t.Fatalf("DeltasTTL = %v, want %v", parsed.DeltasTTL, 30*time.Minute)
}
if parsed.DescriptorTTL != 0 {
t.Fatalf("DescriptorTTL = %v, want %v", parsed.DescriptorTTL, 0*time.Second)
}
}
39 changes: 39 additions & 0 deletions otelcollector/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 (
"log/slog"

prombridge "github.com/ArthurSens/prometheus-collector-bridge"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/receiver"
)

var receiverType = component.MustNewType("stackdriver_exporter")

func NewFactory() receiver.Factory {
return prombridge.NewFactory(
receiverType,
newLifecycleManager(slog.Default()),
configUnmarshaler{},
prombridge.WithComponentDefaults(defaultComponentDefaults()),
)
}

// Keep compiler checks close to factory wiring.
var (
_ prombridge.ExporterLifecycleManager = (*lifecycleManager)(nil)
_ prombridge.ConfigUnmarshaler = (configUnmarshaler{})
)
Loading