-
Notifications
You must be signed in to change notification settings - Fork 450
feat(metrics)!: add server label to per-worker series #2396
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
Open
nicolas-grekas
wants to merge
1
commit into
php:main
Choose a base branch
from
nicolas-grekas:metrics-server-label
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.
Open
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
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
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 |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package caddy | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| "github.com/caddyserver/caddy/v2/modules/caddyhttp" | ||
| ) | ||
|
|
||
| // resolveScopeLabel picks a stable, human-friendly identifier for this | ||
| // module's scope so metric/log emitters can render it (e.g. | ||
| // server="api.example.com") instead of the opaque numeric id. | ||
| // Cascade: | ||
| // 1. First host of the route's host matcher. | ||
| // 2. Caddy server name when user-set (i.e. not the auto srvN form). | ||
| // 3. First listener address of the server. | ||
| func (f *FrankenPHPModule) resolveScopeLabel(srv *caddyhttp.Server) string { | ||
| if h := findHostInRoutes(srv.Routes, f); h != "" { | ||
| return h | ||
| } | ||
| if name := srv.Name(); name != "" && !isAutoServerName(name) { | ||
| return name | ||
| } | ||
| if len(srv.Listen) > 0 { | ||
| return srv.Listen[0] | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| // findHostInRoutes walks routes (recursing into Subroute handlers) to | ||
| // locate the route that contains target, then returns the first host of | ||
| // that route's host matcher. Returns "" if no enclosing route or no host | ||
| // matcher is found. | ||
| func findHostInRoutes(routes caddyhttp.RouteList, target caddyhttp.MiddlewareHandler) string { | ||
| for _, route := range routes { | ||
| if !routeContainsHandler(route, target) { | ||
| continue | ||
| } | ||
| for _, mset := range route.MatcherSets { | ||
| for _, m := range mset { | ||
| hp, ok := m.(*caddyhttp.MatchHost) | ||
| if !ok || hp == nil || len(*hp) == 0 { | ||
| continue | ||
| } | ||
| return (*hp)[0] | ||
| } | ||
| } | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| func routeContainsHandler(route caddyhttp.Route, target caddyhttp.MiddlewareHandler) bool { | ||
| for _, h := range route.Handlers { | ||
| if h == target { | ||
| return true | ||
| } | ||
| if sub, ok := h.(*caddyhttp.Subroute); ok { | ||
| for _, r := range sub.Routes { | ||
| if routeContainsHandler(r, target) { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // isAutoServerName reports whether name is one of Caddy's auto-assigned | ||
| // server names (srv0, srv1, ...). Anything else is treated as user-set. | ||
| func isAutoServerName(name string) bool { | ||
| if !strings.HasPrefix(name, "srv") || len(name) <= 3 { | ||
| return false | ||
| } | ||
| for _, c := range name[3:] { | ||
| if c < '0' || c > '9' { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
this seems like a very common thing to prefix servers with, likely not the best check