-
Notifications
You must be signed in to change notification settings - Fork 2k
metrics: add total http connections metric for agent #26756
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
Draft
mismithhisler
wants to merge
1
commit into
main
Choose a base branch
from
f-add-total-http-connections-metric
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -115,8 +115,13 @@ type HTTPServer struct { | |
| // NewHTTPServers starts an HTTP server for every address.http configured in | ||
| // the agent. | ||
| func NewHTTPServers(agent *Agent, config *Config) ([]*HTTPServer, error) { | ||
| var srvs []*HTTPServer | ||
| var serverInitializationErrors error | ||
| var ( | ||
| srvs []*HTTPServer | ||
| serverInitializationErrors error | ||
|
|
||
| connCount int = 0 | ||
| countMux *sync.Mutex = &sync.Mutex{} | ||
| ) | ||
|
|
||
| // Get connection handshake timeout limit | ||
| handshakeTimeout, err := time.ParseDuration(config.Limits.HTTPSHandshakeTimeout) | ||
|
|
@@ -185,7 +190,7 @@ func NewHTTPServers(agent *Agent, config *Config) ([]*HTTPServer, error) { | |
| httpServer := http.Server{ | ||
| Addr: srv.Addr, | ||
| Handler: handlers.CompressHandler(srv.mux), | ||
| ConnState: makeConnState(config.TLSConfig.EnableHTTP, handshakeTimeout, maxConns, srv.logger), | ||
| ConnState: makeConnState(config.TLSConfig.EnableHTTP, handshakeTimeout, maxConns, &connCount, countMux, srv.logger), | ||
| ErrorLog: newHTTPServerLogger(srv.logger), | ||
| } | ||
|
|
||
|
|
@@ -197,6 +202,19 @@ func NewHTTPServers(agent *Agent, config *Config) ([]*HTTPServer, error) { | |
| srvs = append(srvs, srv) | ||
| } | ||
|
|
||
| go func() { | ||
| ticker := time.NewTicker(config.Telemetry.collectionInterval) | ||
| defer ticker.Stop() | ||
| for { | ||
| <-ticker.C | ||
|
|
||
| // lock connCount to avoid torn reads, as this is updated by ConnState callbacks | ||
| countMux.Lock() | ||
| metrics.SetGauge([]string{"nomad", "agent", "http", "connections"}, float32(connCount)) | ||
| countMux.Unlock() | ||
| } | ||
| }() | ||
|
|
||
| // Return early on errors | ||
| if serverInitializationErrors != nil { | ||
| for _, srv := range srvs { | ||
|
|
@@ -250,44 +268,36 @@ func NewHTTPServers(agent *Agent, config *Config) ([]*HTTPServer, error) { | |
| // | ||
| // If limit > 0, a per-address connection limit will be enabled regardless of | ||
| // TLS. If connLimit == 0 there is no connection limit. | ||
| func makeConnState(isTLS bool, handshakeTimeout time.Duration, connLimit int, logger log.Logger) func(conn net.Conn, state http.ConnState) { | ||
| func makeConnState(isTLS bool, handshakeTimeout time.Duration, connLimit int, connCount *int, connMux *sync.Mutex, logger log.Logger) func(conn net.Conn, state http.ConnState) { | ||
| connLimiter := connLimiter(connLimit, logger) | ||
| if !isTLS || handshakeTimeout == 0 { | ||
| if connLimit > 0 { | ||
| // Still return the connection limiter | ||
| return connLimiter | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| if connLimit > 0 { | ||
| // Return conn state callback with connection limiting and a | ||
| // handshake timeout. | ||
|
|
||
| return func(conn net.Conn, state http.ConnState) { | ||
| connMux.Lock() | ||
|
|
||
| switch state { | ||
| case http.StateNew: | ||
| // Set deadline to prevent slow send before TLS handshake or first | ||
| // byte of request. | ||
| conn.SetDeadline(time.Now().Add(handshakeTimeout)) | ||
| case http.StateActive: | ||
| // Clear read deadline. We should maybe set read timeouts more | ||
| // generally but that's a bigger task as some HTTP endpoints may | ||
| // stream large requests and responses (e.g. snapshot) so we can't | ||
| // set sensible blanket timeouts here. | ||
| conn.SetDeadline(time.Time{}) | ||
| *connCount++ | ||
| case http.StateClosed: | ||
| *connCount-- | ||
| } | ||
|
|
||
| // Call connection limiter | ||
| connLimiter(conn, state) | ||
| connMux.Unlock() | ||
|
|
||
| // Call connection limiter if enabled | ||
| if connLimit > 0 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're already introducing some closures here with the connCount and mutex, adding the |
||
| connLimiter(conn, state) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Return conn state callback with just a handshake timeout | ||
| // (connection limiting disabled). | ||
| // Return conn state callback with connection limiting and a | ||
| // handshake timeout. | ||
| return func(conn net.Conn, state http.ConnState) { | ||
| connMux.Lock() | ||
|
|
||
| switch state { | ||
| case http.StateNew: | ||
| *connCount++ | ||
| // Set deadline to prevent slow send before TLS handshake or first | ||
| // byte of request. | ||
| conn.SetDeadline(time.Now().Add(handshakeTimeout)) | ||
|
|
@@ -297,6 +307,15 @@ func makeConnState(isTLS bool, handshakeTimeout time.Duration, connLimit int, lo | |
| // stream large requests and responses (e.g. snapshot) so we can't | ||
| // set sensible blanket timeouts here. | ||
| conn.SetDeadline(time.Time{}) | ||
| case http.StateClosed: | ||
| *connCount-- | ||
| } | ||
|
|
||
| connMux.Unlock() | ||
|
|
||
| // Call connection limiter if enabled | ||
| if connLimit > 0 { | ||
| connLimiter(conn, state) | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding a lock to the ConnState callback could potentially introduce some contention if there are many incoming requests and a short
telemetry.collectionInterval. This may be worth running some benchmarks.