Skip to content

Commit

Permalink
push.go: initialize nil opts with the zero PushOptions inside InitPus…
Browse files Browse the repository at this point in the history
…hExtWithOptions

This makes the InitPushExtWithOptions code more maintainable because of removed checks for nil opts.
  • Loading branch information
valyala committed Dec 18, 2023
1 parent b969517 commit bee9e4f
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions push.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ func InitPushExt(pushURL string, interval time.Duration, extraLabels string, wri
// It is OK calling InitPushExtWithOptions multiple times with different writeMetrics -
// in this case all the metrics generated by writeMetrics callbacks are written to pushURL.
func InitPushExtWithOptions(ctx context.Context, pushURL string, interval time.Duration, writeMetrics func(w io.Writer), opts *PushOptions) error {
if opts == nil {
opts = &PushOptions{}
}

// validate pushURL
pu, err := url.Parse(pushURL)
if err != nil {
Expand All @@ -197,33 +201,25 @@ func InitPushExtWithOptions(ctx context.Context, pushURL string, interval time.D
}

// validate ExtraLabels
var extraLabels string
if opts != nil {
extraLabels = opts.ExtraLabels
}
extraLabels := opts.ExtraLabels
if err := validateTags(extraLabels); err != nil {
return fmt.Errorf("invalid extraLabels=%q: %w", extraLabels, err)
}

// validate Headers
headers := make(http.Header)
if opts != nil {
for _, h := range opts.Headers {
n := strings.IndexByte(h, ':')
if n < 0 {
return fmt.Errorf("missing `:` delimiter in the header %q", h)
}
name := strings.TrimSpace(h[:n])
value := strings.TrimSpace(h[n+1:])
headers.Add(name, value)
for _, h := range opts.Headers {
n := strings.IndexByte(h, ':')
if n < 0 {
return fmt.Errorf("missing `:` delimiter in the header %q", h)
}
name := strings.TrimSpace(h[:n])
value := strings.TrimSpace(h[n+1:])
headers.Add(name, value)
}

// validate DisableCompression
disableCompression := false
if opts != nil {
disableCompression = opts.DisableCompression
}
disableCompression := opts.DisableCompression

// Initialize metrics for the given pushURL
pushURLRedacted := pu.Redacted()
Expand Down

0 comments on commit bee9e4f

Please sign in to comment.