Add support of EventDomainFronting event

This commit is contained in:
9seconds
2021-03-29 11:56:05 +03:00
parent 36d695118e
commit bef14bd009
17 changed files with 317 additions and 53 deletions
+6 -4
View File
@@ -4,15 +4,17 @@ import "sync"
var streamInfoPool = sync.Pool{
New: func() interface{} {
return streamInfo{}
return &streamInfo{
tags: make(map[string]string),
}
},
}
func acquireStreamInfo() streamInfo {
return streamInfoPool.Get().(streamInfo)
func acquireStreamInfo() *streamInfo {
return streamInfoPool.Get().(*streamInfo)
}
func releaseStreamInfo(info streamInfo) {
func releaseStreamInfo(info *streamInfo) {
info.Reset()
streamInfoPool.Put(info)
}
+47 -17
View File
@@ -13,7 +13,7 @@ import (
)
type prometheusProcessor struct {
streams map[string]streamInfo
streams map[string]*streamInfo
factory *PrometheusFactory
}
@@ -21,15 +21,15 @@ func (p prometheusProcessor) EventStart(evt mtglib.EventStart) {
info := acquireStreamInfo()
if evt.RemoteIP.To4() != nil {
info[TagIPFamily] = TagIPFamilyIPv4
info.tags[TagIPFamily] = TagIPFamilyIPv4
} else {
info[TagIPFamily] = TagIPFamilyIPv6
info.tags[TagIPFamily] = TagIPFamilyIPv6
}
p.streams[evt.StreamID()] = info
p.factory.metricClientConnections.
WithLabelValues(info[TagIPFamily]).
WithLabelValues(info.tags[TagIPFamily]).
Inc()
}
@@ -39,11 +39,25 @@ func (p prometheusProcessor) EventConnectedToDC(evt mtglib.EventConnectedToDC) {
return
}
info[TagTelegramIP] = evt.RemoteIP.String()
info[TagDC] = strconv.Itoa(evt.DC)
info.tags[TagTelegramIP] = evt.RemoteIP.String()
info.tags[TagDC] = strconv.Itoa(evt.DC)
p.factory.metricTelegramConnections.
WithLabelValues(info[TagTelegramIP], info[TagDC]).
WithLabelValues(info.tags[TagTelegramIP], info.tags[TagDC]).
Inc()
}
func (p prometheusProcessor) EventDomainFronting(evt mtglib.EventDomainFronting) {
info, ok := p.streams[evt.StreamID()]
if !ok {
return
}
info.isDomainFronted = true
p.factory.metricDomainFronting.Inc()
p.factory.metricDomainFrontingConnections.
WithLabelValues(info.tags[TagIPFamily]).
Inc()
}
@@ -53,9 +67,17 @@ func (p prometheusProcessor) EventTraffic(evt mtglib.EventTraffic) {
return
}
p.factory.metricTelegramTraffic.
WithLabelValues(info[TagTelegramIP], info[TagDC], getDirection(evt.IsRead)).
Add(float64(evt.Traffic))
direction := getDirection(evt.IsRead)
if info.isDomainFronted {
p.factory.metricDomainFrontingTraffic.
WithLabelValues(direction).
Add(float64(evt.Traffic))
} else {
p.factory.metricTelegramTraffic.
WithLabelValues(info.tags[TagTelegramIP], info.tags[TagDC], direction).
Add(float64(evt.Traffic))
}
}
func (p prometheusProcessor) EventFinish(evt mtglib.EventFinish) {
@@ -70,12 +92,16 @@ func (p prometheusProcessor) EventFinish(evt mtglib.EventFinish) {
}()
p.factory.metricClientConnections.
WithLabelValues(info[TagIPFamily]).
WithLabelValues(info.tags[TagIPFamily]).
Dec()
if telegramIP, ok := info[TagTelegramIP]; ok {
if info.isDomainFronted {
p.factory.metricDomainFrontingConnections.
WithLabelValues(info.tags[TagIPFamily]).
Dec()
} else if telegramIP, ok := info.tags[TagTelegramIP]; ok {
p.factory.metricTelegramConnections.
WithLabelValues(telegramIP, info[TagDC]).
WithLabelValues(telegramIP, info.tags[TagDC]).
Dec()
}
}
@@ -89,7 +115,11 @@ func (p prometheusProcessor) EventIPBlocklisted(evt mtglib.EventIPBlocklisted) {
}
func (p prometheusProcessor) Shutdown() {
p.streams = make(map[string]streamInfo)
for _, v := range p.streams {
releaseStreamInfo(v)
}
p.streams = make(map[string]*streamInfo)
}
type PrometheusFactory struct {
@@ -110,7 +140,7 @@ type PrometheusFactory struct {
func (p *PrometheusFactory) Make() events.Observer {
return prometheusProcessor{
streams: make(map[string]streamInfo),
streams: make(map[string]*streamInfo),
factory: p,
}
}
@@ -149,8 +179,8 @@ func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint
}, []string{TagTelegramIP, TagDC}),
metricDomainFrontingConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: metricPrefix,
Name: MetricDomainFronting,
Help: "A number of connections which talk with front domain.",
Name: MetricDomainFrontingConnections,
Help: "A number of connections which talk to front domain.",
}, []string{TagIPFamily}),
metricTelegramTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
+60 -1
View File
@@ -54,7 +54,7 @@ func (suite *PrometheusTestSuite) TearDownTest() {
suite.httpListener.Close()
}
func (suite *PrometheusTestSuite) TestEventStartFinish() {
func (suite *PrometheusTestSuite) TestTelegramPath() {
suite.prometheus.EventStart(mtglib.EventStart{
CreatedAt: time.Now(),
ConnID: "connID",
@@ -114,6 +114,65 @@ func (suite *PrometheusTestSuite) TestEventStartFinish() {
suite.Contains(data, `mtg_telegram_connections{dc="4",telegram_ip="10.0.0.1"} 0`)
}
func (suite *PrometheusTestSuite) TestDomainFrontingPath() {
suite.prometheus.EventStart(mtglib.EventStart{
CreatedAt: time.Now(),
ConnID: "connID",
RemoteIP: net.ParseIP("10.0.0.10"),
})
time.Sleep(100 * time.Millisecond)
data, err := suite.Get()
suite.NoError(err)
suite.Contains(data, `mtg_client_connections{ip_family="ipv4"} 1`)
suite.prometheus.EventDomainFronting(mtglib.EventDomainFronting{
CreatedAt: time.Now(),
ConnID: "connID",
})
time.Sleep(100 * time.Millisecond)
data, err = suite.Get()
suite.NoError(err)
suite.Contains(data, `mtg_domain_fronting 1`)
suite.Contains(data, `mtg_domain_fronting_connections{ip_family="ipv4"} 1`)
suite.prometheus.EventTraffic(mtglib.EventTraffic{
CreatedAt: time.Now(),
ConnID: "connID",
Traffic: 200,
IsRead: true,
})
time.Sleep(100 * time.Millisecond)
data, err = suite.Get()
suite.NoError(err)
suite.Contains(data, `mtg_domain_fronting_traffic{direction="to_client"} 200`)
suite.prometheus.EventTraffic(mtglib.EventTraffic{
CreatedAt: time.Now(),
ConnID: "connID",
Traffic: 100,
IsRead: false,
})
time.Sleep(100 * time.Millisecond)
data, err = suite.Get()
suite.NoError(err)
suite.Contains(data, `mtg_domain_fronting_traffic{direction="from_client"} 100`)
suite.prometheus.EventFinish(mtglib.EventFinish{
CreatedAt: time.Now(),
ConnID: "connID",
})
time.Sleep(100 * time.Millisecond)
data, err = suite.Get()
suite.NoError(err)
suite.Contains(data, `mtg_client_connections{ip_family="ipv4"} 0`)
suite.Contains(data, `mtg_domain_fronting_connections{ip_family="ipv4"} 0`)
}
func (suite *PrometheusTestSuite) TestEventConcurrencyLimited() {
suite.prometheus.EventConcurrencyLimited(mtglib.EventConcurrencyLimited{
CreatedAt: time.Now(),
+38 -12
View File
@@ -13,7 +13,7 @@ import (
)
type statsdProcessor struct {
streams map[string]streamInfo
streams map[string]*streamInfo
client *statsd.Client
}
@@ -21,9 +21,9 @@ func (s statsdProcessor) EventStart(evt mtglib.EventStart) {
info := acquireStreamInfo()
if evt.RemoteIP.To4() != nil {
info[TagIPFamily] = TagIPFamilyIPv4
info.tags[TagIPFamily] = TagIPFamilyIPv4
} else {
info[TagIPFamily] = TagIPFamilyIPv6
info.tags[TagIPFamily] = TagIPFamilyIPv6
}
s.streams[evt.StreamID()] = info
@@ -39,8 +39,8 @@ func (s statsdProcessor) EventConnectedToDC(evt mtglib.EventConnectedToDC) {
return
}
info[TagTelegramIP] = evt.RemoteIP.String()
info[TagDC] = strconv.Itoa(evt.DC)
info.tags[TagTelegramIP] = evt.RemoteIP.String()
info.tags[TagDC] = strconv.Itoa(evt.DC)
s.client.GaugeDelta(MetricTelegramConnections,
1,
@@ -48,17 +48,39 @@ func (s statsdProcessor) EventConnectedToDC(evt mtglib.EventConnectedToDC) {
info.T(TagDC))
}
func (s statsdProcessor) EventDomainFronting(evt mtglib.EventDomainFronting) {
info, ok := s.streams[evt.StreamID()]
if !ok {
return
}
info.isDomainFronted = true
s.client.Incr(MetricDomainFronting, 1)
s.client.GaugeDelta(MetricDomainFrontingConnections,
1,
info.T(TagIPFamily))
}
func (s statsdProcessor) EventTraffic(evt mtglib.EventTraffic) {
info, ok := s.streams[evt.StreamID()]
if !ok {
return
}
s.client.Incr(MetricTelegramTraffic,
int64(evt.Traffic),
info.T(TagTelegramIP),
info.T(TagDC),
statsd.StringTag(TagDirection, getDirection(evt.IsRead)))
directionTag := statsd.StringTag(TagDirection, getDirection(evt.IsRead))
if info.isDomainFronted {
s.client.Incr(MetricDomainFrontingTraffic,
int64(evt.Traffic),
directionTag)
} else {
s.client.Incr(MetricTelegramTraffic,
int64(evt.Traffic),
info.T(TagTelegramIP),
info.T(TagDC),
directionTag)
}
}
func (s statsdProcessor) EventFinish(evt mtglib.EventFinish) {
@@ -76,7 +98,11 @@ func (s statsdProcessor) EventFinish(evt mtglib.EventFinish) {
-1,
info.T(TagIPFamily))
if _, ok := info[TagTelegramIP]; ok {
if info.isDomainFronted {
s.client.GaugeDelta(MetricDomainFrontingConnections,
-1,
info.T(TagIPFamily))
} else if _, ok := info.tags[TagTelegramIP]; ok {
s.client.GaugeDelta(MetricTelegramConnections,
-1,
info.T(TagTelegramIP),
@@ -119,7 +145,7 @@ func (s StatsdFactory) Close() error {
func (s StatsdFactory) Make() events.Observer {
return statsdProcessor{
client: s.client,
streams: make(map[string]streamInfo),
streams: make(map[string]*streamInfo),
}
}
+57 -2
View File
@@ -16,7 +16,7 @@ import (
"github.com/stretchr/testify/suite"
)
const statsdSleepTime = 3 * statsd.DefaultFlushInterval
const statsdSleepTime = 4 * statsd.DefaultFlushInterval
type statsdFakeServer struct {
conn *net.UDPConn
@@ -104,7 +104,7 @@ func (suite *StatsdTestSuite) TearDownTest() {
suite.statsdServer.Close()
}
func (suite *StatsdTestSuite) TestEventStartFinish() {
func (suite *StatsdTestSuite) TestTelegramPath() {
suite.statsd.EventStart(mtglib.EventStart{
CreatedAt: time.Now(),
ConnID: "connID",
@@ -152,6 +152,61 @@ func (suite *StatsdTestSuite) TestEventStartFinish() {
"mtg.telegram_connections:-1|g|#telegram_ip:10.1.0.10,dc:2")
suite.Contains(suite.statsdServer.String(),
"mtg.client_connections:-1|g|#ip_family:ipv4")
suite.NotContains(suite.statsdServer.String(), "domain_fronting_traffic")
suite.NotContains(suite.statsdServer.String(), "domain_fronting_connections")
}
func (suite *StatsdTestSuite) TestDomainFrontingPath() {
suite.statsd.EventStart(mtglib.EventStart{
CreatedAt: time.Now(),
ConnID: "connID",
RemoteIP: net.ParseIP("10.0.0.10"),
})
time.Sleep(statsdSleepTime)
suite.Equal("mtg.client_connections:+1|g|#ip_family:ipv4", suite.statsdServer.String())
suite.statsd.EventDomainFronting(mtglib.EventDomainFronting{
CreatedAt: time.Now(),
ConnID: "connID",
})
time.Sleep(statsdSleepTime)
suite.Contains(suite.statsdServer.String(), "mtg.domain_fronting:1|c")
suite.Contains(suite.statsdServer.String(),
`mtg.domain_fronting_connections:+1|g|#ip_family:ipv4`)
suite.statsd.EventTraffic(mtglib.EventTraffic{
CreatedAt: time.Now(),
ConnID: "connID",
Traffic: 30,
IsRead: true,
})
time.Sleep(statsdSleepTime)
suite.Contains(suite.statsdServer.String(),
`mtg.domain_fronting_traffic:30|c|#direction:to_client`)
suite.statsd.EventTraffic(mtglib.EventTraffic{
CreatedAt: time.Now(),
ConnID: "connID",
Traffic: 90,
IsRead: false,
})
time.Sleep(statsdSleepTime)
suite.Contains(suite.statsdServer.String(),
`mtg.domain_fronting_traffic:90|c|#direction:from_client`)
suite.statsd.EventFinish(mtglib.EventFinish{
CreatedAt: time.Now(),
ConnID: "connID",
})
time.Sleep(statsdSleepTime)
suite.Contains(suite.statsdServer.String(),
"mtg.domain_fronting_connections:-1|g|#ip_family:ipv4")
suite.Contains(suite.statsdServer.String(),
"mtg.client_connections:-1|g|#ip_family:ipv4")
suite.NotContains(suite.statsdServer.String(), "telegram_traffic")
suite.NotContains(suite.statsdServer.String(), "telegram_connections")
}
func (suite *StatsdTestSuite) TestEventConcurrencyLimited() {
+12 -7
View File
@@ -2,15 +2,20 @@ package stats
import statsd "github.com/smira/go-statsd"
type streamInfo map[string]string
func (s streamInfo) T(key string) statsd.Tag {
return statsd.StringTag(key, s[key])
type streamInfo struct {
isDomainFronted bool
tags map[string]string
}
func (s streamInfo) Reset() {
for k := range s {
delete(s, k)
func (s streamInfo) T(key string) statsd.Tag {
return statsd.StringTag(key, s.tags[key])
}
func (s *streamInfo) Reset() {
s.isDomainFronted = false
for k := range s.tags {
delete(s.tags, k)
}
}