diff options
Diffstat (limited to 'plugins/status/plugin.go')
-rw-r--r-- | plugins/status/plugin.go | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/plugins/status/plugin.go b/plugins/status/plugin.go index 693440bf..dc4e506d 100644 --- a/plugins/status/plugin.go +++ b/plugins/status/plugin.go @@ -37,10 +37,14 @@ func (c *Plugin) Init(log logger.Logger, cfg config.Configurer) error { return errors.E(op, errors.Disabled, err) } + // init defaults for the status plugin + c.cfg.InitDefaults() + c.readyRegistry = make(map[string]Readiness) c.statusRegistry = make(map[string]Checker) c.log = log + return nil } @@ -134,8 +138,8 @@ type Plugins struct { const template string = "Service: %s: Status: %d\n" -func (c *Plugin) readinessHandler(ctx *fiber.Ctx) error { - const op = errors.Op("checker_plugin_readiness_handler") +func (c *Plugin) healthHandler(ctx *fiber.Ctx) error { + const op = errors.Op("checker_plugin_health_handler") plugins := &Plugins{} err := ctx.QueryParser(plugins) if err != nil { @@ -144,16 +148,22 @@ func (c *Plugin) readinessHandler(ctx *fiber.Ctx) error { if len(plugins.Plugins) == 0 { ctx.Status(http.StatusOK) - _, _ = ctx.WriteString("No plugins provided in query. Query should be in form of: ready?plugin=plugin1&plugin=plugin2 \n") + _, _ = ctx.WriteString("No plugins provided in query. Query should be in form of: health?plugin=plugin1&plugin=plugin2 \n") return nil } // iterate over all provided plugins for i := 0; i < len(plugins.Plugins); i++ { // check if the plugin exists - if plugin, ok := c.readyRegistry[plugins.Plugins[i]]; ok { - st := plugin.Ready() - _, _ = ctx.WriteString(fmt.Sprintf(template, plugins.Plugins[i], st.Code)) + if plugin, ok := c.statusRegistry[plugins.Plugins[i]]; ok { + st := plugin.Status() + if st.Code >= 500 { + // if there is 500 or 503 status code return immediately + ctx.Status(c.cfg.UnavailableStatusCode) + return nil + } else if st.Code >= 100 && st.Code <= 400 { + _, _ = ctx.WriteString(fmt.Sprintf(template, plugins.Plugins[i], st.Code)) + } } else { _, _ = ctx.WriteString(fmt.Sprintf("Service: %s not found", plugins.Plugins[i])) } @@ -163,8 +173,10 @@ func (c *Plugin) readinessHandler(ctx *fiber.Ctx) error { return nil } -func (c *Plugin) healthHandler(ctx *fiber.Ctx) error { - const op = errors.Op("checker_plugin_health_handler") +// readinessHandler return 200OK if all plugins are ready to serve +// if one of the plugins return status from the 5xx range, the status for all query will be 503 +func (c *Plugin) readinessHandler(ctx *fiber.Ctx) error { + const op = errors.Op("checker_plugin_readiness_handler") plugins := &Plugins{} err := ctx.QueryParser(plugins) if err != nil { @@ -173,16 +185,22 @@ func (c *Plugin) healthHandler(ctx *fiber.Ctx) error { if len(plugins.Plugins) == 0 { ctx.Status(http.StatusOK) - _, _ = ctx.WriteString("No plugins provided in query. Query should be in form of: health?plugin=plugin1&plugin=plugin2 \n") + _, _ = ctx.WriteString("No plugins provided in query. Query should be in form of: ready?plugin=plugin1&plugin=plugin2 \n") return nil } // iterate over all provided plugins for i := 0; i < len(plugins.Plugins); i++ { // check if the plugin exists - if plugin, ok := c.statusRegistry[plugins.Plugins[i]]; ok { - st := plugin.Status() - _, _ = ctx.WriteString(fmt.Sprintf(template, plugins.Plugins[i], st.Code)) + if plugin, ok := c.readyRegistry[plugins.Plugins[i]]; ok { + st := plugin.Ready() + if st.Code >= 500 { + // if there is 500 or 503 status code return immediately + ctx.Status(c.cfg.UnavailableStatusCode) + return nil + } else if st.Code >= 100 && st.Code <= 400 { + _, _ = ctx.WriteString(fmt.Sprintf(template, plugins.Plugins[i], st.Code)) + } } else { _, _ = ctx.WriteString(fmt.Sprintf("Service: %s not found", plugins.Plugins[i])) } |