Skip to content

Commit

Permalink
feat: Tagging of local source IPs.
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
Tim committed Sep 10, 2023
1 parent e069847 commit 59cc6fe
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions persistence/influxdb_http_request_persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"nginx-proxy-metrics/config"
"nginx-proxy-metrics/logline"
"strconv"
"strings"
)

const SeriesName = "http_requests"
Expand Down Expand Up @@ -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{}{
Expand All @@ -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,
Expand Down

0 comments on commit 59cc6fe

Please sign in to comment.