From 3db63cbb625fab19fb7d8c0c95223448ef04075c Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Tue, 1 Aug 2023 13:55:41 +0200 Subject: [PATCH] telemetrygen: Allow setting custom paths (#24551) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current description for the flag is wrong. ``` ❯ ../../open-telemetry/opentelemetry-collector-contrib/cmd/telemetrygen/telemetrygen metrics --otlp-endpoint example.com/v1/metrics --otlp-http --otlp-insecure 2023-07-25T22:03:45.884+0200 INFO metrics/metrics.go:56 starting HTTP exporter Error: failed to obtain OTLP exporter: parse "http://example.com%2Fv1%2Fmetrics/v1/metrics": invalid URL escape "%2F" ``` I couldn't make it easily work with the existing flag because `url.Parse(c.Endpoint)` puts everything in path, and u.Host is unset. This is because url.Parse expects `http://` or `https://` which is set through a different flag. Signed-off-by: Goutham Co-authored-by: Pablo Baeyens --- .chloggen/telemetrygen-new-path.yaml | 20 ++++++++++++++++++++ cmd/telemetrygen/internal/common/config.go | 4 +++- cmd/telemetrygen/internal/metrics/metrics.go | 1 + cmd/telemetrygen/internal/traces/traces.go | 1 + 4 files changed, 25 insertions(+), 1 deletion(-) create mode 100755 .chloggen/telemetrygen-new-path.yaml diff --git a/.chloggen/telemetrygen-new-path.yaml b/.chloggen/telemetrygen-new-path.yaml new file mode 100755 index 000000000000..2def1cfae278 --- /dev/null +++ b/.chloggen/telemetrygen-new-path.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. +# If your change doesn't affect end users, such as a test fix or a tooling change, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. + +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: enhancement + +# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) +component: cmd/telemetrygen + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: "Add ability to set custom path to endpoint." + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [24551] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: diff --git a/cmd/telemetrygen/internal/common/config.go b/cmd/telemetrygen/internal/common/config.go index 72502a42a4df..9850b3cce754 100644 --- a/cmd/telemetrygen/internal/common/config.go +++ b/cmd/telemetrygen/internal/common/config.go @@ -53,6 +53,7 @@ type Config struct { Endpoint string Insecure bool UseHTTP bool + HTTPPath string Headers KeyValue ResourceAttributes KeyValue } @@ -75,9 +76,10 @@ func (c *Config) CommonFlags(fs *pflag.FlagSet) { fs.DurationVar(&c.TotalDuration, "duration", 0, "For how long to run the test") fs.DurationVar(&c.ReportingInterval, "interval", 1*time.Second, "Reporting interval (default 1 second)") - fs.StringVar(&c.Endpoint, "otlp-endpoint", "localhost:4317", "Target to which the exporter is going to send metrics. This MAY be configured to include a path (e.g. example.com/v1/metrics)") + fs.StringVar(&c.Endpoint, "otlp-endpoint", "localhost:4317", "Target to which the exporter is going to send metrics.") fs.BoolVar(&c.Insecure, "otlp-insecure", false, "Whether to enable client transport security for the exporter's grpc or http connection") fs.BoolVar(&c.UseHTTP, "otlp-http", false, "Whether to use HTTP exporter rather than a gRPC one") + fs.StringVar(&c.HTTPPath, "otlp-http-url-path", "/v1/metrics", "Which URL path to write to (default /v1/metrics)") // custom headers c.Headers = make(map[string]string) diff --git a/cmd/telemetrygen/internal/metrics/metrics.go b/cmd/telemetrygen/internal/metrics/metrics.go index 267362e6917b..6418f7228a95 100644 --- a/cmd/telemetrygen/internal/metrics/metrics.go +++ b/cmd/telemetrygen/internal/metrics/metrics.go @@ -38,6 +38,7 @@ func Start(cfg *Config) error { httpExpOpt := []otlpmetrichttp.Option{ otlpmetrichttp.WithEndpoint(cfg.Endpoint), + otlpmetrichttp.WithURLPath(cfg.HTTPPath), } if cfg.Insecure { diff --git a/cmd/telemetrygen/internal/traces/traces.go b/cmd/telemetrygen/internal/traces/traces.go index 553c71ee8740..78aaf627d948 100644 --- a/cmd/telemetrygen/internal/traces/traces.go +++ b/cmd/telemetrygen/internal/traces/traces.go @@ -40,6 +40,7 @@ func Start(cfg *Config) error { httpExpOpt := []otlptracehttp.Option{ otlptracehttp.WithEndpoint(cfg.Endpoint), + otlptracehttp.WithURLPath(cfg.HTTPPath), } if cfg.Insecure {