push.go: initialize nil opts with the zero PushOptions inside InitPushExtWithOptions
This makes the InitPushExtWithOptions code more maintainable because of removed checks for nil opts.
This commit is contained in:
parent
b96951708c
commit
bee9e4faf1
16
push.go
16
push.go
@ -179,6 +179,10 @@ func InitPushExt(pushURL string, interval time.Duration, extraLabels string, wri
|
|||||||
// It is OK calling InitPushExtWithOptions multiple times with different writeMetrics -
|
// It is OK calling InitPushExtWithOptions multiple times with different writeMetrics -
|
||||||
// in this case all the metrics generated by writeMetrics callbacks are written to pushURL.
|
// 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 {
|
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
|
// validate pushURL
|
||||||
pu, err := url.Parse(pushURL)
|
pu, err := url.Parse(pushURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -197,17 +201,13 @@ func InitPushExtWithOptions(ctx context.Context, pushURL string, interval time.D
|
|||||||
}
|
}
|
||||||
|
|
||||||
// validate ExtraLabels
|
// validate ExtraLabels
|
||||||
var extraLabels string
|
extraLabels := opts.ExtraLabels
|
||||||
if opts != nil {
|
|
||||||
extraLabels = opts.ExtraLabels
|
|
||||||
}
|
|
||||||
if err := validateTags(extraLabels); err != nil {
|
if err := validateTags(extraLabels); err != nil {
|
||||||
return fmt.Errorf("invalid extraLabels=%q: %w", extraLabels, err)
|
return fmt.Errorf("invalid extraLabels=%q: %w", extraLabels, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate Headers
|
// validate Headers
|
||||||
headers := make(http.Header)
|
headers := make(http.Header)
|
||||||
if opts != nil {
|
|
||||||
for _, h := range opts.Headers {
|
for _, h := range opts.Headers {
|
||||||
n := strings.IndexByte(h, ':')
|
n := strings.IndexByte(h, ':')
|
||||||
if n < 0 {
|
if n < 0 {
|
||||||
@ -217,13 +217,9 @@ func InitPushExtWithOptions(ctx context.Context, pushURL string, interval time.D
|
|||||||
value := strings.TrimSpace(h[n+1:])
|
value := strings.TrimSpace(h[n+1:])
|
||||||
headers.Add(name, value)
|
headers.Add(name, value)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// validate DisableCompression
|
// validate DisableCompression
|
||||||
disableCompression := false
|
disableCompression := opts.DisableCompression
|
||||||
if opts != nil {
|
|
||||||
disableCompression = opts.DisableCompression
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize metrics for the given pushURL
|
// Initialize metrics for the given pushURL
|
||||||
pushURLRedacted := pu.Redacted()
|
pushURLRedacted := pu.Redacted()
|
||||||
|
Loading…
Reference in New Issue
Block a user