Skip to content

Commit

Permalink
confighttp: add component.Host parameter to ToServer
Browse files Browse the repository at this point in the history
Signed-off-by: Juraci Paixão Kröhling <[email protected]>
  • Loading branch information
jpkrohling committed Dec 6, 2021
1 parent aee2d67 commit 575c412
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## 🛑 Breaking changes 🛑

- `confighttp`: `ToServer` how accepts a `component.Host`, in line with gRPC's counterpart (#4514)

## 🧰 Bug fixes 🧰

- Fix handling of corrupted records by persistent buffer (experimental) (#4475)
Expand Down
4 changes: 2 additions & 2 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func WithErrorHandler(e middleware.ErrorHandler) ToServerOption {
}

// ToServer creates an http.Server from settings object.
func (hss *HTTPServerSettings) ToServer(handler http.Handler, settings component.TelemetrySettings, opts ...ToServerOption) *http.Server {
func (hss *HTTPServerSettings) ToServer(_ component.Host, settings component.TelemetrySettings, handler http.Handler, opts ...ToServerOption) (*http.Server, error) {
serverOpts := &toServerOptions{}
for _, o := range opts {
o(serverOpts)
Expand Down Expand Up @@ -223,5 +223,5 @@ func (hss *HTTPServerSettings) ToServer(handler http.Handler, settings component

return &http.Server{
Handler: handler,
}
}, nil
}
38 changes: 29 additions & 9 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,14 @@ func TestHttpReception(t *testing.T) {
}
ln, err := hss.ToListener()
assert.NoError(t, err)
s := hss.ToServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, errWrite := fmt.Fprint(w, "test")
assert.NoError(t, errWrite)
}), componenttest.NewNopTelemetrySettings())
s, err := hss.ToServer(
componenttest.NewNopHost(),
componenttest.NewNopTelemetrySettings(),
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, errWrite := fmt.Fprint(w, "test")
assert.NoError(t, errWrite)
}))
assert.NoError(t, err)

go func() {
_ = s.Serve(ln)
Expand Down Expand Up @@ -461,9 +465,14 @@ func TestHttpCors(t *testing.T) {

ln, err := hss.ToListener()
assert.NoError(t, err)
s := hss.ToServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}), componenttest.NewNopTelemetrySettings())
s, err := hss.ToServer(
componenttest.NewNopHost(),
componenttest.NewNopTelemetrySettings(),
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
assert.NoError(t, err)

go func() {
_ = s.Serve(ln)
}()
Expand Down Expand Up @@ -499,7 +508,11 @@ func TestHttpCorsInvalidSettings(t *testing.T) {
}

// This effectively does not enable CORS but should also not cause an error
s := hss.ToServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}), componenttest.NewNopTelemetrySettings())
s, err := hss.ToServer(
componenttest.NewNopHost(),
componenttest.NewNopTelemetrySettings(),
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
require.NoError(t, err)
require.NotNil(t, s)
require.NoError(t, s.Close())
}
Expand Down Expand Up @@ -541,7 +554,14 @@ func ExampleHTTPServerSettings() {
settings := HTTPServerSettings{
Endpoint: ":443",
}
s := settings.ToServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}), componenttest.NewNopTelemetrySettings())
s, err := settings.ToServer(
componenttest.NewNopHost(),
componenttest.NewNopTelemetrySettings(),
http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
if err != nil {
panic(err)
}

l, err := settings.ToListener()
if err != nil {
panic(err)
Expand Down
9 changes: 7 additions & 2 deletions receiver/otlpreceiver/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,16 @@ func (r *otlpReceiver) startProtocolServers(host component.Host) error {
}
}
if r.cfg.HTTP != nil {
r.serverHTTP = r.cfg.HTTP.ToServer(
r.httpMux,
r.serverHTTP, err = r.cfg.HTTP.ToServer(
host,
r.settings.TelemetrySettings,
r.httpMux,
confighttp.WithErrorHandler(errorHandler),
)
if err != nil {
return err
}

err = r.startHTTPServer(r.cfg.HTTP, host)
if err != nil {
return err
Expand Down

0 comments on commit 575c412

Please sign in to comment.