Skip to content

Commit

Permalink
Do not use gotmpl for most of oconf and little cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Apr 17, 2024
1 parent 4b5f95f commit 6252eb4
Show file tree
Hide file tree
Showing 16 changed files with 217 additions and 2,004 deletions.
4 changes: 0 additions & 4 deletions exporters/otlp/otlpmetric/otlpmetricgrpc/internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ package internal // import "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/o
//go:generate gotmpl --body=../../../../../internal/shared/otlp/envconfig/envconfig.go.tmpl "--data={}" --out=envconfig/envconfig.go
//go:generate gotmpl --body=../../../../../internal/shared/otlp/envconfig/envconfig_test.go.tmpl "--data={}" --out=envconfig/envconfig_test.go

//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/envconfig.go.tmpl "--data={\"envconfigImportPath\": \"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/envconfig\"}" --out=oconf/envconfig.go
//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl "--data={}" --out=oconf/envconfig_test.go
//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/options.go.tmpl "--data={\"retryImportPath\": \"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/retry\"}" --out=oconf/options.go
//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/options_test.go.tmpl "--data={\"envconfigImportPath\": \"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc/internal/envconfig\"}" --out=oconf/options_test.go
//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/optiontypes.go.tmpl "--data={}" --out=oconf/optiontypes.go
//go:generate gotmpl --body=../../../../../internal/shared/otlp/otlpmetric/oconf/tls.go.tmpl "--data={}" --out=oconf/tls.go

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/otlp/otlpmetric/oconf/envconfig.go.tmpl

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -37,46 +34,18 @@ func ApplyGRPCEnvConfigs(cfg Config) Config {
return cfg
}

// ApplyHTTPEnvConfigs applies the env configurations for HTTP.
func ApplyHTTPEnvConfigs(cfg Config) Config {
opts := getOptionsFromEnv()
for _, opt := range opts {
cfg = opt.ApplyHTTPOption(cfg)
}
return cfg
}

