-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NETOBSERV-1102: fine-tuning http server settings (#545)
- Loading branch information
Showing
6 changed files
with
148 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package prometheus | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/netobserv/flowlogs-pipeline/pkg/config" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStartPromServer(t *testing.T) { | ||
srv := InitializePrometheus(&config.MetricsSettings{}) | ||
|
||
serverURL := "http://0.0.0.0:9090" | ||
t.Logf("Started test http server: %v", serverURL) | ||
|
||
httpClient := &http.Client{} | ||
|
||
// wait for our test http server to come up | ||
checkHTTPReady(httpClient, serverURL) | ||
|
||
r, err := http.NewRequest("GET", serverURL+"/metrics", nil) | ||
require.NoError(t, err) | ||
|
||
resp, err := httpClient.Do(r) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
|
||
bodyBytes, err := io.ReadAll(resp.Body) | ||
require.NoError(t, err) | ||
|
||
bodyString := string(bodyBytes) | ||
require.Equal(t, http.StatusOK, resp.StatusCode) | ||
require.Contains(t, bodyString, "go_gc_duration_seconds") | ||
|
||
_ = srv.Shutdown(context.Background()) | ||
} | ||
|
||
func TestStartPromServer_HeadersLimit(t *testing.T) { | ||
srv := InitializePrometheus(&config.MetricsSettings{}) | ||
|
||
serverURL := "http://0.0.0.0:9090" | ||
t.Logf("Started test http server: %v", serverURL) | ||
|
||
httpClient := &http.Client{} | ||
|
||
// wait for our test http server to come up | ||
checkHTTPReady(httpClient, serverURL) | ||
|
||
r, err := http.NewRequest("GET", serverURL+"/metrics", nil) | ||
require.NoError(t, err) | ||
|
||
// Set many headers | ||
oneKBString := strings.Repeat(".", 1024) | ||
for i := 0; i < 1025; i++ { | ||
r.Header.Set(fmt.Sprintf("test-header-%d", i), oneKBString) | ||
} | ||
|
||
resp, err := httpClient.Do(r) | ||
require.NoError(t, err) | ||
defer resp.Body.Close() | ||
assert.Equal(t, http.StatusRequestHeaderFieldsTooLarge, resp.StatusCode) | ||
|
||
_ = srv.Shutdown(context.Background()) | ||
} | ||
|
||
func checkHTTPReady(httpClient *http.Client, url string) { | ||
for i := 0; i < 60; i++ { | ||
if r, err := httpClient.Get(url); err == nil { | ||
r.Body.Close() | ||
break | ||
} | ||
time.Sleep(time.Second) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package server | ||
|
||
import ( | ||
"crypto/tls" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var slog = logrus.WithField("module", "server") | ||
|
||
func Default(srv *http.Server) *http.Server { | ||
// defaults taken from https://bruinsslot.jp/post/go-secure-webserver/ can be overriden by caller | ||
if srv.Handler != nil { | ||
// No more than 2MB body | ||
srv.Handler = http.MaxBytesHandler(srv.Handler, 2<<20) | ||
} else { | ||
slog.Warnf("Handler not yet set on server while securing defaults. Make sure a MaxByte middleware is used.") | ||
} | ||
if srv.ReadTimeout == 0 { | ||
srv.ReadTimeout = 10 * time.Second | ||
} | ||
if srv.ReadHeaderTimeout == 0 { | ||
srv.ReadHeaderTimeout = 5 * time.Second | ||
} | ||
if srv.WriteTimeout == 0 { | ||
srv.WriteTimeout = 10 * time.Second | ||
} | ||
if srv.IdleTimeout == 0 { | ||
srv.IdleTimeout = 120 * time.Second | ||
} | ||
if srv.MaxHeaderBytes == 0 { | ||
srv.MaxHeaderBytes = 1 << 20 // 1MB | ||
} | ||
if srv.TLSConfig == nil { | ||
srv.TLSConfig = &tls.Config{} | ||
} | ||
if srv.TLSConfig.MinVersion == 0 { | ||
srv.TLSConfig.MinVersion = tls.VersionTLS13 | ||
} | ||
// Disable http/2 | ||
srv.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0) | ||
|
||
return srv | ||
} |