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

Api to ignore hosts #110

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions go/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ func (config *Configuration) Read(fileNames ...string) error {
return nil
}

func (config *Configuration) Save() error {
fileName := config.readFileNames[len(config.readFileNames)-1]
file, err := os.OpenFile(fileName, os.O_WRONLY, 0644)
if err != nil {
return log.Errorf("Cannot read config file %s, error was: %s", fileName, err)
}
defer file.Close()

enconder, err := json.Marshal(config.settings)
if err != nil {
return err
}
_, err = file.Write(enconder)
if err == nil {
log.Infof("Config save from %s", fileName)
} else {
return fmt.Errorf("Cannot write config file %s, error was: %s", fileName, err)
}
return nil
}

// Reload re-reads configuration from last used files
func (config *Configuration) Reload() error {
return config.Read(config.readFileNames...)
Expand Down
20 changes: 20 additions & 0 deletions go/config/mysql_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,23 @@ func (settings *MySQLConfigurationSettings) postReadAdjustments() error {
}
return nil
}

func (settings *MySQLConfigurationSettings) AddIgnoreHost(hostName string) {
for _, ignoredHost := range settings.IgnoreHosts {
if ignoredHost == hostName {
return
}
}
settings.IgnoreHosts = append(settings.IgnoreHosts, hostName)
}

func (settings *MySQLConfigurationSettings) RemoveIgnoreHost(hostName string) {
newIngoredHosts := []string{}
for _, ignoredHost := range settings.IgnoreHosts {
if ignoredHost == hostName {
continue
}
newIngoredHosts = append(newIngoredHosts, ignoredHost)
}
settings.IgnoreHosts = newIngoredHosts
}
44 changes: 44 additions & 0 deletions go/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type API interface {
WriteCheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
WriteCheckIfExists(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
ReadCheck(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
SkipHost(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
RecoverHost(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
SkippedHosts(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
AggregatedMetrics(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
MetricsHealth(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
ThrottleApp(w http.ResponseWriter, r *http.Request, _ httprouter.Params)
Expand Down Expand Up @@ -188,6 +191,43 @@ func (api *APIImpl) ReadCheck(w http.ResponseWriter, r *http.Request, ps httprou
}
}

// SkipHost Add specific host to ignore host list
func (api *APIImpl) SkipHost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
hostName := ps.ByName("hostName")
config.Settings().Stores.MySQL.AddIgnoreHost(hostName)
err := config.Instance().Save()
if err != nil {
config.Settings().Stores.MySQL.RemoveIgnoreHost(hostName)
api.respondGeneric(w, r, err)
} else {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(NewGeneralResponse(http.StatusOK, hostName))
}
}

// RecoverHost Remove specific host from ignore host list
func (api *APIImpl) RecoverHost(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
hostName := ps.ByName("hostName")
config.Settings().Stores.MySQL.RemoveIgnoreHost(hostName)
err := config.Instance().Save()
if err != nil {
config.Settings().Stores.MySQL.AddIgnoreHost(hostName)
api.respondGeneric(w, r, err)
} else {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(NewGeneralResponse(http.StatusOK, hostName))
}
}

// SkippedHosts List all ignored hosts a specific host
func (api *APIImpl) SkippedHosts(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
type HostList struct {
Hosts []string
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(HostList{Hosts: config.Settings().Stores.MySQL.IgnoreHosts})
}

// AggregatedMetrics returns a snapshot of all current aggregated metrics
func (api *APIImpl) AggregatedMetrics(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
brief := (r.URL.Query().Get("brief") == "true")
Expand Down Expand Up @@ -341,6 +381,10 @@ func ConfigureRoutes(api API) *httprouter.Router {
register(router, "/check-if-exists/:app/:storeType/:storeName", api.WriteCheckIfExists)
register(router, "/check-read/:app/:storeType/:storeName/:threshold", api.ReadCheck)

register(router, "/skip-host/:hostName", api.SkipHost)
register(router, "/skipped-hosts/", api.SkippedHosts)
register(router, "/recover-host/:hostName", api.RecoverHost)

register(router, "/aggregated-metrics", api.AggregatedMetrics)
register(router, "/metrics-health", api.MetricsHealth)

Expand Down