Skip to content

Commit

Permalink
Consistently load all otel collector config from file
Browse files Browse the repository at this point in the history
  • Loading branch information
lahsivjar committed Aug 31, 2023
1 parent 7a347fa commit a8aa889
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 17 deletions.
14 changes: 0 additions & 14 deletions cmd/apmbench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"flag"
"log"
"net/url"
"os"
"os/signal"
"sync"
Expand All @@ -32,19 +31,6 @@ func main() {
// Create otel collector
collectorCfg := otelcollector.DefaultConfig()
if cfg.MonitoringAPMServerURL != "" {

Check failure on line 33 in cmd/apmbench/main.go

View workflow job for this annotation

GitHub Actions / lint

empty branch (SA9003)
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)
Expand Down
11 changes: 11 additions & 0 deletions internal/otelcollector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package otelcollector

import (
"fmt"
"net/url"
"os"

"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -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
}
Expand Down
46 changes: 43 additions & 3 deletions internal/otelcollector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,59 @@ 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
match_label_values:
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",
Expand Down

0 comments on commit a8aa889

Please sign in to comment.