Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure partial ASN batch is written #149

Merged
merged 5 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion internal/metrics/crawler.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,12 @@ func (s *CrawlerServiceMetrics) StartIPMapping(limit uint) {
func (s *CrawlerServiceMetrics) GetIPsAfterTimestamp(ips *rpc.GetIPsAfterTimestampResponse) {
// If we don't have either maxmind DB, bail now
if s.maxMindCountryDB == nil && s.maxMindASNDB == nil {
log.Debug("Missing both ASN and Country maxmind DBs")
return
}

if ips == nil {
log.Debug("IPs after timestamp are nil")
return
}

Expand Down Expand Up @@ -250,6 +252,7 @@ func (s *CrawlerServiceMetrics) ProcessIPCountryMapping(ips *rpc.GetIPsAfterTime
func (s *CrawlerServiceMetrics) ProcessIPASNMapping(ips *rpc.GetIPsAfterTimestampResponse) {
// Don't process if we can't store
if s.metrics.mysqlClient == nil {
log.Debug("MySQL client is nil")
return
}
if s.network == nil {
Expand All @@ -264,10 +267,13 @@ func (s *CrawlerServiceMetrics) ProcessIPASNMapping(ips *rpc.GetIPsAfterTimestam
}
asnCounts := map[uint32]*countStruct{}

log.Debugf("Have %d IPs\n", len(ips.IPs.MustGet()))

if ipresult, hasIPResult := ips.IPs.Get(); hasIPResult {
for _, ip := range ipresult {
asn, err := s.GetASNForIP(ip)
if err != nil {
log.Debugf("Unable to get ASN for IP %s\n", ip)
continue
}

Expand Down Expand Up @@ -303,7 +309,7 @@ func (s *CrawlerServiceMetrics) ProcessIPASNMapping(ips *rpc.GetIPsAfterTimestam

// Execute the batch insert when reaching the batch size or the end of the slice
if (i+1)%batchSize == 0 || i+1 == uint32(len(asnCounts)) {
_, err := s.metrics.mysqlClient.Exec(
_, err = s.metrics.mysqlClient.Exec(
fmt.Sprintf("INSERT INTO asn(asn, organization, count, network) VALUES %s", strings.Join(valueStrings, ",")),
valueArgs...)

Expand All @@ -316,6 +322,17 @@ func (s *CrawlerServiceMetrics) ProcessIPASNMapping(ips *rpc.GetIPsAfterTimestam
valueArgs = []interface{}{}
}
}

// If there are any records left that did not meet the batch size threshold, insert them now
if len(valueStrings) > 0 {
_, err := s.metrics.mysqlClient.Exec(
fmt.Sprintf("INSERT INTO asn(asn, organization, count, network) VALUES %s", strings.Join(valueStrings, ",")),
valueArgs...)

if err != nil {
log.Errorf("error inserting remaining ASN records to mysql: %s\n", err.Error())
}
}
}

// CountryRecord record of a country from maxmind
Expand Down
2 changes: 1 addition & 1 deletion internal/metrics/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func (m *Metrics) initTables() error {
" `organization` VARCHAR(255) NOT NULL," +
" `count` int unsigned NOT NULL," +
" `network` VARCHAR(255) NOT NULL," +
"UNIQUE KEY `asn-unique` (`asn`)" +
"UNIQUE KEY `asn-network-unique` (`asn`, `network`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;"

result, err := m.mysqlClient.Query(query)
Expand Down
4 changes: 2 additions & 2 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ func NewMetrics(port uint16, logLevel log.Level) (*Metrics, error) {

err = metrics.createDBClient()
if err != nil {
log.Debugf("ERROR creating MySQL Client. Will not store any metrics to MySQL")
log.Debugf("Error creating MySQL Client. Will not store any metrics to MySQL. %s\n", err.Error())
}
err = metrics.initTables()
if err != nil {
log.Debugf("ERROR ensuring tables exist in MySQL. Will not process or store any MySQL only metrics")
log.Errorf("Error ensuring tables exist in MySQL. Will not process or store any MySQL only metrics: %s\n", err.Error())
metrics.mysqlClient = nil
}

Expand Down
1 change: 1 addition & 0 deletions internal/metrics/promhandler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package metrics

// Everything in here is to support making a new prometheus registry and having the http endpoint pick it up seamlessly

import (
Expand Down
Loading