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

[confighttp] Add support for cookies #10176

Merged
merged 4 commits into from
Jun 20, 2024
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
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: []
mx-psi marked this conversation as resolved.
Show resolved Hide resolved
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 @@
"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 @@ -103,6 +105,14 @@
// 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 @@ -231,9 +241,18 @@
}
}

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
atoulme marked this conversation as resolved.
Show resolved Hide resolved
}

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

View check run for this annotation

Codecov / codecov/patch

config/confighttp/confighttp.go#L249

Added line #L249 was not covered by tests
}

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
Loading