Skip to content

Commit

Permalink
Merge pull request #89 from cashapp/jnag/2023-01/add-drop-not-a-repli…
Browse files Browse the repository at this point in the history
…ca-option-in-repl.running

Add OPT_REPORT_NOT_A_REPLICA option to repl.running
  • Loading branch information
joycse06 authored Jan 3, 2023
2 parents 850c2ab + ae1ec45 commit 8b48a85
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions metrics/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ const (
NOT_A_REPLICA = -1

ERR_NO_ACCESS = "access-denied"

OPT_REPORT_NOT_A_REPLICA = "report-not-a-replica"
)

type replMetrics struct {
chedkRunning bool
}

type Repl struct {
db *sql.DB
atLevel map[string]replMetrics
errPolicy map[string]*errors.Policy
stop bool
db *sql.DB
atLevel map[string]replMetrics
errPolicy map[string]*errors.Policy
stop bool
dropNotAReplica map[string]bool
}

var _ blip.Collector = &Repl{}
Expand All @@ -43,6 +46,7 @@ func NewRepl(db *sql.DB) *Repl {
errPolicy: map[string]*errors.Policy{
ERR_NO_ACCESS: errors.NewPolicy(""),
},
dropNotAReplica: map[string]bool{},
}
}

Expand All @@ -54,7 +58,17 @@ func (c *Repl) Help() blip.CollectorHelp {
return blip.CollectorHelp{
Domain: DOMAIN,
Description: "Replication status",
Options: map[string]blip.CollectorHelpOption{},
Options: map[string]blip.CollectorHelpOption{
OPT_REPORT_NOT_A_REPLICA: {
Name: OPT_REPORT_NOT_A_REPLICA,
Desc: "Report not a replica as -1",
Default: "yes",
Values: map[string]string{
"yes": "Enabled: report not a replica repl.running = -1",
"no": "Disabled: drop repl.running if not a replica",
},
},
},
Metrics: []blip.CollectorMetric{
{
Name: "running",
Expand Down Expand Up @@ -98,15 +112,15 @@ LEVEL:
return nil, fmt.Errorf("invalid collector metric: %s (run 'blip --print-domains' to list collector metrics)", dom.Metrics[i])
}
}

c.atLevel[level.Name] = m
c.dropNotAReplica[level.Name] = !blip.Bool(dom.Options[OPT_REPORT_NOT_A_REPLICA])

// Apply custom error policies, if any
if len(dom.Errors) > 0 {
if s, ok := dom.Errors[ERR_NO_ACCESS]; ok {
c.errPolicy[ERR_NO_ACCESS] = errors.NewPolicy(s)
}
blip.Debug("error poliy: %s=%s", ERR_NO_ACCESS, c.errPolicy[ERR_NO_ACCESS])
blip.Debug("error policy: %s=%s", ERR_NO_ACCESS, c.errPolicy[ERR_NO_ACCESS])
}

// SHOW REPLICA STATUS as of 8.022
Expand Down Expand Up @@ -163,6 +177,12 @@ func (c *Repl) Collect(ctx context.Context, levelName string) ([]blip.MetricValu
running = 1
}

if running == NOT_A_REPLICA {
if c.dropNotAReplica[levelName] {
return nil, nil
}
}

m := blip.MetricValue{
Name: "running",
Type: blip.GAUGE,
Expand Down

0 comments on commit 8b48a85

Please sign in to comment.