-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubenricher-geoip.go
65 lines (53 loc) · 1.61 KB
/
subenricher-geoip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"io"
"net/http"
"github.com/oschwald/geoip2-golang"
"github.com/telkomindonesia/httpmsg-enricher/ecs"
ecsx "github.com/telkomindonesia/httpmsg-enricher/ecs/custom"
)
var _ subEnricher = &geoipEnricher{}
type geoipEnricher struct {
db *geoip2.Reader
}
func (g *geoipEnricher) requestBodyWriter() io.WriteCloser { return nopwc }
func (g *geoipEnricher) processRequest(req *http.Request) (err error) { return }
func (g *geoipEnricher) responseBodyWriter() io.WriteCloser { return nopwc }
func (g *geoipEnricher) processResponse(res *http.Response) (err error) { return }
func (g *geoipEnricher) enrich(doc *ecsx.Document, msg *httpRecordedMessage) (err error) {
if g.db == nil {
return fmt.Errorf("no geodatabase instantiated")
}
ctx, err := msg.Context()
if err != nil {
return fmt.Errorf("error geting context: %w", err)
}
if ctx == nil || ctx.Connection == nil {
return
}
record, err := g.db.City(ctx.Connection.Client.IP)
if err != nil {
return fmt.Errorf("error geting city information: %w", err)
}
doc.Client = &ecs.Endpoint{
IP: ctx.Connection.Client.IP,
Geo: ecs.Geo{
CityName: record.City.Names["en"],
CountryName: record.Country.Names["en"],
CountryISOCode: record.Country.IsoCode,
ContinentName: record.Continent.Names["en"],
ContinentCode: record.Continent.Code,
Location: &ecs.GeoPoint{
Lon: record.Location.Longitude,
Lat: record.Location.Latitude,
},
PostalCode: record.Postal.Code,
Timezone: record.Location.TimeZone,
},
}
return nil
}
func (g *geoipEnricher) Close() error {
return g.db.Close()
}