Skip to content

Commit

Permalink
refactor to use a cookies section
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed May 24, 2024
1 parent 9e82c4f commit 0ff46bc
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .chloggen/cookie_support.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ change_type: enhancement
component: confighttp

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for cookies in HTTP clients with `cookies_enabled`.
note: Add support for cookies in HTTP clients with `cookies::enabled`.

# One or more tracking issues or pull requests related to the change
issues: [10175]
Expand Down
5 changes: 4 additions & 1 deletion config/confighttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ README](../configtls/README.md).
- [`disable_keep_alives`](https://golang.org/pkg/net/http/#Transport)
- [`http2_read_idle_timeout`](https://pkg.go.dev/golang.org/x/net/http2#Transport)
- [`http2_ping_timeout`](https://pkg.go.dev/golang.org/x/net/http2#Transport)
- [`cookies_enabled`](https://pkg.go.dev/net/http#CookieJar) if enabled, the client will store cookies from server responses and reuse them in subsequent requests.
- [`cookies`](https://pkg.go.dev/net/http#CookieJar)
- [`enabled`] if enabled, the client will store cookies from server responses and reuse them in subsequent requests.

Example:

Expand All @@ -52,6 +53,8 @@ exporter:
test1: "value1"
"test 2": "value 2"
compression: zstd
cookies:
enabled: true
```
## Server Configuration
Expand Down
12 changes: 8 additions & 4 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,14 @@ type ClientConfig struct {
// HTTP2PingTimeout if there's no response to the ping within the configured value, the connection will be closed.
// If not set or set to 0, it defaults to 15s.
HTTP2PingTimeout time.Duration `mapstructure:"http2_ping_timeout"`
// Cookies configures the cookie management of the HTTP client.
Cookies *CookiesConfig `mapstructure:"cookies"`
}

// CookiesEnabled enables reusing cookies between requests.
// This is useful to manage sticky session cookies.
CookiesEnabled bool `mapstructure:"cookies_enabled"`
// CookiesConfig defines the configuration of the HTTP client regarding cookies served by the server.
type CookiesConfig struct {
// Enabled if true, cookies from HTTP responses will be reused in further HTTP requests with the same server.
Enabled bool `mapstructure:"enabled"`
}

// NewDefaultClientConfig returns ClientConfig type object with
Expand Down Expand Up @@ -233,7 +237,7 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett
}

var jar http.CookieJar
if hcs.CookiesEnabled {
if hcs.Cookies != nil && hcs.Cookies.Enabled {
jar, err = cookiejar.New(nil)
if err != nil {
return nil, err

Check warning on line 243 in config/confighttp/confighttp.go

View check run for this annotation

Codecov / codecov/patch

config/confighttp/confighttp.go#L243

Added line #L243 was not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestAllHTTPClientSettings(t *testing.T) {
CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) { return next, nil },
Compression: "",
DisableKeepAlives: true,
CookiesEnabled: true,
Cookies: &CookiesConfig{Enabled: true},
HTTP2ReadIdleTimeout: idleConnTimeout,
HTTP2PingTimeout: http2PingTimeout,
},
Expand Down

0 comments on commit 0ff46bc

Please sign in to comment.