Skip to content

Commit

Permalink
telemetrygen: Allow setting custom paths (#24551)
Browse files Browse the repository at this point in the history
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 <[email protected]>
Co-authored-by: Pablo Baeyens <[email protected]>
  • Loading branch information
gouthamve and mx-psi authored Aug 1, 2023
1 parent 613a43c commit 3db63cb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
20 changes: 20 additions & 0 deletions .chloggen/telemetrygen-new-path.yaml
Original file line number Diff line number Diff line change
@@ -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:
4 changes: 3 additions & 1 deletion cmd/telemetrygen/internal/common/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Config struct {
Endpoint string
Insecure bool
UseHTTP bool
HTTPPath string
Headers KeyValue
ResourceAttributes KeyValue
}
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/telemetrygen/internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func Start(cfg *Config) error {

httpExpOpt := []otlpmetrichttp.Option{
otlpmetrichttp.WithEndpoint(cfg.Endpoint),
otlpmetrichttp.WithURLPath(cfg.HTTPPath),
}

if cfg.Insecure {
Expand Down
1 change: 1 addition & 0 deletions cmd/telemetrygen/internal/traces/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func Start(cfg *Config) error {

httpExpOpt := []otlptracehttp.Option{
otlptracehttp.WithEndpoint(cfg.Endpoint),
otlptracehttp.WithURLPath(cfg.HTTPPath),
}

if cfg.Insecure {
Expand Down

0 comments on commit 3db63cb

Please sign in to comment.