From 59cc6fe1645d4846011cf33d81670f2f30da38c4 Mon Sep 17 00:00:00 2001 From: Tim Date: Sun, 10 Sep 2023 14:17:13 +0200 Subject: [PATCH] feat: Tagging of local source IPs. Added environment variable INFLUX_DB_TAG_SOURCE_IPS_LOCAL="127., 10., 192., 176." that contains IP prefixes which identify source IPs as local. These log entries are tagged with `source_ip_area`=`local`. All others with `public`. --- Dockerfile | 1 + config/config.go | 1 + docker-compose.yml | 1 + persistence/influxdb_http_request_persister.go | 12 ++++++++++++ 4 files changed, 15 insertions(+) diff --git a/Dockerfile b/Dockerfile index ce085a3..3a7f175 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ LABEL org.opencontainers.image.description="nginx-proxy-metrics parses nginx-pro ENV PROXY_CONTAINER_NAME nginx ENV INFLUX_DB_NAME monitoring ENV INFLUX_DB_RETENTION_DURATION 8w +ENV INFLUX_DB_TAG_SOURCE_IPS_LOCAL "127., 10., 192., 176." VOLUME /GeoLite2-City.mmdb COPY .build/main /main diff --git a/config/config.go b/config/config.go index 4cece85..b13cdb3 100644 --- a/config/config.go +++ b/config/config.go @@ -12,6 +12,7 @@ var ( InfluxDbName = getEnvOrPanic("INFLUX_DB_NAME") InfluxDbRetentionDuration = getEnvOrPanic("INFLUX_DB_RETENTION_DURATION") InfluxDbTagInstance = getEnvOrPanic("INFLUX_DB_TAG_INSTANCE") + LocalSourceIPs = getEnvOrPanic("INFLUX_DB_TAG_SOURCE_IPS_LOCAL") ) func getEnvOrPanic(envName string) string { diff --git a/docker-compose.yml b/docker-compose.yml index c1e8936..b83228f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -53,6 +53,7 @@ services: - INFLUX_DB_NAME=monitoring - INFLUX_DB_RETENTION_DURATION=52w - INFLUX_DB_TAG_INSTANCE=my-instance + - INFLUX_DB_TAG_SOURCE_IPS_LOCAL="127., 10., 192., 176." volumes: - /var/run/docker.sock:/var/run/docker.sock:ro - /~nginx-proxy-metrics/data/GeoLite2-City.mmdb:/GeoLite2-City.mmdb:ro diff --git a/persistence/influxdb_http_request_persister.go b/persistence/influxdb_http_request_persister.go index 998aab6..055556d 100644 --- a/persistence/influxdb_http_request_persister.go +++ b/persistence/influxdb_http_request_persister.go @@ -7,6 +7,7 @@ import ( "nginx-proxy-metrics/config" "nginx-proxy-metrics/logline" "strconv" + "strings" ) const SeriesName = "http_requests" @@ -67,6 +68,7 @@ func (persister InfluxdbHttpRequestPersister) Persist(httpRequest logline.HttpRe "city": httpRequest.City, "instance": config.InfluxDbTagInstance, "bot": strconv.FormatBool(httpRequest.Bot), + "source_ip_area": getSourceIpArea(httpRequest.SourceIp), } fields := map[string]interface{}{ @@ -91,6 +93,16 @@ func (persister InfluxdbHttpRequestPersister) Persist(httpRequest logline.HttpRe } } +func getSourceIpArea(ip string) string { + localSourceIpSlice := strings.Split(config.LocalSourceIPs, ",") + for _, prefix := range localSourceIpSlice { + if strings.HasPrefix(ip, strings.TrimSpace(prefix)) { + return "local" + } + } + return "public" +} + func queryDB(clnt client.Client, cmd string) (res []client.Result, err error) { q := client.Query{ Command: cmd,