Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remote/http: add an optional request body #5918

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Main (unreleased)
- Added links between compatible components in the documentation to make it
easier to discover them. (@thampiotr)

- 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