From 1d9a685e716488f7a183ed37ffe666d4ace25992 Mon Sep 17 00:00:00 2001 From: Gergely Madarasz Date: Sat, 7 Dec 2024 11:47:10 +0100 Subject: [PATCH] Configure metrics exporter for loadbalancing when available --- CHANGELOG.md | 4 ++ .../exporter/loadbalancing/loadbalancing.go | 41 ++++++++++++++----- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d9fa0633d..8627c17a31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -262,6 +262,10 @@ v1.4.0 ### Enhancements +- Support for metrics in `otelcol.exporter.loadbalancing' + Configure loadbalancing metrics exporter based on routing_key provided and + stability level `experimental` needed. + - Clustering peer resolution through `--cluster.join-addresses` flag has been improved with more consistent behaviour, better error handling and added support for A/AAAA DNS records. If necessary, users can temporarily opt out of diff --git a/internal/component/otelcol/exporter/loadbalancing/loadbalancing.go b/internal/component/otelcol/exporter/loadbalancing/loadbalancing.go index a6f94c43e2..a403bcf8bb 100644 --- a/internal/component/otelcol/exporter/loadbalancing/loadbalancing.go +++ b/internal/component/otelcol/exporter/loadbalancing/loadbalancing.go @@ -13,6 +13,7 @@ import ( otelcolCfg "github.com/grafana/alloy/internal/component/otelcol/config" "github.com/grafana/alloy/internal/component/otelcol/exporter" "github.com/grafana/alloy/internal/featuregate" + "github.com/grafana/alloy/internal/runtime/logging/level" "github.com/grafana/alloy/syntax" "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/loadbalancingexporter" otelcomponent "go.opentelemetry.io/collector/component" @@ -34,10 +35,33 @@ func init() { Build: func(opts component.Options, args component.Arguments) (component.Component, error) { fact := loadbalancingexporter.NewFactory() - //TODO(ptodev): LB exporter cannot yet work with metrics due to a limitation in Alloy: - // https://github.com/grafana/agent/pull/5684 - // Once the limitation is removed, we may be able to remove the need for exporter.TypeSignal altogether. - return exporter.New(opts, fact, args.(Arguments), exporter.TypeLogs|exporter.TypeTraces) + + myArgs := args.(Arguments) + + var typeSignal exporter.TypeSignal + + // As per https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/exporter/loadbalancingexporter/README.md + // metrics is considered "development" stability level + + switch myArgs.RoutingKey { + case "traceID": + typeSignal = exporter.TypeLogs | exporter.TypeTraces + case "service": + if opts.MinStability.Permits(featuregate.StabilityExperimental) { + typeSignal = exporter.TypeLogs | exporter.TypeTraces | exporter.TypeMetrics + } else { + level.Warn(opts.Logger).Log("msg", "disabling metrics exporter as stability level does not allow it") + typeSignal = exporter.TypeLogs | exporter.TypeTraces + } + case "resource", "metric", "streamID": + if opts.MinStability.Permits(featuregate.StabilityExperimental) { + typeSignal = exporter.TypeMetrics + } else { + level.Warn(opts.Logger).Log("msg", "disabling metrics exporter as stability level does not allow it") + } + } + + return exporter.New(opts, fact, myArgs, typeSignal) }, }) } @@ -69,13 +93,10 @@ func (args *Arguments) SetToDefault() { // Validate implements syntax.Validator. func (args *Arguments) Validate() error { - //TODO(ptodev): Add support for "resource" and "metric" routing keys later. - // The reason we can't add them yet is that otelcol.exporter.loadbalancing - // is labeled as "beta", but those routing keys are experimental. - // We need a way to label otelcol.exporter.loadbalancing as "public-preview" - // for logs and traces, but "experimental" for metrics. + // Allow routing keys for all signal types. Metrics exporter will be disabled + // if stability level is above experimental switch args.RoutingKey { - case "service", "traceID": + case "service", "traceID", "resource", "metric", "streamID": // The routing key is valid. default: return fmt.Errorf("invalid routing key %q", args.RoutingKey)