Skip to content

Commit

Permalink
confighttp: add component.Host parameter to ToServer (open-telemetr…
Browse files Browse the repository at this point in the history
…y#4514)

* `confighttp`: add component.Host parameter to ToServer

Signed-off-by: Juraci Paixão Kröhling <[email protected]>

* s/assert/require/ for some tests, s/how/now/

Signed-off-by: Juraci Paixão Kröhling <[email protected]>
  • Loading branch information
jpkrohling authored Dec 6, 2021
1 parent ce684eb commit 83206c5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Remove `config.NewConfigMapFrom[File|Buffer]`, add testonly version (#4502)
- `configtls`: TLS 1.2 is the new default mininum version (#4503)
- `confighttp`: `ToServer` now accepts a `component.Host`, in line with gRPC's counterpart (#4514)

## 🧰 Bug fixes 🧰

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
}
47 changes: 35 additions & 12 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,16 @@ func TestHttpReception(t *testing.T) {
TLSSetting: tt.tlsServerCreds,
}
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())
require.NoError(t, err)

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)
}))
require.NoError(t, err)

go func() {
_ = s.Serve(ln)
Expand All @@ -403,7 +408,8 @@ func TestHttpReception(t *testing.T) {
TLSSetting: *tt.tlsClientCreds,
}
client, errClient := hcs.ToClient(map[config.ComponentID]component.Extension{})
assert.NoError(t, errClient)
require.NoError(t, errClient)

resp, errResp := client.Get(hcs.Endpoint)
if tt.hasError {
assert.Error(t, errResp)
Expand Down Expand Up @@ -460,10 +466,16 @@ 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())
require.NoError(t, err)

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

go func() {
_ = s.Serve(ln)
}()
Expand Down Expand Up @@ -499,7 +511,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 +557,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 83206c5

Please sign in to comment.