func getOptionsFromEnv() []GenericOption {
opts := []GenericOption{}
func getOptionsFromEnv() []GRPCOption {
opts := []GRPCOption{}

tlsConf := &tls.Config{}
DefaultEnvOptionsReader.Apply(
envconfig.WithURL("ENDPOINT", func(u *url.URL) {
opts = append(opts, withEndpointScheme(u))
opts = append(opts, newSplitOption(func(cfg Config) Config {
cfg.Metrics.Endpoint = u.Host
// For OTLP/HTTP endpoint URLs without a per-signal
// configuration, the passed endpoint is used as a base URL
// and the signals are sent to these paths relative to that.
cfg.Metrics.URLPath = path.Join(u.Path, DefaultMetricsPath)
return cfg
}, withEndpointForGRPC(u)))
opts = append(opts, NewGRPCOption(withEndpointForGRPC(u)))
}),
envconfig.WithURL("METRICS_ENDPOINT", func(u *url.URL) {
opts = append(opts, withEndpointScheme(u))
opts = append(opts, newSplitOption(func(cfg Config) Config {
cfg.Metrics.Endpoint = u.Host
// For endpoint URLs for OTLP/HTTP per-signal variables, the
// URL MUST be used as-is without any modification. The only
// exception is that if an URL contains no path part, the root
// path / MUST be used.
path := u.Path
if path == "" {
path = "/"
}
cfg.Metrics.URLPath = path
return cfg
}, withEndpointForGRPC(u)))
opts = append(opts, NewGRPCOption(withEndpointForGRPC(u)))
}),
envconfig.WithCertPool("CERTIFICATE", func(p *x509.CertPool) { tlsConf.RootCAs = p }),
envconfig.WithCertPool("METRICS_CERTIFICATE", func(p *x509.CertPool) { tlsConf.RootCAs = p }),
Expand Down Expand Up @@ -121,7 +90,7 @@ func WithEnvCompression(n string, fn func(Compression)) func(e *envconfig.EnvOpt
}
}

func withEndpointScheme(u *url.URL) GenericOption {
func withEndpointScheme(u *url.URL) GRPCOption {
switch strings.ToLower(u.Scheme) {
case "http", "unix":
return WithInsecure()
Expand All @@ -131,7 +100,7 @@ func withEndpointScheme(u *url.URL) GenericOption {
}

// revive:disable-next-line:flag-parameter
func withInsecure(b bool) GenericOption {
func withInsecure(b bool) GRPCOption {

Check warning on line 103 in exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/envconfig.go

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/envconfig.go#L103

Added line #L103 was not covered by tests
if b {
return WithInsecure()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/otlp/otlpmetric/oconf/envconfig_test.go.tmpl

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

Expand Down
177 changes: 35 additions & 142 deletions exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/otlp/otlpmetric/oconf/options.go.tmpl

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

Expand Down Expand Up @@ -47,6 +44,7 @@ type (
// This type is compatible with `http.Transport.Proxy` and can be used to set a custom proxy function to the OTLP HTTP client.
HTTPTransportProxyFunc func(*http.Request) (*url.URL, error)

// SignalConfig represents signal specific configuration.
SignalConfig struct {
Endpoint string
Insecure bool
Expand All @@ -65,8 +63,8 @@ type (
Proxy HTTPTransportProxyFunc
}

// Config represents exporter configuration.
Config struct {
// Signal specific configurations
Metrics SignalConfig

RetryConfig retry.Config
Expand All @@ -79,29 +77,6 @@ type (
}
)

// NewHTTPConfig returns a new Config with all settings applied from opts and
// any unset setting using the default HTTP config values.
func NewHTTPConfig(opts ...HTTPOption) Config {
cfg := Config{
Metrics: SignalConfig{
Endpoint: fmt.Sprintf("%s:%d", DefaultCollectorHost, DefaultCollectorHTTPPort),
URLPath: DefaultMetricsPath,
Compression: NoCompression,
Timeout: DefaultTimeout,

TemporalitySelector: metric.DefaultTemporalitySelector,
AggregationSelector: metric.DefaultAggregationSelector,
},
RetryConfig: retry.DefaultConfig,
}
cfg = ApplyHTTPEnvConfigs(cfg)
for _, opt := range opts {
cfg = opt.ApplyHTTPOption(cfg)
}
cfg.Metrics.URLPath = cleanPath(cfg.Metrics.URLPath, DefaultMetricsPath)
return cfg
}

// cleanPath returns a path with all spaces trimmed and all redundancies
// removed. If urlPath is empty or cleaning it results in an empty string,
// defaultPath is returned instead.
Expand Down Expand Up @@ -164,93 +139,14 @@ func NewGRPCConfig(opts ...GRPCOption) Config {
return cfg
}

type (
// GenericOption applies an option to the HTTP or gRPC driver.
GenericOption interface {
ApplyHTTPOption(Config) Config
ApplyGRPCOption(Config) Config

// A private method to prevent users implementing the
// interface and so future additions to it will not
// violate compatibility.
private()
}

// HTTPOption applies an option to the HTTP driver.
HTTPOption interface {
ApplyHTTPOption(Config) Config
// GRPCOption applies an option to the gRPC driver.
type GRPCOption interface {
ApplyGRPCOption(Config) Config

// A private method to prevent users implementing the
// interface and so future additions to it will not
// violate compatibility.
private()
}

// GRPCOption applies an option to the gRPC driver.
GRPCOption interface {
ApplyGRPCOption(Config) Config

// A private method to prevent users implementing the
// interface and so future additions to it will not
// violate compatibility.
private()
}
)

// genericOption is an option that applies the same logic
// for both gRPC and HTTP.
type genericOption struct {
fn func(Config) Config
}

func (g *genericOption) ApplyGRPCOption(cfg Config) Config {
return g.fn(cfg)
}

func (g *genericOption) ApplyHTTPOption(cfg Config) Config {
return g.fn(cfg)
}

func (genericOption) private() {}

func newGenericOption(fn func(cfg Config) Config) GenericOption {
return &genericOption{fn: fn}
}

// splitOption is an option that applies different logics
// for gRPC and HTTP.
type splitOption struct {
httpFn func(Config) Config
grpcFn func(Config) Config
}

func (g *splitOption) ApplyGRPCOption(cfg Config) Config {
return g.grpcFn(cfg)
}

func (g *splitOption) ApplyHTTPOption(cfg Config) Config {
return g.httpFn(cfg)
}

func (splitOption) private() {}

func newSplitOption(httpFn func(cfg Config) Config, grpcFn func(cfg Config) Config) GenericOption {
return &splitOption{httpFn: httpFn, grpcFn: grpcFn}
}

// httpOption is an option that is only applied to the HTTP driver.
type httpOption struct {
fn func(Config) Config
}

func (h *httpOption) ApplyHTTPOption(cfg Config) Config {
return h.fn(cfg)
}

func (httpOption) private() {}

func NewHTTPOption(fn func(cfg Config) Config) HTTPOption {
return &httpOption{fn: fn}
// A private method to prevent users implementing the
// interface and so future additions to it will not
// violate compatibility.
private()
}

// grpcOption is an option that is only applied to the gRPC driver.
Expand All @@ -270,15 +166,15 @@ func NewGRPCOption(fn func(cfg Config) Config) GRPCOption {

// Generic Options

func WithEndpoint(endpoint string) GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithEndpoint(endpoint string) GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
cfg.Metrics.Endpoint = endpoint
return cfg
})
}

func WithEndpointURL(v string) GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithEndpointURL(v string) GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
u, err := url.Parse(v)
if err != nil {
global.Error(err, "otlpmetric: parse endpoint url", "url", v)
Expand All @@ -295,81 +191,78 @@ func WithEndpointURL(v string) GenericOption {
})
}

func WithCompression(compression Compression) GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithCompression(compression Compression) GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
cfg.Metrics.Compression = compression
return cfg
})
}

func WithURLPath(urlPath string) GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithURLPath(urlPath string) GRPCOption {
return NewGRPCOption(func(cfg Config) Config {

Check warning on line 202 in exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/otlpmetric/otlpmetricgrpc/internal/oconf/options.go#L201-L202

Added lines #L201 - L202 were not covered by tests
cfg.Metrics.URLPath = urlPath
return cfg
})
}

func WithRetry(rc retry.Config) GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithRetry(rc retry.Config) GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
cfg.RetryConfig = rc
return cfg
})
}

func WithTLSClientConfig(tlsCfg *tls.Config) GenericOption {
return newSplitOption(func(cfg Config) Config {
cfg.Metrics.TLSCfg = tlsCfg.Clone()
return cfg
}, func(cfg Config) Config {
func WithTLSClientConfig(tlsCfg *tls.Config) GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
cfg.Metrics.GRPCCredentials = credentials.NewTLS(tlsCfg)
return cfg
})
}

func WithInsecure() GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithInsecure() GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
cfg.Metrics.Insecure = true
return cfg
})
}

func WithSecure() GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithSecure() GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
cfg.Metrics.Insecure = false
return cfg
})
}

func WithHeaders(headers map[string]string) GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithHeaders(headers map[string]string) GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
cfg.Metrics.Headers = headers
return cfg
})
}

func WithTimeout(duration time.Duration) GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithTimeout(duration time.Duration) GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
cfg.Metrics.Timeout = duration
return cfg
})
}

func WithTemporalitySelector(selector metric.TemporalitySelector) GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithTemporalitySelector(selector metric.TemporalitySelector) GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
cfg.Metrics.TemporalitySelector = selector
return cfg
})
}

func WithAggregationSelector(selector metric.AggregationSelector) GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithAggregationSelector(selector metric.AggregationSelector) GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
cfg.Metrics.AggregationSelector = selector
return cfg
})
}

func WithProxy(pf HTTPTransportProxyFunc) GenericOption {
return newGenericOption(func(cfg Config) Config {
func WithProxy(pf HTTPTransportProxyFunc) GRPCOption {
return NewGRPCOption(func(cfg Config) Config {
cfg.Metrics.Proxy = pf
return cfg
})
Expand Down
Loading

0 comments on commit 6252eb4

Please sign in to comment.