-
Notifications
You must be signed in to change notification settings - Fork 214
PMM-14858 Skip built-in ClickHouse scrape for external instances #5369
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
4nte
wants to merge
6
commits into
v3
Choose a base branch
from
PMM-14858-fix-external-clickhouse-scrape
base: v3
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9829c1
PMM-14858 Skip built-in ClickHouse scrape for external instances
4nte 3861407
PMM-14858 Improve formatting
4nte e39e7c2
PMM-14858 Extract `127.0.0.1` literal into localhost constant
4nte 9db24f9
PMM-14858 Test NewVictoriaMetrics with nil CH params
4nte d3844dc
Merge branch 'v3' into PMM-14858-fix-external-clickhouse-scrape
4nte d6f828a
PMM-14858 Fix godoc on `URL()`
4nte 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Copyright (C) 2023 Percona LLC | ||
| // | ||
| // 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 <https://www.gnu.org/licenses/>. | ||
|
|
||
| package models | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "net/url" | ||
| "strconv" | ||
| ) | ||
|
|
||
| // ClickHouseParams represents ClickHouse server params. | ||
| type ClickHouseParams struct { | ||
| BuiltinDisabled bool | ||
| url *url.URL | ||
| } | ||
|
|
||
| // ExternalClickHouse returns true if ClickHouse is configured externally. | ||
| func (p *ClickHouseParams) ExternalClickHouse() bool { | ||
| return p.url.Hostname() != "127.0.0.1" | ||
|
4nte marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // URL returns the ClickHouse URL. | ||
| func (p *ClickHouseParams) URL() *url.URL { | ||
| u := *p.url | ||
| return &u | ||
| } | ||
|
|
||
| // NewClickHouseParams returns validated ClickHouse configuration params, | ||
| // or an error if any required field is missing or malformed. | ||
| func NewClickHouseParams(addr, dbName, dbUsername, dbPassword string, builtinDisabled bool) (*ClickHouseParams, error) { | ||
| if addr == "" { | ||
| return nil, fmt.Errorf("addr is required") | ||
| } | ||
| host, port, err := net.SplitHostPort(addr) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid addr %q: %w", addr, err) | ||
| } | ||
| if host == "" { | ||
| return nil, fmt.Errorf("invalid addr %q: empty host", addr) | ||
| } | ||
| if _, err := strconv.ParseUint(port, 10, 16); err != nil { | ||
| return nil, fmt.Errorf("invalid port in addr %q: %w", addr, err) | ||
| } | ||
| if dbName == "" { | ||
| return nil, fmt.Errorf("database name is required") | ||
| } | ||
| if dbUsername == "" { | ||
| return nil, fmt.Errorf("username is required") | ||
| } | ||
|
|
||
| return &ClickHouseParams{ | ||
| BuiltinDisabled: builtinDisabled, | ||
| url: &url.URL{ | ||
| Scheme: "tcp", | ||
| User: url.UserPassword(dbUsername, dbPassword), | ||
| Host: addr, | ||
| Path: dbName, | ||
| }, | ||
| }, 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,101 @@ | ||
| // Copyright (C) 2023 Percona LLC | ||
| // | ||
| // 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 <https://www.gnu.org/licenses/>. | ||
|
|
||
| package models | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestNewClickHouseParams(t *testing.T) { | ||
| t.Run("valid", func(t *testing.T) { | ||
| p, err := NewClickHouseParams("127.0.0.1:9000", "pmm", "default", "clickhouse", false) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "tcp://default:clickhouse@127.0.0.1:9000/pmm", p.URL().String()) | ||
| assert.False(t, p.BuiltinDisabled) | ||
| }) | ||
|
|
||
| t.Run("valid builtin disabled", func(t *testing.T) { | ||
| p, err := NewClickHouseParams("127.0.0.1:9000", "pmm", "default", "clickhouse", true) | ||
| require.NoError(t, err) | ||
| assert.True(t, p.BuiltinDisabled) | ||
| }) | ||
|
|
||
| t.Run("valid empty password", func(t *testing.T) { | ||
| _, err := NewClickHouseParams("127.0.0.1:9000", "pmm", "default", "", false) | ||
| require.NoError(t, err) | ||
| }) | ||
|
|
||
| errCases := []struct { | ||
| name string | ||
| addr string | ||
| dbName string | ||
| dbUsername string | ||
| dbPassword string | ||
| wantErrSub string | ||
| }{ | ||
| {"empty addr", "", "pmm", "default", "clickhouse", "addr is required"}, | ||
| {"missing port", "127.0.0.1", "pmm", "default", "clickhouse", "invalid addr"}, | ||
| {"empty host", ":9000", "pmm", "default", "clickhouse", "empty host"}, | ||
| {"non numeric port", "localhost:abc", "pmm", "default", "clickhouse", "invalid port"}, | ||
| {"port out of range", "localhost:99999", "pmm", "default", "clickhouse", "invalid port"}, | ||
| {"empty db name", "127.0.0.1:9000", "", "default", "clickhouse", "database name is required"}, | ||
| {"empty username", "127.0.0.1:9000", "pmm", "", "clickhouse", "username is required"}, | ||
| } | ||
| for _, tc := range errCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| _, err := NewClickHouseParams(tc.addr, tc.dbName, tc.dbUsername, tc.dbPassword, false) | ||
| require.Error(t, err) | ||
| assert.ErrorContains(t, err, tc.wantErrSub) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCHParamsExternalClickHouse(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| addr string | ||
| want bool | ||
| }{ | ||
| {"loopback", "127.0.0.1:9000", false}, | ||
| {"localhost", "localhost:9000", true}, | ||
| {"external host", "ch-01.test.net:9000", true}, | ||
| {"wildcard", "0.0.0.0:9000", true}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| p, err := NewClickHouseParams(tc.addr, "pmm", "default", "clickhouse", false) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tc.want, p.ExternalClickHouse()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCHParamsURL(t *testing.T) { | ||
| t.Run("simple", func(t *testing.T) { | ||
| p, err := NewClickHouseParams("127.0.0.1:9000", "pmm", "default", "clickhouse", false) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "tcp://default:clickhouse@127.0.0.1:9000/pmm", p.URL().String()) | ||
| }) | ||
|
|
||
| t.Run("password with special chars", func(t *testing.T) { | ||
| p, err := NewClickHouseParams("127.0.0.1:9000", "pmm", "default", "p@ss/word", false) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "tcp://default:p%40ss%2Fword@127.0.0.1:9000/pmm", p.URL().String()) | ||
| }) | ||
| } |
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
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.
I do not see any benefit from this additional var. The fact of set
PMM_CLICKHOUSE_ADDRlooks enough that external CH usage is requested.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.
I was considering following options:
PMM_CLICKHOUSE_ADDRhostname is not127.0.0.1PMM_DISABLE_BUILTIN_CLICKHOUSEis setI agree
PMM_CLICKHOUSE_ADDRis sufficient to determine this and would prefer it too. My concern is thatPMM_DISABLE_BUILTIN_CLICKHOUSEis already explicitly documented as the flag that disables internal CH and managed-init already relies on it.Not checking it here, while checking it in
managed-initseemed inconsistent, unless we treatPMM_DISABLE_BUILTIN_CLICKHOUSEas an env var that is exclusive tomanaged-init.Having all that said, do you still think we should just infer from
PMM_CLICKHOUSE_ADDRand not lookupPMM_DISABLE_BUILTIN_CLICKHOUSE?Uh oh!
There was an error while loading. Please reload this page.
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.
We agreed to deprecate all
PMM_DISABLE_BUILTIN_env variables, since their use conflicts withPMM_<TECH>_ADDRvariables, which should be the only source of truth. Therefore, this fix will rely onPMM_CLICKHOUSE_ADDR.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.
I've also discussed this with @ademidoff, we all agree
PMM_CLICKHOUSE_ADDRis technically sufficient to determine whether CH is internal or external.I will omit
PMM_DISABLE_BUILTIN_CLICKHOUSEand rely onPMM_CLICKHOUSE_ADDRas a single source of truth.Since
PMM_DISABLE_BUILTIN_CLICKHOUSEis obviously redundant and creates ambiguity, I will create a separate issue to deprecatePMM_DISABLE_BUILTIN_CLICKHOUSEin favor of inferring fromPMM_CLICKHOUSE_ADDR