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

feat: supports custom metrics sink export period and timeout #3103

Merged
merged 7 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
14 changes: 14 additions & 0 deletions api/v1alpha1/envoygateway_metrics_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package v1alpha1

import gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"

// EnvoyGatewayMetrics defines control plane push/pull metrics configurations.
type EnvoyGatewayMetrics struct {
// Sinks defines the metric sinks where metrics are sent to.
Expand Down Expand Up @@ -38,6 +40,18 @@ type EnvoyGatewayOpenTelemetrySink struct {
// +kubebuilder:validation:Minimum=0
// +kubebuilder:default=4317
Port int32 `json:"port,omitempty"`
// ExportInterval configures the intervening time between exports for a
// Sink. This option overrides any value set for the
// OTEL_METRIC_EXPORT_INTERVAL environment variable.
// If ExportInterval is less than or equal to zero, 60 seconds
// is used as the default.
ExportInterval *gatewayv1.Duration `json:"exportInterval"`
ShyunnY marked this conversation as resolved.
Show resolved Hide resolved
// ExportTimeout configures the time a Sink waits for an export to
// complete before canceling it. This option overrides any value set for the
// OTEL_METRIC_EXPORT_TIMEOUT environment variable.
// If ExportTimeout is less than or equal to zero, 30 seconds
// is used as the default.
ExportTimeout *gatewayv1.Duration `json:"exportTimeout"`
ShyunnY marked this conversation as resolved.
Show resolved Hide resolved
}

// EnvoyGatewayPrometheusProvider will expose prometheus endpoint in pull mode.
Expand Down
12 changes: 11 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 59 additions & 10 deletions internal/metrics/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@

// Init initializes and registers the global metrics server.
func Init(cfg *config.Server) error {
options := newOptions(cfg)
options, err := newOptions(cfg)
if err != nil {
return err

Check warning on line 36 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L34-L36

Added lines #L34 - L36 were not covered by tests
}

handler, err := registerForHandler(options)
if err != nil {
return err
Expand Down Expand Up @@ -71,7 +75,7 @@
return nil
}

func newOptions(svr *config.Server) registerOptions {
func newOptions(svr *config.Server) (registerOptions, error) {

Check warning on line 78 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L78

Added line #L78 was not covered by tests
newOpts := registerOptions{}
newOpts.address = net.JoinHostPort(v1alpha1.GatewayMetricsHost, fmt.Sprint(v1alpha1.GatewayMetricsPort))

Expand All @@ -84,14 +88,37 @@
}

for _, config := range svr.EnvoyGateway.GetEnvoyGatewayTelemetry().Metrics.Sinks {
newOpts.pushOptions.sinks = append(newOpts.pushOptions.sinks, metricsSink{
sink := metricsSink{

Check warning on line 91 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L91

Added line #L91 was not covered by tests
host: config.OpenTelemetry.Host,
port: config.OpenTelemetry.Port,
protocol: config.OpenTelemetry.Protocol,
})
}

// we do not explicitly set default values for ExporterInterval and ExporterTimeout
// instead, let the upstream repository set default values for it
if config.OpenTelemetry.ExportInterval != nil && len(*config.OpenTelemetry.ExportInterval) != 0 {
interval, err := time.ParseDuration(string(*config.OpenTelemetry.ExportInterval))
if err != nil {
metricsLogger.Error(err, "failed to parse exporter interval time format")
return newOpts, err

Check warning on line 103 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L99-L103

Added lines #L99 - L103 were not covered by tests
}

sink.exporterInterval = interval

Check warning on line 106 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L106

Added line #L106 was not covered by tests
}
if config.OpenTelemetry.ExportTimeout != nil && len(*config.OpenTelemetry.ExportTimeout) != 0 {
timeout, err := time.ParseDuration(string(*config.OpenTelemetry.ExportTimeout))
if err != nil {
metricsLogger.Error(err, "failed to parse exporter timeout time format")
return newOpts, err

Check warning on line 112 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L108-L112

Added lines #L108 - L112 were not covered by tests
}

sink.exporterTimeout = timeout

Check warning on line 115 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L115

Added line #L115 was not covered by tests
}

newOpts.pushOptions.sinks = append(newOpts.pushOptions.sinks, sink)

Check warning on line 118 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L118

Added line #L118 was not covered by tests
}

return newOpts
return newOpts, nil

Check warning on line 121 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L121

Added line #L121 was not covered by tests
}

// registerForHandler sets the global metrics registry to the provided Prometheus registerer.
Expand Down Expand Up @@ -155,7 +182,17 @@
return err
}

otelreader := metric.NewPeriodicReader(httpexporter)
periodOpts := []metric.PeriodicReaderOption{}

Check warning on line 185 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L185

Added line #L185 was not covered by tests
// If we do not set the interval or timeout for the exporter,
// we let the upstream set the default value for it.
if sink.exporterInterval != 0 {
periodOpts = append(periodOpts, metric.WithInterval(sink.exporterInterval))

Check warning on line 189 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L188-L189

Added lines #L188 - L189 were not covered by tests
}
if sink.exporterTimeout != 0 {
periodOpts = append(periodOpts, metric.WithTimeout(sink.exporterTimeout))

Check warning on line 192 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L191-L192

Added lines #L191 - L192 were not covered by tests
}

otelreader := metric.NewPeriodicReader(httpexporter, periodOpts...)

Check warning on line 195 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L195

Added line #L195 was not covered by tests
*otelOpts = append(*otelOpts, metric.WithReader(otelreader))
metricsLogger.Info("initialized otel http metrics push endpoint", "address", address)
}
Expand All @@ -178,7 +215,17 @@
return err
}

