diff --git a/.bingo/.gitignore b/.bingo/.gitignore new file mode 100644 index 0000000..9efccf6 --- /dev/null +++ b/.bingo/.gitignore @@ -0,0 +1,13 @@ + +# Ignore everything +* + +# But not these files: +!.gitignore +!*.mod +!*.sum +!README.md +!Variables.mk +!variables.env + +*tmp.mod diff --git a/.bingo/README.md b/.bingo/README.md new file mode 100644 index 0000000..7a5c2d4 --- /dev/null +++ b/.bingo/README.md @@ -0,0 +1,14 @@ +# Project Development Dependencies. + +This is directory which stores Go modules with pinned buildable package that is used within this repository, managed by https://github.com/bwplotka/bingo. + +* Run `bingo get` to install all tools having each own module file in this directory. +* Run `bingo get ` to install that have own module file in this directory. +* For Makefile: Make sure to put `include .bingo/Variables.mk` in your Makefile, then use $() variable where is the .bingo/.mod. +* For shell: Run `source .bingo/variables.env` to source all environment variable for each tool. +* For go: Import `.bingo/variables.go` to for variable names. +* See https://github.com/bwplotka/bingo or -h on how to add, remove or change binaries dependencies. + +## Requirements + +* Go 1.14+ diff --git a/.bingo/Variables.mk b/.bingo/Variables.mk new file mode 100644 index 0000000..1a26844 --- /dev/null +++ b/.bingo/Variables.mk @@ -0,0 +1,31 @@ +# Auto generated binary variables helper managed by https://github.com/bwplotka/bingo v0.9. DO NOT EDIT. +# All tools are designed to be build inside $GOBIN. +BINGO_DIR := $(dir $(lastword $(MAKEFILE_LIST))) +GOPATH ?= $(shell go env GOPATH) +GOBIN ?= $(firstword $(subst :, ,${GOPATH}))/bin +GO ?= $(shell which go) + +# Below generated variables ensure that every time a tool under each variable is invoked, the correct version +# will be used; reinstalling only if needed. +# For example for bingo variable: +# +# In your main Makefile (for non array binaries): +# +#include .bingo/Variables.mk # Assuming -dir was set to .bingo . +# +#command: $(BINGO) +# @echo "Running bingo" +# @$(BINGO) +# +BINGO := $(GOBIN)/bingo-v0.9.0 +$(BINGO): $(BINGO_DIR)/bingo.mod + @# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies. + @echo "(re)installing $(GOBIN)/bingo-v0.9.0" + @cd $(BINGO_DIR) && GOWORK=off $(GO) build -mod=mod -modfile=bingo.mod -o=$(GOBIN)/bingo-v0.9.0 "github.com/bwplotka/bingo" + +COPYRIGHT := $(GOBIN)/copyright-v0.0.0-20230505153745-6b7392939a60 +$(COPYRIGHT): $(BINGO_DIR)/copyright.mod + @# Install binary/ries using Go 1.14+ build command. This is using bwplotka/bingo-controlled, separate go module with pinned dependencies. + @echo "(re)installing $(GOBIN)/copyright-v0.0.0-20230505153745-6b7392939a60" + @cd $(BINGO_DIR) && GOWORK=off $(GO) build -mod=mod -modfile=copyright.mod -o=$(GOBIN)/copyright-v0.0.0-20230505153745-6b7392939a60 "github.com/efficientgo/tools/copyright" + diff --git a/.bingo/bingo.mod b/.bingo/bingo.mod new file mode 100644 index 0000000..d228867 --- /dev/null +++ b/.bingo/bingo.mod @@ -0,0 +1,5 @@ +module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT + +go 1.24.1 + +require github.com/bwplotka/bingo v0.9.0 diff --git a/.bingo/bingo.sum b/.bingo/bingo.sum new file mode 100644 index 0000000..f231dd1 --- /dev/null +++ b/.bingo/bingo.sum @@ -0,0 +1,29 @@ +github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww= +github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y= +github.com/bwplotka/bingo v0.9.0 h1:slnsdJYExR4iRalHR6/ZiYnr9vSazOuFGmc2LdX293g= +github.com/bwplotka/bingo v0.9.0/go.mod h1:GxC/y/xbmOK5P29cn+B3HuOSw0s2gruddT3r+rDizDw= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/efficientgo/core v1.0.0-rc.0 h1:jJoA0N+C4/knWYVZ6GrdHOtDyrg8Y/TR4vFpTaqTsqs= +github.com/efficientgo/core v1.0.0-rc.0/go.mod h1:kQa0V74HNYMfuJH6jiPiwNdpWXl4xd/K4tzlrcvYDQI= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc= +github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= +github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= +golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +mvdan.cc/sh/v3 v3.7.0 h1:lSTjdP/1xsddtaKfGg7Myu7DnlHItd3/M2tomOcNNBg= +mvdan.cc/sh/v3 v3.7.0/go.mod h1:K2gwkaesF/D7av7Kxl0HbF5kGOd2ArupNTX3X44+8l8= diff --git a/.bingo/copyright.mod b/.bingo/copyright.mod new file mode 100644 index 0000000..28db49d --- /dev/null +++ b/.bingo/copyright.mod @@ -0,0 +1,5 @@ +module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT + +go 1.24.1 + +require github.com/efficientgo/tools/copyright v0.0.0-20230505153745-6b7392939a60 diff --git a/.bingo/copyright.sum b/.bingo/copyright.sum new file mode 100644 index 0000000..50d6298 --- /dev/null +++ b/.bingo/copyright.sum @@ -0,0 +1,125 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4 h1:EBTWhcAX7rNQ80RLwLCpHZBBrJuzallFHnF+yMXo928= +github.com/alecthomas/units v0.0.0-20201120081800-1786d5ef83d4/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= +github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= +github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/efficientgo/tools v0.0.0-20201228165755-e2b84817bf79 h1:Pi2rMrMQeQ45UAZr8Ple+eSk9WCyglYpkFh0r22E00c= +github.com/efficientgo/tools v0.0.0-20201228165755-e2b84817bf79/go.mod h1:jQUsxCcf91LHRhOnGqrx/yrleJbosynzf29/UlCbzlk= +github.com/efficientgo/tools/copyright v0.0.0-20230505153745-6b7392939a60 h1:VzZsCghz61QtXsUsjWt+iV/OafxaqIPxtSenQHElOL0= +github.com/efficientgo/tools/copyright v0.0.0-20230505153745-6b7392939a60/go.mod h1:5J0wuuxLMX06WeEgnpf+SvTCptlR9+RHRNO/WEMAwSw= +github.com/efficientgo/tools/core v0.0.0-20210106193344-1108f4e7d16b h1:yi5z8x/FKDHrqtEFiAsxF5b7Sz2+CJrRwBC2kbyhVcA= +github.com/efficientgo/tools/core v0.0.0-20210106193344-1108f4e7d16b/go.mod h1:RJm2+KCRfMUwgEgRte3obd5uLdVY5YbDZjgSMPY0HSA= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/felixge/fgprof v0.9.1/go.mod h1:7/HK6JFtFaARhIljgP2IV8rJLIoHDoOYoUphsnGvqxE= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM= +github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/pprof v0.0.0-20200615235658-03e1cf38a040/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= +github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/protoconfig/protoconfig/go v0.0.0-20210106192113-733758adefac h1:PWrv6uwNBua14NbS74ukVgXgdRDQPx/2B+Rf6KXXoQk= +github.com/protoconfig/protoconfig/go v0.0.0-20210106192113-733758adefac/go.mod h1:ig8lL2CeTS14ijDIIRoi6pTap0BHc0Xrnke+SKmn9QM= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.0.0-20201020161133-226fd2f889ca/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= +gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/.bingo/go.mod b/.bingo/go.mod new file mode 100644 index 0000000..610249a --- /dev/null +++ b/.bingo/go.mod @@ -0,0 +1 @@ +module _ // Fake go.mod auto-created by 'bingo' for go -moddir compatibility with non-Go projects. Commit this file, together with other .mod files. \ No newline at end of file diff --git a/.bingo/variables.env b/.bingo/variables.env new file mode 100644 index 0000000..7853d48 --- /dev/null +++ b/.bingo/variables.env @@ -0,0 +1,14 @@ +# Auto generated binary variables helper managed by https://github.com/bwplotka/bingo v0.9. DO NOT EDIT. +# All tools are designed to be build inside $GOBIN. +# Those variables will work only until 'bingo get' was invoked, or if tools were installed via Makefile's Variables.mk. +GOBIN=${GOBIN:=$(go env GOBIN)} + +if [ -z "$GOBIN" ]; then + GOBIN="$(go env GOPATH)/bin" +fi + + +BINGO="${GOBIN}/bingo-v0.9.0" + +COPYRIGHT="${GOBIN}/copyright-v0.0.0-20230505153745-6b7392939a60" + diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 36e4039..3509f27 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,7 +1,7 @@ version: 2 updates: - package-ecosystem: gomod - directory: "/" + directories: ["/", "/example", "providers/**/"] schedule: interval: monthly - package-ecosystem: github-actions diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 5377331..a8cb1a2 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -2,29 +2,27 @@ name: Go on: push: - branches: [ "main" ] + branches: [ "main", "v1", "v2" ] pull_request: - branches: [ "main" ] jobs: - build: + test: runs-on: ubuntu-latest strategy: + fail-fast: false matrix: - go: [ '1.23', '1.24' ] - name: Go ${{ matrix.go }} + go: ["oldstable", "stable"] + name: Tests on Go ${{ matrix.go }} steps: - - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + - name: Checkout repository + uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1 - name: Install Go - uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5.3.0 + uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 with: go-version: ${{ matrix.go }} check-latest: true - - name: Build - run: go build -v ./... - - - name: Test - run: go test -v ./... + - name: Run tests + run: make test-race diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 6559285..f51a7fe 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -2,12 +2,7 @@ name: golangci-lint on: push: - paths: - - "go.sum" - - "go.mod" - - "**.go" - - ".github/workflows/golangci-lint.yml" - - ".golangci.yml" + branches: [ "main", "v1", "v2" ] pull_request: permissions: @@ -22,13 +17,23 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout repository - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1 - name: Install Go - uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5.3.0 + uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 with: go-version: 1.24.x - - name: Lint - uses: golangci/golangci-lint-action@2226d7cb06a077cd73e56eedd38eecad18e5d837 # v6.5.0 + - name: Install golangci-lint + uses: golangci/golangci-lint-action@0a35821d5c230e903fcfe077583637dea1b27b47 # v9.0.0 with: - args: --verbose - version: v1.64 + # golangci-lint needs to be run separately for every Go module, and + # its GitHub Action doesn't provide any way to do that. Have it fetch + # the golangci-lint binary, trick it into not running by sending only + # `--help`, then run the full set of lints below. DO NOT run separate + # modules as separate golangci-lint-action steps. Its post run caching + # can be extremely slow, and that's amplified in a very painful way if + # it needs to be run multiple times. + args: --help + version: v2.6.2 + - name: Run lint + run: make lint + diff --git a/.gitignore b/.gitignore index 6739c76..64ae7f3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,160 +1,25 @@ -# Created by .ignore support plugin (hsz.mobi) -### JetBrains template -# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm -# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 - -# User-specific stuff: -.idea -.idea/workspace.xml -.idea/tasks.xml -.idea/dictionaries -.idea/vcs.xml -.idea/jsLibraryMappings.xml - -# Sensitive or high-churn files: -.idea/dataSources.ids -.idea/dataSources.xml -.idea/dataSources.local.xml -.idea/sqlDataSources.xml -.idea/dynamic.xml -.idea/uiDesigner.xml - -# Gradle: -.idea/gradle.xml -.idea/libraries - -# Mongo Explorer plugin: -.idea/mongoSettings.xml - -## File-based project format: -*.iws - -## Plugin-specific files: - -# IntelliJ -/out/ - -# mpeltonen/sbt-idea plugin -.idea_modules/ - -# JIRA plugin -atlassian-ide-plugin.xml - -# Crashlytics plugin (for Android Studio and IntelliJ) -com_crashlytics_export_strings.xml -crashlytics.properties -crashlytics-build.properties -fabric.properties -### Go template -# Compiled Object files, Static and Dynamic libs (Shared Objects) -*.o -*.a +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll *.so +*.dylib -# Folders -_obj -_test - -# Architecture specific extensions/prefixes -*.[568vq] -[568vq].out - -*.cgo1.go -*.cgo2.c -_cgo_defun.c -_cgo_gotypes.go -_cgo_export.* - -_testmain.go - -*.exe +# Test binary, built with `go test -c` *.test -*.prof -### Python template -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# Distribution / packaging -.Python -env/ -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -*.egg-info/ -.installed.cfg -*.egg - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*,cover -.hypothesis/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# IPython Notebook -.ipynb_checkpoints - -# pyenv -.python-version - -# celery beat schedule file -celerybeat-schedule - -# dotenv -.env -# virtualenv -venv/ -ENV/ +# Output of the go coverage tool, specifically when used with LiteIDE +*.out -# Spyder project settings -.spyderproject +# Dependency directories (remove the comment below to include it) +# vendor/ -# Rope project settings -.ropeproject +# Go workspace file +go.work +go.work.sum +# env file +.env \ No newline at end of file diff --git a/.golangci.yml b/.golangci.yml index b7bb5f8..1304988 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,15 +1,31 @@ +version: "2" linters: enable: - misspell - -issues: - exclude-rules: - - path: _test.go - linters: - - errcheck - -linters-settings: - errcheck: - exclude-functions: - # Used in HTTP handlers, any error is handled by the server itself. - - (net/http.ResponseWriter).Write + - revive + settings: + errcheck: + exclude-functions: + - (net/http.ResponseWriter).Write + exclusions: + generated: lax + presets: + - comments + - common-false-positives + - legacy + - std-error-handling + rules: + - linters: + - errcheck + path: _test.go + paths: + - third_party$ + - builtin$ + - examples$ +formatters: + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a9654fa..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -sudo: false -language: go -go: -- "1.8" -- "1.9" -- "1.10" -- "1.11" -- "1.12" - -install: -- go get github.com/stretchr/testify -- go get github.com/prometheus/client_golang/prometheus -- go get golang.org/x/net/context -- go get golang.org/x/net/trace - -script: -- go test -v ./... diff --git a/COPYRIGHT b/COPYRIGHT new file mode 100644 index 0000000..3a05677 --- /dev/null +++ b/COPYRIGHT @@ -0,0 +1,2 @@ +Copyright (c) The go-conntrack Authors. +Licensed under the Apache License 2.0. \ No newline at end of file diff --git a/LICENSE b/LICENSE index 8dada3e..25224b7 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright {yyyy} {name of copyright owner} + Copyright (c) The go-conntrack authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f11ec83 --- /dev/null +++ b/Makefile @@ -0,0 +1,52 @@ +include .bingo/Variables.mk + +# Note: Macos requires `brew install findutils` to support xargs -i + +PROVIDER_MODULES ?= $(shell find $(PWD)/providers -name "go.mod" | xargs dirname) +MODULES ?= $(PROVIDER_MODULES) example +GOBIN ?= $(firstword $(subst :, ,${GOPATH}))/bin + +.PHONY: replace-add replace-drop deps lint test test-race $(MODULES) + +replace-add replace-drop deps lint test test-race: $(MODULES) +$(MODULES): + $(MAKE) -C $@ $(MAKECMDGOALS) + +define require_clean_work_tree + @git update-index -q --ignore-submodules --refresh + + @if ! git diff-files --quiet --ignore-submodules --; then \ + echo >&2 "cannot $1: you have unstaged changes."; \ + git diff-files --name-status -r --ignore-submodules -- >&2; \ + echo >&2 "Please commit or stash them."; \ + exit 1; \ + fi + + @if ! git diff-index --cached --quiet HEAD --ignore-submodules --; then \ + echo >&2 "cannot $1: your index contains uncommitted changes."; \ + git diff-index --cached --name-status -r --ignore-submodules HEAD -- >&2; \ + echo >&2 "Please commit or stash them."; \ + exit 1; \ + fi + +endef + +deps: + go mod tidy + +lint: $(COPYRIGHT) + @echo ">> ensuring copyright headers" + @$(COPYRIGHT) $(shell go list -f "{{.Dir}}" ./... | xargs -i find "{}" -name "*.go") + @$(call require_clean_work_tree,"set copyright headers") + @echo ">> ensured all .go files have copyright headers" + + @echo "Running lint" + @$(call require_clean_work_tree,"before lint") + golangci-lint run --config=.golangci.yml + @$(call require_clean_work_tree,"lint and format files") + +test: + go test ./... + +test-race: + go test -race ./... diff --git a/NOTICE.md b/NOTICE.md index ee9f67c..9970c9d 100644 --- a/NOTICE.md +++ b/NOTICE.md @@ -1 +1 @@ -This library is a fork of https://github.com/mwitkow/go-conntrack: Copyright 2016 Michal Witkowski +This library is a fork of https://github.com/mwitkow/go-conntrack: Copyright 2016 Michal Witkowski (@mwitkow) diff --git a/connection_closer.go b/connection_closer.go new file mode 100644 index 0000000..c28e7b6 --- /dev/null +++ b/connection_closer.go @@ -0,0 +1,40 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package conntrack + +import ( + "context" + "net" + "time" +) + +type connectionCloseTracker struct { + net.Conn + ctx context.Context + isClient bool + tracker ConnectionTracker + beginTime time.Time +} + +func newConnectionCloseTracker(ctx context.Context, inner net.Conn, isClient bool, tracker ConnectionTracker, beginTime time.Time) net.Conn { + return &connectionCloseTracker{ + Conn: inner, + ctx: ctx, + isClient: isClient, + tracker: tracker, + beginTime: beginTime, + } +} + +func (ct *connectionCloseTracker) Close() error { + err := ct.Conn.Close() + ct.tracker.TrackConnection(ct.ctx, &ConnectionClosed{ + Client: ct.isClient, + Error: err, + BeginTime: ct.beginTime, + EndTime: time.Now(), + }) + + return err +} diff --git a/connection_closer_test.go b/connection_closer_test.go new file mode 100644 index 0000000..5a1e3d8 --- /dev/null +++ b/connection_closer_test.go @@ -0,0 +1,82 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package conntrack + +import ( + "context" + "errors" + "net" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestConnectionCloser(t *testing.T) { + expectedCloseErr := errors.New("boom") + + tcs := []struct { + desc string + client bool + closeErr error + }{ + { + desc: "Server connection closed without error", + client: false, + closeErr: nil, + }, + { + desc: "Client connection closed with error", + client: true, + closeErr: expectedCloseErr, + }, + } + + for _, tc := range tcs { + t.Run(tc.desc, func(t *testing.T) { + tracker := &testConnectionTracker{ + stats: []ConnectionStats{}, + } + conn := &connUnderTest{ + closeErr: tc.closeErr, + } + beginTime := time.Now().Add(-time.Hour) + closer := newConnectionCloseTracker(context.Background(), conn, tc.client, tracker, beginTime) + err := closer.Close() + require.Len(t, tracker.stats, 1) + s := tracker.stats[0].(*ConnectionClosed) + require.NotNil(t, s) + + if tc.closeErr == nil { + require.NoError(t, err) + require.Equal(t, tc.client, s.Client) + require.NoError(t, s.Error) + } else { + require.ErrorIs(t, err, expectedCloseErr) + require.Equal(t, tc.client, s.Client) + require.ErrorIs(t, s.Error, expectedCloseErr) + } + + require.Equal(t, s.BeginTime, beginTime) + require.Greater(t, s.EndTime, beginTime) + }) + } +} + +type connUnderTest struct { + net.Conn + closeErr error +} + +func (c *connUnderTest) Close() error { + return c.closeErr +} + +type testConnectionTracker struct { + stats []ConnectionStats +} + +func (t *testConnectionTracker) TrackConnection(_ context.Context, stats ConnectionStats) { + t.stats = append(t.stats, stats) +} diff --git a/connection_tracking.go b/connection_tracking.go new file mode 100644 index 0000000..a4f604c --- /dev/null +++ b/connection_tracking.go @@ -0,0 +1,288 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package conntrack + +import ( + "context" + "net" + "os" + "syscall" + "time" +) + +// DialerConnectionTagInfo defines the relevant information needed by dialer connection context tagger. +type DialerConnectionTagInfo struct { + // DialerName is the name of the dialer. + DialerName string + + // RemoteAddr is the remote address of the corresponding connection. + Network string + + // LocalAddr is the local address of the corresponding connection. + Addr string +} + +// DialerConnectionTagger the interface for tagging (setup) dialer connection contexts. +type DialerConnectionTagger interface { + // TagDialerConnection + // Note: The ctx is propagated/derived from outgoing connections. + TagDialerConnection(ctx context.Context, info *DialerConnectionTagInfo) context.Context +} + +// ListenerConnectionTagInfo defines the relevant information needed by listener connection context tagger. +type ListenerConnectionTagInfo struct { + // ListenerName is the name of the listener. + ListenerName string +} + +// ListenerConnectionTagger the interface for tagging (setup) listener connection contexts. +type ListenerConnectionTagger interface { + // TagListenerConnection + // Note: The ctx is not propagated/derived from incoming connections. + TagListenerConnection(ctx context.Context, info *ListenerConnectionTagInfo) context.Context +} + +// ConnectionStats defines the interface used for tracking dialer and listener connection events. +type ConnectionStats interface { + // IsClient returns true if a client connection triggered the event. + IsClient() bool +} + +// ConnectionAttempt connection tracker event used when a connection attempt is made. +type ConnectionAttempt struct { + // Client returns true if a client connection triggered the event. + Client bool + + // Attempt the sequence number of the connection attempt. + Attempt int + + // BeginTime the tim when the connection attempt started. + BeginTime time.Time +} + +// IsClient returns true if a client connection triggered the event. +func (c ConnectionAttempt) IsClient() bool { + return c.Client +} + +// ConnectionAttemptFailed connection tracker event used when a connection attempt fails. +type ConnectionAttemptFailed struct { + // Client returns true if a client connection triggered the event. + Client bool + + // Attempt the sequence number of the connection attempt. + Attempt int + + // BeginTime the time when the connection attempt started. + BeginTime time.Time + + // EndTime the time when the connection attempt failed. + EndTime time.Time + + // Error the error returned when the connection attempt failed. + Error error + + // Reason the [FailureReason] when the connection attempt failed. + Reason FailureReason +} + +// IsClient returns true if a client connection triggered the event. +func (c ConnectionAttemptFailed) IsClient() bool { + return c.Client +} + +// ConnectionFailed connection tracker event used when a connection fails (after X attempt(s) has failed). +type ConnectionFailed struct { + // Client returns true if a client connection triggered the event. + Client bool + + // Attempts the total number of attempts made before the connection failed. + Attempts int + + // BeginTime the time when the first connection attempt started. + BeginTime time.Time + + // EndTime the time when the connection failed. + EndTime time.Time + + // Error the error returned when the connection failed. + Error error + + // Reason the [FailureReason] when the connection failed. + Reason FailureReason +} + +// IsClient returns true if a client connection triggered the event. +func (c ConnectionFailed) IsClient() bool { + return c.Client +} + +// ConnectionEstablished connection tracker event used when a connection is established. +type ConnectionEstablished struct { + // Client returns true if a client connection triggered the event. + Client bool + + // Attempts the total number of attempts made before the connection was established. + Attempts int + + // BeginTime the time when the first connection attempt started. + BeginTime time.Time + + // EndTime the time when the connection was established. + EndTime time.Time + + // LocalAddr the local address of the established connection. + LocalAddr net.Addr + + // LocalAddr the remote address of the established connection. + RemoteAddr net.Addr +} + +// IsClient returns true if a client connection triggered the event. +func (c ConnectionEstablished) IsClient() bool { + return c.Client +} + +// ConnectionClosed connection tracker event used when a connection is closed. +type ConnectionClosed struct { + // Client returns true if a client connection triggered the event. + Client bool + + // BeginTime the time when the connection was established. + BeginTime time.Time + + // EndTime the time when the connection was closed. + EndTime time.Time + + // Error the error returned when the connection was closed, if any. + Error error +} + +// IsClient returns true if a client connection triggered the event. +func (c ConnectionClosed) IsClient() bool { + return c.Client +} + +// ConnectionTracker is the interface for tracking [net.Conn] connections for +// inbound/server ([net.Listener]) and outbound/client ([net.Dialer]). +type ConnectionTracker interface { + // TrackConnection tracks the + TrackConnection(ctx context.Context, stats ConnectionStats) +} + +// ConnectionTrackerFunc is an adapter to allow the use of +// ordinary functions as [ConnectionTracker]. If f is a function +// with the appropriate signature, ConnectionTrackerFunc(f) is a +// [ConnectionTracker] that calls f. +type ConnectionTrackerFunc func(ctx context.Context, stats ConnectionStats) + +// TrackConnection calls fn(ctx, stats). +func (fn ConnectionTrackerFunc) TrackConnection(ctx context.Context, stats ConnectionStats) { + fn(ctx, stats) +} + +// FailureReason enum defines reasons for connection failures. +type FailureReason string + +const ( + // FailureReasonResolution failed to resolve connection address. + FailureReasonResolution FailureReason = "resolution" + + // FailureReasonConnectionRefused connection was refused. + FailureReasonConnectionRefused FailureReason = "refused" + + // FailureReasonTimeout connection timed out. + FailureReasonTimeout FailureReason = "timeout" + + // FailureReasonUnknown unknown reason. + FailureReasonUnknown FailureReason = "unknown" +) + +// String returns [FailureReason] as string. If empty, [FailureReasonUnknown] is returned. +func (fr FailureReason) String() string { + if string(fr) == "" { + return string(FailureReasonUnknown) + } + + return string(fr) +} + +// FailureReasons returns all defined failure reasons. +func FailureReasons() []FailureReason { + return []FailureReason{ + FailureReasonTimeout, + FailureReasonResolution, + FailureReasonConnectionRefused, + FailureReasonUnknown, + } +} + +// FailureReasonFromError extracts the [FailureReason] from the provided error. +func FailureReasonFromError(err error) FailureReason { + if netErr, ok := err.(*net.OpError); ok { + switch nestErr := netErr.Err.(type) { + case *net.DNSError: + return FailureReasonResolution + case *os.SyscallError: + if nestErr.Err == syscall.ECONNREFUSED { + return FailureReasonConnectionRefused + } + return FailureReasonUnknown + } + if netErr.Timeout() { + return FailureReasonTimeout + } + } else if err == context.Canceled || err == context.DeadlineExceeded { + return FailureReasonTimeout + } + + return FailureReasonUnknown +} + +type chainConnectionTracker struct { + trackers []ConnectionTracker +} + +// ChainConnectionTrackers chain multiple [ConnectionTracker] trackers together into a single [ConnectionTracker]. +// +// Note: If trackers is nil, then a nil [ConnectionTracker] will be returned. +func ChainConnectionTrackers(trackers ...ConnectionTracker) ConnectionTracker { + if trackers == nil { + return nil + } + + if len(trackers) == 1 { + return trackers[0] + } + + return &chainConnectionTracker{ + trackers: trackers, + } +} + +func (ct chainConnectionTracker) TagDialerConnection(ctx context.Context, info *DialerConnectionTagInfo) context.Context { + for _, t := range ct.trackers { + if t, ok := t.(DialerConnectionTagger); ok { + ctx = t.TagDialerConnection(ctx, info) + } + } + + return ctx +} + +func (ct chainConnectionTracker) TagListenerConnection(ctx context.Context, info *ListenerConnectionTagInfo) context.Context { + for _, h := range ct.trackers { + if t, ok := h.(ListenerConnectionTagger); ok { + ctx = t.TagListenerConnection(ctx, info) + } + } + + return ctx +} + +func (ct chainConnectionTracker) TrackConnection(ctx context.Context, stats ConnectionStats) { + for _, h := range ct.trackers { + h.TrackConnection(ctx, stats) + } +} diff --git a/connhelpers/tls.go b/connhelpers/tls.go index 9d902bc..da7aea6 100644 --- a/connhelpers/tls.go +++ b/connhelpers/tls.go @@ -1,5 +1,5 @@ -// Copyright 2016 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. package connhelpers @@ -8,9 +8,9 @@ import ( "fmt" ) -// TlsConfigForServerCerts is a returns a simple `tls.Config` with the given server cert loaded. +// TLSConfigForServerCerts is a returns a simple `tls.Config` with the given server cert loaded. // This is useful if you can't use `http.ListenAndServerTLS` when using a custom `net.Listener`. -func TlsConfigForServerCerts(certFile string, keyFile string) (*tls.Config, error) { +func TLSConfigForServerCerts(certFile string, keyFile string) (*tls.Config, error) { var err error config := new(tls.Config) config.Certificates = make([]tls.Certificate, 1) @@ -21,9 +21,9 @@ func TlsConfigForServerCerts(certFile string, keyFile string) (*tls.Config, erro return config, nil } -// TlsConfigWithHttp2Enabled makes it easy to configure the given `tls.Config` to prefer H2 connections. +// TLSConfigWithHTTP2Enabled makes it easy to configure the given `tls.Config` to prefer H2 connections. // This is useful if you can't use `http.ListenAndServerTLS` when using a custom `net.Listener`. -func TlsConfigWithHttp2Enabled(config *tls.Config) (*tls.Config, error) { +func TLSConfigWithHTTP2Enabled(config *tls.Config) (*tls.Config, error) { // mostly based on http2 code in the standards library. if config.CipherSuites != nil { // If they already provided a CipherSuite list, return diff --git a/dialer_reporter.go b/dialer_reporter.go deleted file mode 100644 index 96da056..0000000 --- a/dialer_reporter.go +++ /dev/null @@ -1,113 +0,0 @@ -// Copyright 2016 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. - -package conntrack - -import ( - "context" - "net" - "os" - "syscall" - - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" -) - -type failureReason string - -const ( - failedResolution = "resolution" - failedConnRefused = "refused" - failedTimeout = "timeout" - failedUnknown = "unknown" -) - -var ( - dialerAttemptedTotal = promauto.NewCounterVec( - prometheus.CounterOpts{ - Namespace: "net", - Subsystem: "conntrack", - Name: "dialer_conn_attempted_total", - Help: "Total number of connections attempted by the given dialer a given name.", - }, []string{"dialer_name"}) - - dialerConnEstablishedTotal = promauto.NewCounterVec( - prometheus.CounterOpts{ - Namespace: "net", - Subsystem: "conntrack", - Name: "dialer_conn_established_total", - Help: "Total number of connections successfully established by the given dialer a given name.", - }, []string{"dialer_name"}) - - dialerConnFailedTotal = promauto.NewCounterVec( - prometheus.CounterOpts{ - Namespace: "net", - Subsystem: "conntrack", - Name: "dialer_conn_failed_total", - Help: "Total number of connections failed to dial by the dialer a given name.", - }, []string{"dialer_name", "reason"}) - - dialerConnClosedTotal = promauto.NewCounterVec( - prometheus.CounterOpts{ - Namespace: "net", - Subsystem: "conntrack", - Name: "dialer_conn_closed_total", - Help: "Total number of connections closed which originated from the dialer of a given name.", - }, []string{"dialer_name"}) - - dialerConnOpen = promauto.NewGaugeVec( - prometheus.GaugeOpts{ - Namespace: "net", - Subsystem: "conntrack", - Name: "dialer_conn_open", - Help: "Number of open connections which originated from the dialer of a given name.", - }, []string{"dialer_name"}) -) - -// preRegisterDialerMetrics pre-populates Prometheus labels for the given dialer name, to avoid Prometheus missing labels issue. -func PreRegisterDialerMetrics(dialerName string) { - dialerAttemptedTotal.WithLabelValues(dialerName) - dialerConnEstablishedTotal.WithLabelValues(dialerName) - for _, reason := range []failureReason{failedTimeout, failedResolution, failedConnRefused, failedUnknown} { - dialerConnFailedTotal.WithLabelValues(dialerName, string(reason)) - } - dialerConnClosedTotal.WithLabelValues(dialerName) - dialerConnOpen.WithLabelValues(dialerName) -} - -func reportDialerConnAttempt(dialerName string) { - dialerAttemptedTotal.WithLabelValues(dialerName).Inc() -} - -func reportDialerConnEstablished(dialerName string) { - dialerConnEstablishedTotal.WithLabelValues(dialerName).Inc() - dialerConnOpen.WithLabelValues(dialerName).Inc() -} - -func reportDialerConnClosed(dialerName string) { - dialerConnClosedTotal.WithLabelValues(dialerName).Inc() - dialerConnOpen.WithLabelValues(dialerName).Dec() -} - -func reportDialerConnFailed(dialerName string, err error) { - if netErr, ok := err.(*net.OpError); ok { - switch nestErr := netErr.Err.(type) { - case *net.DNSError: - dialerConnFailedTotal.WithLabelValues(dialerName, string(failedResolution)).Inc() - return - case *os.SyscallError: - if nestErr.Err == syscall.ECONNREFUSED { - dialerConnFailedTotal.WithLabelValues(dialerName, string(failedConnRefused)).Inc() - } - dialerConnFailedTotal.WithLabelValues(dialerName, string(failedUnknown)).Inc() - return - } - if netErr.Timeout() { - dialerConnFailedTotal.WithLabelValues(dialerName, string(failedTimeout)).Inc() - } - } else if err == context.Canceled || err == context.DeadlineExceeded { - dialerConnFailedTotal.WithLabelValues(dialerName, string(failedTimeout)).Inc() - return - } - dialerConnFailedTotal.WithLabelValues(dialerName, string(failedUnknown)).Inc() -} diff --git a/dialer_test.go b/dialer_test.go deleted file mode 100644 index 3ddd454..0000000 --- a/dialer_test.go +++ /dev/null @@ -1,197 +0,0 @@ -// Copyright 2016 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. - -package conntrack_test - -import ( - "context" - "net" - "net/http" - "testing" - "time" - - "github.com/marefr/go-conntrack" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" -) - -func TestDialerWrapper(t *testing.T) { - suite.Run(t, &DialerTestSuite{}) -} - -type DialerTestSuite struct { - suite.Suite - - serverListener net.Listener - httpServer http.Server -} - -func (s *DialerTestSuite) SetupSuite() { - var err error - s.serverListener, err = net.Listen("tcp", "127.0.0.1:0") - require.NoError(s.T(), err, "must be able to allocate a port for serverListener") - s.httpServer = http.Server{ - Handler: http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { - resp.WriteHeader(http.StatusOK) - }), - } - go func() { - _ = s.httpServer.Serve(s.serverListener) - }() -} - -func (s *DialerTestSuite) TestDialerMetricsArePreregistered() { - conntrack.NewDialContextFunc() // dialer name = default - conntrack.NewDialContextFunc(conntrack.DialWithName("foobar")) - conntrack.PreRegisterDialerMetrics("something_manual") - for testId, testCase := range []struct { - metricName string - existingLabels []string - }{ - {"net_conntrack_dialer_conn_attempted_total", []string{"default"}}, - {"net_conntrack_dialer_conn_attempted_total", []string{"foobar"}}, - {"net_conntrack_dialer_conn_attempted_total", []string{"something_manual"}}, - {"net_conntrack_dialer_conn_closed_total", []string{"default"}}, - {"net_conntrack_dialer_conn_closed_total", []string{"foobar"}}, - {"net_conntrack_dialer_conn_closed_total", []string{"something_manual"}}, - {"net_conntrack_dialer_conn_established_total", []string{"default"}}, - {"net_conntrack_dialer_conn_established_total", []string{"foobar"}}, - {"net_conntrack_dialer_conn_established_total", []string{"something_manual"}}, - {"net_conntrack_dialer_conn_failed_total", []string{"default", "resolution"}}, - {"net_conntrack_dialer_conn_failed_total", []string{"default", "refused"}}, - {"net_conntrack_dialer_conn_failed_total", []string{"default", "timeout"}}, - {"net_conntrack_dialer_conn_failed_total", []string{"default", "unknown"}}, - {"net_conntrack_dialer_conn_open", []string{"default"}}, - {"net_conntrack_dialer_conn_open", []string{"foobar"}}, - {"net_conntrack_dialer_conn_open", []string{"something_manual"}}, - } { - lineCount := len(fetchPrometheusLines(s.T(), testCase.metricName, testCase.existingLabels...)) - assert.NotEqual(s.T(), 0, lineCount, "metrics must exist for test case %d", testId) - } -} - -func (s *DialerTestSuite) TestDialerMetricsAreNotPreregisteredWithMonitoringOff() { - conntrack.NewDialContextFunc(conntrack.DialWithName("nomon"), conntrack.DialWithoutMonitoring()) - for testId, testCase := range []struct { - metricName string - existingLabels []string - }{ - {"net_conntrack_dialer_conn_attempted_total", []string{"nomon"}}, - {"net_conntrack_dialer_conn_closed_total", []string{"nomon"}}, - {"net_conntrack_dialer_conn_established_total", []string{"nomon"}}, - {"net_conntrack_dialer_conn_failed_total", []string{"nomon", "resolution"}}, - {"net_conntrack_dialer_conn_failed_total", []string{"nomon", "refused"}}, - {"net_conntrack_dialer_conn_failed_total", []string{"nomon", "timeout"}}, - {"net_conntrack_dialer_conn_failed_total", []string{"nomon", "unknown"}}, - {"net_conntrack_dialer_conn_open", []string{"nomon"}}, - } { - lineCount := len(fetchPrometheusLines(s.T(), testCase.metricName, testCase.existingLabels...)) - assert.Equal(s.T(), 0, lineCount, "metrics should not be registered exist for test case %d", testId) - } -} - -func (s *DialerTestSuite) TestDialerUnderNormalConnection() { - dialFunc := conntrack.NewDialContextFunc(conntrack.DialWithName("normal_conn")) - - beforeAttempts := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "normal_conn") - beforeEstablished := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "normal_conn") - beforeClosed := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "normal_conn") - beforeOpen := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_open", "normal_conn") - - conn, err := dialFunc(context.TODO(), "tcp", s.serverListener.Addr().String()) - require.NoError(s.T(), err, "NewDialContextFunc should successfully establish a conn here") - assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "normal_conn"), - "the attempted conn counter must be incremented after connection was opened") - assert.Equal(s.T(), beforeEstablished+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "normal_conn"), - "the established conn counter must be incremented after connection was opened") - assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "normal_conn"), - "the closed conn counter must not be incremented after connection was opened") - assert.Equal(s.T(), beforeOpen+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_open", "normal_conn"), - "the open conn gauge must be incremented after connection was opened") - conn.Close() - assert.Equal(s.T(), beforeClosed+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "normal_conn"), - "the closed conn counter must be incremented after connection was closed") - assert.Equal(s.T(), beforeOpen, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_open", "normal_conn"), - "the open conn gauge must be decremented after connection was closed") -} - -func (s *DialerTestSuite) TestDialerWithContextName() { - dialFunc := conntrack.NewDialContextFunc() - conntrack.PreRegisterDialerMetrics("ctx_conn") - - beforeAttempts := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "ctx_conn") - beforeEstablished := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "ctx_conn") - beforeClosed := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "ctx_conn") - - conn, err := dialFunc(conntrack.DialNameToContext(context.TODO(), "ctx_conn"), "tcp", s.serverListener.Addr().String()) - require.NoError(s.T(), err, "NewDialContextFunc should successfully establish a conn here") - assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "ctx_conn"), - "the attempted conn counter must be incremented after connection was opened") - assert.Equal(s.T(), beforeEstablished+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "ctx_conn"), - "the established conn counter must be incremented after connection was opened") - assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "ctx_conn"), - "the closed conn counter must not be incremented after connection was opened") - conn.Close() - assert.Equal(s.T(), beforeClosed+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "ctx_conn"), - "the closed conn counter must be incremented after connection was closed") -} - -func (s *ListenerTestSuite) TestDialerTracingCapturedInPage() { - dialFunc := conntrack.NewDialContextFunc(conntrack.DialWithTracing()) - dialerName := "some_dialer" - conn, err := dialFunc(conntrack.DialNameToContext(context.TODO(), dialerName), "tcp", s.serverListener.Addr().String()) - time.Sleep(5 * time.Millisecond) - require.NoError(s.T(), err, "DialContext should successfully establish a conn here") - assert.Contains(s.T(), fetchTraceEvents(s.T(), "net.ClientConn."+dialerName), conn.LocalAddr().String(), - "the /debug/trace/events page must contain the live connection") - time.Sleep(5 * time.Millisecond) - conn.Close() -} - -func (s *DialerTestSuite) TestDialerResolutionFailure() { - dialFunc := conntrack.NewDialContextFunc(conntrack.DialWithName("res_err")) - - beforeAttempts := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "res_err") - beforeEstablished := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "res_err") - beforeResolutionErrors := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_failed_total", "res_err", "resolution") - beforeClosed := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "res_err") - - _, err := dialFunc(context.TODO(), "tcp", "dialer.test.wrong.domain.wrong:443") - require.Error(s.T(), err, "NewDialContextFunc should fail here") - assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "res_err"), - "the attempted conn counter must be incremented after connection was opened") - assert.Equal(s.T(), beforeEstablished, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "res_err"), - "the established conn counter must not be incremented on a failure") - assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "res_err"), - "the closed conn counter must not be incremented on a failure") - assert.Equal(s.T(), beforeResolutionErrors+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_failed_total", "res_err", "resolution"), - "the failure counter for resolution error should be incremented") -} - -func (s *DialerTestSuite) TestDialerRefusedFailure() { - dialFunc := conntrack.NewDialContextFunc(conntrack.DialWithName("ref_err")) - - beforeAttempts := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "ref_err") - beforeEstablished := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "ref_err") - beforeResolutionErrors := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_failed_total", "ref_err", "resolution") - beforeClosed := sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "ref_err") - - _, err := dialFunc(context.TODO(), "tcp", "127.0.0.1:337") // 337 is a cool port, let's hope its unused. - require.Error(s.T(), err, "NewDialContextFunc should fail here") - assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_attempted_total", "ref_err"), - "the attempted conn counter must be incremented after connection was opened") - assert.Equal(s.T(), beforeEstablished, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_established_total", "ref_err"), - "the established conn counter must not be incremented on a failure") - assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_closed_total", "ref_err"), - "the closed conn counter must not be incremented on a failure") - assert.Equal(s.T(), beforeResolutionErrors+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_dialer_conn_failed_total", "ref_err", "refused"), - "the failure counter for connection refused error should be incremented") -} - -func (s *DialerTestSuite) TearDownSuite() { - if s.serverListener != nil { - s.T().Logf("stopped http.Server at: %v", s.serverListener.Addr().String()) - s.serverListener.Close() - } -} diff --git a/dialer_wrapper.go b/dialer_wrapper.go index bafaad0..2475459 100644 --- a/dialer_wrapper.go +++ b/dialer_wrapper.go @@ -1,66 +1,60 @@ -// Copyright 2016 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. package conntrack import ( "context" - "fmt" "net" - "sync" - - "golang.org/x/net/trace" + "time" ) -type dialerOpts struct { +type DialerOptions struct { name string - monitoring bool - tracing bool parentDialContextFunc dialerContextFunc + tracker ConnectionTracker } -// DialerOpt defines a config option you can set on the dialer. -type DialerOpt func(*dialerOpts) +// DialerOption defines a config option you can set on the dialer. +type DialerOption func(*DialerOptions) type dialerContextFunc func(context.Context, string, string) (net.Conn, error) // DialWithName sets the name of the dialer for tracking and monitoring. // This is the name for the dialer (default is `default`), but for `NewDialContextFunc` can be overwritten from the // Context using `DialNameToContext`. -func DialWithName(name string) DialerOpt { - return func(opts *dialerOpts) { +func DialWithName(name string) DialerOption { + return func(opts *DialerOptions) { opts.name = name } } -// DialWithoutMonitoring turns *off* Prometheus monitoring for this dialer. -func DialWithoutMonitoring() DialerOpt { - return func(opts *dialerOpts) { - opts.monitoring = false - } -} - -// DialWithTracing turns *on* the /debug/events tracing of the dial calls. -func DialWithTracing() DialerOpt { - return func(opts *dialerOpts) { - opts.tracing = true +// DialWithTrackers register [ConnectionTracker] trackers that tracks the dialer interactions. +func DialWithTrackers(trackers ...ConnectionTracker) DialerOption { + return func(opts *DialerOptions) { + opts.tracker = ChainConnectionTrackers(trackers...) } } // DialWithDialer allows you to override the `net.Dialer` instance used to actually conduct the dials. -func DialWithDialer(parentDialer *net.Dialer) DialerOpt { +func DialWithDialer(parentDialer *net.Dialer) DialerOption { return DialWithDialContextFunc(parentDialer.DialContext) } // DialWithDialContextFunc allows you to override func gets used for the actual dialing. The default is `net.Dialer.DialContext`. -func DialWithDialContextFunc(parentDialerFunc dialerContextFunc) DialerOpt { - return func(opts *dialerOpts) { +func DialWithDialContextFunc(parentDialerFunc dialerContextFunc) DialerOption { + return func(opts *DialerOptions) { opts.parentDialContextFunc = parentDialerFunc } } type dialerNameKey struct{} +// WithDialName returns a context that will contain a dialer name override. +func WithDialName(ctx context.Context, dialerName string) context.Context { + return context.WithValue(ctx, dialerNameKey{}, dialerName) +} + // DialNameFromContext returns the name of the dialer from the context of the DialContext func, if any. func DialNameFromContext(ctx context.Context) string { val, ok := ctx.Value(dialerNameKey{}).(string) @@ -70,96 +64,74 @@ func DialNameFromContext(ctx context.Context) string { return val } -// DialNameToContext returns a context that will contain a dialer name override. -func DialNameToContext(ctx context.Context, dialerName string) context.Context { - return context.WithValue(ctx, dialerNameKey{}, dialerName) -} - // NewDialContextFunc returns a `DialContext` function that tracks outbound connections. // The signature is compatible with `http.Tranport.DialContext` and is meant to be used there. -func NewDialContextFunc(optFuncs ...DialerOpt) func(context.Context, string, string) (net.Conn, error) { - opts := &dialerOpts{name: defaultName, monitoring: true, parentDialContextFunc: (&net.Dialer{}).DialContext} +func NewDialContextFunc(optFuncs ...DialerOption) func(context.Context, string, string) (net.Conn, error) { + opts := &DialerOptions{name: defaultName, parentDialContextFunc: (&net.Dialer{}).DialContext} for _, f := range optFuncs { f(opts) } - if opts.monitoring { - PreRegisterDialerMetrics(opts.name) - } + return func(ctx context.Context, network string, addr string) (net.Conn, error) { name := opts.name if ctxName := DialNameFromContext(ctx); ctxName != "" { name = ctxName + } else { + ctx = WithDialName(ctx, name) } return dialClientConnTracker(ctx, network, addr, name, opts) } } -// NewDialFunc returns a `Dial` function that tracks outbound connections. -// The signature is compatible with `http.Tranport.Dial` and is meant to be used there for Go < 1.7. -func NewDialFunc(optFuncs ...DialerOpt) func(string, string) (net.Conn, error) { - dialContextFunc := NewDialContextFunc(optFuncs...) - return func(network string, addr string) (net.Conn, error) { - return dialContextFunc(context.TODO(), network, addr) +func dialClientConnTracker(ctx context.Context, network string, addr string, dialerName string, opts *DialerOptions) (net.Conn, error) { + ctx = context.WithoutCancel(ctx) + if t, ok := opts.tracker.(DialerConnectionTagger); ok { + ctx = t.TagDialerConnection(ctx, &DialerConnectionTagInfo{ + DialerName: dialerName, + Network: network, + Addr: addr, + }) } -} -type clientConnTracker struct { - net.Conn - opts *dialerOpts - dialerName string - event trace.EventLog - mu sync.Mutex -} + beginTime := time.Now() + opts.tracker.TrackConnection(ctx, &ConnectionAttempt{ + Client: true, + Attempt: 1, + BeginTime: beginTime, + }) -func dialClientConnTracker(ctx context.Context, network string, addr string, dialerName string, opts *dialerOpts) (net.Conn, error) { - var event trace.EventLog - if opts.tracing { - event = trace.NewEventLog(fmt.Sprintf("net.ClientConn.%s", dialerName), fmt.Sprintf("%v", addr)) - } - if opts.monitoring { - reportDialerConnAttempt(dialerName) - } conn, err := opts.parentDialContextFunc(ctx, network, addr) if err != nil { - if event != nil { - event.Errorf("failed dialing: %v", err) - event.Finish() - } - if opts.monitoring { - reportDialerConnFailed(dialerName, err) - } + opts.tracker.TrackConnection(ctx, &ConnectionAttemptFailed{ + Client: true, + Attempt: 1, + Error: err, + Reason: FailureReasonFromError(err), + BeginTime: beginTime, + EndTime: time.Now(), + }) + + opts.tracker.TrackConnection(ctx, &ConnectionFailed{ + Client: true, + Attempts: 1, + Error: err, + Reason: FailureReasonFromError(err), + BeginTime: beginTime, + EndTime: time.Now(), + }) + return nil, err } - if event != nil { - event.Printf("established: %s -> %s", conn.LocalAddr(), conn.RemoteAddr()) - } - if opts.monitoring { - reportDialerConnEstablished(dialerName) - } - tracker := &clientConnTracker{ - Conn: conn, - opts: opts, - dialerName: dialerName, - event: event, - } - return tracker, nil -} -func (ct *clientConnTracker) Close() error { - err := ct.Conn.Close() - ct.mu.Lock() - if ct.event != nil { - if err != nil { - ct.event.Errorf("failed closing: %v", err) - } else { - ct.event.Printf("closing") - } - ct.event.Finish() - ct.event = nil - } - ct.mu.Unlock() - if ct.opts.monitoring { - reportDialerConnClosed(ct.dialerName) - } - return err + establishedTime := time.Now() + opts.tracker.TrackConnection(ctx, &ConnectionEstablished{ + Client: true, + Attempts: 1, + BeginTime: beginTime, + EndTime: establishedTime, + LocalAddr: conn.LocalAddr(), + RemoteAddr: conn.RemoteAddr(), + }) + + return newConnectionCloseTracker(ctx, conn, true, opts.tracker, establishedTime), nil } diff --git a/dialer_wrapper_test.go b/dialer_wrapper_test.go new file mode 100644 index 0000000..bb6bf04 --- /dev/null +++ b/dialer_wrapper_test.go @@ -0,0 +1,170 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package conntrack_test + +import ( + "context" + "net" + "net/http" + "testing" + "time" + + "github.com/marefr/go-conntrack/v2" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +func TestDialer(t *testing.T) { + suite.Run(t, &DialerTestSuite{}) +} + +type DialerTestSuite struct { + suite.Suite + + serverListener net.Listener + httpServer http.Server +} + +func (s *DialerTestSuite) SetupSuite() { + var err error + s.serverListener, err = net.Listen("tcp", "127.0.0.1:0") + require.NoError(s.T(), err, "must be able to allocate a port for serverListener") + s.httpServer = http.Server{ + Handler: http.HandlerFunc(func(resp http.ResponseWriter, _ *http.Request) { + resp.WriteHeader(http.StatusOK) + }), + } + go func() { + _ = s.httpServer.Serve(s.serverListener) + }() +} + +func (s *DialerTestSuite) TestDialerUnderNormalConnection() { + tracker := newTestConnectionTracker() + dialFunc := conntrack.NewDialContextFunc( + conntrack.DialWithName("normal_conn"), + conntrack.DialWithTrackers(tracker), + ) + + beginTime := time.Now() + conn, err := dialFunc(context.Background(), "tcp", s.serverListener.Addr().String()) + require.NoError(s.T(), err, "NewDialContextFunc should successfully establish a conn here") + + require.NotNil(s.T(), tracker.dialerConnectionTagInfo) + require.Equal(s.T(), "normal_conn", tracker.dialerConnectionTagInfo.DialerName) + + require.Len(s.T(), tracker.stats, 2) + sAttempt := tracker.stats[0].(*conntrack.ConnectionAttempt) + assert.NotNil(s.T(), sAttempt) + assert.True(s.T(), sAttempt.Client) + assert.Equal(s.T(), 1, sAttempt.Attempt) + assert.Greater(s.T(), sAttempt.BeginTime, beginTime) + + sEstablished := tracker.stats[1].(*conntrack.ConnectionEstablished) + assert.NotNil(s.T(), sEstablished) + assert.True(s.T(), sEstablished.Client) + assert.Equal(s.T(), 1, sEstablished.Attempts) + assert.Greater(s.T(), sEstablished.BeginTime, beginTime) + assert.Greater(s.T(), sEstablished.EndTime, sEstablished.BeginTime) + assert.Equal(s.T(), s.serverListener.Addr().String(), sEstablished.RemoteAddr.String()) + assert.NotEmpty(s.T(), sEstablished.LocalAddr.String()) + + err = conn.Close() + assert.NoError(s.T(), err) + + require.Len(s.T(), tracker.stats, 3) + sClosed := tracker.stats[2].(*conntrack.ConnectionClosed) + assert.NotNil(s.T(), sClosed) + assert.True(s.T(), sClosed.Client) + assert.NoError(s.T(), sClosed.Error) + assert.Greater(s.T(), sClosed.BeginTime, beginTime) + assert.Greater(s.T(), sClosed.EndTime, sEstablished.BeginTime) +} + +func (s *DialerTestSuite) TestDialerWithContextName() { + tracker := newTestConnectionTracker() + dialFunc := conntrack.NewDialContextFunc( + conntrack.DialWithTrackers(tracker), + ) + + _, err := dialFunc(conntrack.WithDialName(context.Background(), "ctx_conn"), "tcp", s.serverListener.Addr().String()) + require.NoError(s.T(), err, "NewDialContextFunc should successfully establish a conn here") + + require.NotNil(s.T(), tracker.dialerConnectionTagInfo) + require.Equal(s.T(), "ctx_conn", tracker.dialerConnectionTagInfo.DialerName) + require.Equal(s.T(), s.serverListener.Addr().String(), tracker.dialerConnectionTagInfo.Addr) + + dialerNameFromCtx := conntrack.DialNameFromContext(tracker.dialerConnectionTagCtx) + require.Equal(s.T(), "ctx_conn", dialerNameFromCtx) +} + +func (s *DialerTestSuite) TestDialerResolutionFailure() { + tracker := newTestConnectionTracker() + dialFunc := conntrack.NewDialContextFunc( + conntrack.DialWithName("res_err"), + conntrack.DialWithTrackers(tracker), + ) + + beginTime := time.Now() + _, err := dialFunc(context.Background(), "tcp", "dialer.test.wrong.domain.wrong:443") + require.Error(s.T(), err, "NewDialContextFunc should fail here") + + require.Len(s.T(), tracker.stats, 3) + sAttempt := tracker.stats[0].(*conntrack.ConnectionAttempt) + assert.NotNil(s.T(), sAttempt) + assert.True(s.T(), sAttempt.Client) + assert.Equal(s.T(), 1, sAttempt.Attempt) + assert.Greater(s.T(), sAttempt.BeginTime, beginTime) + + sAttemptFailed := tracker.stats[1].(*conntrack.ConnectionAttemptFailed) + assert.NotNil(s.T(), sAttemptFailed) + assert.True(s.T(), sAttemptFailed.Client) + assert.Equal(s.T(), 1, sAttemptFailed.Attempt) + assert.Error(s.T(), sAttemptFailed.Error) + assert.Equal(s.T(), conntrack.FailureReasonResolution, sAttemptFailed.Reason) + assert.Greater(s.T(), sAttemptFailed.BeginTime, beginTime) + assert.Greater(s.T(), sAttemptFailed.EndTime, sAttemptFailed.BeginTime) + + sConnFailed := tracker.stats[2].(*conntrack.ConnectionFailed) + assert.NotNil(s.T(), sConnFailed) + assert.True(s.T(), sConnFailed.Client) + assert.Error(s.T(), sConnFailed.Error) + assert.Equal(s.T(), conntrack.FailureReasonResolution, sConnFailed.Reason) + assert.Equal(s.T(), 1, sConnFailed.Attempts) + assert.Greater(s.T(), sConnFailed.BeginTime, beginTime) + assert.Greater(s.T(), sConnFailed.EndTime, sConnFailed.BeginTime) + assert.Greater(s.T(), sConnFailed.EndTime, sAttemptFailed.BeginTime) + assert.Greater(s.T(), sConnFailed.EndTime, sAttemptFailed.EndTime) +} + +func (s *DialerTestSuite) TestDialerRefusedFailure() { + tracker := newTestConnectionTracker() + dialFunc := conntrack.NewDialContextFunc( + conntrack.DialWithName("ref_err"), + conntrack.DialWithTrackers(tracker), + ) + + beginTime := time.Now() + _, err := dialFunc(context.Background(), "tcp", "127.0.0.1:337") // 337 is a cool port, let's hope its unused. + require.Error(s.T(), err, "NewDialContextFunc should fail here") + + require.Len(s.T(), tracker.stats, 3) + sConnFailed := tracker.stats[2].(*conntrack.ConnectionFailed) + assert.NotNil(s.T(), sConnFailed) + assert.True(s.T(), sConnFailed.Client) + assert.Error(s.T(), sConnFailed.Error) + assert.Equal(s.T(), conntrack.FailureReasonConnectionRefused, sConnFailed.Reason) + assert.Equal(s.T(), 1, sConnFailed.Attempts) + assert.Greater(s.T(), sConnFailed.BeginTime, beginTime) + assert.Greater(s.T(), sConnFailed.EndTime, sConnFailed.BeginTime) +} + +func (s *DialerTestSuite) TearDownSuite() { + if s.serverListener != nil { + s.T().Logf("stopping http.Server at: %v", s.serverListener.Addr().String()) + s.serverListener.Close() + s.T().Logf("stopped http.Server at: %v", s.serverListener.Addr().String()) + } +} diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..8ae6660 --- /dev/null +++ b/doc.go @@ -0,0 +1,5 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +// Package conntrack provides [net.Conn] connection tracking based on Dialer (client) and Listener (server) interactions. +package conntrack diff --git a/example/Makefile b/example/Makefile new file mode 100644 index 0000000..b69b99c --- /dev/null +++ b/example/Makefile @@ -0,0 +1,21 @@ +replace-add: + go mod edit -replace github.com/marefr/go-conntrack/v2=../ + go mod edit -replace github.com/marefr/go-conntrack/providers/prometheus=../providers/prometheus + go mod edit -replace github.com/marefr/go-conntrack/providers/trace=../providers/trace + go mod tidy + +replace-drop: + go mod edit -dropreplace=github.com/marefr/go-conntrack/v2 + go mod edit -dropreplace=github.com/marefr/go-conntrack/providers/prometheus + go mod edit -dropreplace=github.com/marefr/go-conntrack/providers/trace + go mod tidy + +deps: + go mod tidy + +lint: + golangci-lint run --config=../.golangci.yml + +test: + +test-race: diff --git a/example/go.mod b/example/go.mod new file mode 100644 index 0000000..0096946 --- /dev/null +++ b/example/go.mod @@ -0,0 +1,32 @@ +module github.com/marefr/go-conntrack/v2/example + +go 1.24.0 + +toolchain go1.24.1 + +require ( + github.com/marefr/go-conntrack/providers/prometheus v0.0.0-00010101000000-000000000000 + github.com/marefr/go-conntrack/providers/trace v0.0.0-00010101000000-000000000000 + github.com/marefr/go-conntrack/v2 v2.0.9 + github.com/prometheus/client_golang v1.23.2 + golang.org/x/net v0.48.0 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/jpillora/backoff v1.0.0 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/prometheus/client_model v0.6.2 // indirect + github.com/prometheus/common v0.66.1 // indirect + github.com/prometheus/procfs v0.16.1 // indirect + go.yaml.in/yaml/v2 v2.4.2 // indirect + golang.org/x/sys v0.39.0 // indirect + google.golang.org/protobuf v1.36.8 // indirect +) + +replace github.com/marefr/go-conntrack/v2 => ../ + +replace github.com/marefr/go-conntrack/providers/prometheus => ../providers/prometheus + +replace github.com/marefr/go-conntrack/providers/trace => ../providers/trace diff --git a/example/go.sum b/example/go.sum new file mode 100644 index 0000000..b61c554 --- /dev/null +++ b/example/go.sum @@ -0,0 +1,49 @@ +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= +github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= +github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= +github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o= +github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg= +github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= +github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE= +github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs= +github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA= +github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg= +github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI= +go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU= +golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU= +golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY= +golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk= +golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc= +google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/example/server.go b/example/server.go index 7c9c4eb..5b04747 100644 --- a/example/server.go +++ b/example/server.go @@ -1,5 +1,5 @@ -// Copyright 2016 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. package main @@ -7,13 +7,20 @@ import ( "crypto/tls" "flag" "fmt" - "log" + "log/slog" "net" "net/http" + "os" "time" - "github.com/marefr/go-conntrack" - "github.com/marefr/go-conntrack/connhelpers" + promtracker "github.com/marefr/go-conntrack/providers/prometheus" + "github.com/marefr/go-conntrack/providers/trace" + "github.com/marefr/go-conntrack/v2" + "github.com/marefr/go-conntrack/v2/connhelpers" + "github.com/marefr/go-conntrack/v2/logging" + "github.com/marefr/go-conntrack/v2/logging/slogadapter" + + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "golang.org/x/net/context/ctxhttp" _ "golang.org/x/net/trace" @@ -21,7 +28,7 @@ import ( var ( port = flag.Int("port", 9090, "whether to use tls or not") - useTls = flag.Bool("tls", true, "Whether to use TLS and HTTP2.") + useTLS = flag.Bool("tls", true, "Whether to use TLS and HTTP2.") tlsCertFilePath = flag.String("tls_cert_file", "certs/localhost.crt", "Path to the CRT/PEM file.") tlsKeyFilePath = flag.String("tls_key_file", "certs/localhost.key", "Path to the private key file.") ) @@ -29,27 +36,47 @@ var ( func main() { flag.Parse() + tracingTracker := trace.New() + logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})) + loggingTracker, err := logging.New(slogadapter.Logger(logger)) + if err != nil { + logger.Error("Failed to create logging tracker", "error", err) + os.Exit(1) + } + + dialerMetrics := promtracker.NewDialerMetrics() + prometheus.MustRegister(dialerMetrics) + // Since we're using a dynamic name, let's preregister it with prometheus. + dialerMetrics.InitializeMetrics("google") + // Make sure all outbound connections use the wrapped dialer. http.DefaultTransport.(*http.Transport).DialContext = conntrack.NewDialContextFunc( - conntrack.DialWithTracing(), conntrack.DialWithDialer(&net.Dialer{ Timeout: 30 * time.Second, KeepAlive: 30 * time.Second, }), + conntrack.DialWithTrackers(tracingTracker, loggingTracker, dialerMetrics.TrackDialer()), ) - // Since we're using a dynamic name, let's preregister it with prometheus. - conntrack.PreRegisterDialerMetrics("google") - handler := func(resp http.ResponseWriter, req *http.Request) { - resp.WriteHeader(http.StatusOK) - resp.Header().Add("Content-Type", "application/json") - if _, err := resp.Write([]byte(`{"msg": "hello"}`)); err != nil { - log.Printf("Failed to write response: %v", err) + handler := func(rw http.ResponseWriter, req *http.Request) { + rw.WriteHeader(http.StatusOK) + rw.Header().Add("Content-Type", "application/json") + if _, err := rw.Write([]byte(`{"msg": "hello"}`)); err != nil { + logger.Error("Failed to write response", "error", err) + } + callCtx := conntrack.WithDialName(req.Context(), "google") + resp, err := ctxhttp.Get(callCtx, http.DefaultClient, "https://www.google.comx") + if err != nil { + logger.Error("Failed to get response from Google", "error", err) + } else { + defer func() { + if err := resp.Body.Close(); err != nil { + logger.Error("Failed to close response body", "error", err) + } + }() + + logger.Info("Received response from Google", "status", resp.Status) } - callCtx := conntrack.DialNameToContext(req.Context(), "google") - _, err := ctxhttp.Get(callCtx, http.DefaultClient, "https://www.google.comx") - log.Printf("Google reached with err: %v", err) - log.Printf("Got request: %v", req) } http.DefaultServeMux.Handle("/", http.HandlerFunc(handler)) @@ -61,27 +88,37 @@ func main() { var httpListener net.Listener listener, err := net.Listen("tcp", fmt.Sprintf(":%d", *port)) if err != nil { - log.Fatalf("Failed to listen: %v", err) + logger.Error("Failed to listen", "error", err) + os.Exit(1) } - listener = conntrack.NewListener(listener, conntrack.TrackWithTracing()) - if !*useTls { + + listenerMetrics := promtracker.NewListenerMetrics() + prometheus.MustRegister(listenerMetrics) + listenerMetrics.InitializeMetrics("default") + listener = conntrack.NewListener(listener, + conntrack.ListenerWithTrackers(tracingTracker, loggingTracker, listenerMetrics.TrackListener())) + if !*useTLS { httpListener = listener } else { - tlsConfig, err := connhelpers.TlsConfigForServerCerts(*tlsCertFilePath, *tlsKeyFilePath) + tlsConfig, err := connhelpers.TLSConfigForServerCerts(*tlsCertFilePath, *tlsKeyFilePath) if err != nil { - log.Fatalf("Failed configuring TLS: %v", err) + logger.Error("Failed configuring TLS", "error", err) + os.Exit(1) } - tlsConfig, err = connhelpers.TlsConfigWithHttp2Enabled(tlsConfig) + tlsConfig, err = connhelpers.TLSConfigWithHTTP2Enabled(tlsConfig) if err != nil { - log.Fatalf("Failed configuring TLS: %v", err) + logger.Error("Failed configuring TLS with HTTP2", "error", err) + os.Exit(1) } - log.Printf("Listening with TLS") + + logger.Info("Listening with TLS") tlsListener := tls.NewListener(listener, tlsConfig) httpListener = tlsListener } - //httpListener.Addr() - log.Printf("Listening on: %s", listener.Addr().String()) + + logger.Info("Listening", "addr", listener.Addr().String()) if err := httpServer.Serve(httpListener); err != nil { - log.Fatalf("Failed listning: %v", err) + logger.Error("Failed listening", "error", err) + os.Exit(1) } } diff --git a/go.mod b/go.mod index 17dd85b..9a84f08 100644 --- a/go.mod +++ b/go.mod @@ -1,26 +1,17 @@ -module github.com/marefr/go-conntrack +module github.com/marefr/go-conntrack/v2 -go 1.23.0 +go 1.24.0 require ( github.com/jpillora/backoff v1.0.0 - github.com/prometheus/client_golang v1.21.1 - github.com/stretchr/testify v1.10.0 - golang.org/x/net v0.37.0 + github.com/stretchr/testify v1.11.1 ) require ( - github.com/beorn7/perks v1.0.1 // indirect - github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/klauspost/compress v1.17.11 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/kr/pretty v0.3.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_model v0.6.1 // indirect - github.com/prometheus/common v0.62.0 // indirect - github.com/prometheus/procfs v0.15.1 // indirect - golang.org/x/sys v0.31.0 // indirect - google.golang.org/protobuf v1.36.1 // indirect + github.com/rogpeppe/go-internal v1.10.0 // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index b246388..bbf0929 100644 --- a/go.sum +++ b/go.sum @@ -1,44 +1,23 @@ -github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= -github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= -github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= -github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= -github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc= -github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= -github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= -github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= -github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v1.21.1 h1:DOvXXTqVzvkIewV/CDPFdejpMCGeMcbGCQ8YOmu+Ibk= -github.com/prometheus/client_golang v1.21.1/go.mod h1:U9NM32ykUErtVBxdvD3zfi+EuFkkaBvMb09mIfe0Zgg= -github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= -github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= -github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io= -github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I= -github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= -github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= -github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= -github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -golang.org/x/net v0.37.0 h1:1zLorHbz+LYj7MQlSf1+2tPIIgibq2eL5xkrGk6f+2c= -golang.org/x/net v0.37.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8= -golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik= -golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= -google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk= -google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/listener_reporter.go b/listener_reporter.go deleted file mode 100644 index f6e3c97..0000000 --- a/listener_reporter.go +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2016 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. - -package conntrack - -import ( - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" -) - -var ( - listenerAcceptedTotal = promauto.NewCounterVec( - prometheus.CounterOpts{ - Namespace: "net", - Subsystem: "conntrack", - Name: "listener_conn_accepted_total", - Help: "Total number of connections opened to the listener of a given name.", - }, []string{"listener_name"}) - - listenerClosedTotal = promauto.NewCounterVec( - prometheus.CounterOpts{ - Namespace: "net", - Subsystem: "conntrack", - Name: "listener_conn_closed_total", - Help: "Total number of connections closed that were made to the listener of a given name.", - }, []string{"listener_name"}) - listenerOpen = promauto.NewGaugeVec( - prometheus.GaugeOpts{ - Namespace: "net", - Subsystem: "conntrack", - Name: "listener_conn_open", - Help: "Number of open connections to the listener of a given name.", - }, []string{"listener_name"}) -) - -// preRegisterListener pre-populates Prometheus labels for the given listener name, to avoid Prometheus missing labels issue. -func preRegisterListenerMetrics(listenerName string) { - listenerAcceptedTotal.WithLabelValues(listenerName) - listenerClosedTotal.WithLabelValues(listenerName) - listenerOpen.WithLabelValues(listenerName) -} - -func reportListenerConnAccepted(listenerName string) { - listenerAcceptedTotal.WithLabelValues(listenerName).Inc() - listenerOpen.WithLabelValues(listenerName).Inc() -} - -func reportListenerConnClosed(listenerName string) { - listenerClosedTotal.WithLabelValues(listenerName).Inc() - listenerOpen.WithLabelValues(listenerName).Dec() -} diff --git a/listener_test.go b/listener_test.go deleted file mode 100644 index 9bbed79..0000000 --- a/listener_test.go +++ /dev/null @@ -1,107 +0,0 @@ -// Copyright 2016 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. - -package conntrack_test - -import ( - "net" - "net/http" - "testing" - - "context" - - "time" - - "github.com/marefr/go-conntrack" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" -) - -func TestListenerTestSuite(t *testing.T) { - suite.Run(t, &ListenerTestSuite{}) -} - -var ( - listenerName = "some_name" -) - -type ListenerTestSuite struct { - suite.Suite - - serverListener net.Listener - httpServer http.Server -} - -func (s *ListenerTestSuite) SetupSuite() { - var err error - s.serverListener, err = net.Listen("tcp", "127.0.0.1:0") - require.NoError(s.T(), err, "must be able to allocate a port for serverListener") - s.serverListener = conntrack.NewListener(s.serverListener, conntrack.TrackWithName(listenerName), conntrack.TrackWithTracing()) - s.httpServer = http.Server{ - Handler: http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { - resp.WriteHeader(http.StatusOK) - }), - } - go func() { - _ = s.httpServer.Serve(s.serverListener) - }() -} - -func (s *ListenerTestSuite) TestTrackingMetricsPreregistered() { - // this will create the default listener, check if it is registered - conntrack.NewListener(s.serverListener) - - for testId, testCase := range []struct { - metricName string - existingLabels []string - }{ - {"net_conntrack_listener_conn_accepted_total", []string{"default"}}, - {"net_conntrack_listener_conn_closed_total", []string{"default"}}, - {"net_conntrack_listener_conn_open", []string{"default"}}, - {"net_conntrack_listener_conn_accepted_total", []string{listenerName}}, - {"net_conntrack_listener_conn_closed_total", []string{listenerName}}, - {"net_conntrack_listener_conn_open", []string{listenerName}}, - } { - lineCount := len(fetchPrometheusLines(s.T(), testCase.metricName, testCase.existingLabels...)) - assert.NotEqual(s.T(), 0, lineCount, "metrics must exist for test case %d", testId) - } -} - -func (s *ListenerTestSuite) TestMonitoringNormalConns() { - - beforeAccepted := sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_accepted_total", listenerName) - beforeClosed := sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_closed_total", listenerName) - beforeOpen := sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_open", listenerName) - - conn, err := (&net.Dialer{}).DialContext(context.TODO(), "tcp", s.serverListener.Addr().String()) - require.NoError(s.T(), err, "DialContext should successfully establish a conn here") - assert.Equal(s.T(), beforeAccepted+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_accepted_total", listenerName), - "the accepted conn counter must be incremented after connection was opened") - assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_closed_total", listenerName), - "the closed conn counter must not be incremented before the connection is closed") - assert.Equal(s.T(), beforeOpen+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_open", listenerName), - "the open conn must be incremented when the connection is opened") - conn.Close() - assert.Equal(s.T(), beforeClosed+1, sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_closed_total", listenerName), - "the closed conn counter must be incremented after connection was closed") - assert.Equal(s.T(), beforeOpen, sumCountersForMetricAndLabels(s.T(), "net_conntrack_listener_conn_open", listenerName), - "the open conn must be decremented when the connection is closed") -} - -func (s *ListenerTestSuite) TestTracingNormalComms() { - conn, err := (&net.Dialer{}).DialContext(context.TODO(), "tcp", s.serverListener.Addr().String()) - require.NoError(s.T(), err, "DialContext should successfully establish a conn here") - time.Sleep(5 * time.Millisecond) - assert.Contains(s.T(), fetchTraceEvents(s.T(), "net.ServerConn."+listenerName), conn.LocalAddr().String(), - "the /debug/trace/events page must contain the live connection") - time.Sleep(5 * time.Millisecond) - conn.Close() -} - -func (s *ListenerTestSuite) TearDownSuite() { - if s.serverListener != nil { - s.T().Logf("stopped http.Server at: %v", s.serverListener.Addr().String()) - s.serverListener.Close() - } -} diff --git a/listener_wrapper.go b/listener_wrapper.go index 7006fb1..9486361 100644 --- a/listener_wrapper.go +++ b/listener_wrapper.go @@ -1,89 +1,76 @@ -// Copyright 2016 Michal Witkowski. All Rights Reserved. -// See LICENSE for licensing terms. +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. package conntrack import ( + "context" "fmt" "net" - "sync" "time" "github.com/jpillora/backoff" - "golang.org/x/net/trace" ) const ( defaultName = "default" ) -type listenerOpts struct { +type ListenerOptions struct { name string - monitoring bool - tracing bool + tracker ConnectionTracker tcpKeepAlive time.Duration retryBackoff *backoff.Backoff } -type listenerOpt func(*listenerOpts) +type ListenerOption func(*ListenerOptions) -// TrackWithName sets the name of the Listener for use in tracking and monitoring. -func TrackWithName(name string) listenerOpt { - return func(opts *listenerOpts) { +// ListenerWithName sets the name of the Listener for use in tracking and monitoring. +func ListenerWithName(name string) ListenerOption { + return func(opts *ListenerOptions) { opts.name = name } } -// TrackWithoutMonitoring turns *off* Prometheus monitoring for this listener. -func TrackWithoutMonitoring() listenerOpt { - return func(opts *listenerOpts) { - opts.monitoring = false +// ListenerWithTracker register [ConnectionTracker] trackers that tracks the listener interactions. +func ListenerWithTrackers(trackers ...ConnectionTracker) ListenerOption { + return func(opts *ListenerOptions) { + opts.tracker = ChainConnectionTrackers(trackers...) } } -// TrackWithTracing turns *on* the /debug/events tracing of the live listener connections. -func TrackWithTracing() listenerOpt { - return func(opts *listenerOpts) { - opts.tracing = true - } -} - -// TrackWithRetries enables retrying of temporary Accept() errors, with the given backoff between attempts. +// ListenerWithRetries enables retrying of temporary Accept() errors, with the given backoff between attempts. // Concurrent accept calls that receive temporary errors have independent backoff scaling. -func TrackWithRetries(b backoff.Backoff) listenerOpt { - return func(opts *listenerOpts) { +func ListenerWithRetries(b backoff.Backoff) ListenerOption { + return func(opts *ListenerOptions) { opts.retryBackoff = &b } } -// TrackWithTcpKeepAlive makes sure that any `net.TCPConn` that get accepted have a keep-alive. +// ListenerWithTCPKeepAlive makes sure that any `net.TCPConn` that get accepted have a keep-alive. // This is useful for HTTP servers in order for, for example laptops, to not use up resources on the // server while they don't utilise their connection. // A value of 0 disables it. -func TrackWithTcpKeepAlive(keepalive time.Duration) listenerOpt { - return func(opts *listenerOpts) { +func ListenerWithTCPKeepAlive(keepalive time.Duration) ListenerOption { + return func(opts *ListenerOptions) { opts.tcpKeepAlive = keepalive } } type connTrackListener struct { net.Listener - opts *listenerOpts + opts *ListenerOptions } // NewListener returns the given listener wrapped in connection tracking listener. -func NewListener(inner net.Listener, optFuncs ...listenerOpt) net.Listener { - opts := &listenerOpts{ - name: defaultName, - monitoring: true, - tracing: false, +func NewListener(inner net.Listener, optFuncs ...ListenerOption) net.Listener { + opts := &ListenerOptions{ + name: defaultName, } for _, f := range optFuncs { f(opts) } - if opts.monitoring { - preRegisterListenerMetrics(opts.name) - } + return &connTrackListener{ Listener: inner, opts: opts, @@ -91,24 +78,68 @@ func NewListener(inner net.Listener, optFuncs ...listenerOpt) net.Listener { } func (ct *connTrackListener) Accept() (net.Conn, error) { - // TODO(mwitkow): Add monitoring of failed accept. + ctx := WithListenerName(context.Background(), ct.opts.name) + if t, ok := ct.opts.tracker.(ListenerConnectionTagger); ok { + ctx = t.TagListenerConnection(ctx, &ListenerConnectionTagInfo{ + ListenerName: ct.opts.name, + }) + } + + acceptStart := time.Now() var ( conn net.Conn err error ) - for attempt := 0; ; attempt++ { + + attempt := 0 + + for attempt = 0; ; attempt++ { + attempBegin := time.Now() + ct.opts.tracker.TrackConnection(ctx, &ConnectionAttempt{ + Client: false, + Attempt: attempt + 1, + BeginTime: attempBegin, + }) conn, err = ct.Listener.Accept() if err == nil || ct.opts.retryBackoff == nil { break } + if t, ok := err.(interface{ Temporary() bool }); !ok || !t.Temporary() { break } + ct.opts.tracker.TrackConnection(ctx, &ConnectionAttemptFailed{ + Client: false, + Attempt: attempt + 1, + BeginTime: acceptStart, + EndTime: time.Now(), + Error: err, + Reason: FailureReasonFromError(err), + }) time.Sleep(ct.opts.retryBackoff.ForAttempt(float64(attempt))) } if err != nil { + ct.opts.tracker.TrackConnection(ctx, &ConnectionFailed{ + Client: false, + Attempts: attempt + 1, + BeginTime: acceptStart, + EndTime: time.Now(), + Error: err, + Reason: FailureReasonFromError(err), + }) return nil, err } + + establishedTime := time.Now() + ct.opts.tracker.TrackConnection(ctx, &ConnectionEstablished{ + Client: false, + Attempts: attempt + 1, + BeginTime: acceptStart, + EndTime: establishedTime, + LocalAddr: conn.LocalAddr(), + RemoteAddr: conn.RemoteAddr(), + }) + if tcpConn, ok := conn.(*net.TCPConn); ok && ct.opts.tcpKeepAlive > 0 { if err := tcpConn.SetKeepAlive(true); err != nil { return nil, fmt.Errorf("failed to enable keep alive: %w", err) @@ -118,46 +149,22 @@ func (ct *connTrackListener) Accept() (net.Conn, error) { return nil, fmt.Errorf("failed to set keep alive period: %w", err) } } - return newServerConnTracker(conn, ct.opts), nil -} -type serverConnTracker struct { - net.Conn - opts *listenerOpts - event trace.EventLog - mu sync.Mutex + return newConnectionCloseTracker(ctx, conn, false, ct.opts.tracker, establishedTime), nil } -func newServerConnTracker(inner net.Conn, opts *listenerOpts) net.Conn { - tracker := &serverConnTracker{ - Conn: inner, - opts: opts, - } - if opts.tracing { - tracker.event = trace.NewEventLog(fmt.Sprintf("net.ServerConn.%s", opts.name), fmt.Sprintf("%v", inner.RemoteAddr())) - tracker.event.Printf("accepted: %v -> %v", inner.RemoteAddr(), inner.LocalAddr()) - } - if opts.monitoring { - reportListenerConnAccepted(opts.name) +type listenerNameKey struct{} + +// ListenerNameFromContext returns the name of the listener from the context. +func ListenerNameFromContext(ctx context.Context) string { + val, ok := ctx.Value(listenerNameKey{}).(string) + if !ok { + return "" } - return tracker + return val } -func (ct *serverConnTracker) Close() error { - err := ct.Conn.Close() - ct.mu.Lock() - if ct.event != nil { - if err != nil { - ct.event.Errorf("failed closing: %v", err) - } else { - ct.event.Printf("closing") - } - ct.event.Finish() - ct.event = nil - } - ct.mu.Unlock() - if ct.opts.monitoring { - reportListenerConnClosed(ct.opts.name) - } - return err +// WithListenerName returns a context that will contain a listener name. +func WithListenerName(ctx context.Context, listenerName string) context.Context { + return context.WithValue(ctx, listenerNameKey{}, listenerName) } diff --git a/listener_wrapper_test.go b/listener_wrapper_test.go new file mode 100644 index 0000000..e22358d --- /dev/null +++ b/listener_wrapper_test.go @@ -0,0 +1,118 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package conntrack_test + +import ( + "context" + "net" + "net/http" + "testing" + "time" + + "github.com/marefr/go-conntrack/v2" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const listenerName = "some_name" + +func TestListenerUnderNormalConnection(t *testing.T) { + listener, tracker := setupServerListener(t) + + beginTime := time.Now() + conn, err := (&net.Dialer{}).DialContext(context.Background(), "tcp", listener.Addr().String()) + require.NoError(t, err, "DialContext should successfully establish a conn here") + + time.Sleep(time.Millisecond) + + stats := tracker.GetStats() + + require.Len(t, stats, 3) + sAttempt := stats[0].(*conntrack.ConnectionAttempt) + assert.NotNil(t, sAttempt) + assert.False(t, sAttempt.Client) + assert.Equal(t, 1, sAttempt.Attempt) + assert.NotZero(t, sAttempt.BeginTime) + + sEstablished := stats[1].(*conntrack.ConnectionEstablished) + assert.NotNil(t, sEstablished) + assert.False(t, sEstablished.Client) + assert.Equal(t, 1, sEstablished.Attempts) + assert.NotZero(t, sEstablished.BeginTime) + assert.Greater(t, sEstablished.EndTime, sEstablished.BeginTime) + assert.Equal(t, listener.Addr().String(), sEstablished.LocalAddr.String()) + assert.NotEmpty(t, sEstablished.LocalAddr.String()) + + sAttemptTwo := stats[2].(*conntrack.ConnectionAttempt) + assert.NotNil(t, sAttemptTwo) + assert.False(t, sAttemptTwo.Client) + assert.Equal(t, 1, sAttemptTwo.Attempt) + assert.Greater(t, sAttemptTwo.BeginTime, beginTime) + + err = conn.Close() + assert.NoError(t, err) + + time.Sleep(time.Millisecond) + + stats = tracker.GetStats() + + require.Len(t, stats, 4) + sClosed := stats[3].(*conntrack.ConnectionClosed) + assert.NotNil(t, sClosed) + assert.False(t, sClosed.Client) + assert.NoError(t, sClosed.Error) + assert.Greater(t, sClosed.BeginTime, beginTime) + assert.Greater(t, sClosed.EndTime, sEstablished.BeginTime) + + t.Logf("stopping http.Server at: %v", listener.Addr().String()) + err = listener.Close() + require.NoError(t, err, "should be able to close serverListener") +} + +func TestListenerWithContextName(t *testing.T) { + listener, tracker := setupServerListener(t) + + _, err := (&net.Dialer{}).DialContext(context.Background(), "tcp", listener.Addr().String()) + require.NoError(t, err, "DialContext should successfully establish a connection here") + + time.Sleep(time.Millisecond) + + tagCtx, tagInfo := tracker.GetListenerTagInfo() + require.NotNil(t, tagInfo) + require.Equal(t, listenerName, tagInfo.ListenerName) + + require.NotNil(t, tagCtx) + + listenerNameFromCtx := conntrack.ListenerNameFromContext(tagCtx) + require.Equal(t, listenerName, listenerNameFromCtx) + + t.Logf("stopping http.Server at: %v", listener.Addr().String()) + err = listener.Close() + require.NoError(t, err, "should be able to close serverListener") +} + +func setupServerListener(t *testing.T) (net.Listener, *testConnectionTracker) { + t.Helper() + + serverListener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err, "must be able to allocate a port for serverListener") + + tracker := newTestConnectionTracker() + serverListener = conntrack.NewListener(serverListener, + conntrack.ListenerWithName(listenerName), + conntrack.ListenerWithTrackers(tracker), + ) + httpServer := http.Server{ + Handler: http.HandlerFunc(func(resp http.ResponseWriter, _ *http.Request) { + resp.WriteHeader(http.StatusOK) + }), + } + + go func() { + err = httpServer.Serve(serverListener) + require.NotErrorIs(t, err, http.ErrServerClosed, "http.Server should only be closed normally") + }() + + return serverListener, tracker +} diff --git a/logging/doc.go b/logging/doc.go new file mode 100644 index 0000000..7c4eefa --- /dev/null +++ b/logging/doc.go @@ -0,0 +1,6 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +// Package logging provides logging of outbound and inbound connection interactions +// using a logger [Logger]. +package logging diff --git a/logging/logging.go b/logging/logging.go new file mode 100644 index 0000000..5aa4576 --- /dev/null +++ b/logging/logging.go @@ -0,0 +1,216 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package logging + +import ( + "context" + "errors" + "log/slog" + + "github.com/marefr/go-conntrack/v2" +) + +// Logger interface for logging messages. +type Logger interface { + Log(ctx context.Context, level slog.Level, msg string, fields ...slog.Attr) +} + +// LoggerFunc is an adapter to allow the use of +// ordinary functions as [Logger]. If f is a function +// with the appropriate signature, LoggerFunc(f) is a +// [Logger] that calls f. +type LoggerFunc func(ctx context.Context, level slog.Level, msg string, fields ...slog.Attr) + +// Log calls fn(ctx, level, msg, attrs...). +func (fn LoggerFunc) Log(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) { + fn(ctx, level, msg, attrs...) +} + +// LoggerOption defines a config option you can set on the logging tracker. +type LoggerOption func(*connectionTrackerLogger) + +// WithLogger sets the logger used for logging connection tracker interactions. +func WithLogger(logger Logger) LoggerOption { + return func(ctl *connectionTrackerLogger) { + ctl.logger = logger + } +} + +// WithAttemptLogLevel sets the logging level used for logging connection attempts. +func WithAttemptLogLevel(lvl slog.Level) LoggerOption { + return func(ctl *connectionTrackerLogger) { + ctl.attemptLevel = lvl + } +} + +var _ conntrack.DialerConnectionTagger = &connectionTrackerLogger{} +var _ conntrack.ListenerConnectionTagger = &connectionTrackerLogger{} + +type connectionTrackerLogger struct { + logger Logger + attemptLevel slog.Level +} + +// New returns a new [conntrack.ConnectionTracker] that logs outbound and inbound connection interactions +// using the provided [Logger] and optional [LoggerOption]. +func New(logger Logger, options ...LoggerOption) (conntrack.ConnectionTracker, error) { + if logger == nil { + return nil, errors.New("logger cannot be nil") + } + + ctl := &connectionTrackerLogger{ + logger: logger, + attemptLevel: slog.LevelDebug, + } + + for _, opt := range options { + opt(ctl) + } + + return ctl, nil +} + +func (connectionTrackerLogger) TagDialerConnection(ctx context.Context, info *conntrack.DialerConnectionTagInfo) context.Context { + return withLogAttributes(ctx, &logAttributes{ + attrs: []slog.Attr{ + slog.String("component", "net.ClientConn"), + slog.String("dialerName", info.DialerName), + slog.String("addr", info.Addr), + }, + }) +} + +func (ct connectionTrackerLogger) TagListenerConnection(ctx context.Context, info *conntrack.ListenerConnectionTagInfo) context.Context { + return withLogAttributes(ctx, &logAttributes{ + attrs: []slog.Attr{ + slog.String("component", "net.ServerConn"), + slog.String("listenerName", info.ListenerName), + }, + }) +} + +func (ct connectionTrackerLogger) TrackConnection(ctx context.Context, stats conntrack.ConnectionStats) { + logAttrs := logAttributesFromContext(ctx) + if logAttrs == nil { + return + } + + if stats.IsClient() { + ct.handleDialerConnection(ctx, stats, logAttrs) + return + } + + ct.handleListenerConnection(ctx, stats, logAttrs) +} + +func (ct connectionTrackerLogger) handleDialerConnection(ctx context.Context, stats conntrack.ConnectionStats, logAttrs *logAttributes) { + attrs := logAttrs.attrs + + switch s := stats.(type) { + case *conntrack.ConnectionAttempt: + attrs = append(attrs, slog.Int("attempt", s.Attempt)) + ct.logger.Log(ctx, ct.attemptLevel, "Dial attempt", attrs...) + + case *conntrack.ConnectionAttemptFailed: + attrs = append(attrs, + slog.Int("attempt", s.Attempt), + slog.String("reason", string(s.Reason)), + slog.String("error", s.Error.Error()), + slog.Duration("took", s.EndTime.Sub(s.BeginTime)), + ) + ct.logger.Log(ctx, slog.LevelError, "Dial attempt failed", attrs...) + + case *conntrack.ConnectionFailed: + attrs = append(attrs, + slog.Int("attempts", s.Attempts), + slog.String("reason", string(s.Reason)), + slog.String("error", s.Error.Error()), + slog.Duration("took", s.EndTime.Sub(s.BeginTime))) + ct.logger.Log(ctx, slog.LevelError, "Dial failed", attrs...) + + case *conntrack.ConnectionEstablished: + attrs = append(attrs, + slog.String("remoteAddr", s.RemoteAddr.String()), + slog.String("localAddr", s.LocalAddr.String()), + slog.Duration("took", s.EndTime.Sub(s.BeginTime)), + ) + ct.logger.Log(ctx, slog.LevelInfo, "Connection established", attrs...) + + case *conntrack.ConnectionClosed: + attrs = append(attrs, + slog.Duration("connectionTime", s.EndTime.Sub(s.BeginTime)), + ) + if s.Error != nil { + attrs = append(attrs, slog.String("error", s.Error.Error())) + ct.logger.Log(ctx, slog.LevelError, "Failed to close connection", attrs...) + } else { + ct.logger.Log(ctx, slog.LevelInfo, "Connection closed", attrs...) + } + } +} + +func (ct connectionTrackerLogger) handleListenerConnection(ctx context.Context, stats conntrack.ConnectionStats, logAttrs *logAttributes) { + attrs := logAttrs.attrs + + switch s := stats.(type) { + case *conntrack.ConnectionAttempt: + attrs = append(attrs, slog.Int("attempt", s.Attempt)) + ct.logger.Log(ctx, ct.attemptLevel, "Accept attempt", attrs...) + + case *conntrack.ConnectionAttemptFailed: + attrs = append(attrs, + slog.Int("attempt", s.Attempt), + slog.String("reason", string(s.Reason)), + slog.String("error", s.Error.Error()), + slog.Duration("took", s.EndTime.Sub(s.BeginTime)), + ) + ct.logger.Log(ctx, slog.LevelError, "Accept attempt failed", attrs...) + + case *conntrack.ConnectionFailed: + attrs = append(attrs, + slog.Int("attempts", s.Attempts), + slog.String("reason", string(s.Reason)), + slog.String("error", s.Error.Error()), + slog.Duration("took", s.EndTime.Sub(s.BeginTime)), + ) + ct.logger.Log(ctx, slog.LevelError, "Accept failed", attrs...) + + case *conntrack.ConnectionEstablished: + attrs = append(attrs, + slog.String("remoteAddr", s.RemoteAddr.String()), + slog.String("localAddr", s.LocalAddr.String()), + slog.Duration("took", s.EndTime.Sub(s.BeginTime)), + ) + ct.logger.Log(ctx, slog.LevelInfo, "Connection accepted", attrs...) + + case *conntrack.ConnectionClosed: + attrs = append(attrs, + slog.Duration("connectionTime", s.EndTime.Sub(s.BeginTime)), + ) + if s.Error != nil { + attrs = append(attrs, slog.String("error", s.Error.Error())) + ct.logger.Log(ctx, slog.LevelError, "Failed to close connection", attrs...) + } else { + ct.logger.Log(ctx, slog.LevelInfo, "Connection closed", attrs...) + } + } +} + +type logAttributes struct { + attrs []slog.Attr +} + +type logAttrsCtxKey struct{} + +func withLogAttributes(ctx context.Context, logAttrs *logAttributes) context.Context { + return context.WithValue(ctx, logAttrsCtxKey{}, logAttrs) +} + +func logAttributesFromContext(ctx context.Context) *logAttributes { + if val := ctx.Value(logAttrsCtxKey{}); val != nil { + return val.(*logAttributes) + } + + return nil +} diff --git a/logging/slogadapter/logger.go b/logging/slogadapter/logger.go new file mode 100644 index 0000000..af06ee3 --- /dev/null +++ b/logging/slogadapter/logger.go @@ -0,0 +1,19 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +// Package slogadapter provides a logging adapter for [logging.Logger]. +package slogadapter + +import ( + "log/slog" + + "github.com/marefr/go-conntrack/v2/logging" +) + +func Logger(logger *slog.Logger) logging.LoggerFunc { + if logger == nil { + return nil + } + + return logger.LogAttrs +} diff --git a/promhelper_test.go b/promhelper_test.go deleted file mode 100644 index 1209b65..0000000 --- a/promhelper_test.go +++ /dev/null @@ -1,56 +0,0 @@ -package conntrack_test - -import ( - "bufio" - "io" - "net/http" - "net/http/httptest" - "strconv" - "strings" - "testing" - - "github.com/prometheus/client_golang/prometheus/promhttp" - "github.com/stretchr/testify/require" -) - -func fetchPrometheusLines(t *testing.T, metricName string, matchingLabelValues ...string) []string { - resp := httptest.NewRecorder() - req, err := http.NewRequest("GET", "/", nil) - require.NoError(t, err, "failed creating request for Prometheus handler") - promhttp.Handler().ServeHTTP(resp, req) - reader := bufio.NewReader(resp.Body) - ret := []string{} - for { - line, err := reader.ReadString('\n') - if err == io.EOF { - break - } else { - require.NoError(t, err, "error reading stuff") - } - if !strings.HasPrefix(line, metricName) { - continue - } - matches := true - for _, labelValue := range matchingLabelValues { - if !strings.Contains(line, `"`+labelValue+`"`) { - matches = false - } - } - if matches { - ret = append(ret, line) - } - - } - return ret -} - -func sumCountersForMetricAndLabels(t *testing.T, metricName string, matchingLabelValues ...string) int { - count := 0 - for _, line := range fetchPrometheusLines(t, metricName, matchingLabelValues...) { - valueString := line[strings.LastIndex(line, " ")+1 : len(line)-1] - valueFloat, err := strconv.ParseFloat(valueString, 32) - require.NoError(t, err, "failed parsing value for line: %v", line) - count += int(valueFloat) - } - return count -} diff --git a/providers/prometheus/Makefile b/providers/prometheus/Makefile new file mode 100644 index 0000000..35a87c4 --- /dev/null +++ b/providers/prometheus/Makefile @@ -0,0 +1,21 @@ +replace-add: + go mod edit -replace github.com/marefr/go-conntrack/v2=../../ + go mod tidy + +replace-drop: + go mod edit -dropreplace=github.com/marefr/go-conntrack/v2 + go mod tidy + +deps: + go mod tidy + +lint: + golangci-lint run --config=../../.golangci.yml + +test: + go test ./... + +test-race: + go test -race ./... + +.PHONY: replace-add replace-drop deps lint test test-race diff --git a/providers/prometheus/dialer_metrics.go b/providers/prometheus/dialer_metrics.go new file mode 100644 index 0000000..7f3cb18 --- /dev/null +++ b/providers/prometheus/dialer_metrics.go @@ -0,0 +1,152 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package prometheus + +import ( + "time" + + "github.com/marefr/go-conntrack/v2" + "github.com/prometheus/client_golang/prometheus" +) + +var _ prometheus.Collector = &DialerMetrics{} + +// DialerMetrics collects Prometheus metrics of outbound/client connection interactions. +type DialerMetrics struct { + dialerAttemptedTotal *prometheus.CounterVec + dialerConnEstablishedTotal *prometheus.CounterVec + dialerConnFailedTotal *prometheus.CounterVec + dialerConnClosedTotal *prometheus.CounterVec + dialerConnOpen *prometheus.GaugeVec + dialerLifetimeHistogram *prometheus.HistogramVec +} + +// NewDialerMetrics returns a new [DialerMetrics] that can be used to report Prometheus +// metrics of outbound/client connection interactions. +// +// NOTE: [DialerMetrics] implements [prometheus.Collector] which means that you have to +// register the collector in a Prometheus registry in order for the metrics to be collected, +// e.g. prometheus.MustRegister(myDialerMetrics). +func NewDialerMetrics(opts ...ClientMetricsOption) *DialerMetrics { + var config dialerMetricsConfig + config.apply(opts) + + return &DialerMetrics{ + dialerAttemptedTotal: prometheus.NewCounterVec( + config.options.applyCounter(Options{ + Namespace: "net", + Subsystem: "conntrack", + Name: "dialer_conn_attempted_total", + Help: "Total number of connections attempted by the given dialer of a given name.", + }), []string{"dialer_name"}), + dialerConnEstablishedTotal: prometheus.NewCounterVec( + config.options.applyCounter(Options{ + Namespace: "net", + Subsystem: "conntrack", + Name: "dialer_conn_established_total", + Help: "Total number of connections successfully established by the given dialer of a given name.", + }), []string{"dialer_name"}), + dialerConnFailedTotal: prometheus.NewCounterVec( + config.options.applyCounter(Options{ + Namespace: "net", + Subsystem: "conntrack", + Name: "dialer_conn_failed_total", + Help: "Total number of connections failed to dial by the dialer of a given name.", + }), []string{"dialer_name", "reason"}), + dialerConnClosedTotal: prometheus.NewCounterVec( + config.options.applyCounter(Options{ + Namespace: "net", + Subsystem: "conntrack", + Name: "dialer_conn_closed_total", + Help: "Total number of connections closed which originated from the dialer of a given name.", + }), []string{"dialer_name"}), + dialerConnOpen: prometheus.NewGaugeVec( + config.options.applyGauge(Options{ + Namespace: "net", + Subsystem: "conntrack", + Name: "dialer_conn_open", + Help: "Number of open connections which originated from the dialer of a given name.", + }), []string{"dialer_name"}), + dialerLifetimeHistogram: config.dialerLifetimeHistogram, + } +} + +// Describe sends the super-set of all possible descriptors of metrics collected by this +// Collector to the provided channel and returns once the last descriptor has been sent. +func (m *DialerMetrics) Describe(ch chan<- *prometheus.Desc) { + m.dialerAttemptedTotal.Describe(ch) + m.dialerConnEstablishedTotal.Describe(ch) + m.dialerConnFailedTotal.Describe(ch) + m.dialerConnClosedTotal.Describe(ch) + m.dialerConnOpen.Describe(ch) + + if m.dialerLifetimeHistogram != nil { + m.dialerLifetimeHistogram.Describe(ch) + } +} + +// Collect is called by the Prometheus registry when collecting metrics. The implementation +// sends each collected metric via the provided channel and returns once the last metric has +// been sent. +func (m *DialerMetrics) Collect(ch chan<- prometheus.Metric) { + m.dialerAttemptedTotal.Collect(ch) + m.dialerConnEstablishedTotal.Collect(ch) + m.dialerConnFailedTotal.Collect(ch) + m.dialerConnClosedTotal.Collect(ch) + m.dialerConnOpen.Collect(ch) + + if m.dialerLifetimeHistogram != nil { + m.dialerLifetimeHistogram.Collect(ch) + } +} + +// InitializeMetrics initializes all metrics, with their appropriate null value, +// for all known dialer names. This is useful, to ensure that all metrics exist +// when collecting and querying. +func (m *DialerMetrics) InitializeMetrics(dialerNames ...string) { + for _, name := range dialerNames { + // These are just references (no increments), as just referencing will create the labels but not set values. + _, _ = m.dialerAttemptedTotal.GetMetricWithLabelValues(name) + _, _ = m.dialerConnEstablishedTotal.GetMetricWithLabelValues(name) + + for _, reason := range conntrack.FailureReasons() { + _, _ = m.dialerConnFailedTotal.GetMetricWithLabelValues(name, reason.String()) + } + + _, _ = m.dialerConnClosedTotal.GetMetricWithLabelValues(name) + _, _ = m.dialerConnOpen.GetMetricWithLabelValues(name) + + if m.dialerLifetimeHistogram != nil { + _, _ = m.dialerLifetimeHistogram.GetMetricWithLabelValues(name) + } + } +} + +// TrackDialer returns a [conntrack.ConnectionTracker] that reports metrics for this [DialerMetrics] of outbound/client +// connection interactions. +func (m *DialerMetrics) TrackDialer() conntrack.ConnectionTracker { + return newDialerReporter(m) +} + +func (m *DialerMetrics) reportDialerConnAttempt(dialerName string) { + m.dialerAttemptedTotal.WithLabelValues(dialerName).Inc() +} + +func (m *DialerMetrics) reportDialerConnEstablished(dialerName string) { + m.dialerConnEstablishedTotal.WithLabelValues(dialerName).Inc() + m.dialerConnOpen.WithLabelValues(dialerName).Inc() +} + +func (m *DialerMetrics) reportDialerConnClosed(dialerName string, lifetime time.Duration) { + m.dialerConnClosedTotal.WithLabelValues(dialerName).Inc() + m.dialerConnOpen.WithLabelValues(dialerName).Dec() + + if m.dialerLifetimeHistogram != nil { + m.dialerLifetimeHistogram.WithLabelValues(dialerName).Observe(lifetime.Seconds()) + } +} + +func (m *DialerMetrics) reportDialerConnFailed(dialerName string, failureReason conntrack.FailureReason) { + m.dialerConnFailedTotal.WithLabelValues(dialerName, failureReason.String()).Inc() +} diff --git a/providers/prometheus/dialer_metrics_options.go b/providers/prometheus/dialer_metrics_options.go new file mode 100644 index 0000000..9fc8b16 --- /dev/null +++ b/providers/prometheus/dialer_metrics_options.go @@ -0,0 +1,43 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package prometheus + +import "github.com/prometheus/client_golang/prometheus" + +type dialerMetricsConfig struct { + options options + // dialerLifetimeHistogram can be nil. + dialerLifetimeHistogram *prometheus.HistogramVec +} + +type ClientMetricsOption func(*dialerMetricsConfig) + +func (c *dialerMetricsConfig) apply(opts []ClientMetricsOption) { + for _, o := range opts { + o(c) + } +} + +func WithDialerOptions(opts ...Option) ClientMetricsOption { + return func(o *dialerMetricsConfig) { + o.options = opts + } +} + +// WithDialerConnectionLifetimeHistogram turns on recording of dialer connection lifetime. +// Histogram metrics can be very expensive for Prometheus to retain and query. +func WithDialerConnectionLifetimeHistogram(opts ...HistogramOption) ClientMetricsOption { + return func(o *dialerMetricsConfig) { + o.dialerLifetimeHistogram = prometheus.NewHistogramVec( + histogramOptions(opts).apply(prometheus.HistogramOpts{ + Namespace: "net", + Subsystem: "conntrack", + Name: "dialer_conn_lifetime_seconds", + Help: "Histogram of lifetime (seconds) of a connection from established to closed.", + Buckets: prometheus.DefBuckets, + }), + []string{"dialer_name"}, + ) + } +} diff --git a/providers/prometheus/dialer_metrics_test.go b/providers/prometheus/dialer_metrics_test.go new file mode 100644 index 0000000..8dc1738 --- /dev/null +++ b/providers/prometheus/dialer_metrics_test.go @@ -0,0 +1,254 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package prometheus_test + +import ( + "context" + "net" + "net/http" + "testing" + "time" + + promtracker "github.com/marefr/go-conntrack/providers/prometheus" + "github.com/marefr/go-conntrack/v2" + "github.com/prometheus/client_golang/prometheus" + io_prometheus_client "github.com/prometheus/client_model/go" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +func TestDialerMetrics(t *testing.T) { + suite.Run(t, &DialerMetricsTestSuite{}) +} + +type DialerMetricsTestSuite struct { + suite.Suite + + serverListener net.Listener + httpServer http.Server + metrics *promtracker.DialerMetrics + registry *prometheus.Registry +} + +func (s *DialerMetricsTestSuite) SetupSuite() { + var err error + s.serverListener, err = net.Listen("tcp", "127.0.0.1:0") + require.NoError(s.T(), err, "must be able to allocate a port for serverListener") + s.httpServer = http.Server{ + Handler: http.HandlerFunc(func(resp http.ResponseWriter, _ *http.Request) { + resp.WriteHeader(http.StatusOK) + }), + } + go func() { + _ = s.httpServer.Serve(s.serverListener) + }() + + s.metrics = promtracker.NewDialerMetrics() + s.registry = prometheus.NewRegistry() + s.registry.MustRegister(s.metrics) +} + +func (s *DialerMetricsTestSuite) TestShouldInitializeMetrics() { + s.metrics.InitializeMetrics("default", "foobar", "something_manual") + for testID, testCase := range []struct { + metricName string + existingLabels []*io_prometheus_client.LabelPair + }{ + {"net_conntrack_dialer_conn_attempted_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "default"), + }}, + {"net_conntrack_dialer_conn_attempted_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "foobar"), + }}, + {"net_conntrack_dialer_conn_attempted_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "something_manual"), + }}, + {"net_conntrack_dialer_conn_closed_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "default"), + }}, + {"net_conntrack_dialer_conn_closed_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "foobar"), + }}, + {"net_conntrack_dialer_conn_closed_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "something_manual"), + }}, + {"net_conntrack_dialer_conn_established_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "default"), + }}, + {"net_conntrack_dialer_conn_established_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "foobar"), + }}, + {"net_conntrack_dialer_conn_established_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "something_manual"), + }}, + {"net_conntrack_dialer_conn_failed_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "default"), + makeLabelPair("reason", conntrack.FailureReasonResolution.String()), + }}, + {"net_conntrack_dialer_conn_failed_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "default"), + makeLabelPair("reason", conntrack.FailureReasonConnectionRefused.String()), + }}, + {"net_conntrack_dialer_conn_failed_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "default"), + makeLabelPair("reason", conntrack.FailureReasonTimeout.String()), + }}, + {"net_conntrack_dialer_conn_failed_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "default"), + makeLabelPair("reason", conntrack.FailureReasonUnknown.String()), + }}, + {"net_conntrack_dialer_conn_open", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "default"), + }}, + {"net_conntrack_dialer_conn_open", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "foobar"), + }}, + {"net_conntrack_dialer_conn_open", []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "something_manual"), + }}, + } { + m := matchMetricWithLabels(s.T(), s.registry, testCase.metricName, testCase.existingLabels...) + assert.NotNil(s.T(), m, "metrics must exist for test case %d", testID) + } +} + +func (s *DialerMetricsTestSuite) TestDialerUnderNormalConnection() { + dialFunc := conntrack.NewDialContextFunc( + conntrack.DialWithName("normal_conn"), + conntrack.DialWithTrackers(s.metrics.TrackDialer()), + ) + s.metrics.InitializeMetrics("normal_conn") + + labels := []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "normal_conn"), + } + beforeAttempts := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_attempted_total", labels...) + beforeEstablished := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_established_total", labels...) + beforeClosed := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_closed_total", labels...) + beforeOpen := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_open", labels...) + + conn, err := dialFunc(context.TODO(), "tcp", s.serverListener.Addr().String()) + require.NoError(s.T(), err, "NewDialContextFunc should successfully establish a conn here") + + time.Sleep(time.Millisecond) + + assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_attempted_total", labels...), + "the attempted conn counter must be incremented after connection was opened") + assert.Equal(s.T(), beforeEstablished+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_established_total", labels...), + "the established conn counter must be incremented after connection was opened") + assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_closed_total", labels...), + "the closed conn counter must not be incremented after connection was opened") + assert.Equal(s.T(), beforeOpen+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_open", labels...), + "the open conn gauge must be incremented after connection was opened") + conn.Close() + assert.NoError(s.T(), err) + + time.Sleep(time.Millisecond) + + assert.Equal(s.T(), beforeClosed+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_established_total", labels...), + "the closed conn counter must be incremented after connection was closed") + assert.Equal(s.T(), beforeOpen, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_open", labels...), + "the open conn gauge must be decremented after connection was closed") +} + +func (s *DialerMetricsTestSuite) TestDialerWithContextName() { + dialFunc := conntrack.NewDialContextFunc( + conntrack.DialWithTrackers(s.metrics.TrackDialer()), + ) + s.metrics.InitializeMetrics("ctx_conn") + + labels := []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "ctx_conn"), + } + beforeAttempts := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_attempted_total", labels...) + beforeEstablished := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_established_total", labels...) + beforeClosed := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_closed_total", labels...) + + conn, err := dialFunc(conntrack.WithDialName(context.TODO(), "ctx_conn"), "tcp", s.serverListener.Addr().String()) + require.NoError(s.T(), err, "NewDialContextFunc should successfully establish a conn here") + + time.Sleep(time.Millisecond) + + assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_attempted_total", labels...), + "the attempted conn counter must be incremented after connection was opened") + assert.Equal(s.T(), beforeEstablished+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_established_total", labels...), + "the established conn counter must be incremented after connection was opened") + assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_closed_total", labels...), + "the closed conn counter must not be incremented after connection was opened") + err = conn.Close() + assert.NoError(s.T(), err) + + time.Sleep(time.Millisecond) + + assert.Equal(s.T(), beforeClosed+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_established_total", labels...), + "the closed conn counter must be incremented after connection was closed") +} + +func (s *DialerMetricsTestSuite) TestDialerResolutionFailure() { + dialFunc := conntrack.NewDialContextFunc( + conntrack.DialWithName("res_err"), + conntrack.DialWithTrackers(s.metrics.TrackDialer()), + ) + s.metrics.InitializeMetrics("res_err") + + labels := []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "res_err"), + } + failedLabels := append(labels, makeLabelPair("reason", conntrack.FailureReasonResolution.String())) + + beforeAttempts := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_attempted_total", labels...) + beforeEstablished := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_established_total", labels...) + beforeResolutionErrors := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_failed_total", failedLabels...) + beforeClosed := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_closed_total", labels...) + + _, err := dialFunc(context.TODO(), "tcp", "dialer.test.wrong.domain.wrong:443") + require.Error(s.T(), err, "NewDialContextFunc should fail here") + + time.Sleep(time.Millisecond) + + assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_attempted_total", labels...), + "the attempted conn counter must be incremented after connection was opened") + assert.Equal(s.T(), beforeEstablished, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_established_total", labels...), + "the established conn counter must not be incremented on a failure") + assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_closed_total", labels...), + "the closed conn counter must not be incremented on a failure") + assert.Equal(s.T(), beforeResolutionErrors+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_failed_total", failedLabels...), + "the failure counter for resolution error should be incremented") +} + +func (s *DialerMetricsTestSuite) TestDialerRefusedFailure() { + dialFunc := conntrack.NewDialContextFunc( + conntrack.DialWithName("ref_err"), + conntrack.DialWithTrackers(s.metrics.TrackDialer()), + ) + + labels := []*io_prometheus_client.LabelPair{ + makeLabelPair("dialer_name", "ref_err"), + } + failedLabels := append(labels, makeLabelPair("reason", conntrack.FailureReasonConnectionRefused.String())) + + beforeAttempts := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_attempted_total", labels...) + beforeEstablished := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_established_total", labels...) + beforeResolutionErrors := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_failed_total", failedLabels...) + beforeClosed := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_closed_total", labels...) + + _, err := dialFunc(context.TODO(), "tcp", "127.0.0.1:337") // 337 is a cool port, let's hope its unused. + require.Error(s.T(), err, "NewDialContextFunc should fail here") + assert.Equal(s.T(), beforeAttempts+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_attempted_total", labels...), + "the attempted conn counter must be incremented after connection was opened") + assert.Equal(s.T(), beforeEstablished, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_established_total", labels...), + "the established conn counter must not be incremented on a failure") + assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_closed_total", labels...), + "the closed conn counter must not be incremented on a failure") + assert.Equal(s.T(), beforeResolutionErrors+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_dialer_conn_failed_total", failedLabels...), + "the failure counter for connection refused error should be incremented") +} + +func (s *DialerMetricsTestSuite) TearDownSuite() { + if s.serverListener != nil { + s.T().Logf("stopped http.Server at: %v", s.serverListener.Addr().String()) + s.serverListener.Close() + } +} diff --git a/providers/prometheus/doc.go b/providers/prometheus/doc.go new file mode 100644 index 0000000..4566f4c --- /dev/null +++ b/providers/prometheus/doc.go @@ -0,0 +1,6 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +// Package prometheus provides collection of Prometheus metrics of outbound and inbound connection interactions +// using the github.com/prometheus/client_golang/prometheus package. +package prometheus diff --git a/providers/prometheus/go.mod b/providers/prometheus/go.mod new file mode 100644 index 0000000..71529fe --- /dev/null +++ b/providers/prometheus/go.mod @@ -0,0 +1,28 @@ +module github.com/marefr/go-conntrack/providers/prometheus + +go 1.24.0 + +require ( + github.com/marefr/go-conntrack/v2 v2.0.0 + github.com/prometheus/client_golang v1.23.2 + github.com/prometheus/client_model v0.6.2 + github.com/stretchr/testify v1.11.1 +) + +require ( + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/jpillora/backoff v1.0.0 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/prometheus/common v0.66.1 // indirect + github.com/prometheus/procfs v0.16.1 // indirect + go.yaml.in/yaml/v2 v2.4.2 // indirect + golang.org/x/sys v0.35.0 // indirect + google.golang.org/protobuf v1.36.8 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +replace github.com/marefr/go-conntrack/v2 => ../../ diff --git a/providers/prometheus/go.sum b/providers/prometheus/go.sum new file mode 100644 index 0000000..e00d122 --- /dev/null +++ b/providers/prometheus/go.sum @@ -0,0 +1,44 @@ +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= +github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o= +github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg= +github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= +github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE= +github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs= +github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA= +github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg= +github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI= +go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU= +golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI= +golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc= +google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/providers/prometheus/listener_metrics.go b/providers/prometheus/listener_metrics.go new file mode 100644 index 0000000..54c127c --- /dev/null +++ b/providers/prometheus/listener_metrics.go @@ -0,0 +1,148 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package prometheus + +import ( + "time" + + "github.com/marefr/go-conntrack/v2" + "github.com/prometheus/client_golang/prometheus" +) + +var _ prometheus.Collector = &ListenerMetrics{} + +// ListenerMetrics collects Prometheus metrics of inbound/server connection interactions. +type ListenerMetrics struct { + listenerAttemptedTotal *prometheus.CounterVec + listenerAcceptedTotal *prometheus.CounterVec + listenerConnFailedTotal *prometheus.CounterVec + listenerClosedTotal *prometheus.CounterVec + listenerOpen *prometheus.GaugeVec + listenerLifetimeHistogram *prometheus.HistogramVec +} + +// NewListenerMetrics returns a new [ListenerMetrics] that can be used to report Prometheus +// metrics of inbound/server connection interactions. +// +// NOTE: [ListenerMetrics] implements [prometheus.Collector] which means that you have to +// register the collector in a Prometheus registry in order for the metrics to be collected, +// e.g. prometheus.MustRegister(myListenerMetrics). +func NewListenerMetrics(opts ...ListenerMetricsOption) *ListenerMetrics { + var config listenerMetricsConfig + config.apply(opts) + + return &ListenerMetrics{ + listenerAttemptedTotal: prometheus.NewCounterVec( + config.options.applyCounter(Options{ + Namespace: "net", + Subsystem: "conntrack", + Name: "listener_conn_attempted_total", + Help: "Total number of connections attempted to the given listener of a given name.", + }), []string{"listener_name"}), + listenerAcceptedTotal: prometheus.NewCounterVec( + config.options.applyCounter(Options{ + Namespace: "net", + Subsystem: "conntrack", + Name: "listener_conn_accepted_total", + Help: "Total number of connections opened to the listener of a given name.", + }), []string{"listener_name"}), + listenerConnFailedTotal: prometheus.NewCounterVec( + config.options.applyCounter(Options{ + Namespace: "net", + Subsystem: "conntrack", + Name: "listener_conn_failed_total", + Help: "Total number of connections failed to accept to the listener of a given name.", + }), []string{"listener_name", "reason"}), + listenerClosedTotal: prometheus.NewCounterVec( + config.options.applyCounter(Options{ + Namespace: "net", + Subsystem: "conntrack", + Name: "listener_conn_closed_total", + Help: "Total number of connections closed that were made to the listener of a given name.", + }), []string{"listener_name"}), + listenerOpen: prometheus.NewGaugeVec( + config.options.applyGauge(Options{ + Namespace: "net", + Subsystem: "conntrack", + Name: "listener_conn_open", + Help: "Number of open connections to the listener of a given name.", + }), []string{"listener_name"}), + listenerLifetimeHistogram: config.listenerLifetimeHistogram, + } +} + +// Describe sends the super-set of all possible descriptors of metrics collected by this +// Collector to the provided channel and returns once the last descriptor has been sent. +func (m *ListenerMetrics) Describe(ch chan<- *prometheus.Desc) { + m.listenerAttemptedTotal.Describe(ch) + m.listenerAcceptedTotal.Describe(ch) + m.listenerConnFailedTotal.Describe(ch) + m.listenerClosedTotal.Describe(ch) + m.listenerOpen.Describe(ch) + + if m.listenerLifetimeHistogram != nil { + m.listenerLifetimeHistogram.Describe(ch) + } +} + +// Collect is called by the Prometheus registry when collecting metrics. The implementation +// sends each collected metric via the provided channel and returns once the last metric has +// been sent. +func (m *ListenerMetrics) Collect(ch chan<- prometheus.Metric) { + m.listenerAttemptedTotal.Collect(ch) + m.listenerAcceptedTotal.Collect(ch) + m.listenerConnFailedTotal.Collect(ch) + m.listenerClosedTotal.Collect(ch) + m.listenerOpen.Collect(ch) + + if m.listenerLifetimeHistogram != nil { + m.listenerLifetimeHistogram.Collect(ch) + } +} + +// InitializeMetrics initializes all metrics, with their appropriate null value, +// for all known listener names. This is useful, to ensure that all metrics exist +// when collecting and querying. +func (m *ListenerMetrics) InitializeMetrics(listenerNames ...string) { + for _, name := range listenerNames { + // These are just references (no increments), as just referencing will create the labels but not set values. + _, _ = m.listenerAttemptedTotal.GetMetricWithLabelValues(name) + _, _ = m.listenerAcceptedTotal.GetMetricWithLabelValues(name) + _, _ = m.listenerConnFailedTotal.GetMetricWithLabelValues(name, conntrack.FailureReasonUnknown.String()) + _, _ = m.listenerClosedTotal.GetMetricWithLabelValues(name) + _, _ = m.listenerOpen.GetMetricWithLabelValues(name) + + if m.listenerLifetimeHistogram != nil { + _, _ = m.listenerLifetimeHistogram.GetMetricWithLabelValues(name) + } + } +} + +// TrackListener returns a [conntrack.ConnectionTracker] that reports metrics for this [ListenerMetrics] of inbound/server +// connection interactions. +func (m *ListenerMetrics) TrackListener() conntrack.ConnectionTracker { + return newListenerReporter(m) +} + +func (m *ListenerMetrics) reportListenerConnAttempt(listenerName string) { + m.listenerAttemptedTotal.WithLabelValues(listenerName).Inc() +} + +func (m *ListenerMetrics) reportListenerConnAccepted(listenerName string) { + m.listenerAcceptedTotal.WithLabelValues(listenerName).Inc() + m.listenerOpen.WithLabelValues(listenerName).Inc() +} + +func (m *ListenerMetrics) reportListenerConnFailed(listenerName string, failureReason conntrack.FailureReason) { + m.listenerConnFailedTotal.WithLabelValues(listenerName, failureReason.String()).Inc() +} + +func (m *ListenerMetrics) reportListenerConnClosed(listenerName string, lifetime time.Duration) { + m.listenerClosedTotal.WithLabelValues(listenerName).Inc() + m.listenerOpen.WithLabelValues(listenerName).Dec() + + if m.listenerLifetimeHistogram != nil { + m.listenerLifetimeHistogram.WithLabelValues(listenerName).Observe(lifetime.Seconds()) + } +} diff --git a/providers/prometheus/listener_metrics_options.go b/providers/prometheus/listener_metrics_options.go new file mode 100644 index 0000000..ac38cb0 --- /dev/null +++ b/providers/prometheus/listener_metrics_options.go @@ -0,0 +1,43 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package prometheus + +import "github.com/prometheus/client_golang/prometheus" + +type listenerMetricsConfig struct { + options options + // listenerLifetimeHistogram can be nil. + listenerLifetimeHistogram *prometheus.HistogramVec +} + +type ListenerMetricsOption func(*listenerMetricsConfig) + +func (c *listenerMetricsConfig) apply(opts []ListenerMetricsOption) { + for _, o := range opts { + o(c) + } +} + +func WithListenerOptions(opts ...Option) ListenerMetricsOption { + return func(o *listenerMetricsConfig) { + o.options = opts + } +} + +// WithListenerConnectionLifetimeHistogram turns on recording of listener connection lifetime. +// Histogram metrics can be very expensive for Prometheus to retain and query. +func WithListenerConnectionLifetimeHistogram(opts ...HistogramOption) ListenerMetricsOption { + return func(o *listenerMetricsConfig) { + o.listenerLifetimeHistogram = prometheus.NewHistogramVec( + histogramOptions(opts).apply(prometheus.HistogramOpts{ + Namespace: "net", + Subsystem: "conntrack", + Name: "listener_conn_lifetime_seconds", + Help: "Histogram of lifetime (seconds) of a connection from established to closed.", + Buckets: prometheus.DefBuckets, + }), + []string{"listener_name"}, + ) + } +} diff --git a/providers/prometheus/listener_metrics_test.go b/providers/prometheus/listener_metrics_test.go new file mode 100644 index 0000000..5e2e60d --- /dev/null +++ b/providers/prometheus/listener_metrics_test.go @@ -0,0 +1,138 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package prometheus_test + +import ( + "context" + "net" + "net/http" + "testing" + "time" + + promtracker "github.com/marefr/go-conntrack/providers/prometheus" + "github.com/marefr/go-conntrack/v2" + "github.com/prometheus/client_golang/prometheus" + io_prometheus_client "github.com/prometheus/client_model/go" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +const listenerName = "some_name" + +func TestListenerMetrics(t *testing.T) { + suite.Run(t, &ListenerMetricsTestSuite{}) +} + +type ListenerMetricsTestSuite struct { + suite.Suite + + serverListener net.Listener + httpServer http.Server + metrics *promtracker.ListenerMetrics + registry *prometheus.Registry +} + +func (s *ListenerMetricsTestSuite) SetupSuite() { + s.metrics = promtracker.NewListenerMetrics() + s.registry = prometheus.NewRegistry() + s.registry.MustRegister(s.metrics) + + var err error + s.serverListener, err = net.Listen("tcp", "127.0.0.1:0") + require.NoError(s.T(), err, "must be able to allocate a port for serverListener") + s.serverListener = conntrack.NewListener(s.serverListener, + conntrack.ListenerWithName(listenerName), + conntrack.ListenerWithTrackers(s.metrics.TrackListener()), + ) + s.httpServer = http.Server{ + Handler: http.HandlerFunc(func(resp http.ResponseWriter, _ *http.Request) { + resp.WriteHeader(http.StatusOK) + }), + } + go func() { + _ = s.httpServer.Serve(s.serverListener) + }() +} + +func (s *ListenerMetricsTestSuite) TestShouldInitializeMetrics() { + s.metrics.InitializeMetrics("default", listenerName) + + for testID, testCase := range []struct { + metricName string + existingLabels []*io_prometheus_client.LabelPair + }{ + {"net_conntrack_listener_conn_attempted_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("listener_name", "default"), + }}, + {"net_conntrack_listener_conn_accepted_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("listener_name", "default"), + }}, + {"net_conntrack_listener_conn_failed_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("listener_name", "default"), + makeLabelPair("reason", conntrack.FailureReasonUnknown.String()), + }}, + {"net_conntrack_listener_conn_closed_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("listener_name", "default"), + }}, + {"net_conntrack_listener_conn_open", []*io_prometheus_client.LabelPair{ + makeLabelPair("listener_name", "default"), + }}, + {"net_conntrack_listener_conn_attempted_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("listener_name", listenerName), + }}, + {"net_conntrack_listener_conn_accepted_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("listener_name", listenerName), + }}, + {"net_conntrack_listener_conn_failed_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("listener_name", listenerName), + }}, + {"net_conntrack_listener_conn_closed_total", []*io_prometheus_client.LabelPair{ + makeLabelPair("listener_name", listenerName), + }}, + {"net_conntrack_listener_conn_open", []*io_prometheus_client.LabelPair{ + makeLabelPair("listener_name", listenerName), + }}, + } { + m := matchMetricWithLabels(s.T(), s.registry, testCase.metricName, testCase.existingLabels...) + assert.NotNil(s.T(), m, "metrics must exist for test case %d", testID) + } +} + +func (s *ListenerMetricsTestSuite) TestListenerUnderNormalConnection() { + labels := []*io_prometheus_client.LabelPair{ + makeLabelPair("listener_name", listenerName), + } + beforeAccepted := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_listener_conn_accepted_total", labels...) + beforeClosed := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_listener_conn_closed_total", labels...) + beforeOpen := sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_listener_conn_open", labels...) + + conn, err := (&net.Dialer{}).DialContext(context.TODO(), "tcp", s.serverListener.Addr().String()) + require.NoError(s.T(), err, "DialContext should successfully establish a conn here") + + time.Sleep(time.Millisecond) + + assert.Equal(s.T(), beforeAccepted+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_listener_conn_accepted_total", labels...), + "the accepted conn counter must be incremented after connection was opened") + assert.Equal(s.T(), beforeClosed, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_listener_conn_closed_total", labels...), + "the closed conn counter must not be incremented before the connection is closed") + assert.Equal(s.T(), beforeOpen+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_listener_conn_open", labels...), + "the open conn must be incremented when the connection is opened") + err = conn.Close() + assert.NoError(s.T(), err) + + time.Sleep(time.Millisecond) + + assert.Equal(s.T(), beforeClosed+1, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_listener_conn_closed_total", labels...), + "the closed conn counter must be incremented after connection was closed") + assert.Equal(s.T(), beforeOpen, sumCountersForMetricAndLabels(s.T(), s.registry, "net_conntrack_listener_conn_open", labels...), + "the open conn must be decremented when the connection is closed") +} + +func (s *ListenerMetricsTestSuite) TearDownSuite() { + if s.serverListener != nil { + s.T().Logf("stopped http.Server at: %v", s.serverListener.Addr().String()) + s.serverListener.Close() + } +} diff --git a/providers/prometheus/options.go b/providers/prometheus/options.go new file mode 100644 index 0000000..2e65682 --- /dev/null +++ b/providers/prometheus/options.go @@ -0,0 +1,147 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package prometheus + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +// A Option lets you add options to metrics using With* funcs. +type Option func(*Options) + +type Options struct { + // Namespace, Subsystem, and Name are components of the fully-qualified + // name of the Metric (created by joining these components with + // "_"). Only Name is mandatory, the others merely help structuring the + // name. Note that the fully-qualified name of the metric must be a + // valid Prometheus metric name. + Namespace string + Subsystem string + Name string + + // Help provides information about this metric. + // + // Metrics with the same fully-qualified name must have the same Help + // string. + Help string + + // ConstLabels are used to attach fixed labels to this metric. Metrics + // with the same fully-qualified name must have the same label names in + // their ConstLabels. + // + // ConstLabels are only used rarely. In particular, do not use them to + // attach the same labels to all your metrics. Those use cases are + // better covered by target labels set by the scraping Prometheus + // server, or by one specific metric (e.g. a build_info or a + // machine_role metric). See also + // https://prometheus.io/docs/instrumenting/writing_exporters/#target-labels-not-static-scraped-labels + ConstLabels prometheus.Labels +} + +type options []Option + +func (co options) applyCounter(o Options) prometheus.CounterOpts { + for _, f := range co { + f(&o) + } + + return prometheus.CounterOpts{ + Namespace: o.Namespace, + Subsystem: o.Subsystem, + Name: o.Name, + Help: o.Help, + ConstLabels: o.ConstLabels, + } +} + +func (co options) applyGauge(o Options) prometheus.GaugeOpts { + for _, f := range co { + f(&o) + } + + return prometheus.GaugeOpts{ + Namespace: o.Namespace, + Subsystem: o.Subsystem, + Name: o.Name, + Help: o.Help, + ConstLabels: o.ConstLabels, + } +} + +// WithConstLabels allows you to add ConstLabels to metrics. +func WithConstLabels(labels prometheus.Labels) Option { + return func(o *Options) { + o.ConstLabels = labels + } +} + +// WithSubsystem allows you to add a Subsystem to metrics. +func WithSubsystem(subsystem string) Option { + return func(o *Options) { + o.Subsystem = subsystem + } +} + +// WithNamespace allows you to add a Namespace to metrics. +func WithNamespace(namespace string) Option { + return func(o *Options) { + o.Namespace = namespace + } +} + +// A HistogramOption lets you add options to Histogram metrics using With* +// funcs. +type HistogramOption func(*prometheus.HistogramOpts) + +type histogramOptions []HistogramOption + +func (ho histogramOptions) apply(o prometheus.HistogramOpts) prometheus.HistogramOpts { + for _, f := range ho { + f(&o) + } + return o +} + +// WithHistogramBuckets allows you to specify custom bucket ranges for histograms if EnableHandlingTimeHistogram is on. +func WithHistogramBuckets(buckets []float64) HistogramOption { + return func(o *prometheus.HistogramOpts) { o.Buckets = buckets } +} + +// WithHistogramOpts allows you to specify HistogramOpts but makes sure the correct name and label is used. +// This function is helpful when specifying more than just the buckets, like using NativeHistograms. +func WithHistogramOpts(opts *prometheus.HistogramOpts) HistogramOption { + // TODO: This isn't ideal either if new fields are added to prometheus.HistogramOpts. + // Maybe we can change the interface to accept arbitrary HistogramOpts and + // only make sure to overwrite the necessary fields (name, labels). + return func(o *prometheus.HistogramOpts) { + o.Buckets = opts.Buckets + o.NativeHistogramBucketFactor = opts.NativeHistogramBucketFactor + o.NativeHistogramZeroThreshold = opts.NativeHistogramZeroThreshold + o.NativeHistogramMaxBucketNumber = opts.NativeHistogramMaxBucketNumber + o.NativeHistogramMinResetDuration = opts.NativeHistogramMinResetDuration + o.NativeHistogramMaxZeroThreshold = opts.NativeHistogramMaxZeroThreshold + } +} + +// WithHistogramConstLabels allows you to add custom ConstLabels to +// histograms metrics. +func WithHistogramConstLabels(labels prometheus.Labels) HistogramOption { + return func(o *prometheus.HistogramOpts) { + o.ConstLabels = labels + } +} + +// WithHistogramSubsystem allows you to add a Subsystem to histograms metrics. +func WithHistogramSubsystem(subsystem string) HistogramOption { + return func(o *prometheus.HistogramOpts) { + o.Subsystem = subsystem + } +} + +// WithHistogramNamespace allows you to add a Namespace to histograms metrics. +func WithHistogramNamespace(namespace string) HistogramOption { + return func(o *prometheus.HistogramOpts) { + o.Namespace = namespace + } +} diff --git a/providers/prometheus/promhelper_test.go b/providers/prometheus/promhelper_test.go new file mode 100644 index 0000000..fb069cd --- /dev/null +++ b/providers/prometheus/promhelper_test.go @@ -0,0 +1,64 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package prometheus_test + +import ( + "testing" + + "github.com/prometheus/client_golang/prometheus" + io_prometheus_client "github.com/prometheus/client_model/go" + "github.com/stretchr/testify/require" +) + +func matchMetricWithLabels(t *testing.T, gatherer prometheus.Gatherer, metricName string, matchingLabels ...*io_prometheus_client.LabelPair) *io_prometheus_client.Metric { + metricFamilies, err := gatherer.Gather() + require.NoError(t, err) + + for _, mf := range metricFamilies { + if mf.GetName() != metricName { + continue + } + + for _, m := range mf.GetMetric() { + found := 0 + for _, lPair := range matchingLabels { + for _, l := range m.Label { + if l.GetName() == lPair.GetName() && l.GetValue() == lPair.GetValue() { + found++ + } + } + } + + if found == len(matchingLabels) { + return m + } + } + } + + return nil +} + +func sumCountersForMetricAndLabels(t *testing.T, gatherer prometheus.Gatherer, metricName string, matchingLabels ...*io_prometheus_client.LabelPair) float64 { + m := matchMetricWithLabels(t, gatherer, metricName, matchingLabels...) + if m == nil { + return 0 + } + + if m.Counter != nil { + return m.GetCounter().GetValue() + } + + if m.Gauge != nil { + return m.GetGauge().GetValue() + } + + return 0 +} + +func makeLabelPair(name string, value string) *io_prometheus_client.LabelPair { + return &io_prometheus_client.LabelPair{ + Name: &name, + Value: &value, + } +} diff --git a/providers/prometheus/reporter.go b/providers/prometheus/reporter.go new file mode 100644 index 0000000..467043c --- /dev/null +++ b/providers/prometheus/reporter.go @@ -0,0 +1,66 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package prometheus + +import ( + "context" + + "github.com/marefr/go-conntrack/v2" +) + +type reporter struct { + dialerMetrics *DialerMetrics + listenerMetrics *ListenerMetrics +} + +func newDialerReporter(dialerMetrics *DialerMetrics) *reporter { + return &reporter{ + dialerMetrics: dialerMetrics, + } +} + +func newListenerReporter(listenerMetrics *ListenerMetrics) *reporter { + return &reporter{ + listenerMetrics: listenerMetrics, + } +} + +func (r reporter) TrackConnection(ctx context.Context, stats conntrack.ConnectionStats) { + if stats.IsClient() { + r.trackDialerConnection(ctx, stats) + return + } + + r.trackListenerConnection(ctx, stats) +} + +func (r reporter) trackDialerConnection(ctx context.Context, stats conntrack.ConnectionStats) { + dialerName := conntrack.DialNameFromContext(ctx) + + switch s := stats.(type) { + case *conntrack.ConnectionAttempt: + r.dialerMetrics.reportDialerConnAttempt(dialerName) + case *conntrack.ConnectionFailed: + r.dialerMetrics.reportDialerConnFailed(dialerName, s.Reason) + case *conntrack.ConnectionEstablished: + r.dialerMetrics.reportDialerConnEstablished(dialerName) + case *conntrack.ConnectionClosed: + r.dialerMetrics.reportDialerConnClosed(dialerName, s.EndTime.Sub(s.BeginTime)) + } +} + +func (r reporter) trackListenerConnection(ctx context.Context, stats conntrack.ConnectionStats) { + listenerName := conntrack.ListenerNameFromContext(ctx) + + switch s := stats.(type) { + case *conntrack.ConnectionAttempt: + r.listenerMetrics.reportListenerConnAttempt(listenerName) + case *conntrack.ConnectionFailed: + r.listenerMetrics.reportListenerConnFailed(listenerName, s.Reason) + case *conntrack.ConnectionEstablished: + r.listenerMetrics.reportListenerConnAccepted(listenerName) + case *conntrack.ConnectionClosed: + r.listenerMetrics.reportListenerConnClosed(listenerName, s.EndTime.Sub(s.BeginTime)) + } +} diff --git a/providers/trace/Makefile b/providers/trace/Makefile new file mode 100644 index 0000000..35a87c4 --- /dev/null +++ b/providers/trace/Makefile @@ -0,0 +1,21 @@ +replace-add: + go mod edit -replace github.com/marefr/go-conntrack/v2=../../ + go mod tidy + +replace-drop: + go mod edit -dropreplace=github.com/marefr/go-conntrack/v2 + go mod tidy + +deps: + go mod tidy + +lint: + golangci-lint run --config=../../.golangci.yml + +test: + go test ./... + +test-race: + go test -race ./... + +.PHONY: replace-add replace-drop deps lint test test-race diff --git a/providers/trace/doc.go b/providers/trace/doc.go new file mode 100644 index 0000000..d6d60b1 --- /dev/null +++ b/providers/trace/doc.go @@ -0,0 +1,9 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +// Package trace provides tracing of outbound and inbound connection interactions using the +// golang.org/x/net/trace package. +// +// Note: Importing this package automatically enables tracing and exports HTTP interfaces +// on /debug/requests and /debug/events by automatically register with [http.DefaultServeMux]. +package trace diff --git a/providers/trace/go.mod b/providers/trace/go.mod new file mode 100644 index 0000000..9998479 --- /dev/null +++ b/providers/trace/go.mod @@ -0,0 +1,21 @@ +module github.com/marefr/go-conntrack/providers/trace + +go 1.24.0 + +toolchain go1.24.1 + +require ( + github.com/marefr/go-conntrack/v2 v2.0.9 + github.com/stretchr/testify v1.11.1 + golang.org/x/net v0.48.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/jpillora/backoff v1.0.0 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +replace github.com/marefr/go-conntrack/v2 => ../../ diff --git a/providers/trace/go.sum b/providers/trace/go.sum new file mode 100644 index 0000000..90016ea --- /dev/null +++ b/providers/trace/go.sum @@ -0,0 +1,22 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU= +golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/providers/trace/tracer.go b/providers/trace/tracer.go new file mode 100644 index 0000000..961c03a --- /dev/null +++ b/providers/trace/tracer.go @@ -0,0 +1,117 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package trace + +import ( + "context" + "fmt" + + "github.com/marefr/go-conntrack/v2" + "golang.org/x/net/trace" +) + +var _ conntrack.DialerConnectionTagger = &tracer{} +var _ conntrack.ListenerConnectionTagger = &tracer{} + +type tracer struct{} + +// New returns a new [conntrack.ConnectionTracker] that traces outbound and inbound connection interactions. +// using the golang.org/x/net/trace package. +// +// Note: Importing this package automatically enables tracing and exports HTTP interfaces +// on /debug/requests and /debug/events by automatically register with [http.DefaultServeMux]. +func New() conntrack.ConnectionTracker { + return &tracer{} +} + +func (t tracer) TagDialerConnection(ctx context.Context, info *conntrack.DialerConnectionTagInfo) context.Context { + return withTraceCtx(ctx, &traceCtx{ + name: info.DialerName, + eventLog: trace.NewEventLog(fmt.Sprintf("net.ClientConn.%s", info.DialerName), info.Addr), + }) +} + +func (t tracer) TagListenerConnection(ctx context.Context, info *conntrack.ListenerConnectionTagInfo) context.Context { + return withTraceCtx(ctx, &traceCtx{ + name: info.ListenerName, + }) +} + +func (t tracer) TrackConnection(ctx context.Context, stats conntrack.ConnectionStats) { + traceCtx := traceCtxFromContext(ctx) + if traceCtx == nil { + return + } + + if stats.IsClient() { + trackDialerConnection(stats, traceCtx) + return + } + + trackListenerConnection(stats, traceCtx) +} + +func trackDialerConnection(stats conntrack.ConnectionStats, traceCtx *traceCtx) { + switch s := stats.(type) { + case *conntrack.ConnectionEstablished: + if traceCtx.eventLog == nil { + return + } + + traceCtx.eventLog.Printf("accepted: %s -> %s", s.RemoteAddr, s.LocalAddr) + case *conntrack.ConnectionClosed: + if traceCtx.eventLog == nil { + return + } + + if s.Error != nil { + traceCtx.eventLog.Errorf("failed closing: %v", s.Error) + } else { + traceCtx.eventLog.Printf("closing") + } + + traceCtx.eventLog.Finish() + traceCtx.eventLog = nil + } +} + +func trackListenerConnection(stats conntrack.ConnectionStats, traceCtx *traceCtx) { + switch s := stats.(type) { + case *conntrack.ConnectionEstablished: + traceCtx.eventLog = trace.NewEventLog(fmt.Sprintf("net.ServerConn.%s", traceCtx.name), s.RemoteAddr.String()) + traceCtx.eventLog.Printf("accepted: %s -> %s", s.RemoteAddr, s.LocalAddr) + case *conntrack.ConnectionClosed: + if traceCtx.eventLog == nil { + return + } + + if s.Error != nil { + traceCtx.eventLog.Errorf("failed closing: %v", s.Error) + } else { + traceCtx.eventLog.Printf("closing") + } + + traceCtx.eventLog.Finish() + traceCtx.eventLog = nil + } +} + +type traceCtx struct { + name string + eventLog trace.EventLog +} + +type traceCtxKey struct{} + +func withTraceCtx(ctx context.Context, traceCtx *traceCtx) context.Context { + return context.WithValue(ctx, traceCtxKey{}, traceCtx) +} + +func traceCtxFromContext(ctx context.Context) *traceCtx { + if val := ctx.Value(traceCtxKey{}); val != nil { + return val.(*traceCtx) + } + + return nil +} diff --git a/providers/trace/tracer_test.go b/providers/trace/tracer_test.go new file mode 100644 index 0000000..9e91099 --- /dev/null +++ b/providers/trace/tracer_test.go @@ -0,0 +1,95 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package trace_test + +import ( + "context" + "fmt" + "io" + "net" + "net/http" + "net/http/httptest" + "testing" + "time" + + tracetracker "github.com/marefr/go-conntrack/providers/trace" + "github.com/marefr/go-conntrack/v2" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/net/trace" +) + +const listenerName = "some_name" + +func TestDialerTracingCapturedInPage(t *testing.T) { + listener, tracker := setupServerListener(t) + + dialFunc := conntrack.NewDialContextFunc(conntrack.DialWithTrackers(tracker)) + dialerName := "some_dialer" + conn, err := dialFunc(conntrack.WithDialName(context.Background(), dialerName), "tcp", listener.Addr().String()) + time.Sleep(5 * time.Millisecond) + require.NoError(t, err, "DialContext should successfully establish a conn here") + assert.Contains(t, fetchTraceEvents(t, "net.ClientConn."+dialerName), conn.LocalAddr().String(), + "the /debug/trace/events page must contain the live connection") + time.Sleep(5 * time.Millisecond) + conn.Close() + + t.Logf("stopping http.Server at: %v", listener.Addr().String()) + err = listener.Close() + require.NoError(t, err, "should be able to close serverListener") +} + +func TestListenerTracingCapturedInPage(t *testing.T) { + listener, _ := setupServerListener(t) + + conn, err := (&net.Dialer{}).DialContext(context.Background(), "tcp", listener.Addr().String()) + require.NoError(t, err, "DialContext should successfully establish a conn here") + time.Sleep(5 * time.Millisecond) + assert.Contains(t, fetchTraceEvents(t, "net.ServerConn."+listenerName), conn.LocalAddr().String(), + "the /debug/trace/events page must contain the live connection") + time.Sleep(5 * time.Millisecond) + conn.Close() + + t.Logf("stopping http.Server at: %v", listener.Addr().String()) + err = listener.Close() + require.NoError(t, err, "should be able to close serverListener") +} + +func setupServerListener(t *testing.T) (net.Listener, conntrack.ConnectionTracker) { + t.Helper() + + serverListener, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err, "must be able to allocate a port for serverListener") + + tracker := tracetracker.New() + serverListener = conntrack.NewListener(serverListener, + conntrack.ListenerWithName(listenerName), + conntrack.ListenerWithTrackers(tracker), + ) + httpServer := http.Server{ + Handler: http.HandlerFunc(func(resp http.ResponseWriter, _ *http.Request) { + resp.WriteHeader(http.StatusOK) + }), + } + + go func() { + err = httpServer.Serve(serverListener) + require.NotErrorIs(t, err, http.ErrServerClosed, "http.Server should only be closed normally") + }() + + return serverListener, tracker +} + +func fetchTraceEvents(t *testing.T, familyName string) string { + t.Helper() + + resp := httptest.NewRecorder() + url := fmt.Sprintf("/debug/events?fam=%s&b=0&exp=1", familyName) + req, err := http.NewRequest("GET", url, nil) + require.NoError(t, err, "failed creating request for trace events handler") + trace.RenderEvents(resp, req, true) + out, err := io.ReadAll(resp.Body) + require.NoError(t, err, "failed reading the trace page") + return string(out) +} diff --git a/tracehelper_test.go b/tracehelper_test.go deleted file mode 100644 index d6f1b28..0000000 --- a/tracehelper_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package conntrack_test - -import ( - "fmt" - "io" - "net/http" - "net/http/httptest" - "testing" - - "github.com/stretchr/testify/require" - "golang.org/x/net/trace" -) - -func fetchTraceEvents(t *testing.T, familyName string) string { - resp := httptest.NewRecorder() - url := fmt.Sprintf("/debug/events?fam=%s&b=0&exp=1", familyName) - req, err := http.NewRequest("GET", url, nil) - require.NoError(t, err, "failed creating request for Prometheus handler") - trace.RenderEvents(resp, req, true) - out, err := io.ReadAll(resp.Body) - require.NoError(t, err, "failed reading the trace page") - return string(out) -} diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 0000000..cd14927 --- /dev/null +++ b/utils_test.go @@ -0,0 +1,71 @@ +// Copyright (c) The go-conntrack Authors. +// Licensed under the Apache License 2.0. + +package conntrack_test + +import ( + "context" + "sync" + + "github.com/marefr/go-conntrack/v2" +) + +var _ conntrack.ConnectionTracker = &testConnectionTracker{} +var _ conntrack.DialerConnectionTagger = &testConnectionTracker{} +var _ conntrack.ListenerConnectionTagger = &testConnectionTracker{} + +type testConnectionTracker struct { + stats []conntrack.ConnectionStats + dialerConnectionTagCtx context.Context + dialerConnectionTagInfo *conntrack.DialerConnectionTagInfo + listenerConnectionTagCtx context.Context + listenerConnectionTagInfo *conntrack.ListenerConnectionTagInfo + m sync.RWMutex +} + +func newTestConnectionTracker() *testConnectionTracker { + return &testConnectionTracker{ + stats: []conntrack.ConnectionStats{}, + } +} + +func (t *testConnectionTracker) TagDialerConnection(ctx context.Context, info *conntrack.DialerConnectionTagInfo) context.Context { + t.m.Lock() + defer t.m.Unlock() + t.dialerConnectionTagCtx = ctx + t.dialerConnectionTagInfo = info + return ctx +} + +func (t *testConnectionTracker) TagListenerConnection(ctx context.Context, info *conntrack.ListenerConnectionTagInfo) context.Context { + t.m.Lock() + defer t.m.Unlock() + t.listenerConnectionTagCtx = ctx + t.listenerConnectionTagInfo = info + return ctx +} + +func (t *testConnectionTracker) TrackConnection(_ context.Context, stats conntrack.ConnectionStats) { + t.m.Lock() + defer t.m.Unlock() + t.stats = append(t.stats, stats) +} + +func (t *testConnectionTracker) GetStats() []conntrack.ConnectionStats { + t.m.RLock() + s := t.stats + t.m.RUnlock() + return s +} + +func (t *testConnectionTracker) GetDialerTagInfo() (context.Context, *conntrack.DialerConnectionTagInfo) { + t.m.RLock() + defer t.m.RUnlock() + return t.dialerConnectionTagCtx, t.dialerConnectionTagInfo +} + +func (t *testConnectionTracker) GetListenerTagInfo() (context.Context, *conntrack.ListenerConnectionTagInfo) { + t.m.RLock() + defer t.m.RUnlock() + return t.listenerConnectionTagCtx, t.listenerConnectionTagInfo +}