Skip to content
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

Add disable_http_keep_alive option #1035

Merged
merged 8 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, disableHTTPKeepAlive bool) (*mackerel.API, error) {
api, err := mackerel.NewAPI(apibase, apikey, verbose, disableHTTPKeepAlive)
if err != nil {
return nil, err
}
Expand All @@ -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.DisableHTTPKeepAlive)
if err != nil {
return nil, fmt.Errorf("failed to prepare an api: %s", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion command/command_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, false)
if err != nil {
t.Errorf("something went wrong while creating new mackerel client: %+v", err)
}
Expand Down
12 changes: 6 additions & 6 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -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, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -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, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -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, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -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, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -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, false)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.DisableHTTPKeepAlive)
if err != nil {
return fmt.Errorf("faild to create api client: %s", err)
}
Expand Down
36 changes: 18 additions & 18 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +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"`
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.
Expand Down Expand Up @@ -447,7 +448,6 @@ func LoadConfig(conffile string) (*Config, error) {
if !config.Diagnostic {
config.Diagnostic = DefaultConfig.Diagnostic
}

return config, err
}

Expand Down
4 changes: 4 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.DisableHTTPKeepAlive != false {
t.Error("should be false (default value should be used)")
}
}

var sampleConfigWithHostStatus = `
Expand Down
7 changes: 6 additions & 1 deletion mackerel/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mackerel
import (
"errors"
"fmt"
"net/http"
"net/url"

"github.com/mackerelio/golib/logging"
Expand Down Expand Up @@ -56,12 +57,16 @@ 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, 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
c.HTTPClient.Transport = t

return &API{Client: c}, nil
}

Expand Down
3 changes: 2 additions & 1 deletion mackerel/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func TestNewAPI(t *testing.T) {
"http://example.com",
"dummy-key",
true,
false,
)

if err != nil {
Expand Down Expand Up @@ -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, false)

var tests = []struct {
customIdentifier string
Expand Down
4 changes: 2 additions & 2 deletions mackerel/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, false)

err := api.ReportCheckMonitors("9rxGOHfVF8F", []*checks.Report{
{
Expand Down Expand Up @@ -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, false)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
api.ReportCheckMonitors("xxx", []*checks.Report{
Expand Down
Loading