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

[otlphttpreceiver] Allow H2C connections to otlp http receiver #10996

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
01bd498
Allow H2C connections to otlp http receiver
iblancasa Aug 28, 2024
73c1ab3
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
iblancasa Aug 28, 2024
a9c2ac2
Merge branch 'main' of github.com:open-telemetry/opentelemetry-collec…
iblancasa Aug 30, 2024
a7ea55c
Add changelog
iblancasa Aug 30, 2024
13ce0c7
Merge branch 'main' into bug/10952
iblancasa Sep 9, 2024
97593dd
Merge branch 'bug/10952' of github.com:iblancasa/opentelemetry-collec…
iblancasa Sep 9, 2024
56a30f3
Fix lint
iblancasa Sep 9, 2024
edcb1f5
Merge branch 'main' into bug/10952
iblancasa Sep 11, 2024
b2ee7c4
Merge branch 'main' into bug/10952
iblancasa Sep 11, 2024
1d3065e
Merge branch 'main' into bug/10952
iblancasa Sep 12, 2024
d8af96d
Merge branch 'main' into bug/10952
iblancasa Sep 12, 2024
5e8f227
Merge branch 'main' into bug/10952
iblancasa Sep 13, 2024
2391022
Merge branch 'main' into bug/10952
iblancasa Sep 16, 2024
757aed1
Merge branch 'main' into bug/10952
iblancasa Sep 16, 2024
cefe5e7
Merge branch 'main' into bug/10952
iblancasa Sep 17, 2024
09bf71f
Merge branch 'main' into bug/10952
iblancasa Sep 18, 2024
3362372
Merge branch 'main' into bug/10952
iblancasa Sep 18, 2024
aaebad1
Merge branch 'main' into bug/10952
iblancasa Sep 19, 2024
c04b177
Merge branch 'main' into bug/10952
iblancasa Sep 19, 2024
e50647c
Merge branch 'bug/10952' of github.com:iblancasa/opentelemetry-collec…
iblancasa Sep 23, 2024
d81a742
Add changes requested in code review
iblancasa Sep 23, 2024
0d55f82
Merge branch 'main' into bug/10952
iblancasa Sep 23, 2024
2c4bb0b
Merge branch 'main' into bug/10952
iblancasa Sep 24, 2024
a526dcf
Merge branch 'main' into bug/10952
iblancasa Sep 25, 2024
9e78457
Merge branch 'main' into bug/10952
iblancasa Sep 26, 2024
9a2a893
Merge branch 'main' into bug/10952
iblancasa Sep 27, 2024
6bb2a59
Merge branch 'main' into bug/10952
iblancasa Sep 30, 2024
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/10952-allow-h2c-connections-otlphttp.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: 'bug_fix'

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: otlpreceiver

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Allow H2C connections to otlp http receiver"

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

# (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:

# 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: []
15 changes: 15 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"golang.org/x/net/publicsuffix"

"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -105,6 +106,10 @@ type ClientConfig struct {
HTTP2PingTimeout time.Duration `mapstructure:"http2_ping_timeout"`
// Cookies configures the cookie management of the HTTP client.
Cookies *CookiesConfig `mapstructure:"cookies"`

// AllowH2CUpgrade enables clients to upgrade an existing
// HTTP/1.1 connection to an HTTP/2 connection without using TLS.
AllowH2CUpgrade bool `mapstructure:"allow_h2c_upgrade"`
}

// CookiesConfig defines the configuration of the HTTP client regarding cookies served by the server.
Expand Down Expand Up @@ -327,6 +332,10 @@ type ServerConfig struct {
// is zero, the value of ReadTimeout is used. If both are
// zero, there is no timeout.
IdleTimeout time.Duration `mapstructure:"idle_timeout"`

// AllowH2CUpgrade enables clients to upgrade an existing
// HTTP/1.1 connection to an HTTP/2 connection without using TLS.
AllowH2CUpgrade bool `mapstructure:"allow_h2c_upgrade"`
}

// NewDefaultServerConfig returns ServerConfig type object with default values.
Expand All @@ -340,6 +349,7 @@ func NewDefaultServerConfig() ServerConfig {
WriteTimeout: 30 * time.Second,
ReadHeaderTimeout: 1 * time.Minute,
IdleTimeout: 1 * time.Minute,
AllowH2CUpgrade: true,
}
}

Expand Down Expand Up @@ -419,6 +429,11 @@ func (hss *ServerConfig) ToServer(_ context.Context, host component.Host, settin
o.apply(serverOpts)
}

if hss.AllowH2CUpgrade {
h2s := &http2.Server{IdleTimeout: 0}
iblancasa marked this conversation as resolved.
Show resolved Hide resolved
handler = h2c.NewHandler(handler, h2s)
}

if hss.MaxRequestBodySize <= 0 {
hss.MaxRequestBodySize = defaultMaxRequestBodySize
}
Expand Down
33 changes: 29 additions & 4 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package confighttp
import (
"bytes"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
Expand All @@ -23,6 +24,7 @@ import (
"go.opentelemetry.io/otel/metric"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
"golang.org/x/net/http2"

"go.opentelemetry.io/collector/client"
"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -573,6 +575,7 @@ func TestHttpReception(t *testing.T) {
tlsClientCreds *configtls.ClientConfig
hasError bool
forceHTTP1 bool
allowh2c bool
}{
{
name: "noTLS",
Expand All @@ -581,6 +584,14 @@ func TestHttpReception(t *testing.T) {
Insecure: true,
},
},
{
name: "noTLS and AllowH2CUpgrade",
tlsServerCreds: nil,
tlsClientCreds: &configtls.ClientConfig{
Insecure: true,
},
allowh2c: true,
},
{
name: "TLS",
tlsServerCreds: &configtls.ServerConfig{
Expand Down Expand Up @@ -692,8 +703,9 @@ func TestHttpReception(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hss := &ServerConfig{
Endpoint: "localhost:0",
TLSSetting: tt.tlsServerCreds,
Endpoint: "localhost:0",
TLSSetting: tt.tlsServerCreds,
AllowH2CUpgrade: tt.allowh2c,
}
ln, err := hss.ToListener(context.Background())
require.NoError(t, err)
Expand All @@ -712,11 +724,13 @@ func TestHttpReception(t *testing.T) {
_ = s.Serve(ln)
}()

prefix := "https://"
expectedProto := "HTTP/2.0"
prefix := "https://"
if tt.tlsClientCreds.Insecure {
prefix = "http://"
expectedProto = "HTTP/1.1"
if !tt.allowh2c {
expectedProto = "HTTP/1.1"
}
}

hcs := &ClientConfig{
Expand All @@ -727,6 +741,17 @@ func TestHttpReception(t *testing.T) {
client, errClient := hcs.ToClient(context.Background(), componenttest.NewNopHost(), nilProvidersSettings)
require.NoError(t, errClient)

if tt.allowh2c {
client.Transport = &http2.Transport{
AllowHTTP: true,
// Pretend we are dialing a TLS endpoint. (Note, we ignore the passed tls.Config)
DialTLS: func(netw, addr string, _ *tls.Config) (net.Conn, error) {
return net.Dial(netw, addr)
},
}
defer client.Transport.(*http2.Transport).CloseIdleConnections()
}

if tt.forceHTTP1 {
expectedProto = "HTTP/1.1"
client.Transport.(*http.Transport).ForceAttemptHTTP2 = false
Expand Down
Loading