-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[tctl] Add support for debug sock endpoint with fallback in top #56886
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
830996e
[tctl] Add support for debug sock endpoint with fallback in top
okraport 54712f2
refactor: Simplify top address behaviour based on PR feedback
okraport fa9c2a8
Refactor top to reuse debug client for metrics.
okraport bc6dc5a
fix linting issues
okraport 467344a
Update docs to match support for unix sockets
okraport afb40e4
Fix imports
okraport 7776890
refactor debug service
rosstimothy 8b5fd15
clean up error handling in top command
okraport b0cfc0e
Add footer display for addr
okraport c5fcaed
Docs for diag/client
okraport df22bf7
update docs and add addr parser tests
okraport dd62fff
More docs fixes
okraport 2d0aca7
Fix linting after rebase
okraport 293b27b
Better godocs, more URL test cases, fixed user facing errors
okraport 2f9806c
Fix err trace wrapping semantics
okraport 0679d5e
Merge branch 'master' into okraport/tctl-top-metrics-fallback
okraport 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
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,102 @@ | ||
| // Teleport | ||
| // Copyright (C) 2025 Gravitational, Inc. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package diag | ||
|
|
||
| import ( | ||
| "context" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| dto "github.com/prometheus/client_model/go" | ||
| "github.com/prometheus/common/expfmt" | ||
|
|
||
| "github.com/gravitational/teleport/lib/defaults" | ||
| ) | ||
|
|
||
| // Client is a wrapper around [*http.Client] that provides | ||
| // helpers for fetching metrics from the diagnostic endpoint of Teleport. | ||
| type Client struct { | ||
| endpoint string | ||
| clt *http.Client | ||
| } | ||
|
|
||
| // parseAddress takes a string address and attempts to parse it into a valid URL. | ||
| // The input can either be a valid string URL or a <host>:<port> pair. | ||
| func parseAddress(addr string) (*url.URL, error) { | ||
| u, err := url.Parse(addr) | ||
|
|
||
| if err != nil || u.Scheme == "" || u.Host == "" { | ||
| // Attempt to parse the input as a host:port tuple instead. | ||
| _, _, err = net.SplitHostPort(addr) | ||
| if err != nil { | ||
| return nil, trace.Errorf("address %s is neither a valid URL nor <host>:<port>", addr) | ||
| } | ||
|
|
||
| u = &url.URL{ | ||
| Scheme: "http", | ||
| Host: addr, | ||
| } | ||
| } | ||
|
|
||
| return u, nil | ||
| } | ||
|
|
||
| // NewClient creates a new Client for a given address. | ||
| func NewClient(addr string) (*Client, error) { | ||
| clt, err := defaults.HTTPClient() | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| u, err := parseAddress(addr) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| if u.Scheme != "http" { | ||
| return nil, trace.Errorf("unsupported scheme: %s, please provide a http address", u.Scheme) | ||
| } | ||
|
|
||
| return &Client{ | ||
| endpoint: u.JoinPath("metrics").String(), | ||
| clt: clt, | ||
| }, nil | ||
| } | ||
|
|
||
| // GetMetrics returns prometheus metrics as a map keyed by metric name. | ||
| func (c *Client) GetMetrics(ctx context.Context) (map[string]*dto.MetricFamily, error) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.endpoint, http.NoBody) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| resp, err := c.clt.Do(req) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| var parser expfmt.TextParser | ||
| metrics, err := parser.TextToMetricFamilies(resp.Body) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| return metrics, nil | ||
| } |
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,87 @@ | ||
| /* | ||
| * Teleport | ||
| * Copyright (C) 2025 Gravitational, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package diag | ||
|
|
||
| import ( | ||
| "net/url" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestClientAddressParsing(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testCases := []struct { | ||
| addr string | ||
| url *url.URL | ||
| }{ | ||
| { | ||
| addr: "http://127.0.0.1:3000", | ||
| url: &url.URL{ | ||
| Scheme: "http", | ||
| Host: "127.0.0.1:3000", | ||
| }, | ||
| }, | ||
| { | ||
| addr: "http://localhost:3000", | ||
| url: &url.URL{ | ||
| Scheme: "http", | ||
| Host: "localhost:3000", | ||
| }, | ||
| }, | ||
| { | ||
| addr: "localhost:3000", | ||
| url: &url.URL{ | ||
| Scheme: "http", | ||
| Host: "localhost:3000", | ||
| }, | ||
| }, | ||
| { | ||
| addr: "127.0.0.1:3000", | ||
| url: &url.URL{ | ||
| Scheme: "http", | ||
| Host: "127.0.0.1:3000", | ||
| }, | ||
| }, | ||
| { | ||
| addr: "badurl:300:9:1", | ||
| }, | ||
| { | ||
| addr: "http//badurl", | ||
| }, | ||
| { | ||
| addr: "/var/lib/file.sock", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.addr, func(t *testing.T) { | ||
| u, err := parseAddress(tc.addr) | ||
| if tc.url == nil { | ||
|
okraport marked this conversation as resolved.
|
||
| require.Error(t, err) | ||
| require.Nil(t, u) | ||
| } else { | ||
| require.NoError(t, err) | ||
| require.Equal(t, tc.url.Host, u.Host) | ||
| require.Equal(t, tc.url.Scheme, u.Scheme) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.