Skip to content

Commit

Permalink
Add ability to change reroute traffic at runtime
Browse files Browse the repository at this point in the history
This is going to be used mostly by the remote configuration features. At the
moment you can configure `Host` and `APMHost` options to route your traffic,
however these values are static (cannot be changed at runtime).

With this commit we can redefine these two options and runtime and the traffic
will be redirected to the new location.
  • Loading branch information
kyrylo committed Aug 10, 2020
1 parent f57955e commit 3f99d56
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
16 changes: 8 additions & 8 deletions notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ func (rs *routes) Notify(c context.Context, metric *RouteMetric) error {
}

type Notifier struct {
opt *NotifierOptions
createNoticeURL string

opt *NotifierOptions
filters []filter

inFlight int32 // atomic
Expand All @@ -198,10 +196,7 @@ func NewNotifierWithOptions(opt *NotifierOptions) *Notifier {
opt.init()

n := &Notifier{
opt: opt,
createNoticeURL: fmt.Sprintf("%s/api/v3/projects/%d/notices",
opt.Host, opt.ProjectId),

opt: opt,
limit: make(chan struct{}, 2*runtime.NumCPU()),

Routes: newRoutes(opt),
Expand Down Expand Up @@ -295,7 +290,12 @@ func (n *Notifier) sendNotice(notice *Notice) (string, error) {
return "", errNoticeTooBig
}

req, err := http.NewRequest("POST", n.createNoticeURL, buf)
req, err := http.NewRequest(
"POST",
fmt.Sprintf("%s/api/v3/projects/%d/notices",
n.opt.Host, n.opt.ProjectId),
buf,
)
if err != nil {
return "", err
}
Expand Down
13 changes: 7 additions & 6 deletions queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ type queryKeyStat struct {
}

type queryStats struct {
opt *NotifierOptions
apiURL string

opt *NotifierOptions
flushTimer *time.Timer
addWG *sync.WaitGroup

Expand All @@ -50,8 +48,6 @@ type queryStats struct {
func newQueryStats(opt *NotifierOptions) *queryStats {
return &queryStats{
opt: opt,
apiURL: fmt.Sprintf("%s/api/v5/projects/%d/queries-stats",
opt.APMHost, opt.ProjectId),
}
}

Expand Down Expand Up @@ -113,7 +109,12 @@ func (s *queryStats) send(m map[queryKey]*tdigestStat) error {
return err
}

req, err := http.NewRequest("PUT", s.apiURL, buf)
req, err := http.NewRequest(
"PUT",
fmt.Sprintf("%s/api/v5/projects/%d/queries-stats",
s.opt.APMHost, s.opt.ProjectId),
buf,
)
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions queues.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func (b *queueBreakdown) Add(total time.Duration, groups map[string]time.Duratio
}

type queueStats struct {
opt *NotifierOptions
apiURL string

opt *NotifierOptions
flushTimer *time.Timer
addWG *sync.WaitGroup

Expand All @@ -45,8 +43,6 @@ type queueStats struct {
func newQueueStats(opt *NotifierOptions) *queueStats {
return &queueStats{
opt: opt,
apiURL: fmt.Sprintf("%s/api/v5/projects/%d/queues-stats",
opt.APMHost, opt.ProjectId),
}
}

Expand Down Expand Up @@ -104,7 +100,12 @@ func (s *queueStats) send(m map[queueKey]*queueBreakdown) error {
return err
}

req, err := http.NewRequest("PUT", s.apiURL, buf)
req, err := http.NewRequest(
"PUT",
fmt.Sprintf("%s/api/v5/projects/%d/queues-stats",
s.opt.APMHost, s.opt.ProjectId),
buf,
)
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions route_breakdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ type routeBreakdown struct {
}

type routeBreakdowns struct {
opt *NotifierOptions
apiURL string

opt *NotifierOptions
flushTimer *time.Timer
addWG *sync.WaitGroup

Expand All @@ -36,8 +34,6 @@ type routeBreakdowns struct {
func newRouteBreakdowns(opt *NotifierOptions) *routeBreakdowns {
return &routeBreakdowns{
opt: opt,
apiURL: fmt.Sprintf("%s/api/v5/projects/%d/routes-breakdowns",
opt.APMHost, opt.ProjectId),
}
}

Expand Down Expand Up @@ -100,7 +96,12 @@ func (s *routeBreakdowns) send(m map[routeBreakdownKey]*routeBreakdown) error {
return err
}

req, err := http.NewRequest("PUT", s.apiURL, buf)
req, err := http.NewRequest(
"PUT",
fmt.Sprintf("%s/api/v5/projects/%d/routes-breakdowns",
s.opt.APMHost, s.opt.ProjectId),
buf,
)
if err != nil {
return err
}
Expand Down
13 changes: 7 additions & 6 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ type routeFilter func(*RouteMetric) *RouteMetric
// routeStats aggregates information about requests and periodically sends
// collected data to Airbrake.
type routeStats struct {
opt *NotifierOptions
apiURL string

opt *NotifierOptions
flushTimer *time.Timer
addWG *sync.WaitGroup

Expand All @@ -42,8 +40,6 @@ type routeStats struct {
func newRouteStats(opt *NotifierOptions) *routeStats {
return &routeStats{
opt: opt,
apiURL: fmt.Sprintf("%s/api/v5/projects/%d/routes-stats",
opt.APMHost, opt.ProjectId),
}
}

Expand Down Expand Up @@ -110,7 +106,12 @@ func (s *routeStats) send(m map[routeKey]*tdigestStat) error {
return err
}

req, err := http.NewRequest("PUT", s.apiURL, buf)
req, err := http.NewRequest(
"PUT",
fmt.Sprintf("%s/api/v5/projects/%d/routes-stats",
s.opt.APMHost, s.opt.ProjectId),
buf,
)
if err != nil {
return err
}
Expand Down

0 comments on commit 3f99d56

Please sign in to comment.