-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added statistics collection and aggregation
- Loading branch information
Showing
16 changed files
with
580 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package resolver | ||
|
||
import ( | ||
"blocky/stats" | ||
"blocky/util" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/jedib0t/go-pretty/table" | ||
"github.com/miekg/dns" | ||
) | ||
|
||
type StatsResolver struct { | ||
NextResolver | ||
recorders []*resolverStatRecorder | ||
statsChan chan *statsEntry | ||
} | ||
|
||
type statsEntry struct { | ||
request *Request | ||
response *Response | ||
} | ||
|
||
type resolverStatRecorder struct { | ||
aggregator *stats.Aggregator | ||
fn func(*statsEntry) string | ||
} | ||
|
||
func newRecorder(name string, fn func(*statsEntry) string) *resolverStatRecorder { | ||
return &resolverStatRecorder{ | ||
aggregator: stats.NewAggregator(name), | ||
fn: fn, | ||
} | ||
} | ||
|
||
func newRecorderWithMax(name string, max uint, fn func(*statsEntry) string) *resolverStatRecorder { | ||
return &resolverStatRecorder{ | ||
aggregator: stats.NewAggregatorWithMax(name, max), | ||
fn: fn, | ||
} | ||
} | ||
|
||
func (r *StatsResolver) collectStats() { | ||
for statsEntry := range r.statsChan { | ||
for _, rec := range r.recorders { | ||
rec.recordStats(statsEntry) | ||
} | ||
} | ||
} | ||
|
||
func (r *StatsResolver) Resolve(request *Request) (*Response, error) { | ||
resp, err := r.next.Resolve(request) | ||
|
||
if err == nil { | ||
r.statsChan <- &statsEntry{ | ||
request: request, | ||
response: resp, | ||
} | ||
} | ||
|
||
return resp, err | ||
} | ||
|
||
func (r *StatsResolver) Configuration() (result []string) { | ||
result = append(result, "stats:") | ||
for _, rec := range r.recorders { | ||
result = append(result, fmt.Sprintf(" - %s", rec.aggregator.Name)) | ||
} | ||
|
||
return | ||
} | ||
|
||
func (r StatsResolver) String() string { | ||
return fmt.Sprintf("statistic resolver") | ||
} | ||
|
||
func (r *resolverStatRecorder) recordStats(e *statsEntry) { | ||
r.aggregator.Put(r.fn(e)) | ||
} | ||
|
||
func NewStatsResolver() ChainedResolver { | ||
resolver := &StatsResolver{ | ||
statsChan: make(chan *statsEntry, 20), | ||
recorders: createRecorders(), | ||
} | ||
|
||
go resolver.collectStats() | ||
|
||
signals := make(chan os.Signal) | ||
signal.Notify(signals, syscall.SIGUSR2) | ||
|
||
go func() { | ||
for { | ||
<-signals | ||
resolver.printStats() | ||
} | ||
}() | ||
|
||
return resolver | ||
} | ||
|
||
func (r *StatsResolver) printStats() { | ||
logger := logger("stats_resover") | ||
|
||
w := logger.Writer() | ||
defer w.Close() | ||
|
||
logger.Info("******* STATS 24h *******") | ||
|
||
for _, s := range r.recorders { | ||
t := table.NewWriter() | ||
t.SetOutputMirror(w) | ||
t.SetTitle(s.aggregator.Name) | ||
|
||
t.SetStyle(table.StyleLight) | ||
|
||
util.IterateValueSorted(s.aggregator.AggregateResult(), func(k string, v int) { | ||
t.AppendRow([]interface{}{fmt.Sprintf("%50s", k), v}) | ||
}) | ||
|
||
t.Render() | ||
} | ||
} | ||
|
||
func createRecorders() []*resolverStatRecorder { | ||
return []*resolverStatRecorder{ | ||
newRecorderWithMax("Top 20 queries", 20, func(e *statsEntry) string { | ||
return util.ExtractDomain(e.request.Req.Question[0]) | ||
}), | ||
newRecorderWithMax("Top 20 blocked queries", 20, func(e *statsEntry) string { | ||
if e.response.rType == BLOCKED { | ||
return util.ExtractDomain(e.request.Req.Question[0]) | ||
} | ||
return "" | ||
}), | ||
newRecorder("Query count per client", func(e *statsEntry) string { | ||
return strings.Join(e.request.ClientNames, ",") | ||
}), | ||
newRecorder("Reason", func(e *statsEntry) string { | ||
return e.response.Reason | ||
}), | ||
newRecorder("Query type", func(e *statsEntry) string { | ||
return util.QTypeToString()(e.request.Req.Question[0].Qtype) | ||
}), | ||
newRecorder("Response type", func(e *statsEntry) string { | ||
return dns.RcodeToString[e.response.Res.Rcode] | ||
}), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package resolver | ||
|
||
import ( | ||
"blocky/util" | ||
"testing" | ||
|
||
"github.com/miekg/dns" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func Test_Resolve_WithStats(t *testing.T) { | ||
sut := NewStatsResolver() | ||
m := &resolverMock{} | ||
|
||
resp, err := util.NewMsgWithAnswer("example.com. 300 IN A 123.122.121.120") | ||
assert.NoError(t, err) | ||
|
||
m.On("Resolve", mock.Anything).Return(&Response{Res: resp, Reason: "reason"}, nil) | ||
sut.Next(m) | ||
|
||
request := &Request{ | ||
Req: util.NewMsgWithQuestion("example.com.", dns.TypeA), | ||
Log: logrus.NewEntry(logrus.New()), | ||
} | ||
|
||
_, err = sut.Resolve(request) | ||
assert.NoError(t, err) | ||
m.AssertExpectations(t) | ||
|
||
sut.(*StatsResolver).printStats() | ||
} | ||
|
||
func Test_Configuration_StatsResolverg(t *testing.T) { | ||
sut := NewStatsResolver() | ||
c := sut.Configuration() | ||
assert.True(t, len(c) > 1) | ||
} |
Oops, something went wrong.