From a96077587ea2355eda9f2cf73d0f66796d46bc8c Mon Sep 17 00:00:00 2001 From: Shouichi Kamiya Date: Fri, 18 Dec 2020 10:38:01 +0900 Subject: [PATCH 1/3] Add grpc unary server interceptor --- go.mod | 3 +- go.sum | 67 ++++++++++ grpc/doc.go | 3 + grpc/server.go | 84 +++++++++++++ grpc/server_test.go | 293 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 449 insertions(+), 1 deletion(-) create mode 100644 grpc/doc.go create mode 100644 grpc/server.go create mode 100644 grpc/server_test.go diff --git a/go.mod b/go.mod index 24962a979..8773304ba 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/gin-gonic/gin v1.4.0 github.com/go-errors/errors v1.0.1 github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab - github.com/google/go-cmp v0.4.0 + github.com/google/go-cmp v0.5.0 github.com/google/go-querystring v1.0.0 // indirect github.com/imkira/go-interpol v1.1.0 // indirect github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect @@ -31,4 +31,5 @@ require ( github.com/yudai/gojsondiff v1.0.0 // indirect github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect github.com/yudai/pp v2.0.1+incompatible // indirect + google.golang.org/grpc v1.34.0 ) diff --git a/go.sum b/go.sum index 2f5857a9b..347baee49 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= @@ -14,6 +15,9 @@ github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible h1:Ppm0npCCsmuR9oQaBtRuZcmILVE74aXE+AmrJj8L2ns= github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0 h1:sDMmm+q/3+BukdIpxwO365v/Rbspp2Nt5XntgQRXq8Q= github.com/codegangsta/inject v0.0.0-20150114235600-33e0aa1cb7c0/go.mod h1:4Zcjuz89kmFXt9morQgcfYZAYZ5n8WHjt81YYWIwtTM= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= @@ -30,6 +34,10 @@ github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUn github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 h1:clC1lXBpe2kTj2VHdaIu9ajZQe4kcEY9j0NsnDDBZ3o= github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw= github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072 h1:DddqAaWDpywytcG8w/qoQ5sAN8X12d3Z3koB0C3Rxsc= github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8= @@ -52,15 +60,32 @@ github.com/go-martini/martini v0.0.0-20170121215854-22fa46961aab/go.mod h1:/P9AE github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo= github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw= github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM= +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.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= github.com/golang/protobuf v1.3.1/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.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/gomodule/redigo v1.7.1-0.20190724094224-574c33c3df38/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= +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 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM= @@ -148,6 +173,7 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/ryanuber/columnize v2.1.0+incompatible h1:j1Wcmh8OrK4Q7GXY+V7SVSY8nUWQxHW5TkBe7YUl+2s= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= @@ -172,6 +198,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= @@ -209,8 +237,15 @@ golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49N golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876 h1:sKJQZMuxjOAR/Uo2LBfU90onWEf1dF4C+0hPJCc9Mpc= golang.org/x/crypto v0.0.0-20191227163750-53104e6ec876/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/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-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181220203305-927f97764cc3/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-20190327091125-710a502c58a2/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -220,8 +255,12 @@ golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +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-20190911185100-cd5d95a43a6e/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-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -236,11 +275,37 @@ golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20181221001348-537d06c36207/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +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-20190327201419-c70d86f8b7cf/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/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 h1:+kGHl1aib/qcwaRi1CbqBZ1rk19r85MNUf8HaBghugY= +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.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.34.0 h1:raiipEjMOIC/TO2AvyTxP25XFdLxNIBwzDh3FM3XztI= +google.golang.org/grpc v1.34.0/go.mod h1:WotjhfgOW/POjDeRt8vscBtXq+2VjORFy659qA51WJ8= +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/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= @@ -261,3 +326,5 @@ gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2 h1:XZx7nhd5GMaZpmDaEHFVafUZC7ya0fuo7cSJ3UCKYmM= gopkg.in/yaml.v3 v3.0.0-20191120175047-4206685974f2/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/grpc/doc.go b/grpc/doc.go new file mode 100644 index 000000000..0183350a1 --- /dev/null +++ b/grpc/doc.go @@ -0,0 +1,3 @@ +// Package sentrygrpc provides Sentry integration for servers and clients based +// on the google.golang.org/grpc package. +package sentrygrpc diff --git a/grpc/server.go b/grpc/server.go new file mode 100644 index 000000000..a198c8702 --- /dev/null +++ b/grpc/server.go @@ -0,0 +1,84 @@ +package sentrygrpc + +import ( + "context" + + "github.com/getsentry/sentry-go" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +// UnaryServerInterceptor is a grpc interceptor that reports errors and panics +// to sentry. It also sets *sentry.Hub to context. +func UnaryServerInterceptor(opts UnaryServerInterceptorOptions) grpc.UnaryServerInterceptor { + if opts.ReportOn == nil { + opts.ReportOn = ReportAlways + } + + return func( + ctx context.Context, + req interface{}, + info *grpc.UnaryServerInfo, + handler grpc.UnaryHandler, + ) (_ interface{}, err error) { + hub := sentry.GetHubFromContext(ctx) + if hub == nil { + hub = sentry.CurrentHub().Clone() + ctx = sentry.SetHubOnContext(ctx, hub) + } + + defer func() { + if r := recover(); r != nil { + hub.RecoverWithContext(ctx, r) + + if opts.Repanic { + panic(r) + } + + err = status.Errorf(codes.Internal, "%s", r) + } + }() + + resp, err := handler(ctx, req) + + if opts.ReportOn(err) { + hub.CaptureException(err) + } + + return resp, err + } +} + +// UnaryServerInterceptor configure UnaryServerInterceptor. +type UnaryServerInterceptorOptions struct { + // Repanic configures whether to panic again after recovering from a + // panic. Use this option if you have other panic handlers. + Repanic bool + + // ReportOn configures whether to report an error. Defaults to + // ReportAlways. + ReportOn ReportOn +} + +// ReportOn decides error should be reported to sentry. +type ReportOn func(error) bool + +// ReportAlways returns true if err is non-nil. +func ReportAlways(err error) bool { + return err != nil +} + +// ReportOnCodes returns true if error code matches on of the given codes. +func ReportOnCodes(cc ...codes.Code) ReportOn { + return func(err error) bool { + c := status.Code(err) + for i := range cc { + if c == cc[i] { + return true + } + } + + return false + } +} diff --git a/grpc/server_test.go b/grpc/server_test.go new file mode 100644 index 000000000..18a7171ca --- /dev/null +++ b/grpc/server_test.go @@ -0,0 +1,293 @@ +package sentrygrpc_test + +import ( + "context" + "errors" + "net" + "testing" + "time" + + "github.com/getsentry/sentry-go" + sentrygrpc "github.com/getsentry/sentry-go/grpc" + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + grpchealth "google.golang.org/grpc/health/grpc_health_v1" + "google.golang.org/grpc/status" +) + +func TestUnaryServerInterceptor(t *testing.T) { + for _, tt := range []struct { + name string + ctx context.Context + opts sentrygrpc.UnaryServerInterceptorOptions + handler func( + context.Context, + *grpchealth.HealthCheckRequest, + ) (*grpchealth.HealthCheckResponse, error) + code codes.Code + events []*sentry.Event + }{ + { + name: "does not report when err is nil", + ctx: context.Background(), + opts: sentrygrpc.UnaryServerInterceptorOptions{}, + handler: func( + ctx context.Context, + _ *grpchealth.HealthCheckRequest, + ) (*grpchealth.HealthCheckResponse, error) { + return &grpchealth.HealthCheckResponse{}, nil + }, + code: codes.OK, + }, + { + name: "reports all errors by default", + ctx: context.Background(), + opts: sentrygrpc.UnaryServerInterceptorOptions{}, + handler: func( + context.Context, + *grpchealth.HealthCheckRequest, + ) (*grpchealth.HealthCheckResponse, error) { + return nil, status.Error(codes.NotFound, "not found") + }, + code: codes.NotFound, + events: []*sentry.Event{ + { + Level: sentry.LevelError, + Exception: []sentry.Exception{ + { + Type: "*status.Error", + Value: "rpc error: code = NotFound desc = not found", + }, + }, + }, + }, + }, + { + name: "reports errors that ReportOn returns true", + ctx: context.Background(), + opts: sentrygrpc.UnaryServerInterceptorOptions{ + ReportOn: func(err error) bool { + return errors.Is(err, grpc.ErrServerStopped) + }, + }, + handler: func( + context.Context, + *grpchealth.HealthCheckRequest, + ) (*grpchealth.HealthCheckResponse, error) { + return nil, grpc.ErrServerStopped + }, + code: codes.Unknown, + events: []*sentry.Event{ + { + Level: sentry.LevelError, + Exception: []sentry.Exception{ + { + Type: "*errors.errorString", + Value: "grpc: the server has been stopped", + }, + }, + }, + }, + }, + { + name: "does not report errors that ReportOn returns false", + ctx: context.Background(), + opts: sentrygrpc.UnaryServerInterceptorOptions{ + ReportOn: func(err error) bool { + return false + }, + }, + handler: func( + context.Context, + *grpchealth.HealthCheckRequest, + ) (*grpchealth.HealthCheckResponse, error) { + return nil, grpc.ErrServerStopped + }, + code: codes.Unknown, + }, + { + name: "recovers from panic and returns internal error", + ctx: context.Background(), + opts: sentrygrpc.UnaryServerInterceptorOptions{}, + handler: func( + context.Context, + *grpchealth.HealthCheckRequest, + ) (*grpchealth.HealthCheckResponse, error) { + panic("simulated panic") + }, + code: codes.Internal, + events: []*sentry.Event{ + { + Level: sentry.LevelFatal, + Message: "simulated panic", + }, + }, + }, + { + name: "sets hub on context", + ctx: context.Background(), + opts: sentrygrpc.UnaryServerInterceptorOptions{}, + handler: func( + ctx context.Context, + _ *grpchealth.HealthCheckRequest, + ) (*grpchealth.HealthCheckResponse, error) { + if !sentry.HasHubOnContext(ctx) { + t.Fatal("context must have hub") + } + + return &grpchealth.HealthCheckResponse{}, nil + }, + code: codes.OK, + }, + } { + tt := tt + + t.Run(tt.name, func(t *testing.T) { + var events []*sentry.Event + + if err := sentry.Init(sentry.ClientOptions{ + BeforeSend: func( + event *sentry.Event, + hint *sentry.EventHint, + ) *sentry.Event { + events = append(events, event) + return event + }, + }); err != nil { + t.Fatalf("sentry.Init error: %s", err) + } + + lis, err := net.Listen("tcp", "localhost:0") + if err != nil { + t.Fatalf("net.Listen error: %s", err) + } + defer lis.Close() + + opt := grpc.UnaryInterceptor(sentrygrpc.UnaryServerInterceptor(tt.opts)) + server := grpc.NewServer(opt) + defer server.Stop() + + grpchealth.RegisterHealthServer( + server, + &mockHealthServer{handler: tt.handler}, + ) + + go func() { + if err := server.Serve(lis); err != nil { + t.Errorf("grpc serve error: %s", err) + } + }() + + conn, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure()) + if err != nil { + t.Fatalf("grpc.Dial error: %s", err) + } + defer conn.Close() + + client := grpchealth.NewHealthClient(conn) + + req := &grpchealth.HealthCheckRequest{} + _, err = client.Check(tt.ctx, req) + if w, g := tt.code, status.Code(err); w != g { + t.Fatalf("status mismatch: want %s, got %s", w, g) + } + + if !sentry.Flush(time.Second) { + t.Fatal("sentry.Flush timed out") + } + + opts := cmp.Options{ + cmpopts.IgnoreFields( + sentry.Event{}, + "Contexts", + "EventID", + "Extra", + "Platform", + "Sdk", + "ServerName", + "Tags", + "Timestamp", + ), + cmpopts.IgnoreFields( + sentry.Request{}, + "Env", + ), + cmpopts.IgnoreFields( + sentry.Exception{}, + "Stacktrace", + ), + } + + if d := cmp.Diff(tt.events, events, opts); d != "" { + t.Fatalf("events mismatch (-want +got):\n%s", d) + } + }) + } +} + +type mockHealthServer struct { + grpchealth.UnimplementedHealthServer + + handler func( + context.Context, + *grpchealth.HealthCheckRequest, + ) (*grpchealth.HealthCheckResponse, error) +} + +func (m *mockHealthServer) Check( + ctx context.Context, + req *grpchealth.HealthCheckRequest, +) (*grpchealth.HealthCheckResponse, error) { + return m.handler(ctx, req) +} + +func TestReportOnCodes(t *testing.T) { + for _, tt := range []struct { + name string + err error + codes []codes.Code + want bool + }{ + { + name: "returns true on code match", + err: status.Error(codes.Aborted, ""), + codes: []codes.Code{codes.Aborted}, + want: true, + }, + { + name: "returns false on code mismatch", + err: status.Error(codes.Aborted, ""), + codes: []codes.Code{codes.Canceled}, + want: false, + }, + } { + tt := tt + + t.Run(tt.name, func(t *testing.T) { + if w, g := tt.want, sentrygrpc.ReportOnCodes(tt.codes...)(tt.err); w != g { + t.Fatalf("ReportOnCodes: want %t, got %t", w, g) + } + }) + } +} + +func ExampleUnaryServerInterceptor() { + opts := sentrygrpc.UnaryServerInterceptorOptions{ + // Reports on OutOfRange or Internal error. + ReportOn: sentrygrpc.ReportOnCodes( + codes.OutOfRange, + codes.Internal, + ), + // Recovers from panic, reports it and returns internal error. + Repanic: false, + } + + // This middleware sets *sentry.Hub to context. You can set user to + // hub's scope in the later interceptor for example. + sentry := sentrygrpc.UnaryServerInterceptor(opts) + + server := grpc.NewServer(grpc.UnaryInterceptor(sentry)) + defer server.Stop() +} From 456ebca9152d566f73de0bd40d71163b0f1967e4 Mon Sep 17 00:00:00 2001 From: Shouichi Kamiya Date: Wed, 20 Jan 2021 16:12:08 +0900 Subject: [PATCH 2/3] Use the functional options pattern to configure interceptor Also adresse other minor/style issues. --- grpc/options.go | 63 +++++++++++++++++++++++++++++++++++++++++++++ grpc/server.go | 47 +++++---------------------------- grpc/server_test.go | 44 ++++++++++++++++--------------- 3 files changed, 92 insertions(+), 62 deletions(-) create mode 100644 grpc/options.go diff --git a/grpc/options.go b/grpc/options.go new file mode 100644 index 000000000..2930b8781 --- /dev/null +++ b/grpc/options.go @@ -0,0 +1,63 @@ +package sentrygrpc + +import ( + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +type options struct { + repanic bool + reportOn ReportOn +} + +func buildOptions(ff ...Option) options { + opts := options{ + reportOn: ReportAlways, + } + + for _, f := range ff { + f(&opts) + } + + return opts +} + +// Option configures reporting behavior. +type Option func(*options) + +// WithRepanic configures whether to panic again after recovering from +// a panic. Use this option if you have other panic handlers. +func WithRepanic(b bool) Option { + return func(o *options) { + o.repanic = b + } +} + +// WithReportOn configures whether to report on errors. +func WithReportOn(r ReportOn) Option { + return func(o *options) { + o.reportOn = r + } +} + +// ReportOn decides error should be reported to sentry. +type ReportOn func(error) bool + +// ReportAlways returns true if err is non-nil. +func ReportAlways(err error) bool { + return err != nil +} + +// ReportOnCodes returns true if error code matches on of the given codes. +func ReportOnCodes(cc ...codes.Code) ReportOn { + return func(err error) bool { + c := status.Code(err) + for i := range cc { + if c == cc[i] { + return true + } + } + + return false + } +} diff --git a/grpc/server.go b/grpc/server.go index a198c8702..ecab7c747 100644 --- a/grpc/server.go +++ b/grpc/server.go @@ -11,17 +11,15 @@ import ( // UnaryServerInterceptor is a grpc interceptor that reports errors and panics // to sentry. It also sets *sentry.Hub to context. -func UnaryServerInterceptor(opts UnaryServerInterceptorOptions) grpc.UnaryServerInterceptor { - if opts.ReportOn == nil { - opts.ReportOn = ReportAlways - } +func UnaryServerInterceptor(options ...Option) grpc.UnaryServerInterceptor { + opts := buildOptions(options...) return func( ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, - ) (_ interface{}, err error) { + ) (resp interface{}, err error) { hub := sentry.GetHubFromContext(ctx) if hub == nil { hub = sentry.CurrentHub().Clone() @@ -32,7 +30,7 @@ func UnaryServerInterceptor(opts UnaryServerInterceptorOptions) grpc.UnaryServer if r := recover(); r != nil { hub.RecoverWithContext(ctx, r) - if opts.Repanic { + if opts.repanic { panic(r) } @@ -40,45 +38,12 @@ func UnaryServerInterceptor(opts UnaryServerInterceptorOptions) grpc.UnaryServer } }() - resp, err := handler(ctx, req) + resp, err = handler(ctx, req) - if opts.ReportOn(err) { + if opts.reportOn(err) { hub.CaptureException(err) } return resp, err } } - -// UnaryServerInterceptor configure UnaryServerInterceptor. -type UnaryServerInterceptorOptions struct { - // Repanic configures whether to panic again after recovering from a - // panic. Use this option if you have other panic handlers. - Repanic bool - - // ReportOn configures whether to report an error. Defaults to - // ReportAlways. - ReportOn ReportOn -} - -// ReportOn decides error should be reported to sentry. -type ReportOn func(error) bool - -// ReportAlways returns true if err is non-nil. -func ReportAlways(err error) bool { - return err != nil -} - -// ReportOnCodes returns true if error code matches on of the given codes. -func ReportOnCodes(cc ...codes.Code) ReportOn { - return func(err error) bool { - c := status.Code(err) - for i := range cc { - if c == cc[i] { - return true - } - } - - return false - } -} diff --git a/grpc/server_test.go b/grpc/server_test.go index 18a7171ca..bc97724f3 100644 --- a/grpc/server_test.go +++ b/grpc/server_test.go @@ -21,7 +21,7 @@ func TestUnaryServerInterceptor(t *testing.T) { for _, tt := range []struct { name string ctx context.Context - opts sentrygrpc.UnaryServerInterceptorOptions + opts []sentrygrpc.Option handler func( context.Context, *grpchealth.HealthCheckRequest, @@ -32,7 +32,6 @@ func TestUnaryServerInterceptor(t *testing.T) { { name: "does not report when err is nil", ctx: context.Background(), - opts: sentrygrpc.UnaryServerInterceptorOptions{}, handler: func( ctx context.Context, _ *grpchealth.HealthCheckRequest, @@ -44,7 +43,6 @@ func TestUnaryServerInterceptor(t *testing.T) { { name: "reports all errors by default", ctx: context.Background(), - opts: sentrygrpc.UnaryServerInterceptorOptions{}, handler: func( context.Context, *grpchealth.HealthCheckRequest, @@ -67,10 +65,12 @@ func TestUnaryServerInterceptor(t *testing.T) { { name: "reports errors that ReportOn returns true", ctx: context.Background(), - opts: sentrygrpc.UnaryServerInterceptorOptions{ - ReportOn: func(err error) bool { - return errors.Is(err, grpc.ErrServerStopped) - }, + opts: []sentrygrpc.Option{ + sentrygrpc.WithReportOn( + func(err error) bool { + return errors.Is(err, grpc.ErrServerStopped) + }, + ), }, handler: func( context.Context, @@ -94,10 +94,12 @@ func TestUnaryServerInterceptor(t *testing.T) { { name: "does not report errors that ReportOn returns false", ctx: context.Background(), - opts: sentrygrpc.UnaryServerInterceptorOptions{ - ReportOn: func(err error) bool { - return false - }, + opts: []sentrygrpc.Option{ + sentrygrpc.WithReportOn( + func(err error) bool { + return false + }, + ), }, handler: func( context.Context, @@ -110,7 +112,6 @@ func TestUnaryServerInterceptor(t *testing.T) { { name: "recovers from panic and returns internal error", ctx: context.Background(), - opts: sentrygrpc.UnaryServerInterceptorOptions{}, handler: func( context.Context, *grpchealth.HealthCheckRequest, @@ -128,7 +129,6 @@ func TestUnaryServerInterceptor(t *testing.T) { { name: "sets hub on context", ctx: context.Background(), - opts: sentrygrpc.UnaryServerInterceptorOptions{}, handler: func( ctx context.Context, _ *grpchealth.HealthCheckRequest, @@ -165,7 +165,7 @@ func TestUnaryServerInterceptor(t *testing.T) { } defer lis.Close() - opt := grpc.UnaryInterceptor(sentrygrpc.UnaryServerInterceptor(tt.opts)) + opt := grpc.UnaryInterceptor(sentrygrpc.UnaryServerInterceptor(tt.opts...)) server := grpc.NewServer(opt) defer server.Stop() @@ -267,26 +267,28 @@ func TestReportOnCodes(t *testing.T) { t.Run(tt.name, func(t *testing.T) { if w, g := tt.want, sentrygrpc.ReportOnCodes(tt.codes...)(tt.err); w != g { - t.Fatalf("ReportOnCodes: want %t, got %t", w, g) + t.Fatalf("want %t, got %t", w, g) } }) } } func ExampleUnaryServerInterceptor() { - opts := sentrygrpc.UnaryServerInterceptorOptions{ + opts := []sentrygrpc.Option{ // Reports on OutOfRange or Internal error. - ReportOn: sentrygrpc.ReportOnCodes( - codes.OutOfRange, - codes.Internal, + sentrygrpc.WithReportOn( + sentrygrpc.ReportOnCodes( + codes.OutOfRange, + codes.Internal, + ), ), // Recovers from panic, reports it and returns internal error. - Repanic: false, + sentrygrpc.WithRepanic(false), } // This middleware sets *sentry.Hub to context. You can set user to // hub's scope in the later interceptor for example. - sentry := sentrygrpc.UnaryServerInterceptor(opts) + sentry := sentrygrpc.UnaryServerInterceptor(opts...) server := grpc.NewServer(grpc.UnaryInterceptor(sentry)) defer server.Stop() From 93183e76c695c137f2b6331d8a868e8447d683ff Mon Sep 17 00:00:00 2001 From: Shouichi Kamiya Date: Thu, 21 Jan 2021 09:31:55 +0900 Subject: [PATCH 3/3] Use map to lookup status code Co-authored-by: Aubrey Dean --- grpc/options.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/grpc/options.go b/grpc/options.go index 2930b8781..53461980d 100644 --- a/grpc/options.go +++ b/grpc/options.go @@ -50,14 +50,11 @@ func ReportAlways(err error) bool { // ReportOnCodes returns true if error code matches on of the given codes. func ReportOnCodes(cc ...codes.Code) ReportOn { + cm := make(map[codes.Code]bool) + for _, c := range cc { + cm[c] = true + } return func(err error) bool { - c := status.Code(err) - for i := range cc { - if c == cc[i] { - return true - } - } - - return false + return cm[status.Code(err)] } }