otelreader := metric.NewPeriodicReader(httpexporter)
periodOpts := []metric.PeriodicReaderOption{}

Check warning on line 218 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L218

Added line #L218 was not covered by tests
// If we do not set the interval or timeout for the exporter,
// we let the upstream set the default value for it.
if sink.exporterInterval != 0 {
periodOpts = append(periodOpts, metric.WithInterval(sink.exporterInterval))

Check warning on line 222 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L221-L222

Added lines #L221 - L222 were not covered by tests
}
if sink.exporterTimeout != 0 {
periodOpts = append(periodOpts, metric.WithTimeout(sink.exporterTimeout))

Check warning on line 225 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L224-L225

Added lines #L224 - L225 were not covered by tests
}

otelreader := metric.NewPeriodicReader(httpexporter, periodOpts...)

Check warning on line 228 in internal/metrics/register.go

View check run for this annotation

Codecov / codecov/patch

internal/metrics/register.go#L228

Added line #L228 was not covered by tests
*otelOpts = append(*otelOpts, metric.WithReader(otelreader))
metricsLogger.Info("initialized otel grpc metrics push endpoint", "address", address)
}
Expand All @@ -200,7 +247,9 @@
}

type metricsSink struct {
protocol string
host string
port int32
protocol string
host string
port int32
exporterTimeout time.Duration
ShyunnY marked this conversation as resolved.
Show resolved Hide resolved
exporterInterval time.Duration
}
2 changes: 2 additions & 0 deletions site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,8 @@ _Appears in:_
| `host` | _string_ | true | Host define the sink service hostname. |
| `protocol` | _string_ | true | Protocol define the sink service protocol. |
| `port` | _integer_ | false | Port defines the port the sink service is exposed on. |
| `exportInterval` | _[Duration](#duration)_ | true | ExportInterval configures the intervening time between exports for a<br />Sink. This option overrides any value set for the<br />OTEL_METRIC_EXPORT_INTERVAL environment variable.<br />If ExportInterval is less than or equal to zero, 60 seconds<br />is used as the default. |
| `exportTimeout` | _[Duration](#duration)_ | true | ExportTimeout configures the time a Sink waits for an export to<br />complete before canceling it. This option overrides any value set for the<br />OTEL_METRIC_EXPORT_TIMEOUT environment variable.<br />If ExportTimeout is less than or equal to zero, 30 seconds<br />is used as the default. |


#### EnvoyGatewayPrometheusProvider
Expand Down