Skip to content

Commit

Permalink
Merge pull request #70 from aierui/request_method
Browse files Browse the repository at this point in the history
feat: Support configure request method
  • Loading branch information
f41gh7 authored Jun 24, 2024
2 parents 464c46d + 9032bb9 commit d5fb341
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions push.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ type PushOptions struct {
// By default the compression is enabled.
DisableCompression bool

// Method is an optional of HTTP request method.
// By default the Method is GET.
Method string

// Optional WaitGroup for waiting until all the push workers created with this WaitGroup are stopped.
WaitGroup *sync.WaitGroup
}
Expand Down Expand Up @@ -264,6 +268,7 @@ func PushMetricsExt(ctx context.Context, pushURL string, writeMetrics func(w io.

type pushContext struct {
pushURL *url.URL
method string
pushURLRedacted string
extraLabels string
headers http.Header
Expand Down Expand Up @@ -295,6 +300,11 @@ func newPushContext(pushURL string, opts *PushOptions) (*pushContext, error) {
return nil, fmt.Errorf("missing host in pushURL=%q", pushURL)
}

method := opts.Method
if len(method) == 0 {
method = http.MethodGet
}

// validate ExtraLabels
extraLabels := opts.ExtraLabels
if err := validateTags(extraLabels); err != nil {
Expand All @@ -317,6 +327,7 @@ func newPushContext(pushURL string, opts *PushOptions) (*pushContext, error) {
client := &http.Client{}
return &pushContext{
pushURL: pu,
method: method,
pushURLRedacted: pushURLRedacted,
extraLabels: extraLabels,
headers: headers,
Expand Down Expand Up @@ -367,18 +378,18 @@ func (pc *pushContext) pushMetrics(ctx context.Context, writeMetrics func(w io.W

// Prepare the request to sent to pc.pushURL
reqBody := bytes.NewReader(bb.B)
req, err := http.NewRequestWithContext(ctx, "GET", pc.pushURL.String(), reqBody)
req, err := http.NewRequestWithContext(ctx, pc.method, pc.pushURL.String(), reqBody)
if err != nil {
panic(fmt.Errorf("BUG: metrics.push: cannot initialize request for metrics push to %q: %w", pc.pushURLRedacted, err))
}

// Set the needed headers
req.Header.Set("Content-Type", "text/plain")
// Set the needed headers, and `Content-Type` allowed be overwrited.
for name, values := range pc.headers {
for _, value := range values {
req.Header.Add(name, value)
}
}
req.Header.Set("Content-Type", "text/plain")
if !pc.disableCompression {
req.Header.Set("Content-Encoding", "gzip")
}
Expand Down

0 comments on commit d5fb341

Please sign in to comment.