Skip to content

Commit

Permalink
[confighttp] Add support for cookies (#10176)
Browse files Browse the repository at this point in the history
#### Description
Add support for cookies in HTTP clients with `cookies::enabled`.

#### Link to tracking issue
Fixes #10175

#### Testing
Unit test

#### Documentation
Added to README
  • Loading branch information
atoulme authored Jun 20, 2024
1 parent 72c23aa commit 8022caa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .chloggen/cookie_support.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
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`.

# One or more tracking issues or pull requests related to the change
issues: [10175]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: The method `confighttp.ToClient` will return a client with a `cookiejar.Jar` which will reuse cookies from server responses in subsequent requests.

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
4 changes: 4 additions & 0 deletions config/confighttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +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`](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 @@ -51,6 +53,8 @@ exporter:
test1: "value1"
"test 2": "value 2"
compression: zstd
cookies:
enabled: true
```
## Server Configuration
Expand Down
19 changes: 19 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"io"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"time"

"github.com/rs/cors"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"golang.org/x/net/http2"
"golang.org/x/net/publicsuffix"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configauth"
Expand Down Expand Up @@ -104,6 +106,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"`
}

// 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 @@ -232,9 +242,18 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett
}
}

var jar http.CookieJar
if hcs.Cookies != nil && hcs.Cookies.Enabled {
jar, err = cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
if err != nil {
return nil, err
}
}

return &http.Client{
Transport: clientTransport,
Timeout: hcs.Timeout,
Jar: jar,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func TestAllHTTPClientSettings(t *testing.T) {
IdleConnTimeout: &idleConnTimeout,
Compression: "",
DisableKeepAlives: true,
Cookies: &CookiesConfig{Enabled: true},
HTTP2ReadIdleTimeout: idleConnTimeout,
HTTP2PingTimeout: http2PingTimeout,
},
Expand Down

0 comments on commit 8022caa

Please sign in to comment.