From 935ed7ad7c9e174e2f272f200648ab79f3f4bf9a Mon Sep 17 00:00:00 2001 From: appare45 Date: Fri, 29 Nov 2024 15:02:22 +0900 Subject: [PATCH 1/8] add keep_alive option for mackerel api client --- command/command.go | 6 +++--- command/command_api_test.go | 2 +- command/command_test.go | 12 ++++++------ commands.go | 2 +- config/config.go | 4 ++++ config/config_test.go | 4 ++++ mackerel/api.go | 6 +++++- mackerel/api_test.go | 3 ++- mackerel/check_test.go | 4 ++-- 9 files changed, 28 insertions(+), 15 deletions(-) diff --git a/command/command.go b/command/command.go index d60815660..380103ba1 100644 --- a/command/command.go +++ b/command/command.go @@ -693,8 +693,8 @@ func buildUA(ver, rev string) string { } // NewMackerelClient returns Mackerel API client for mackerel-agent -func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool) (*mackerel.API, error) { - api, err := mackerel.NewAPI(apibase, apikey, verbose) +func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool, keepAlive bool) (*mackerel.API, error) { + api, err := mackerel.NewAPI(apibase, apikey, verbose, keepAlive) if err != nil { return nil, err } @@ -708,7 +708,7 @@ func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool) (*mackere // Prepare sets up API and registers the host data to the Mackerel server. // Use returned values to call Run(). func Prepare(conf *config.Config, ameta *AgentMeta) (*App, error) { - api, err := NewMackerelClient(conf.Apibase, conf.Apikey, ameta.Version, ameta.Revision, conf.Verbose) + api, err := NewMackerelClient(conf.Apibase, conf.Apikey, ameta.Version, ameta.Revision, conf.Verbose, conf.KeepAlive) if err != nil { return nil, fmt.Errorf("failed to prepare an api: %s", err.Error()) } diff --git a/command/command_api_test.go b/command/command_api_test.go index 8e808be29..cbab04ea8 100644 --- a/command/command_api_test.go +++ b/command/command_api_test.go @@ -33,7 +33,7 @@ func TestAPIRequestHeader(t *testing.T) { })) defer ts.Close() - api, err := NewMackerelClient(ts.URL, apiKey, ver, rev, false) + api, err := NewMackerelClient(ts.URL, apiKey, ver, rev, false, true) if err != nil { t.Errorf("something went wrong while creating new mackerel client: %+v", err) } diff --git a/command/command_test.go b/command/command_test.go index 1de72cec9..3ed37934f 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -346,7 +346,7 @@ func TestLoop(t *testing.T) { }, } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) if err != nil { t.Fatal(err) } @@ -442,7 +442,7 @@ func TestLoop_NetworkError(t *testing.T) { }, } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) if err != nil { t.Fatal(err) } @@ -524,7 +524,7 @@ func TestLoop_NetworkErrorWithRecovery(t *testing.T) { }, } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) if err != nil { t.Fatal(err) } @@ -585,7 +585,7 @@ func TestReportCheckMonitors(t *testing.T) { return tc.Status, jsonObject{} } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) if err != nil { t.Fatal(err) } @@ -643,7 +643,7 @@ func TestReportCheckMonitors_NetworkError(t *testing.T) { return http.StatusSeeOther, jsonObject{} } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) if err != nil { t.Fatal(err) } @@ -708,7 +708,7 @@ func TestReportCheckMonitors_NetworkErrorWithRecovery(t *testing.T) { return http.StatusOK, jsonObject{} } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) if err != nil { t.Fatal(err) } diff --git a/commands.go b/commands.go index 68be727f4..0ceab5066 100644 --- a/commands.go +++ b/commands.go @@ -156,7 +156,7 @@ func doRetire(fs *flag.FlagSet, argv []string) error { return fmt.Errorf("hostID file is not found or empty") } - api, err := command.NewMackerelClient(conf.Apibase, conf.Apikey, version, gitcommit, conf.Verbose) + api, err := command.NewMackerelClient(conf.Apibase, conf.Apikey, version, gitcommit, conf.Verbose, conf.KeepAlive) if err != nil { return fmt.Errorf("faild to create api client: %s", err) } diff --git a/config/config.go b/config/config.go index 7979b04cd..79ffe9d3b 100644 --- a/config/config.go +++ b/config/config.go @@ -102,6 +102,7 @@ type Config struct { Verbose bool Silent bool Diagnostic bool `toml:"diagnostic"` + KeepAlive bool `toml:"keep_alive"` DisplayName string `toml:"display_name"` HostStatus HostStatus `toml:"host_status" conf:"parent"` Disks Disks `toml:"disks" conf:"parent"` @@ -447,6 +448,9 @@ func LoadConfig(conffile string) (*Config, error) { if !config.Diagnostic { config.Diagnostic = DefaultConfig.Diagnostic } + if !config.KeepAlive { + config.KeepAlive = true + } return config, err } diff --git a/config/config_test.go b/config/config_test.go index efbc93f06..0babd42e7 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -99,6 +99,10 @@ func TestLoadConfig(t *testing.T) { if config.Filesystems.UseMountpoint != false { t.Error("should be false (default value should be used)") } + + if config.KeepAlive != true { + t.Error("should be true (default value should be used)") + } } var sampleConfigWithHostStatus = ` diff --git a/mackerel/api.go b/mackerel/api.go index c4a35c1d5..3147ca02a 100644 --- a/mackerel/api.go +++ b/mackerel/api.go @@ -3,6 +3,7 @@ package mackerel import ( "errors" "fmt" + "net/http" "net/url" "github.com/mackerelio/golib/logging" @@ -56,12 +57,15 @@ func infoError(msg string) *InfoError { } // NewAPI creates a new instance of API. -func NewAPI(rawurl string, apiKey string, verbose bool) (*API, error) { +func NewAPI(rawurl string, apiKey string, verbose bool, keepAlive bool) (*API, error) { c, err := mkr.NewClientWithOptions(apiKey, rawurl, verbose) if err != nil { return nil, err } c.PrioritizedLogger = logger + c.HTTPClient.Transport = &http.Transport{ + DisableKeepAlives: true, + } return &API{Client: c}, nil } diff --git a/mackerel/api_test.go b/mackerel/api_test.go index 7eee7fad7..07823846d 100644 --- a/mackerel/api_test.go +++ b/mackerel/api_test.go @@ -18,6 +18,7 @@ func TestNewAPI(t *testing.T) { "http://example.com", "dummy-key", true, + true, ) if err != nil { @@ -72,7 +73,7 @@ func TestFindHostByCustomIdentifier(t *testing.T) { })) defer ts.Close() - api, _ := NewAPI(ts.URL, "dummy-key", false) + api, _ := NewAPI(ts.URL, "dummy-key", false, true) var tests = []struct { customIdentifier string diff --git a/mackerel/check_test.go b/mackerel/check_test.go index 18e1ee8ce..458be48de 100644 --- a/mackerel/check_test.go +++ b/mackerel/check_test.go @@ -61,7 +61,7 @@ func TestReportCheckMonitors(t *testing.T) { })) defer ts.Close() - api, _ := NewAPI(ts.URL, "dummy-key", false) + api, _ := NewAPI(ts.URL, "dummy-key", false, true) err := api.ReportCheckMonitors("9rxGOHfVF8F", []*checks.Report{ { @@ -153,7 +153,7 @@ func TestReportCheckMonitorsCompat(t *testing.T) { maxAttemts: 0, }, } - api, _ := NewAPI(ts.URL, "dummy-key", false) + api, _ := NewAPI(ts.URL, "dummy-key", false, true) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { api.ReportCheckMonitors("xxx", []*checks.Report{ From 11d9b0228448cd4010afedc98ecac67574d13680 Mon Sep 17 00:00:00 2001 From: appare45 Date: Wed, 11 Dec 2024 14:35:47 +0900 Subject: [PATCH 2/8] change keep_alive option to disable_keep_alive --- command/command.go | 6 +++--- commands.go | 2 +- config/config.go | 40 ++++++++++++++++++++-------------------- config/config_test.go | 4 ++-- mackerel/api.go | 10 ++++++---- 5 files changed, 32 insertions(+), 30 deletions(-) diff --git a/command/command.go b/command/command.go index 380103ba1..b9367814c 100644 --- a/command/command.go +++ b/command/command.go @@ -693,8 +693,8 @@ func buildUA(ver, rev string) string { } // NewMackerelClient returns Mackerel API client for mackerel-agent -func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool, keepAlive bool) (*mackerel.API, error) { - api, err := mackerel.NewAPI(apibase, apikey, verbose, keepAlive) +func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool, disableKeepAlive bool) (*mackerel.API, error) { + api, err := mackerel.NewAPI(apibase, apikey, verbose, disableKeepAlive) if err != nil { return nil, err } @@ -708,7 +708,7 @@ func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool, keepAlive // Prepare sets up API and registers the host data to the Mackerel server. // Use returned values to call Run(). func Prepare(conf *config.Config, ameta *AgentMeta) (*App, error) { - api, err := NewMackerelClient(conf.Apibase, conf.Apikey, ameta.Version, ameta.Revision, conf.Verbose, conf.KeepAlive) + api, err := NewMackerelClient(conf.Apibase, conf.Apikey, ameta.Version, ameta.Revision, conf.Verbose, conf.DisableKeepAlive) if err != nil { return nil, fmt.Errorf("failed to prepare an api: %s", err.Error()) } diff --git a/commands.go b/commands.go index 0ceab5066..022187ba3 100644 --- a/commands.go +++ b/commands.go @@ -156,7 +156,7 @@ func doRetire(fs *flag.FlagSet, argv []string) error { return fmt.Errorf("hostID file is not found or empty") } - api, err := command.NewMackerelClient(conf.Apibase, conf.Apikey, version, gitcommit, conf.Verbose, conf.KeepAlive) + api, err := command.NewMackerelClient(conf.Apibase, conf.Apikey, version, gitcommit, conf.Verbose, conf.DisableKeepAlive) if err != nil { return fmt.Errorf("faild to create api client: %s", err) } diff --git a/config/config.go b/config/config.go index 79ffe9d3b..db77858a3 100644 --- a/config/config.go +++ b/config/config.go @@ -93,24 +93,24 @@ func (c *CloudPlatform) UnmarshalText(text []byte) error { // Config represents mackerel-agent's configuration file. type Config struct { - Apibase string - Apikey string - Root string - Pidfile string - Conffile string - Roles []string - Verbose bool - Silent bool - Diagnostic bool `toml:"diagnostic"` - KeepAlive bool `toml:"keep_alive"` - DisplayName string `toml:"display_name"` - HostStatus HostStatus `toml:"host_status" conf:"parent"` - Disks Disks `toml:"disks" conf:"parent"` - Filesystems Filesystems `toml:"filesystems" conf:"parent"` - Interfaces Interfaces `toml:"interfaces" conf:"parent"` - HTTPProxy string `toml:"http_proxy"` - HTTPSProxy string `toml:"https_proxy"` - CloudPlatform CloudPlatform `toml:"cloud_platform"` + Apibase string + Apikey string + Root string + Pidfile string + Conffile string + Roles []string + Verbose bool + Silent bool + Diagnostic bool `toml:"diagnostic"` + DisableKeepAlive bool `toml:"disable_keep_alive"` + DisplayName string `toml:"display_name"` + HostStatus HostStatus `toml:"host_status" conf:"parent"` + Disks Disks `toml:"disks" conf:"parent"` + Filesystems Filesystems `toml:"filesystems" conf:"parent"` + Interfaces Interfaces `toml:"interfaces" conf:"parent"` + HTTPProxy string `toml:"http_proxy"` + HTTPSProxy string `toml:"https_proxy"` + CloudPlatform CloudPlatform `toml:"cloud_platform"` // This Plugin field is used to decode the toml file. After reading the // configuration from file, this field is set to nil. @@ -448,8 +448,8 @@ func LoadConfig(conffile string) (*Config, error) { if !config.Diagnostic { config.Diagnostic = DefaultConfig.Diagnostic } - if !config.KeepAlive { - config.KeepAlive = true + if !config.DisableKeepAlive { + config.DisableKeepAlive = DefaultConfig.DisableKeepAlive } return config, err diff --git a/config/config_test.go b/config/config_test.go index 0babd42e7..208c39e91 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -100,8 +100,8 @@ func TestLoadConfig(t *testing.T) { t.Error("should be false (default value should be used)") } - if config.KeepAlive != true { - t.Error("should be true (default value should be used)") + if config.DisableKeepAlive != false { + t.Error("should be false (default value should be used)") } } diff --git a/mackerel/api.go b/mackerel/api.go index 3147ca02a..0b58e2cd6 100644 --- a/mackerel/api.go +++ b/mackerel/api.go @@ -57,15 +57,17 @@ func infoError(msg string) *InfoError { } // NewAPI creates a new instance of API. -func NewAPI(rawurl string, apiKey string, verbose bool, keepAlive bool) (*API, error) { +func NewAPI(rawurl string, apiKey string, verbose bool, disableKeepAlive bool) (*API, error) { c, err := mkr.NewClientWithOptions(apiKey, rawurl, verbose) if err != nil { return nil, err } c.PrioritizedLogger = logger - c.HTTPClient.Transport = &http.Transport{ - DisableKeepAlives: true, - } + t := http.DefaultTransport.(*http.Transport).Clone() + print(disableKeepAlive) + t.DisableKeepAlives = disableKeepAlive + c.HTTPClient.Transport = t + return &API{Client: c}, nil } From c1278064c4b5f742a0b58c7b2933206aa0fcd72d Mon Sep 17 00:00:00 2001 From: appare45 Date: Wed, 11 Dec 2024 14:56:35 +0900 Subject: [PATCH 3/8] Changed disable_keep_alive options for the tests --- mackerel/api_test.go | 4 ++-- mackerel/check_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mackerel/api_test.go b/mackerel/api_test.go index 07823846d..cc2120a21 100644 --- a/mackerel/api_test.go +++ b/mackerel/api_test.go @@ -18,7 +18,7 @@ func TestNewAPI(t *testing.T) { "http://example.com", "dummy-key", true, - true, + false, ) if err != nil { @@ -73,7 +73,7 @@ func TestFindHostByCustomIdentifier(t *testing.T) { })) defer ts.Close() - api, _ := NewAPI(ts.URL, "dummy-key", false, true) + api, _ := NewAPI(ts.URL, "dummy-key", false, false) var tests = []struct { customIdentifier string diff --git a/mackerel/check_test.go b/mackerel/check_test.go index 458be48de..74fc82583 100644 --- a/mackerel/check_test.go +++ b/mackerel/check_test.go @@ -61,7 +61,7 @@ func TestReportCheckMonitors(t *testing.T) { })) defer ts.Close() - api, _ := NewAPI(ts.URL, "dummy-key", false, true) + api, _ := NewAPI(ts.URL, "dummy-key", false, false) err := api.ReportCheckMonitors("9rxGOHfVF8F", []*checks.Report{ { @@ -153,7 +153,7 @@ func TestReportCheckMonitorsCompat(t *testing.T) { maxAttemts: 0, }, } - api, _ := NewAPI(ts.URL, "dummy-key", false, true) + api, _ := NewAPI(ts.URL, "dummy-key", false, false) for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { api.ReportCheckMonitors("xxx", []*checks.Report{ From fb780e6e9e9672c3d65a77b8fd431e424914bf0c Mon Sep 17 00:00:00 2001 From: appare45 Date: Fri, 13 Dec 2024 12:21:24 +0900 Subject: [PATCH 4/8] Remove pring debugging --- mackerel/api.go | 1 - 1 file changed, 1 deletion(-) diff --git a/mackerel/api.go b/mackerel/api.go index 0b58e2cd6..fd06b1b06 100644 --- a/mackerel/api.go +++ b/mackerel/api.go @@ -64,7 +64,6 @@ func NewAPI(rawurl string, apiKey string, verbose bool, disableKeepAlive bool) ( } c.PrioritizedLogger = logger t := http.DefaultTransport.(*http.Transport).Clone() - print(disableKeepAlive) t.DisableKeepAlives = disableKeepAlive c.HTTPClient.Transport = t From 7022f7c45b4f19ef3958c39b9ba09a8f6297d6fc Mon Sep 17 00:00:00 2001 From: appare45 Date: Fri, 20 Dec 2024 11:15:33 +0900 Subject: [PATCH 5/8] Changed Mackerel api client tests' disable_keep_alive --- command/command_api_test.go | 2 +- command/command_test.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/command/command_api_test.go b/command/command_api_test.go index cbab04ea8..bcdd8091c 100644 --- a/command/command_api_test.go +++ b/command/command_api_test.go @@ -33,7 +33,7 @@ func TestAPIRequestHeader(t *testing.T) { })) defer ts.Close() - api, err := NewMackerelClient(ts.URL, apiKey, ver, rev, false, true) + api, err := NewMackerelClient(ts.URL, apiKey, ver, rev, false, false) if err != nil { t.Errorf("something went wrong while creating new mackerel client: %+v", err) } diff --git a/command/command_test.go b/command/command_test.go index 3ed37934f..974256e3d 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -346,7 +346,7 @@ func TestLoop(t *testing.T) { }, } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false) if err != nil { t.Fatal(err) } @@ -442,7 +442,7 @@ func TestLoop_NetworkError(t *testing.T) { }, } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false) if err != nil { t.Fatal(err) } @@ -524,7 +524,7 @@ func TestLoop_NetworkErrorWithRecovery(t *testing.T) { }, } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false) if err != nil { t.Fatal(err) } @@ -585,7 +585,7 @@ func TestReportCheckMonitors(t *testing.T) { return tc.Status, jsonObject{} } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false) if err != nil { t.Fatal(err) } @@ -643,7 +643,7 @@ func TestReportCheckMonitors_NetworkError(t *testing.T) { return http.StatusSeeOther, jsonObject{} } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false) if err != nil { t.Fatal(err) } @@ -708,7 +708,7 @@ func TestReportCheckMonitors_NetworkErrorWithRecovery(t *testing.T) { return http.StatusOK, jsonObject{} } - api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, true) + api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, true, false) if err != nil { t.Fatal(err) } From 05dcf6b0f4106f07add25777c6b3f197ef54986d Mon Sep 17 00:00:00 2001 From: appare45 Date: Fri, 20 Dec 2024 11:20:47 +0900 Subject: [PATCH 6/8] Changed disable_keep_alive to disable_http_keep_alive --- command/command.go | 6 +++--- commands.go | 2 +- config/config.go | 40 ++++++++++++++++++++-------------------- config/config_test.go | 2 +- mackerel/api.go | 4 ++-- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/command/command.go b/command/command.go index b9367814c..e291429ec 100644 --- a/command/command.go +++ b/command/command.go @@ -693,8 +693,8 @@ func buildUA(ver, rev string) string { } // NewMackerelClient returns Mackerel API client for mackerel-agent -func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool, disableKeepAlive bool) (*mackerel.API, error) { - api, err := mackerel.NewAPI(apibase, apikey, verbose, disableKeepAlive) +func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool, disableHttpKeepAlive bool) (*mackerel.API, error) { + api, err := mackerel.NewAPI(apibase, apikey, verbose, disableHttpKeepAlive) if err != nil { return nil, err } @@ -708,7 +708,7 @@ func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool, disableKe // Prepare sets up API and registers the host data to the Mackerel server. // Use returned values to call Run(). func Prepare(conf *config.Config, ameta *AgentMeta) (*App, error) { - api, err := NewMackerelClient(conf.Apibase, conf.Apikey, ameta.Version, ameta.Revision, conf.Verbose, conf.DisableKeepAlive) + api, err := NewMackerelClient(conf.Apibase, conf.Apikey, ameta.Version, ameta.Revision, conf.Verbose, conf.DisableHttpKeepAlive) if err != nil { return nil, fmt.Errorf("failed to prepare an api: %s", err.Error()) } diff --git a/commands.go b/commands.go index 022187ba3..069bea95c 100644 --- a/commands.go +++ b/commands.go @@ -156,7 +156,7 @@ func doRetire(fs *flag.FlagSet, argv []string) error { return fmt.Errorf("hostID file is not found or empty") } - api, err := command.NewMackerelClient(conf.Apibase, conf.Apikey, version, gitcommit, conf.Verbose, conf.DisableKeepAlive) + api, err := command.NewMackerelClient(conf.Apibase, conf.Apikey, version, gitcommit, conf.Verbose, conf.DisableHttpKeepAlive) if err != nil { return fmt.Errorf("faild to create api client: %s", err) } diff --git a/config/config.go b/config/config.go index db77858a3..d11903298 100644 --- a/config/config.go +++ b/config/config.go @@ -93,24 +93,24 @@ func (c *CloudPlatform) UnmarshalText(text []byte) error { // Config represents mackerel-agent's configuration file. type Config struct { - Apibase string - Apikey string - Root string - Pidfile string - Conffile string - Roles []string - Verbose bool - Silent bool - Diagnostic bool `toml:"diagnostic"` - DisableKeepAlive bool `toml:"disable_keep_alive"` - DisplayName string `toml:"display_name"` - HostStatus HostStatus `toml:"host_status" conf:"parent"` - Disks Disks `toml:"disks" conf:"parent"` - Filesystems Filesystems `toml:"filesystems" conf:"parent"` - Interfaces Interfaces `toml:"interfaces" conf:"parent"` - HTTPProxy string `toml:"http_proxy"` - HTTPSProxy string `toml:"https_proxy"` - CloudPlatform CloudPlatform `toml:"cloud_platform"` + Apibase string + Apikey string + Root string + Pidfile string + Conffile string + Roles []string + Verbose bool + Silent bool + Diagnostic bool `toml:"diagnostic"` + DisableHttpKeepAlive bool `toml:"disable_http_keep_alive"` + DisplayName string `toml:"display_name"` + HostStatus HostStatus `toml:"host_status" conf:"parent"` + Disks Disks `toml:"disks" conf:"parent"` + Filesystems Filesystems `toml:"filesystems" conf:"parent"` + Interfaces Interfaces `toml:"interfaces" conf:"parent"` + HTTPProxy string `toml:"http_proxy"` + HTTPSProxy string `toml:"https_proxy"` + CloudPlatform CloudPlatform `toml:"cloud_platform"` // This Plugin field is used to decode the toml file. After reading the // configuration from file, this field is set to nil. @@ -448,8 +448,8 @@ func LoadConfig(conffile string) (*Config, error) { if !config.Diagnostic { config.Diagnostic = DefaultConfig.Diagnostic } - if !config.DisableKeepAlive { - config.DisableKeepAlive = DefaultConfig.DisableKeepAlive + if !config.DisableHttpKeepAlive { + config.DisableHttpKeepAlive = DefaultConfig.DisableHttpKeepAlive } return config, err diff --git a/config/config_test.go b/config/config_test.go index 208c39e91..ae0fe6ef3 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -100,7 +100,7 @@ func TestLoadConfig(t *testing.T) { t.Error("should be false (default value should be used)") } - if config.DisableKeepAlive != false { + if config.DisableHttpKeepAlive != false { t.Error("should be false (default value should be used)") } } diff --git a/mackerel/api.go b/mackerel/api.go index fd06b1b06..bb661c3ad 100644 --- a/mackerel/api.go +++ b/mackerel/api.go @@ -57,14 +57,14 @@ func infoError(msg string) *InfoError { } // NewAPI creates a new instance of API. -func NewAPI(rawurl string, apiKey string, verbose bool, disableKeepAlive bool) (*API, error) { +func NewAPI(rawurl string, apiKey string, verbose bool, disableHttpKeepAlive bool) (*API, error) { c, err := mkr.NewClientWithOptions(apiKey, rawurl, verbose) if err != nil { return nil, err } c.PrioritizedLogger = logger t := http.DefaultTransport.(*http.Transport).Clone() - t.DisableKeepAlives = disableKeepAlive + t.DisableKeepAlives = disableHttpKeepAlive c.HTTPClient.Transport = t return &API{Client: c}, nil From 4474546d10f4c95f35cfbec952db395bfcbfa065 Mon Sep 17 00:00:00 2001 From: appare45 Date: Thu, 26 Dec 2024 10:52:50 +0900 Subject: [PATCH 7/8] Renamed Http to HTTP https://github.com/mackerelio/mackerel-agent/pull/1035#discussion_r1895178546 --- command/command.go | 6 +++--- commands.go | 2 +- config/config.go | 6 +++--- config/config_test.go | 2 +- mackerel/api.go | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/command/command.go b/command/command.go index e291429ec..0e3fad0da 100644 --- a/command/command.go +++ b/command/command.go @@ -693,8 +693,8 @@ func buildUA(ver, rev string) string { } // NewMackerelClient returns Mackerel API client for mackerel-agent -func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool, disableHttpKeepAlive bool) (*mackerel.API, error) { - api, err := mackerel.NewAPI(apibase, apikey, verbose, disableHttpKeepAlive) +func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool, disableHTTPKeepAlive bool) (*mackerel.API, error) { + api, err := mackerel.NewAPI(apibase, apikey, verbose, disableHTTPKeepAlive) if err != nil { return nil, err } @@ -708,7 +708,7 @@ func NewMackerelClient(apibase, apikey, ver, rev string, verbose bool, disableHt // Prepare sets up API and registers the host data to the Mackerel server. // Use returned values to call Run(). func Prepare(conf *config.Config, ameta *AgentMeta) (*App, error) { - api, err := NewMackerelClient(conf.Apibase, conf.Apikey, ameta.Version, ameta.Revision, conf.Verbose, conf.DisableHttpKeepAlive) + api, err := NewMackerelClient(conf.Apibase, conf.Apikey, ameta.Version, ameta.Revision, conf.Verbose, conf.DisableHTTPKeepAlive) if err != nil { return nil, fmt.Errorf("failed to prepare an api: %s", err.Error()) } diff --git a/commands.go b/commands.go index 069bea95c..c41aa3517 100644 --- a/commands.go +++ b/commands.go @@ -156,7 +156,7 @@ func doRetire(fs *flag.FlagSet, argv []string) error { return fmt.Errorf("hostID file is not found or empty") } - api, err := command.NewMackerelClient(conf.Apibase, conf.Apikey, version, gitcommit, conf.Verbose, conf.DisableHttpKeepAlive) + api, err := command.NewMackerelClient(conf.Apibase, conf.Apikey, version, gitcommit, conf.Verbose, conf.DisableHTTPKeepAlive) if err != nil { return fmt.Errorf("faild to create api client: %s", err) } diff --git a/config/config.go b/config/config.go index d11903298..27c85e73f 100644 --- a/config/config.go +++ b/config/config.go @@ -102,7 +102,7 @@ type Config struct { Verbose bool Silent bool Diagnostic bool `toml:"diagnostic"` - DisableHttpKeepAlive bool `toml:"disable_http_keep_alive"` + DisableHTTPKeepAlive bool `toml:"disable_http_keep_alive"` DisplayName string `toml:"display_name"` HostStatus HostStatus `toml:"host_status" conf:"parent"` Disks Disks `toml:"disks" conf:"parent"` @@ -448,8 +448,8 @@ func LoadConfig(conffile string) (*Config, error) { if !config.Diagnostic { config.Diagnostic = DefaultConfig.Diagnostic } - if !config.DisableHttpKeepAlive { - config.DisableHttpKeepAlive = DefaultConfig.DisableHttpKeepAlive + if !config.DisableHTTPKeepAlive { + config.DisableHTTPKeepAlive = DefaultConfig.DisableHTTPKeepAlive } return config, err diff --git a/config/config_test.go b/config/config_test.go index ae0fe6ef3..ee3a5795d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -100,7 +100,7 @@ func TestLoadConfig(t *testing.T) { t.Error("should be false (default value should be used)") } - if config.DisableHttpKeepAlive != false { + if config.DisableHTTPKeepAlive != false { t.Error("should be false (default value should be used)") } } diff --git a/mackerel/api.go b/mackerel/api.go index bb661c3ad..c6a4e812b 100644 --- a/mackerel/api.go +++ b/mackerel/api.go @@ -57,14 +57,14 @@ func infoError(msg string) *InfoError { } // NewAPI creates a new instance of API. -func NewAPI(rawurl string, apiKey string, verbose bool, disableHttpKeepAlive bool) (*API, error) { +func NewAPI(rawurl string, apiKey string, verbose bool, disableHTTPKeepAlive bool) (*API, error) { c, err := mkr.NewClientWithOptions(apiKey, rawurl, verbose) if err != nil { return nil, err } c.PrioritizedLogger = logger t := http.DefaultTransport.(*http.Transport).Clone() - t.DisableKeepAlives = disableHttpKeepAlive + t.DisableKeepAlives = disableHTTPKeepAlive c.HTTPClient.Transport = t return &API{Client: c}, nil From 78589bd227a6cf6a38585dd3d89ee7d366f170d7 Mon Sep 17 00:00:00 2001 From: appare45 Date: Thu, 26 Dec 2024 11:39:43 +0900 Subject: [PATCH 8/8] Removed unused code --- config/config.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/config/config.go b/config/config.go index 27c85e73f..ad6383aa1 100644 --- a/config/config.go +++ b/config/config.go @@ -448,10 +448,6 @@ func LoadConfig(conffile string) (*Config, error) { if !config.Diagnostic { config.Diagnostic = DefaultConfig.Diagnostic } - if !config.DisableHTTPKeepAlive { - config.DisableHTTPKeepAlive = DefaultConfig.DisableHTTPKeepAlive - } - return config, err }