Skip to content

Commit

Permalink
remote/http: add an optional request body (#5918)
Browse files Browse the repository at this point in the history
Signed-off-by: Paschalis Tsilias <[email protected]>
  • Loading branch information
tpaschalis authored Dec 8, 2023
1 parent 5052176 commit 5f62b01
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Main (unreleased)

- Allow defining `HTTPClientConfig` for `discovery.ec2`. (@cmbrad)

- The `remote.http` component can optionally define a request body. (@tpaschalis)

### Bugfixes

- Update `pyroscope.ebpf` to fix a logical bug causing to profile to many kthreads instead of regular processes https://github.com/grafana/pyroscope/pull/2778 (@korniltsev)
Expand Down
8 changes: 7 additions & 1 deletion component/remote/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Arguments struct {

Method string `river:"method,attr,optional"`
Headers map[string]string `river:"headers,attr,optional"`
Body string `river:"body,attr,optional"`

Client common_config.HTTPClientConfig `river:"client,block,optional"`
}
Expand Down Expand Up @@ -193,7 +194,12 @@ func (c *Component) pollError() error {
ctx, cancel := context.WithTimeout(context.Background(), c.args.PollTimeout)
defer cancel()

req, err := http.NewRequest(c.args.Method, c.args.URL, nil)
var body io.Reader
if c.args.Body != "" {
body = strings.NewReader(c.args.Body)
}

req, err := http.NewRequest(c.args.Method, c.args.URL, body)
if err != nil {
level.Error(c.log).Log("msg", "failed to build request", "err", err)
return fmt.Errorf("building request: %w", err)
Expand Down
7 changes: 6 additions & 1 deletion component/remote/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package http_test
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"sync"
Expand All @@ -27,6 +28,9 @@ func Test(t *testing.T) {
defer srv.Close()

handler.SetHandler(func(w http.ResponseWriter, r *http.Request) {
b, err := io.ReadAll(r.Body)
require.NoError(t, err)
require.Equal(t, string(b), "hello there!")
fmt.Fprintln(w, "Hello, world!")
})

Expand All @@ -40,10 +44,11 @@ func Test(t *testing.T) {
"x-custom" = "value",
"User-Agent" = "custom_useragent",
}
body = "%s"
poll_frequency = "50ms"
poll_timeout = "25ms"
`, srv.URL, http.MethodPut)
`, srv.URL, http.MethodPut, "hello there!")
var args http_component.Arguments
require.NoError(t, river.Unmarshal([]byte(cfg), &args))

Expand Down
1 change: 1 addition & 0 deletions docs/sources/flow/reference/components/remote.http.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Name | Type | Description | Default | Required
`url` | `string` | URL to poll. | | yes
`method` | `string` | Define HTTP method for the request | `"GET"` | no
`headers` | `map(string)` | Custom headers for the request. | `{}` | no
`body` | `string` | The request body. | `""` | no
`poll_frequency` | `duration` | Frequency to poll the URL. | `"1m"` | no
`poll_timeout` | `duration` | Timeout when polling the URL. | `"10s"` | no
`is_secret` | `bool` | Whether the response body should be treated as a secret. | false | no
Expand Down

0 comments on commit 5f62b01

Please sign in to comment.