fix(inputs.clickhouse): Omit zookeeper metrics on clickhouse cloud (#14443)

This commit is contained in:
Joshua Powers 2023-12-13 09:14:19 -07:00 committed by GitHub
parent e328e0680d
commit e05d622ac6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 2 deletions

View File

@ -3,6 +3,9 @@
This plugin gathers the statistic data from
[ClickHouse](https://github.com/ClickHouse/ClickHouse) server.
User's on Clickhouse Cloud will not see the Zookeeper metrics as they may not
have permissions to query those tables.
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
In addition to the plugin-specific configuration settings, plugins support
@ -33,6 +36,12 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## https://clickhouse.tech/docs/en/interfaces/http/
servers = ["http://127.0.0.1:8123"]
## Server Variant
## When set to "managed", some queries are excluded from being run. This is
## useful for instances hosted in ClickHouse Cloud where certain tables are
## not available.
# variant = "self-hosted"
## If "auto_discovery"" is "true" plugin tries to connect to all servers
## available in the cluster with using same "user:password" described in
## "user" and "password" parameters and get this server hostname list from

View File

@ -54,7 +54,9 @@ type ClickHouse struct {
ClusterInclude []string `toml:"cluster_include"`
ClusterExclude []string `toml:"cluster_exclude"`
Timeout config.Duration `toml:"timeout"`
HTTPClient http.Client
Variant string `toml:"variant"`
HTTPClient http.Client
tls.ClientConfig
}
@ -62,6 +64,19 @@ func (*ClickHouse) SampleConfig() string {
return sampleConfig
}
func (ch *ClickHouse) Init() error {
switch ch.Variant {
case "":
ch.Variant = "self-hosted"
case "self-hosted", "managed":
// valid options
default:
return fmt.Errorf("unknown variant %q", ch.Variant)
}
return nil
}
// Start ClickHouse input service
func (ch *ClickHouse) Start(telegraf.Accumulator) error {
timeout := defaultTimeout
@ -130,7 +145,6 @@ func (ch *ClickHouse) Gather(acc telegraf.Accumulator) (err error) {
for i := range connects {
metricsFuncs := []func(acc telegraf.Accumulator, conn *connect) error{
ch.tables,
ch.zookeeper,
ch.replicationQueue,
ch.detachedParts,
ch.dictionaries,
@ -140,6 +154,12 @@ func (ch *ClickHouse) Gather(acc telegraf.Accumulator) (err error) {
ch.textLog,
}
// Managed instances on Clickhouse Cloud does not give a user
// permissions to the zookeeper table
if ch.Variant != "managed" {
metricsFuncs = append(metricsFuncs, ch.zookeeper)
}
for _, metricFunc := range metricsFuncs {
if err := metricFunc(acc, &connects[i]); err != nil {
acc.AddError(err)

View File

@ -492,6 +492,48 @@ func TestGatherWithSomeTablesNotExists(t *testing.T) {
acc.AssertDoesNotContainMeasurement(t, "clickhouse_text_log")
}
func TestGatherClickhouseCloud(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type result struct {
Data interface{} `json:"data"`
}
enc := json.NewEncoder(w)
switch query := r.URL.Query().Get("query"); {
case strings.Contains(query, "zk_exists"):
err := enc.Encode(result{
Data: []struct {
ZkExists chUInt64 `json:"zk_exists"`
}{
{
ZkExists: 1,
},
},
})
require.NoError(t, err)
case strings.Contains(query, "zk_root_nodes"):
err := enc.Encode(result{
Data: []struct {
ZkRootNodes chUInt64 `json:"zk_root_nodes"`
}{
{
ZkRootNodes: 2,
},
},
})
require.NoError(t, err)
}
}))
defer ts.Close()
ch := &ClickHouse{
Servers: []string{ts.URL},
Variant: "managed",
}
acc := &testutil.Accumulator{}
require.NoError(t, ch.Gather(acc))
acc.AssertDoesNotContainMeasurement(t, "clickhouse_zookeeper")
}
func TestWrongJSONMarshalling(t *testing.T) {
var (
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

View File

@ -16,6 +16,12 @@
## https://clickhouse.tech/docs/en/interfaces/http/
servers = ["http://127.0.0.1:8123"]
## Server Variant
## When set to "managed", some queries are excluded from being run. This is
## useful for instances hosted in ClickHouse Cloud where certain tables are
## not available.
# variant = "self-hosted"
## If "auto_discovery"" is "true" plugin tries to connect to all servers
## available in the cluster with using same "user:password" described in
## "user" and "password" parameters and get this server hostname list from