Skip to content

Commit

Permalink
reorganize code
Browse files Browse the repository at this point in the history
  • Loading branch information
chimanjain committed Sep 6, 2022
1 parent 5b51c3b commit ed544cd
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 61 deletions.
66 changes: 9 additions & 57 deletions notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gobrake

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -16,15 +15,15 @@ import (
"time"
)

const notifierName = "gobrake"
const notifierVersion = "5.5.2"
const userAgent = notifierName + "/" + notifierVersion

const waitTimeout = 5 * time.Second

const httpEnhanceYourCalm = 420

const maxNoticeLen = 64 * 1024
const (
notifierName = "gobrake"
notifierVersion = "5.5.2"
userAgent = notifierName + "/" + notifierVersion
waitTimeout = 5 * time.Second
flushPeriod = 15 * time.Second
httpEnhanceYourCalm = 420
maxNoticeLen = 64 * 1024
)

var (
errClosed = errors.New("gobrake: notifier is closed")
Expand Down Expand Up @@ -148,53 +147,6 @@ func (opt *NotifierOptions) Copy() *NotifierOptions {
}
}

type routes struct {
filters []routeFilter

stats *routeStats
breakdowns *routeBreakdowns
}

func newRoutes(opt *NotifierOptions) *routes {
return &routes{
stats: newRouteStats(opt),
breakdowns: newRouteBreakdowns(opt),
}
}

// AddFilter adds filter that can change route stat or ignore it by returning nil.
func (rs *routes) AddFilter(fn func(*RouteMetric) *RouteMetric) {
rs.filters = append(rs.filters, fn)
}

func (rs *routes) Flush() {
rs.stats.Flush()
rs.breakdowns.Flush()
}

func (rs *routes) Notify(c context.Context, metric *RouteMetric) error {
metric.finish()

for _, fn := range rs.filters {
metric = fn(metric)
if metric == nil {
return nil
}
}

err := rs.stats.Notify(c, metric)
if err != nil {
return err
}

err = rs.breakdowns.Notify(c, metric)
if err != nil {
return err
}

return nil
}

type Notifier struct {
opt *NotifierOptions
filters []filter
Expand Down
8 changes: 8 additions & 0 deletions queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
Expand Down Expand Up @@ -143,6 +144,13 @@ func (s *queryStats) send(m map[queryKey]*tdigestStat) error {
return errUnauthorized
case http.StatusTooManyRequests:
return errIPRateLimited
case http.StatusBadRequest:
var sendResp sendResponse
err = json.NewDecoder(buf).Decode(&sendResp)
if err != nil {
return err
}
return errors.New(sendResp.Message)
}

err = fmt.Errorf("got unexpected response status=%q", resp.Status)
Expand Down
8 changes: 8 additions & 0 deletions queues.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
Expand Down Expand Up @@ -134,6 +135,13 @@ func (s *queueStats) send(m map[queueKey]*queueBreakdown) error {
return errUnauthorized
case http.StatusTooManyRequests:
return errIPRateLimited
case http.StatusBadRequest:
var sendResp sendResponse
err = json.NewDecoder(buf).Decode(&sendResp)
if err != nil {
return err
}
return errors.New(sendResp.Message)
}

err = fmt.Errorf("got unexpected response status=%q", resp.Status)
Expand Down
52 changes: 52 additions & 0 deletions route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package gobrake

import (
"context"
)

type routes struct {
filters []routeFilter

stats *routeStats
breakdowns *routeBreakdowns
}

func newRoutes(opt *NotifierOptions) *routes {
return &routes{
stats: newRouteStats(opt),
breakdowns: newRouteBreakdowns(opt),
}
}

// AddFilter adds filter that can change route stat or ignore it by returning nil.
func (rs *routes) AddFilter(fn func(*RouteMetric) *RouteMetric) {
rs.filters = append(rs.filters, fn)
}

func (rs *routes) Flush() {
rs.stats.Flush()
rs.breakdowns.Flush()
}

func (rs *routes) Notify(c context.Context, metric *RouteMetric) error {
metric.finish()

for _, fn := range rs.filters {
metric = fn(metric)
if metric == nil {
return nil
}
}

err := rs.stats.Notify(c, metric)
if err != nil {
return err
}

err = rs.breakdowns.Notify(c, metric)
if err != nil {
return err
}

return nil
}
10 changes: 10 additions & 0 deletions route_breakdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
Expand Down Expand Up @@ -128,6 +129,15 @@ func (s *routeBreakdowns) send(m map[routeBreakdownKey]*routeBreakdown) error {
switch resp.StatusCode {
case http.StatusUnauthorized:
return errUnauthorized
case http.StatusTooManyRequests:
return errIPRateLimited
case http.StatusBadRequest:
var sendResp sendResponse
err = json.NewDecoder(buf).Decode(&sendResp)
if err != nil {
return err
}
return errors.New(sendResp.Message)
}

err = fmt.Errorf("got unexpected response status=%q", resp.Status)
Expand Down
14 changes: 10 additions & 4 deletions routes.go → route_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"time"
)

const flushPeriod = 15 * time.Second

type routeKey struct {
Method string `json:"method"`
Route string `json:"route"`
Expand All @@ -24,8 +23,6 @@ type routeKeyStat struct {
*tdigestStat
}

type routeFilter func(*RouteMetric) *RouteMetric

// routeStats aggregates information about requests and periodically sends
// collected data to Airbrake.
type routeStats struct {
Expand All @@ -37,6 +34,8 @@ type routeStats struct {
m map[routeKey]*tdigestStat
}

type routeFilter func(*RouteMetric) *RouteMetric

func newRouteStats(opt *NotifierOptions) *routeStats {
return &routeStats{
opt: opt,
Expand Down Expand Up @@ -140,6 +139,13 @@ func (s *routeStats) send(m map[routeKey]*tdigestStat) error {
return errUnauthorized
case http.StatusTooManyRequests:
return errIPRateLimited
case http.StatusBadRequest:
var sendResp sendResponse
err = json.NewDecoder(buf).Decode(&sendResp)
if err != nil {
return err
}
return errors.New(sendResp.Message)
}

err = fmt.Errorf("got unexpected response status=%q", resp.Status)
Expand Down

0 comments on commit ed544cd

Please sign in to comment.