summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkali kaneko (leap communications) <kali@leap.se>2020-03-25 18:57:41 +0100
committerkali kaneko (leap communications) <kali@leap.se>2020-03-26 17:09:43 +0100
commit9cad6ad4aebb3f41918ba6738fe0c85c1bf818b3 (patch)
tree9b92c42cd70c0c4814d57851ab175f60f3543a92
parenta9db940ca8187514f49f6c456df760744e503482 (diff)
[feat] add prometheus metrics for country
-rw-r--r--README.rst2
-rw-r--r--main.go16
-rw-r--r--metrics.go13
3 files changed, 31 insertions, 0 deletions
diff --git a/README.rst b/README.rst
index 5c95617..d978019 100644
--- a/README.rst
+++ b/README.rst
@@ -14,6 +14,8 @@ You can use ``geoipupdate`` to download MaxMind's City database::
sudo cp /usr/share/doc/geoipupdate/examples/GeoIP.conf.default /etc/GeoIP.conf
sudo geoipupdate -v
+(note: this service now requires a license key)
+
Usage
-----------------------
diff --git a/main.go b/main.go
index ad69312..aa57c86 100644
--- a/main.go
+++ b/main.go
@@ -32,6 +32,8 @@ import (
"github.com/StefanSchroeder/Golang-Ellipsoid/ellipsoid"
"github.com/hongshibao/go-kdtree"
"github.com/oschwald/geoip2-golang"
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/tidwall/cities"
)
@@ -178,6 +180,8 @@ func (jh *jsonHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
record := jh.geoipdb.getRecordForIP(ipstr)
sortedGateways := jh.geoipdb.sortGateways(record.Location.Latitude, record.Location.Longitude)
+ hitsPerCountry.With(prometheus.Labels{"country": record.Country.IsoCode}).Inc()
+
data := &GeolocationJSON{
ipstr,
record.Country.IsoCode,
@@ -210,6 +214,7 @@ func (th *txtHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
func main() {
rand.Seed(time.Now().UnixNano())
var port = flag.Int("port", 9001, "port where the service listens on")
+ var metricsPort = flag.Int("metricsPort", 9002, "port where the metrics server listens on")
var dbpath = flag.String("geodb", "/var/lib/GeoIP/GeoLite2-City.mmdb", "path to the GeoLite2-City database")
var notls = flag.Bool("notls", false, "disable TLS on the service")
var key = flag.String("server_key", "", "path to the key file for TLS")
@@ -255,6 +260,17 @@ func main() {
th := &txtHandler{&geoipdb}
mux.Handle("/", th)
+ mtr := http.NewServeMux()
+ mtr.Handle("/metrics", promhttp.Handler())
+
+ /* prometheus metrics */
+ go func() {
+ pstr := ":" + strconv.Itoa(*metricsPort)
+ log.Println("/metrics endpoint listening in port", *metricsPort)
+ log.Fatal(http.ListenAndServe(pstr, mtr))
+ }()
+
+ /* geolocation api */
log.Println("Started Geolocation Service")
log.Printf("Listening on port %v...\n", *port)
diff --git a/metrics.go b/metrics.go
new file mode 100644
index 0000000..2c581ba
--- /dev/null
+++ b/metrics.go
@@ -0,0 +1,13 @@
+package main
+
+import (
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promauto"
+)
+
+var hitsPerCountry = promauto.NewCounterVec(prometheus.CounterOpts{
+ Name: "getmyip_hits",
+ Help: "Number of hits in the geolocation service",
+},
+ []string{"country"},
+)