From a8aa889b8ff81ceebfa1eacc5ed8f6126b48798e Mon Sep 17 00:00:00 2001 From: Vishal Raj Date: Thu, 31 Aug 2023 15:54:17 +0800 Subject: [PATCH] Consistently load all otel collector config from file --- cmd/apmbench/main.go | 14 -------- internal/otelcollector/config.go | 11 +++++++ internal/otelcollector/config_test.go | 46 +++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 17 deletions(-) diff --git a/cmd/apmbench/main.go b/cmd/apmbench/main.go index e9584ae..708e256 100644 --- a/cmd/apmbench/main.go +++ b/cmd/apmbench/main.go @@ -9,7 +9,6 @@ import ( "errors" "flag" "log" - "net/url" "os" "os/signal" "sync" @@ -32,19 +31,6 @@ func main() { // Create otel collector collectorCfg := otelcollector.DefaultConfig() if cfg.MonitoringAPMServerURL != "" { - apmURL, err := url.Parse(cfg.MonitoringAPMServerURL) - if err != nil { - logger.Fatal("invalid apm-server-url specified") - } - collectorCfg.OTLPExporterEndpoint = apmURL.Host - if apmURL.Port() == "" { - collectorCfg.OTLPExporterEndpoint += ":443" - } - if cfg.MonitoringAPMSecretToken != "" { - collectorCfg.OTLPExporterHeaders = map[string]string{ - "Authorization": "Bearer " + cfg.MonitoringAPMSecretToken, - } - } } if cfg.CollectorConfigYaml != "" { err := collectorCfg.LoadConfigFromYamlFile(cfg.CollectorConfigYaml) diff --git a/internal/otelcollector/config.go b/internal/otelcollector/config.go index c76cf14..3f938f8 100644 --- a/internal/otelcollector/config.go +++ b/internal/otelcollector/config.go @@ -6,6 +6,7 @@ package otelcollector import ( "fmt" + "net/url" "os" "go.uber.org/zap/zapcore" @@ -37,6 +38,16 @@ func (cfg *CollectorConfig) LoadConfigFromYamlFile(path string) error { if err := dec.Decode(cfg); err != nil { return fmt.Errorf("failed to decode config file: %w", err) } + if cfg.OTLPExporterEndpoint != "" { + ep, err := url.Parse(cfg.OTLPExporterEndpoint) + if err != nil { + return fmt.Errorf("invalid OTLP exporter endpoint specified: %w", err) + } + cfg.OTLPExporterEndpoint = ep.Host + if ep.Port() == "" { + cfg.OTLPExporterEndpoint += ":443" + } + } return nil } diff --git a/internal/otelcollector/config_test.go b/internal/otelcollector/config_test.go index 12c6243..3ad86d4 100644 --- a/internal/otelcollector/config_test.go +++ b/internal/otelcollector/config_test.go @@ -29,10 +29,13 @@ func TestLoadConfigFromYamlFile(t *testing.T) { errMsg: "failed to decode config file", }, { - name: "valid_yaml", + name: "valid_yaml_without_exporter_ep_port", input: []byte(` http_endpoint: localhost:8181 grpc_endpoint: localhost:8182 +otlp_exporter_endpoint: https://apm-server +otlp_exporter_headers: + Authorization: Bearer secret_token store: - name: test.metric.count alias: events/s @@ -40,8 +43,45 @@ store: k_1: v_1 k_2: v_2`), output: CollectorConfig{ - HTTPEndpoint: "localhost:8181", - GRPCEndpoint: "localhost:8182", + HTTPEndpoint: "localhost:8181", + GRPCEndpoint: "localhost:8182", + OTLPExporterEndpoint: "apm-server:443", + OTLPExporterHeaders: map[string]string{ + "Authorization": "Bearer secret_token", + }, + InMemoryStoreConfig: []inmemexporter.AggregationConfig{ + { + Name: "test.metric.count", + Alias: "events/s", + MatchLabelValues: map[string]string{ + "k_1": "v_1", + "k_2": "v_2", + }, + }, + }, + }, + }, + { + name: "valid_yaml_with_exporter_ep_port", + input: []byte(` +http_endpoint: localhost:8181 +grpc_endpoint: localhost:8182 +otlp_exporter_endpoint: https://apm-server:4171 +otlp_exporter_headers: + Authorization: Bearer secret_token +store: + - name: test.metric.count + alias: events/s + match_label_values: + k_1: v_1 + k_2: v_2`), + output: CollectorConfig{ + HTTPEndpoint: "localhost:8181", + GRPCEndpoint: "localhost:8182", + OTLPExporterEndpoint: "apm-server:4171", + OTLPExporterHeaders: map[string]string{ + "Authorization": "Bearer secret_token", + }, InMemoryStoreConfig: []inmemexporter.AggregationConfig{ { Name: "test.metric.count",