From 03914a8f68b423f70d6311728c3fb3714eb39f9a Mon Sep 17 00:00:00 2001 From: zhangwei Date: Fri, 3 Jul 2020 09:35:19 +0800 Subject: [PATCH 1/7] migration plugin --- .github/workflows/ci.yaml | 35 ++++++++-- .gitignore | 3 + Makefile | 41 +++++++++++ gear/doc.go | 19 ++++++ gear/example_gear_test.go | 91 +++++++++++++++++++++++++ gear/gear.go | 61 +++++++++++++++++ gear/go.mod | 8 +++ gear/go.sum | 120 +++++++++++++++++++++++++++++++++ gin/v1/doc.go | 20 ++++++ gin/v1/example_gin_test.go | 94 ++++++++++++++++++++++++++ gin/v1/gin.go | 101 +++++++++++++++++++++++++++ gin/v1/go.mod | 8 +++ gin/v1/go.sum | 92 +++++++++++++++++++++++++ gin/v2/doc.go | 20 ++++++ gin/v2/example_gin_test.go | 94 ++++++++++++++++++++++++++ gin/v2/gin.go | 70 +++++++++++++++++++ gin/v2/go.mod | 8 +++ gin/v2/go.sum | 100 +++++++++++++++++++++++++++ golangci.yml | 45 +++++++++++++ resty/doc.go | 19 ++++++ resty/example_go_resty_test.go | 73 ++++++++++++++++++++ resty/go.mod | 9 +++ resty/go.sum | 79 ++++++++++++++++++++++ resty/go_resty.go | 36 ++++++++++ tools/check-license-header.py | 74 ++++++++++++++++++++ tools/deps.sh | 25 +++++++ tools/lint.sh | 55 +++++++++++++++ tools/test.sh | 27 ++++++++ 28 files changed, 1423 insertions(+), 4 deletions(-) create mode 100644 Makefile create mode 100644 gear/doc.go create mode 100644 gear/example_gear_test.go create mode 100644 gear/gear.go create mode 100644 gear/go.mod create mode 100644 gear/go.sum create mode 100644 gin/v1/doc.go create mode 100644 gin/v1/example_gin_test.go create mode 100644 gin/v1/gin.go create mode 100644 gin/v1/go.mod create mode 100644 gin/v1/go.sum create mode 100644 gin/v2/doc.go create mode 100644 gin/v2/example_gin_test.go create mode 100644 gin/v2/gin.go create mode 100644 gin/v2/go.mod create mode 100644 gin/v2/go.sum create mode 100644 golangci.yml create mode 100644 resty/doc.go create mode 100644 resty/example_go_resty_test.go create mode 100644 resty/go.mod create mode 100644 resty/go.sum create mode 100644 resty/go_resty.go create mode 100644 tools/check-license-header.py create mode 100755 tools/deps.sh create mode 100755 tools/lint.sh create mode 100755 tools/test.sh diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f386581..2d51264 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -25,15 +25,42 @@ on: jobs: build: - name: Build + name: Build & Test runs-on: ubuntu-latest + strategy: + matrix: + go-version: [1.12, 1.13, 1.14] + fail-fast: false steps: - - name: Set up Go 1.12 + - name: Set up ${{ matrix.go-version }} uses: actions/setup-go@v1 with: - go-version: 1.12 + go-version: ${{ matrix.go-version }} id: go + - name: Check out code into the Go module directory uses: actions/checkout@v2 + + - name: Get dependencies + run: make deps + + - name: Lint + run: make lint + - name: Test - run: echo "test" \ No newline at end of file + run: make test + + license: + name: Check License + runs-on: ubuntu-latest + steps: + - name: Set up Python 3.7 + uses: actions/setup-python@v2 + with: + python-version: 3.7 + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: Check license header + run: make license \ No newline at end of file diff --git a/.gitignore b/.gitignore index 66fd13c..96ae31d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +# IDE +.idea/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9080a54 --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +# +# Licensed to the SkyAPM org under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +.PHONY: test +test: + ./tools/test.sh + +.PHONY: deps +deps: + ./tools/deps.sh + +LINTER := bin/golangci-lint +$(LINTER): + wget -q -O- https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.20.1 + +.PHONY: lint +lint: $(LINTER) + ./tools/lint.sh lint + +.PHONY: fix +fix: $(LINTER) + ./tools/lint.sh fix + +.PHONY: license +license: + python3 tools/check-license-header.py + diff --git a/gear/doc.go b/gear/doc.go new file mode 100644 index 0000000..2c07656 --- /dev/null +++ b/gear/doc.go @@ -0,0 +1,19 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Package gear is a plugin that can be used to trace Gear framework. +package gear diff --git a/gear/example_gear_test.go b/gear/example_gear_test.go new file mode 100644 index 0000000..73597ee --- /dev/null +++ b/gear/example_gear_test.go @@ -0,0 +1,91 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package gear + +import ( + "fmt" + "log" + "net/http" + "sync" + "time" + + "github.com/SkyAPM/go2sky" + h "github.com/SkyAPM/go2sky/plugins/http" + "github.com/SkyAPM/go2sky/reporter" + "github.com/teambition/gear" +) + +func ExampleMiddleware() { + re, err := reporter.NewLogReporter() + if err != nil { + log.Fatalf("new reporter error %v \n", err) + } + + defer re.Close() + + tracer, err := go2sky.NewTracer("gear", go2sky.WithReporter(re)) + if err != nil { + log.Fatalf("create tracer error %v \n", err) + } + + app := gear.New() + app.Use(Middleware(tracer)) + + router := gear.NewRouter() + router.Get("/user", func(ctx *gear.Context) error { + name := ctx.Param("name") + return ctx.End(http.StatusOK, []byte(fmt.Sprintf("Hello %s", name))) + }) + + app.UseHandler(router) + + go func() { + app.Error(app.Listen(":8080")) + }() + // Wait for the server to start + time.Sleep(time.Second) + + wg := sync.WaitGroup{} + wg.Add(1) + + go func() { + defer wg.Done() + request(tracer) + }() + wg.Wait() + // Output: +} + +func request(tracer *go2sky.Tracer) { + client, err := h.NewClient(tracer) + if err != nil { + log.Fatalf("create client error %v \n", err) + } + + request, err := http.NewRequest("GET", "http://127.0.0.1:8080/user?name=gear", nil) + if err != nil { + log.Fatalf("unable to create http request: %+v\n", err) + } + + res, err := client.Do(request) + if err != nil { + log.Fatalf("unable to do http request: %+v\n", err) + } + + _ = res.Body.Close() +} diff --git a/gear/gear.go b/gear/gear.go new file mode 100644 index 0000000..a8f655b --- /dev/null +++ b/gear/gear.go @@ -0,0 +1,61 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package gear + +import ( + "strconv" + "time" + + "github.com/SkyAPM/go2sky" + "github.com/SkyAPM/go2sky/propagation" + v3 "github.com/SkyAPM/go2sky/reporter/grpc/language-agent" + "github.com/teambition/gear" +) + +const componentIDGearServer = 5007 + +//Middleware gear middleware return HandlerFunc with tracing. +func Middleware(tracer *go2sky.Tracer) gear.Middleware { + return func(ctx *gear.Context) error { + if tracer == nil { + return nil + } + + span, _, err := tracer.CreateEntrySpan(ctx, ctx.Path, func() (string, error) { + return ctx.GetHeader(propagation.Header), nil + }) + if err != nil { + return nil + } + + span.SetComponent(componentIDGearServer) + span.Tag(go2sky.TagHTTPMethod, ctx.Method) + span.Tag(go2sky.TagURL, ctx.Host+ctx.Path) + span.SetSpanLayer(v3.SpanLayer_Http) + + ctx.OnEnd(func() { + code := ctx.Res.Status() + span.Tag(go2sky.TagStatusCode, strconv.Itoa(code)) + if code >= 400 { + span.Error(time.Now(), string(ctx.Res.Body())) + } + span.End() + }) + return nil + } +} diff --git a/gear/go.mod b/gear/go.mod new file mode 100644 index 0000000..0a0f4e6 --- /dev/null +++ b/gear/go.mod @@ -0,0 +1,8 @@ +module github.com/SkyAPM/go2sky-plugins/gear + +go 1.12 + +require ( + github.com/SkyAPM/go2sky v0.4.0 + github.com/teambition/gear v1.21.2 +) diff --git a/gear/go.sum b/gear/go.sum new file mode 100644 index 0000000..bf21c6f --- /dev/null +++ b/gear/go.sum @@ -0,0 +1,120 @@ +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/GitbookIO/mimedb v0.0.0-20180329142916-39fdfdb4def4/go.mod h1:0JA2lIXs/dl3RUgHP5ivwjl3f0g+X2BQz3zWnq8IJa4= +github.com/SkyAPM/go2sky v0.4.0 h1:kJAdy1a34BgxCK2HYtNO3RuOoEGULFFHJuPlSOGc2YA= +github.com/SkyAPM/go2sky v0.4.0/go.mod h1:9Q2BjSwndQEvLKIfaB80Nm9JWmtScSgIY9u2vVjRL7s= +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/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/dimfeld/httptreemux v5.0.1+incompatible h1:Qj3gVcDNoOthBAqftuD596rm4wg/adLLz5xh5CmpiCA= +github.com/dimfeld/httptreemux v5.0.1+incompatible/go.mod h1:rbUlSV+CCpv/SuqUTP/8Bk2O3LyUV436/yaRGkhP6Z0= +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/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= +github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= +github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 h1:DujepqpGd1hyOd7aW59XpK7Qymp8iy83xq74fLr21is= +github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= +github.com/go-http-utils/cookie v1.3.1 h1:GCdTeqVV5vDcjP7LrgYpH8pbt3dOYKS+Wrs7Jo3/k/w= +github.com/go-http-utils/cookie v1.3.1/go.mod h1:ATl4rfG3bEemjiVa+8WIfgNcBUWdYBTasfXKjJ3Avt8= +github.com/go-http-utils/negotiator v1.0.0 h1:Qp1zofD6Nw7KXApXa3pAjehP06Js0ILguEBCnHhZeVA= +github.com/go-http-utils/negotiator v1.0.0/go.mod h1:mTQe1sH0XhdFkeDiWpCY3QSk7Apo5jwOlIwLWJbJe2c= +github.com/go-resty/resty/v2 v2.2.0 h1:vgZ1cdblp8Aw4jZj3ZsKh6yKAlMg3CHMrqFSFFd+jgY= +github.com/go-resty/resty/v2 v2.2.0/go.mod h1:nYW/8rxqQCmI3bPz9Fsmjbr2FBjGuR2Mzt6kDh3zZ7w= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +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/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk= +github.com/golang/mock v1.2.0/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 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/mailgun/timetools v0.0.0-20170619190023-f3a7b8ffff47/go.mod h1:RYmqHbhWwIz3z9eVmQ2rx82rulEMG0t+Q1bzfc9DYN4= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo= +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/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +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/teambition/compressible-go v1.0.1/go.mod h1:K91wjCUqzpuY2ZpSi039mt4WzzjMGxPFMZHHTEoTvak= +github.com/teambition/gear v1.21.2 h1:mwmfe5SOh9a5kQdoyYqdix/FGswtvu4QPkDzHNvkTLg= +github.com/teambition/gear v1.21.2/go.mod h1:berJp67mxgCZ1ZKgv1oCVV9WpWko3zAgIGYtaN4iGW0= +github.com/teambition/trie-mux v1.4.2 h1:HgbwXfQDsingRLzyYdxEyut3i2Z9To/GOlVZD2gKRiM= +github.com/teambition/trie-mux v1.4.2/go.mod h1:ZWBopELDBGsgw9l8lFD4WCkpZTmmEKhu/8w3FbsxBgo= +github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= +github.com/vulcand/oxy v0.0.0-20181019102601-ac21a760928b/go.mod h1:giFb8dicROVdV5W0HXlA5siMBLWKnVXZlkA4Y5ZIzrY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +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-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-20200222125558-5a598a2470a0 h1:MsuvTghUPjX762sGLnGsxC3HM0B5r83wEtYcYR8/vRs= +golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/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/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-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.1-0.20181010134911-4d1c5fb19474 h1:4l+CHZwCUFzGF11IlLbqggmpYvJyXOKSlGBZ8M0Ag/w= +golang.org/x/text v0.3.1-0.20181010134911-4d1c5fb19474/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-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= +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 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +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.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= +gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= +gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +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/gin/v1/doc.go b/gin/v1/doc.go new file mode 100644 index 0000000..2e8576e --- /dev/null +++ b/gin/v1/doc.go @@ -0,0 +1,20 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//Package v1 (GIN) is a HTTP web framework written in +//Go (Golang) plugin which can be used for integration with Gin http server. +package v1 diff --git a/gin/v1/example_gin_test.go b/gin/v1/example_gin_test.go new file mode 100644 index 0000000..d14fdb9 --- /dev/null +++ b/gin/v1/example_gin_test.go @@ -0,0 +1,94 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package v1 + +import ( + "fmt" + "log" + "net/http" + "sync" + "time" + + "github.com/SkyAPM/go2sky" + h "github.com/SkyAPM/go2sky/plugins/http" + "github.com/SkyAPM/go2sky/reporter" + "github.com/gin-gonic/gin" +) + +func ExampleMiddleware() { + // Use gRPC reporter for production + re, err := reporter.NewLogReporter() + if err != nil { + log.Fatalf("new reporter error %v \n", err) + } + defer re.Close() + + tracer, err := go2sky.NewTracer("gin-server", go2sky.WithReporter(re)) + if err != nil { + log.Fatalf("create tracer error %v \n", err) + } + + gin.SetMode(gin.ReleaseMode) + r := gin.New() + + //Use go2sky middleware with tracing + r.Use(Middleware(r, tracer)) + + r.GET("/user/:name", func(c *gin.Context) { + name := c.Param("name") + c.String(200, "Hello %s", name) + }) + + go func() { + if err := http.ListenAndServe(":8080", r); err != nil { + panic(err) + } + }() + // Wait for the server to start + time.Sleep(time.Second) + + wg := sync.WaitGroup{} + wg.Add(1) + + go func() { + defer wg.Done() + request(tracer) + }() + wg.Wait() + // Output: +} + +func request(tracer *go2sky.Tracer, _ ...h.ClientOption) { + //NewClient returns an HTTP Client with tracer + client, err := h.NewClient(tracer) + if err != nil { + log.Fatalf("create client error %v \n", err) + } + + request, err := http.NewRequest("GET", fmt.Sprintf("%s/user/gin", "http://127.0.0.1:8080"), nil) + if err != nil { + log.Fatalf("unable to create http request: %+v\n", err) + } + + res, err := client.Do(request) + if err != nil { + log.Fatalf("unable to do http request: %+v\n", err) + } + + _ = res.Body.Close() +} diff --git a/gin/v1/gin.go b/gin/v1/gin.go new file mode 100644 index 0000000..23d0ff7 --- /dev/null +++ b/gin/v1/gin.go @@ -0,0 +1,101 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//Package v1 is a HTTP web framework written in +//Go (Golang) plugin which can be used for integration with Gin http server. +package v1 + +import ( + "fmt" + "strconv" + "sync" + "time" + + "github.com/SkyAPM/go2sky" + "github.com/SkyAPM/go2sky/propagation" + v3 "github.com/SkyAPM/go2sky/reporter/grpc/language-agent" + "github.com/gin-gonic/gin" +) + +const componentIDGINHttpServer = 5006 + +type routeInfo struct { + operationName string +} + +type middleware struct { + routeMap map[string]map[string]routeInfo + routeMapOnce sync.Once +} + +//Middleware gin middleware return HandlerFunc with tracing. +func Middleware(engine *gin.Engine, tracer *go2sky.Tracer) gin.HandlerFunc { + if engine == nil || tracer == nil { + return func(c *gin.Context) { + c.Next() + } + } + + m := new(middleware) + + return func(c *gin.Context) { + m.routeMapOnce.Do(func() { + routes := engine.Routes() + rm := make(map[string]map[string]routeInfo) + for _, r := range routes { + mm := rm[r.Method] + if mm == nil { + mm = make(map[string]routeInfo) + rm[r.Method] = mm + } + mm[r.Handler] = routeInfo{ + operationName: fmt.Sprintf("/%s%s", r.Method, r.Path), + } + } + m.routeMap = rm + }) + var operationName string + handlerName := c.HandlerName() + if routeInfo, ok := m.routeMap[c.Request.Method][handlerName]; ok { + operationName = routeInfo.operationName + } + if operationName == "" { + operationName = c.Request.Method + } + span, ctx, err := tracer.CreateEntrySpan(c.Request.Context(), operationName, func() (string, error) { + return c.Request.Header.Get(propagation.Header), nil + }) + if err != nil { + c.Next() + return + } + span.SetComponent(componentIDGINHttpServer) + span.Tag(go2sky.TagHTTPMethod, c.Request.Method) + span.Tag(go2sky.TagURL, c.Request.Host+c.Request.URL.Path) + span.SetSpanLayer(v3.SpanLayer_Http) + + c.Request = c.Request.WithContext(ctx) + + c.Next() + + if len(c.Errors) > 0 { + span.Error(time.Now(), c.Errors.String()) + } + span.Tag(go2sky.TagStatusCode, strconv.Itoa(c.Writer.Status())) + span.End() + } +} diff --git a/gin/v1/go.mod b/gin/v1/go.mod new file mode 100644 index 0000000..f137026 --- /dev/null +++ b/gin/v1/go.mod @@ -0,0 +1,8 @@ +module github.com/SkyAPM/go2sky-plugins/gin/v1 + +go 1.12 + +require ( + github.com/SkyAPM/go2sky v0.4.0 + github.com/gin-gonic/gin v1.3.0 +) diff --git a/gin/v1/go.sum b/gin/v1/go.sum new file mode 100644 index 0000000..d798b20 --- /dev/null +++ b/gin/v1/go.sum @@ -0,0 +1,92 @@ +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/SkyAPM/go2sky v0.4.0 h1:kJAdy1a34BgxCK2HYtNO3RuOoEGULFFHJuPlSOGc2YA= +github.com/SkyAPM/go2sky v0.4.0/go.mod h1:9Q2BjSwndQEvLKIfaB80Nm9JWmtScSgIY9u2vVjRL7s= +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/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= +github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= +github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs= +github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= +github.com/go-resty/resty/v2 v2.2.0 h1:vgZ1cdblp8Aw4jZj3ZsKh6yKAlMg3CHMrqFSFFd+jgY= +github.com/go-resty/resty/v2 v2.2.0/go.mod h1:nYW/8rxqQCmI3bPz9Fsmjbr2FBjGuR2Mzt6kDh3zZ7w= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +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/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk= +github.com/golang/mock v1.2.0/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 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/mux v1.7.1 h1:Dw4jY2nghMMRsh1ol8dv1axHkDwMQK2DHerMNJsIpJU= +github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +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 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= +github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +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-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-20200222125558-5a598a2470a0 h1:MsuvTghUPjX762sGLnGsxC3HM0B5r83wEtYcYR8/vRs= +golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/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/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-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +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= +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 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +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 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +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/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= +gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= +gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= +gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +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/gin/v2/doc.go b/gin/v2/doc.go new file mode 100644 index 0000000..6e76377 --- /dev/null +++ b/gin/v2/doc.go @@ -0,0 +1,20 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//Package v2 (GIN) is a HTTP web framework written in +//Go (Golang) plugin which can be used for integration with Gin http server. +package v2 diff --git a/gin/v2/example_gin_test.go b/gin/v2/example_gin_test.go new file mode 100644 index 0000000..397ca96 --- /dev/null +++ b/gin/v2/example_gin_test.go @@ -0,0 +1,94 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package v2 + +import ( + "fmt" + "log" + "net/http" + "sync" + "time" + + "github.com/SkyAPM/go2sky" + h "github.com/SkyAPM/go2sky/plugins/http" + "github.com/SkyAPM/go2sky/reporter" + "github.com/gin-gonic/gin" +) + +func ExampleMiddleware() { + // Use gRPC reporter for production + re, err := reporter.NewLogReporter() + if err != nil { + log.Fatalf("new reporter error %v \n", err) + } + defer re.Close() + + tracer, err := go2sky.NewTracer("gin-server", go2sky.WithReporter(re)) + if err != nil { + log.Fatalf("create tracer error %v \n", err) + } + + gin.SetMode(gin.ReleaseMode) + r := gin.New() + + //Use go2sky middleware with tracing + r.Use(Middleware(r, tracer)) + + r.GET("/user/:name", func(c *gin.Context) { + name := c.Param("name") + c.String(200, "Hello %s", name) + }) + + go func() { + if err := http.ListenAndServe(":8080", r); err != nil { + panic(err) + } + }() + // Wait for the server to start + time.Sleep(time.Second) + + wg := sync.WaitGroup{} + wg.Add(1) + + go func() { + defer wg.Done() + request(tracer) + }() + wg.Wait() + // Output: +} + +func request(tracer *go2sky.Tracer, _ ...h.ClientOption) { + //NewClient returns an HTTP Client with tracer + client, err := h.NewClient(tracer) + if err != nil { + log.Fatalf("create client error %v \n", err) + } + + request, err := http.NewRequest("GET", fmt.Sprintf("%s/user/gin", "http://127.0.0.1:8080"), nil) + if err != nil { + log.Fatalf("unable to create http request: %+v\n", err) + } + + res, err := client.Do(request) + if err != nil { + log.Fatalf("unable to do http request: %+v\n", err) + } + + _ = res.Body.Close() +} diff --git a/gin/v2/gin.go b/gin/v2/gin.go new file mode 100644 index 0000000..eadc6ec --- /dev/null +++ b/gin/v2/gin.go @@ -0,0 +1,70 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//Package v2 (Gin) is a HTTP web framework written in +//Go (Golang) plugin which can be used for integration with Gin http server. +package v2 + +import ( + "fmt" + "strconv" + "time" + + "github.com/SkyAPM/go2sky" + "github.com/SkyAPM/go2sky/propagation" + v3 "github.com/SkyAPM/go2sky/reporter/grpc/language-agent" + "github.com/gin-gonic/gin" +) + +const componentIDGINHttpServer = 5006 + +//Middleware gin middleware return HandlerFunc with tracing. +func Middleware(engine *gin.Engine, tracer *go2sky.Tracer) gin.HandlerFunc { + if engine == nil || tracer == nil { + return func(c *gin.Context) { + c.Next() + } + } + + return func(c *gin.Context) { + span, ctx, err := tracer.CreateEntrySpan(c.Request.Context(), getOperationName(c), func() (string, error) { + return c.Request.Header.Get(propagation.Header), nil + }) + if err != nil { + c.Next() + return + } + span.SetComponent(componentIDGINHttpServer) + span.Tag(go2sky.TagHTTPMethod, c.Request.Method) + span.Tag(go2sky.TagURL, c.Request.Host+c.Request.URL.Path) + span.SetSpanLayer(v3.SpanLayer_Http) + + c.Request = c.Request.WithContext(ctx) + + c.Next() + + if len(c.Errors) > 0 { + span.Error(time.Now(), c.Errors.String()) + } + span.Tag(go2sky.TagStatusCode, strconv.Itoa(c.Writer.Status())) + span.End() + } +} + +func getOperationName(c *gin.Context) string { + return fmt.Sprintf("/%s%s", c.Request.Method, c.FullPath()) +} diff --git a/gin/v2/go.mod b/gin/v2/go.mod new file mode 100644 index 0000000..2893b95 --- /dev/null +++ b/gin/v2/go.mod @@ -0,0 +1,8 @@ +module github.com/SkyAPM/go2sky-plugins/gin/v2 + +go 1.12 + +require ( + github.com/SkyAPM/go2sky v0.4.0 + github.com/gin-gonic/gin v1.5.0 +) diff --git a/gin/v2/go.sum b/gin/v2/go.sum new file mode 100644 index 0000000..b7fae6f --- /dev/null +++ b/gin/v2/go.sum @@ -0,0 +1,100 @@ +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/SkyAPM/go2sky v0.4.0 h1:kJAdy1a34BgxCK2HYtNO3RuOoEGULFFHJuPlSOGc2YA= +github.com/SkyAPM/go2sky v0.4.0/go.mod h1:9Q2BjSwndQEvLKIfaB80Nm9JWmtScSgIY9u2vVjRL7s= +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/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/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/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= +github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc= +github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do= +github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc= +github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM= +github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM= +github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY= +github.com/go-resty/resty/v2 v2.2.0 h1:vgZ1cdblp8Aw4jZj3ZsKh6yKAlMg3CHMrqFSFFd+jgY= +github.com/go-resty/resty/v2 v2.2.0/go.mod h1:nYW/8rxqQCmI3bPz9Fsmjbr2FBjGuR2Mzt6kDh3zZ7w= +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/mock v1.2.0/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 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8= +github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +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= +github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +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-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-20200222125558-5a598a2470a0 h1:MsuvTghUPjX762sGLnGsxC3HM0B5r83wEtYcYR8/vRs= +golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/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/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-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +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= +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 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +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 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= +gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= +gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc= +gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +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/golangci.yml b/golangci.yml new file mode 100644 index 0000000..b9014cd --- /dev/null +++ b/golangci.yml @@ -0,0 +1,45 @@ +# Licensed to the SkyAPM org under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +linters: + enable: + - deadcode + - errcheck + - goconst + - golint + - ineffassign + - lll + - maligned + - misspell + - structcheck + - unconvert + - varcheck + - govet + - goimports + - prealloc + - unused + - staticcheck + - gosimple + - megacheck + disable: + - interfacer +linters-settings: + lll: + line-length: 160 + goconst: + min-occurrences: 4 + govet: + check-shadowing: true \ No newline at end of file diff --git a/resty/doc.go b/resty/doc.go new file mode 100644 index 0000000..a8a9dbf --- /dev/null +++ b/resty/doc.go @@ -0,0 +1,19 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +// Package resty (go-resty) is a plugin that can be used to trace GO Resty framework. +package resty diff --git a/resty/example_go_resty_test.go b/resty/example_go_resty_test.go new file mode 100644 index 0000000..dc938ae --- /dev/null +++ b/resty/example_go_resty_test.go @@ -0,0 +1,73 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package resty + +import ( + "fmt" + "log" + "net/http" + "net/http/httptest" + "time" + + "github.com/SkyAPM/go2sky" + httpPlugin "github.com/SkyAPM/go2sky/plugins/http" + "github.com/SkyAPM/go2sky/reporter" +) + +func ExampleNewGoResty() { + // Use log reporter for production + r, err := reporter.NewLogReporter() + if err != nil { + log.Fatalf("new reporter error %v \n", err) + } + defer r.Close() + + tracer, err := go2sky.NewTracer("example", go2sky.WithReporter(r)) + if err != nil { + log.Fatalf("create tracer error %v \n", err) + } + + sm, err := httpPlugin.NewServerMiddleware(tracer) + if err != nil { + log.Fatalf("create server middleware error %v \n", err) + } + + // create test server + ts := httptest.NewServer(sm(endFunc())) + defer ts.Close() + + // create resty client + client := NewGoResty(tracer) + resp, err := client.R().Get(fmt.Sprintf("%s/end", ts.URL)) + + if err != nil { + log.Fatalf("unable to do http request: %+v\n", err) + } + + _ = resp.RawResponse.Body.Close() + + time.Sleep(time.Second) + // Output: +} + +func endFunc() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + log.Printf("end func called with method: %s\n", r.Method) + time.Sleep(50 * time.Millisecond) + } +} diff --git a/resty/go.mod b/resty/go.mod new file mode 100644 index 0000000..369de19 --- /dev/null +++ b/resty/go.mod @@ -0,0 +1,9 @@ +module github.com/SkyAPM/go2sky-plugins/resty + +go 1.12 + +require ( + github.com/SkyAPM/go2sky v0.4.0 + github.com/go-resty/resty/v2 v2.2.0 + github.com/gorilla/mux v1.7.4 // indirect +) diff --git a/resty/go.sum b/resty/go.sum new file mode 100644 index 0000000..c9c3e90 --- /dev/null +++ b/resty/go.sum @@ -0,0 +1,79 @@ +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/SkyAPM/go2sky v0.4.0 h1:kJAdy1a34BgxCK2HYtNO3RuOoEGULFFHJuPlSOGc2YA= +github.com/SkyAPM/go2sky v0.4.0/go.mod h1:9Q2BjSwndQEvLKIfaB80Nm9JWmtScSgIY9u2vVjRL7s= +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/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= +github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= +github.com/go-resty/resty/v2 v2.2.0 h1:vgZ1cdblp8Aw4jZj3ZsKh6yKAlMg3CHMrqFSFFd+jgY= +github.com/go-resty/resty/v2 v2.2.0/go.mod h1:nYW/8rxqQCmI3bPz9Fsmjbr2FBjGuR2Mzt6kDh3zZ7w= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +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/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk= +github.com/golang/mock v1.2.0/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 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc= +github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +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= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +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-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-20200222125558-5a598a2470a0 h1:MsuvTghUPjX762sGLnGsxC3HM0B5r83wEtYcYR8/vRs= +golang.org/x/net v0.0.0-20200222125558-5a598a2470a0/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/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-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8= +golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +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= +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 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +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 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= +gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +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/resty/go_resty.go b/resty/go_resty.go new file mode 100644 index 0000000..9985c87 --- /dev/null +++ b/resty/go_resty.go @@ -0,0 +1,36 @@ +// Licensed to SkyAPM org under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. SkyAPM org licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package resty + +import ( + "log" + + "github.com/SkyAPM/go2sky" + "github.com/SkyAPM/go2sky/plugins/http" + "github.com/go-resty/resty/v2" +) + +// NewGoResty returns a resty Client with tracer +func NewGoResty(tracer *go2sky.Tracer, options ...http.ClientOption) *resty.Client { + hc, err := http.NewClient(tracer, options...) + if err != nil { + log.Fatalf("create client error %v \n", err) + } + + return resty.NewWithClient(hc) +} diff --git a/tools/check-license-header.py b/tools/check-license-header.py new file mode 100644 index 0000000..6c394a4 --- /dev/null +++ b/tools/check-license-header.py @@ -0,0 +1,74 @@ +# Licensed to SkyAPM org under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. SkyAPM org licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import os +import sys + +ignored_chars = '//\n \t' + +ignored_paths = [] + +license_header = ' '.join( + [ + line.strip(ignored_chars) for line in """ + // Licensed to SkyAPM org under one or more contributor + // license agreements. See the NOTICE file distributed with + // this work for additional information regarding copyright + // ownership. SkyAPM org licenses this file to you under + // the Apache License, Version 2.0 (the "License"); you may + // not use this file except in compliance with the License. + // You may obtain a copy of the License at + // + // http://www.apache.org/licenses/LICENSE-2.0 + // + // Unless required by applicable law or agreed to in writing, + // software distributed under the License is distributed on an + // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + // KIND, either express or implied. See the License for the + // specific language governing permissions and limitations + // under the License. + """.splitlines() + ] +).strip(ignored_chars) + + +def walk_through_dir(d) -> bool: + checked = True + for root, sub_dirs, files in os.walk(d): + ignored = False + for ignored_path in ignored_paths: + if root.__contains__(ignored_path): + ignored = True + break + if ignored: + continue + + for filename in files: + if not filename.endswith(".go"): + continue + + file_path = os.path.join(root, filename) + with open(file_path, 'r') as f: + header = ' '.join([line.strip(ignored_chars) for line in f.readlines() if line.startswith('//')]).strip() + print('%s license header in file: %s' % ('✅' if header.startswith(license_header) else '❌', file_path)) + checked &= header.startswith(license_header) + return checked + + +if __name__ == "__main__": + if not walk_through_dir("./"): + sys.exit(1) diff --git a/tools/deps.sh b/tools/deps.sh new file mode 100755 index 0000000..ef44991 --- /dev/null +++ b/tools/deps.sh @@ -0,0 +1,25 @@ +#!/bin/bash -e +# Licensed to SkyAPM org under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. SkyAPM org licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +PKGS="" +for d in $(find * -name 'go.mod'); do + pushd $(dirname $d) + echo "🟢 download `sed -n 1p go.mod|cut -d ' ' -f2`" + go mod download + popd +done \ No newline at end of file diff --git a/tools/lint.sh b/tools/lint.sh new file mode 100755 index 0000000..5c3ed62 --- /dev/null +++ b/tools/lint.sh @@ -0,0 +1,55 @@ +#!/bin/bash -e +# Licensed to SkyAPM org under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. SkyAPM org licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +PLUGINS_HOME=$(cd "$(dirname "$0")";cd ..;pwd) +LINTER=${PLUGINS_HOME}/bin/golangci-lint +LINTER_CONFIG=${PLUGINS_HOME}/golangci.yml + +PKGS="" + +function lint() { + for d in $(find * -name 'go.mod'); do + pushd $(dirname $d) + echo "🟢 golangci lint `sed -n 1p go.mod|cut -d ' ' -f2`" + ${LINTER} run --timeout=10m --exclude-use-default=false --config=${LINTER_CONFIG} + popd + done +} + +function fix() { + for d in $(find * -name 'go.mod'); do + pushd $(dirname $d) + echo "🟢 golangci fix `sed -n 1p go.mod|cut -d ' ' -f2`" + ${LINTER} run -v --fix ./... + popd + done +} + +case $1 in + fix) + fix + ;; + *) + lint + ;; +esac + + + + + diff --git a/tools/test.sh b/tools/test.sh new file mode 100755 index 0000000..f2c7877 --- /dev/null +++ b/tools/test.sh @@ -0,0 +1,27 @@ +#!/bin/bash -e +# Licensed to SkyAPM org under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. SkyAPM org licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +mod="github.com/SkyAPM/go2sky-plugins" +PKGS="" +for d in $(find * -name 'go.mod'); do + pushd $(dirname $d) >/dev/null + echo "🟢 testing `sed -n 1p go.mod|cut -d ' ' -f2`" + go mod download + go test -v ./... + popd >/dev/null +done \ No newline at end of file From a2dd4bc775a78cb413b9703940d400cc86a39d80 Mon Sep 17 00:00:00 2001 From: zhangwei Date: Mon, 6 Jul 2020 21:58:03 +0800 Subject: [PATCH 2/7] add more doc --- .github/workflows/ci.yaml | 10 ++--- Makefile | 10 +++-- README.md | 8 +++- gear/README.md | 42 +++++++++++++++++ gin/README.md | 4 ++ gin/v0/README.md | 40 +++++++++++++++++ gin/{v1 => v0}/doc.go | 4 +- gin/{v1 => v0}/example_gin_test.go | 2 +- gin/{v1 => v0}/gin.go | 4 +- gin/{v1 => v0}/go.mod | 2 +- gin/{v1 => v0}/go.sum | 0 gin/v2/README.md | 43 ++++++++++++++++++ http/README.md | 1 + resty/README.md | 1 + tools/deps.sh | 25 ----------- tools/lint.sh | 55 ----------------------- tools/test.sh | 72 ++++++++++++++++++++++++++---- 17 files changed, 217 insertions(+), 106 deletions(-) create mode 100644 gear/README.md create mode 100644 gin/README.md create mode 100644 gin/v0/README.md rename gin/{v1 => v0}/doc.go (92%) rename gin/{v1 => v0}/example_gin_test.go (99%) rename gin/{v1 => v0}/gin.go (97%) rename gin/{v1 => v0}/go.mod (65%) rename gin/{v1 => v0}/go.sum (100%) create mode 100644 gin/v2/README.md create mode 100644 http/README.md create mode 100644 resty/README.md delete mode 100755 tools/deps.sh delete mode 100755 tools/lint.sh diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 2d51264..e901aaf 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -27,15 +27,11 @@ jobs: build: name: Build & Test runs-on: ubuntu-latest - strategy: - matrix: - go-version: [1.12, 1.13, 1.14] - fail-fast: false steps: - - name: Set up ${{ matrix.go-version }} - uses: actions/setup-go@v1 + - name: Set up 1.12 + uses: actions/setup-go@v0 with: - go-version: ${{ matrix.go-version }} + go-version: 1.12 id: go - name: Check out code into the Go module directory diff --git a/Makefile b/Makefile index 9080a54..2872fb1 100644 --- a/Makefile +++ b/Makefile @@ -15,13 +15,15 @@ # limitations under the License. # +TEST_SHELL="./tools/test.sh" + .PHONY: test test: - ./tools/test.sh + ${TEST_SHELL} test .PHONY: deps deps: - ./tools/deps.sh + ${TEST_SHELL} deps LINTER := bin/golangci-lint $(LINTER): @@ -29,11 +31,11 @@ $(LINTER): .PHONY: lint lint: $(LINTER) - ./tools/lint.sh lint + ${TEST_SHELL} lint .PHONY: fix fix: $(LINTER) - ./tools/lint.sh fix + ${TEST_SHELL} fix .PHONY: license license: diff --git a/README.md b/README.md index c93971c..efa9df9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ # go2sky-plugins - [![Build](https://github.com/SkyAPM/go2sky-plugins/workflows/Build/badge.svg?branch=master)](https://github.com/SkyAPM/go2sky-plugins/actions?query=branch%3Amaster+event%3Apush+workflow%3ABuild) The plugins of go2sky + +### Plugin Summary + +1. [HTTP Server & Client](http/README.md) +1. [Gin](gin/README.md) +1. [Gear](gear/README.md) +1. [GO-Resty](resty/README.md) \ No newline at end of file diff --git a/gear/README.md b/gear/README.md new file mode 100644 index 0000000..7256fd6 --- /dev/null +++ b/gear/README.md @@ -0,0 +1,42 @@ +# Go2sky with gear (v1.21.2) + +## Installation + +```bash +go get -u github.com/SkyAPM/go2sky-plugins/gear +``` + +## Usage +```go +package main + +import ( + "log" + + "github.com/SkyAPM/go2sky" + gearplugin "github.com/SkyAPM/go2sky-plugins/gear" + "github.com/SkyAPM/go2sky/reporter" + "github.com/teambition/gear" +) + +func main() { + re, err := reporter.NewLogReporter() + if err != nil { + log.Fatalf("new reporter error %v \n", err) + } + + defer re.Close() + + tracer, err := go2sky.NewTracer("gear", go2sky.WithReporter(re)) + if err != nil { + log.Fatalf("create tracer error %v \n", err) + } + + app := gear.New() + + //Use go2sky middleware with tracing + app.Use(gearplugin.Middleware(tracer)) + + // do something +} +``` \ No newline at end of file diff --git a/gin/README.md b/gin/README.md new file mode 100644 index 0000000..25d9b01 --- /dev/null +++ b/gin/README.md @@ -0,0 +1,4 @@ +# Go2sky with gin + +1. [v0](v0/README.md) +1. [v2](v2/README.md) \ No newline at end of file diff --git a/gin/v0/README.md b/gin/v0/README.md new file mode 100644 index 0000000..7ce2ac8 --- /dev/null +++ b/gin/v0/README.md @@ -0,0 +1,40 @@ +# Go2sky with gin(1.3.0) + +## Installation + +```bash +go get -u github.com/SkyAPM/go2sky-plugins/gin +``` + +## Usage +```go +package main + +import ( + "log" + + "github.com/SkyAPM/go2sky" + gearplugin "github.com/SkyAPM/go2sky-plugins/gear" + "github.com/SkyAPM/go2sky/reporter" + "github.com/teambition/gear" +) + +func main() { + re, err := reporter.NewLogReporter() + if err != nil { + log.Fatalf("new reporter error %v \n", err) + } + + defer re.Close() + + tracer, err := go2sky.NewTracer("gear", go2sky.WithReporter(re)) + if err != nil { + log.Fatalf("create tracer error %v \n", err) + } + + app := gear.New() + app.Use(gearplugin.Middleware(tracer)) + + // do something +} +``` \ No newline at end of file diff --git a/gin/v1/doc.go b/gin/v0/doc.go similarity index 92% rename from gin/v1/doc.go rename to gin/v0/doc.go index 2e8576e..546a1de 100644 --- a/gin/v1/doc.go +++ b/gin/v0/doc.go @@ -15,6 +15,6 @@ // specific language governing permissions and limitations // under the License. -//Package v1 (GIN) is a HTTP web framework written in +//Package v0 (GIN) is a HTTP web framework written in //Go (Golang) plugin which can be used for integration with Gin http server. -package v1 +package v0 diff --git a/gin/v1/example_gin_test.go b/gin/v0/example_gin_test.go similarity index 99% rename from gin/v1/example_gin_test.go rename to gin/v0/example_gin_test.go index d14fdb9..1a23866 100644 --- a/gin/v1/example_gin_test.go +++ b/gin/v0/example_gin_test.go @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -package v1 +package v0 import ( "fmt" diff --git a/gin/v1/gin.go b/gin/v0/gin.go similarity index 97% rename from gin/v1/gin.go rename to gin/v0/gin.go index 23d0ff7..3bbd07a 100644 --- a/gin/v1/gin.go +++ b/gin/v0/gin.go @@ -15,9 +15,9 @@ // specific language governing permissions and limitations // under the License. -//Package v1 is a HTTP web framework written in +//Package v0 is a HTTP web framework written in //Go (Golang) plugin which can be used for integration with Gin http server. -package v1 +package v0 import ( "fmt" diff --git a/gin/v1/go.mod b/gin/v0/go.mod similarity index 65% rename from gin/v1/go.mod rename to gin/v0/go.mod index f137026..9eaf537 100644 --- a/gin/v1/go.mod +++ b/gin/v0/go.mod @@ -1,4 +1,4 @@ -module github.com/SkyAPM/go2sky-plugins/gin/v1 +module github.com/SkyAPM/go2sky-plugins/gin/v0 go 1.12 diff --git a/gin/v1/go.sum b/gin/v0/go.sum similarity index 100% rename from gin/v1/go.sum rename to gin/v0/go.sum diff --git a/gin/v2/README.md b/gin/v2/README.md new file mode 100644 index 0000000..4e7fbe8 --- /dev/null +++ b/gin/v2/README.md @@ -0,0 +1,43 @@ +# Go2sky with gin (v1.5.0) + +## Installation + +```bash +go get -u github.com/SkyAPM/go2sky-plugins/gin/v2 +``` + +## Usage +```go +package main + +import ( + "log" + + "github.com/SkyAPM/go2sky" + v2 "github.com/SkyAPM/go2sky-plugins/gin/v2" + "github.com/SkyAPM/go2sky/reporter" + "github.com/gin-gonic/gin" +) + +func main() { + // Use gRPC reporter for production + re, err := reporter.NewLogReporter() + if err != nil { + log.Fatalf("new reporter error %v \n", err) + } + defer re.Close() + + tracer, err := go2sky.NewTracer("gin-server", go2sky.WithReporter(re)) + if err != nil { + log.Fatalf("create tracer error %v \n", err) + } + + gin.SetMode(gin.ReleaseMode) + r := gin.New() + + //Use go2sky middleware with tracing + r.Use(v2.Middleware(r, tracer)) + + // do something +} +``` \ No newline at end of file diff --git a/http/README.md b/http/README.md new file mode 100644 index 0000000..a446903 --- /dev/null +++ b/http/README.md @@ -0,0 +1 @@ +# Go2sky with net/http \ No newline at end of file diff --git a/resty/README.md b/resty/README.md new file mode 100644 index 0000000..143a5cd --- /dev/null +++ b/resty/README.md @@ -0,0 +1 @@ +# Go2sky with go-resty \ No newline at end of file diff --git a/tools/deps.sh b/tools/deps.sh deleted file mode 100755 index ef44991..0000000 --- a/tools/deps.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/bin/bash -e -# Licensed to SkyAPM org under one or more contributor -# license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright -# ownership. SkyAPM org licenses this file to you under -# the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -PKGS="" -for d in $(find * -name 'go.mod'); do - pushd $(dirname $d) - echo "🟢 download `sed -n 1p go.mod|cut -d ' ' -f2`" - go mod download - popd -done \ No newline at end of file diff --git a/tools/lint.sh b/tools/lint.sh deleted file mode 100755 index 5c3ed62..0000000 --- a/tools/lint.sh +++ /dev/null @@ -1,55 +0,0 @@ -#!/bin/bash -e -# Licensed to SkyAPM org under one or more contributor -# license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright -# ownership. SkyAPM org licenses this file to you under -# the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -PLUGINS_HOME=$(cd "$(dirname "$0")";cd ..;pwd) -LINTER=${PLUGINS_HOME}/bin/golangci-lint -LINTER_CONFIG=${PLUGINS_HOME}/golangci.yml - -PKGS="" - -function lint() { - for d in $(find * -name 'go.mod'); do - pushd $(dirname $d) - echo "🟢 golangci lint `sed -n 1p go.mod|cut -d ' ' -f2`" - ${LINTER} run --timeout=10m --exclude-use-default=false --config=${LINTER_CONFIG} - popd - done -} - -function fix() { - for d in $(find * -name 'go.mod'); do - pushd $(dirname $d) - echo "🟢 golangci fix `sed -n 1p go.mod|cut -d ' ' -f2`" - ${LINTER} run -v --fix ./... - popd - done -} - -case $1 in - fix) - fix - ;; - *) - lint - ;; -esac - - - - - diff --git a/tools/test.sh b/tools/test.sh index f2c7877..ce8a1a7 100755 --- a/tools/test.sh +++ b/tools/test.sh @@ -16,12 +16,68 @@ # specific language governing permissions and limitations # under the License. -mod="github.com/SkyAPM/go2sky-plugins" +GO111MODULE=on +PLUGINS_HOME=$(cd "$(dirname "$0")";cd ..;pwd) PKGS="" -for d in $(find * -name 'go.mod'); do - pushd $(dirname $d) >/dev/null - echo "🟢 testing `sed -n 1p go.mod|cut -d ' ' -f2`" - go mod download - go test -v ./... - popd >/dev/null -done \ No newline at end of file + +function test() { + for d in $(find * -name 'go.mod'); do + pushd $(dirname $d) >/dev/null + echo "🟢 testing `sed -n 1p go.mod|cut -d ' ' -f2`" + go mod download + go test -v ./... + popd >/dev/null + done +} + +function deps() { + for d in $(find * -name 'go.mod'); do + pushd $(dirname $d) + echo "🟢 download `sed -n 1p go.mod|cut -d ' ' -f2`" + go mod download + popd + done +} + +function lint() { + LINTER=${PLUGINS_HOME}/bin/golangci-lint + LINTER_CONFIG=${PLUGINS_HOME}/golangci.yml + for d in $(find * -name 'go.mod'); do + pushd $(dirname $d) + echo "🟢 golangci lint `sed -n 1p go.mod|cut -d ' ' -f2`" + ${LINTER} run --timeout=10m --exclude-use-default=false --config=${LINTER_CONFIG} + popd + done +} + +function fix() { + LINTER=${PLUGINS_HOME}/bin/golangci-lint + for d in $(find * -name 'go.mod'); do + pushd $(dirname $d) + echo "🟢 golangci fix `sed -n 1p go.mod|cut -d ' ' -f2`" + ${LINTER} run -v --fix ./... + popd + done +} + +function print_help(){ + echo "options: deps, test, lint, fix" +} + +case $1 in + deps) + deps + ;; + test) + test + ;; + lint) + lint + ;; + fix) + fix + ;; + *) + print_help + ;; +esac \ No newline at end of file From 7fd037ad47cd437f73be754efdf2843627af62b9 Mon Sep 17 00:00:00 2001 From: zhangwei Date: Mon, 6 Jul 2020 22:03:29 +0800 Subject: [PATCH 3/7] fix invalid: malformed module path --- README.md | 8 +++--- gin/README.md | 8 ++++-- gin/v0/README.md | 40 -------------------------- gin/v0/go.mod | 8 ------ gin/v2/gin.go | 43 ++++++++++++++++++++++++---- gin/v2/go.mod | 2 +- gin/v2/go.sum | 44 ++++++++++++----------------- gin/v3/README.md | 43 ++++++++++++++++++++++++++++ gin/{v0 => v3}/doc.go | 4 +-- gin/{v0 => v3}/example_gin_test.go | 2 +- gin/{v0 => v3}/gin.go | 45 +++++------------------------- gin/v3/go.mod | 8 ++++++ gin/{v0 => v3}/go.sum | 44 +++++++++++++++++------------ 13 files changed, 153 insertions(+), 146 deletions(-) delete mode 100644 gin/v0/README.md delete mode 100644 gin/v0/go.mod create mode 100644 gin/v3/README.md rename gin/{v0 => v3}/doc.go (92%) rename gin/{v0 => v3}/example_gin_test.go (99%) rename gin/{v0 => v3}/gin.go (68%) create mode 100644 gin/v3/go.mod rename gin/{v0 => v3}/go.sum (75%) diff --git a/README.md b/README.md index efa9df9..0e4615a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ The plugins of go2sky ### Plugin Summary -1. [HTTP Server & Client](http/README.md) -1. [Gin](gin/README.md) -1. [Gear](gear/README.md) -1. [GO-Resty](resty/README.md) \ No newline at end of file +1. [http server & client](http/README.md) +1. [gin](gin/README.md) +1. [gear](gear/README.md) +1. [go-resty](resty/README.md) \ No newline at end of file diff --git a/gin/README.md b/gin/README.md index 25d9b01..e9d7290 100644 --- a/gin/README.md +++ b/gin/README.md @@ -1,4 +1,8 @@ # Go2sky with gin -1. [v0](v0/README.md) -1. [v2](v2/README.md) \ No newline at end of file +1. [v2](v2/README.md) +1. [v3](v3/README.md) + + +## FAQ + diff --git a/gin/v0/README.md b/gin/v0/README.md deleted file mode 100644 index 7ce2ac8..0000000 --- a/gin/v0/README.md +++ /dev/null @@ -1,40 +0,0 @@ -# Go2sky with gin(1.3.0) - -## Installation - -```bash -go get -u github.com/SkyAPM/go2sky-plugins/gin -``` - -## Usage -```go -package main - -import ( - "log" - - "github.com/SkyAPM/go2sky" - gearplugin "github.com/SkyAPM/go2sky-plugins/gear" - "github.com/SkyAPM/go2sky/reporter" - "github.com/teambition/gear" -) - -func main() { - re, err := reporter.NewLogReporter() - if err != nil { - log.Fatalf("new reporter error %v \n", err) - } - - defer re.Close() - - tracer, err := go2sky.NewTracer("gear", go2sky.WithReporter(re)) - if err != nil { - log.Fatalf("create tracer error %v \n", err) - } - - app := gear.New() - app.Use(gearplugin.Middleware(tracer)) - - // do something -} -``` \ No newline at end of file diff --git a/gin/v0/go.mod b/gin/v0/go.mod deleted file mode 100644 index 9eaf537..0000000 --- a/gin/v0/go.mod +++ /dev/null @@ -1,8 +0,0 @@ -module github.com/SkyAPM/go2sky-plugins/gin/v0 - -go 1.12 - -require ( - github.com/SkyAPM/go2sky v0.4.0 - github.com/gin-gonic/gin v1.3.0 -) diff --git a/gin/v2/gin.go b/gin/v2/gin.go index eadc6ec..3c18855 100644 --- a/gin/v2/gin.go +++ b/gin/v2/gin.go @@ -15,13 +15,14 @@ // specific language governing permissions and limitations // under the License. -//Package v2 (Gin) is a HTTP web framework written in +//Package v2 is a HTTP web framework written in //Go (Golang) plugin which can be used for integration with Gin http server. package v2 import ( "fmt" "strconv" + "sync" "time" "github.com/SkyAPM/go2sky" @@ -32,6 +33,15 @@ import ( const componentIDGINHttpServer = 5006 +type routeInfo struct { + operationName string +} + +type middleware struct { + routeMap map[string]map[string]routeInfo + routeMapOnce sync.Once +} + //Middleware gin middleware return HandlerFunc with tracing. func Middleware(engine *gin.Engine, tracer *go2sky.Tracer) gin.HandlerFunc { if engine == nil || tracer == nil { @@ -40,8 +50,33 @@ func Middleware(engine *gin.Engine, tracer *go2sky.Tracer) gin.HandlerFunc { } } + m := new(middleware) + return func(c *gin.Context) { - span, ctx, err := tracer.CreateEntrySpan(c.Request.Context(), getOperationName(c), func() (string, error) { + m.routeMapOnce.Do(func() { + routes := engine.Routes() + rm := make(map[string]map[string]routeInfo) + for _, r := range routes { + mm := rm[r.Method] + if mm == nil { + mm = make(map[string]routeInfo) + rm[r.Method] = mm + } + mm[r.Handler] = routeInfo{ + operationName: fmt.Sprintf("/%s%s", r.Method, r.Path), + } + } + m.routeMap = rm + }) + var operationName string + handlerName := c.HandlerName() + if routeInfo, ok := m.routeMap[c.Request.Method][handlerName]; ok { + operationName = routeInfo.operationName + } + if operationName == "" { + operationName = c.Request.Method + } + span, ctx, err := tracer.CreateEntrySpan(c.Request.Context(), operationName, func() (string, error) { return c.Request.Header.Get(propagation.Header), nil }) if err != nil { @@ -64,7 +99,3 @@ func Middleware(engine *gin.Engine, tracer *go2sky.Tracer) gin.HandlerFunc { span.End() } } - -func getOperationName(c *gin.Context) string { - return fmt.Sprintf("/%s%s", c.Request.Method, c.FullPath()) -} diff --git a/gin/v2/go.mod b/gin/v2/go.mod index 2893b95..8a322ac 100644 --- a/gin/v2/go.mod +++ b/gin/v2/go.mod @@ -4,5 +4,5 @@ go 1.12 require ( github.com/SkyAPM/go2sky v0.4.0 - github.com/gin-gonic/gin v1.5.0 + github.com/gin-gonic/gin v1.3.0 ) diff --git a/gin/v2/go.sum b/gin/v2/go.sum index b7fae6f..d798b20 100644 --- a/gin/v2/go.sum +++ b/gin/v2/go.sum @@ -4,57 +4,48 @@ github.com/SkyAPM/go2sky v0.4.0 h1:kJAdy1a34BgxCK2HYtNO3RuOoEGULFFHJuPlSOGc2YA= github.com/SkyAPM/go2sky v0.4.0/go.mod h1:9Q2BjSwndQEvLKIfaB80Nm9JWmtScSgIY9u2vVjRL7s= 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/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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/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/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= -github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= -github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs= github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= -github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc= -github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do= -github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc= -github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM= -github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM= -github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY= github.com/go-resty/resty/v2 v2.2.0 h1:vgZ1cdblp8Aw4jZj3ZsKh6yKAlMg3CHMrqFSFFd+jgY= github.com/go-resty/resty/v2 v2.2.0/go.mod h1:nYW/8rxqQCmI3bPz9Fsmjbr2FBjGuR2Mzt6kDh3zZ7w= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= 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/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk= github.com/golang/mock v1.2.0/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 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= -github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/mux v1.7.1 h1:Dw4jY2nghMMRsh1ol8dv1axHkDwMQK2DHerMNJsIpJU= github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= +github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= -github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8= -github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw= +github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc= github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= -github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -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 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= 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= -github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= -github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 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= @@ -72,8 +63,8 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/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-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= 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= @@ -89,11 +80,12 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +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/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= +gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= -gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc= -gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/gin/v3/README.md b/gin/v3/README.md new file mode 100644 index 0000000..3ee4ded --- /dev/null +++ b/gin/v3/README.md @@ -0,0 +1,43 @@ +# Go2sky with gin (v1.5.0) + +## Installation + +```bash +go get -u github.com/SkyAPM/go2sky-plugins/gin/v3 +``` + +## Usage +```go +package main + +import ( + "log" + + "github.com/SkyAPM/go2sky" + v3 "github.com/SkyAPM/go2sky-plugins/gin/v3" + "github.com/SkyAPM/go2sky/reporter" + "github.com/gin-gonic/gin" +) + +func main() { + // Use gRPC reporter for production + re, err := reporter.NewLogReporter() + if err != nil { + log.Fatalf("new reporter error %v \n", err) + } + defer re.Close() + + tracer, err := go2sky.NewTracer("gin-server", go2sky.WithReporter(re)) + if err != nil { + log.Fatalf("create tracer error %v \n", err) + } + + gin.SetMode(gin.ReleaseMode) + r := gin.New() + + //Use go2sky middleware with tracing + r.Use(v3.Middleware(r, tracer)) + + // do something +} +``` \ No newline at end of file diff --git a/gin/v0/doc.go b/gin/v3/doc.go similarity index 92% rename from gin/v0/doc.go rename to gin/v3/doc.go index 546a1de..575d6d8 100644 --- a/gin/v0/doc.go +++ b/gin/v3/doc.go @@ -15,6 +15,6 @@ // specific language governing permissions and limitations // under the License. -//Package v0 (GIN) is a HTTP web framework written in +//Package v3 (GIN) is a HTTP web framework written in //Go (Golang) plugin which can be used for integration with Gin http server. -package v0 +package v3 diff --git a/gin/v0/example_gin_test.go b/gin/v3/example_gin_test.go similarity index 99% rename from gin/v0/example_gin_test.go rename to gin/v3/example_gin_test.go index 1a23866..abb1c38 100644 --- a/gin/v0/example_gin_test.go +++ b/gin/v3/example_gin_test.go @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -package v0 +package v3 import ( "fmt" diff --git a/gin/v0/gin.go b/gin/v3/gin.go similarity index 68% rename from gin/v0/gin.go rename to gin/v3/gin.go index 3bbd07a..01d90c8 100644 --- a/gin/v0/gin.go +++ b/gin/v3/gin.go @@ -15,14 +15,13 @@ // specific language governing permissions and limitations // under the License. -//Package v0 is a HTTP web framework written in +//Package v3 (Gin) is a HTTP web framework written in //Go (Golang) plugin which can be used for integration with Gin http server. -package v0 +package v3 import ( "fmt" "strconv" - "sync" "time" "github.com/SkyAPM/go2sky" @@ -33,15 +32,6 @@ import ( const componentIDGINHttpServer = 5006 -type routeInfo struct { - operationName string -} - -type middleware struct { - routeMap map[string]map[string]routeInfo - routeMapOnce sync.Once -} - //Middleware gin middleware return HandlerFunc with tracing. func Middleware(engine *gin.Engine, tracer *go2sky.Tracer) gin.HandlerFunc { if engine == nil || tracer == nil { @@ -50,33 +40,8 @@ func Middleware(engine *gin.Engine, tracer *go2sky.Tracer) gin.HandlerFunc { } } - m := new(middleware) - return func(c *gin.Context) { - m.routeMapOnce.Do(func() { - routes := engine.Routes() - rm := make(map[string]map[string]routeInfo) - for _, r := range routes { - mm := rm[r.Method] - if mm == nil { - mm = make(map[string]routeInfo) - rm[r.Method] = mm - } - mm[r.Handler] = routeInfo{ - operationName: fmt.Sprintf("/%s%s", r.Method, r.Path), - } - } - m.routeMap = rm - }) - var operationName string - handlerName := c.HandlerName() - if routeInfo, ok := m.routeMap[c.Request.Method][handlerName]; ok { - operationName = routeInfo.operationName - } - if operationName == "" { - operationName = c.Request.Method - } - span, ctx, err := tracer.CreateEntrySpan(c.Request.Context(), operationName, func() (string, error) { + span, ctx, err := tracer.CreateEntrySpan(c.Request.Context(), getOperationName(c), func() (string, error) { return c.Request.Header.Get(propagation.Header), nil }) if err != nil { @@ -99,3 +64,7 @@ func Middleware(engine *gin.Engine, tracer *go2sky.Tracer) gin.HandlerFunc { span.End() } } + +func getOperationName(c *gin.Context) string { + return fmt.Sprintf("/%s%s", c.Request.Method, c.FullPath()) +} diff --git a/gin/v3/go.mod b/gin/v3/go.mod new file mode 100644 index 0000000..5b4bca2 --- /dev/null +++ b/gin/v3/go.mod @@ -0,0 +1,8 @@ +module github.com/SkyAPM/go2sky-plugins/gin/v3 + +go 1.12 + +require ( + github.com/SkyAPM/go2sky v0.4.0 + github.com/gin-gonic/gin v1.5.0 +) diff --git a/gin/v0/go.sum b/gin/v3/go.sum similarity index 75% rename from gin/v0/go.sum rename to gin/v3/go.sum index d798b20..b7fae6f 100644 --- a/gin/v0/go.sum +++ b/gin/v3/go.sum @@ -4,48 +4,57 @@ github.com/SkyAPM/go2sky v0.4.0 h1:kJAdy1a34BgxCK2HYtNO3RuOoEGULFFHJuPlSOGc2YA= github.com/SkyAPM/go2sky v0.4.0/go.mod h1:9Q2BjSwndQEvLKIfaB80Nm9JWmtScSgIY9u2vVjRL7s= 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/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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/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/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g= github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s= -github.com/gin-gonic/gin v1.3.0 h1:kCmZyPklC0gVdL728E6Aj20uYBJV93nj/TkwBTKhFbs= +github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= +github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc= +github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do= +github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc= +github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM= +github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM= +github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY= github.com/go-resty/resty/v2 v2.2.0 h1:vgZ1cdblp8Aw4jZj3ZsKh6yKAlMg3CHMrqFSFFd+jgY= github.com/go-resty/resty/v2 v2.2.0/go.mod h1:nYW/8rxqQCmI3bPz9Fsmjbr2FBjGuR2Mzt6kDh3zZ7w= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= 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/mock v1.2.0 h1:28o5sBqPkBsMGnC6b4MvE2TzSr5/AT4c/1fLqVGIwlk= github.com/golang/mock v1.2.0/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 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/gorilla/mux v1.7.1 h1:Dw4jY2nghMMRsh1ol8dv1axHkDwMQK2DHerMNJsIpJU= github.com/gorilla/mux v1.7.1/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= -github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= -github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc= +github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8= +github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw= github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= +github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw= 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= +github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 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= @@ -63,8 +72,8 @@ golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/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-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= 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= @@ -80,12 +89,11 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg= google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= -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/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= -gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= +gopkg.in/go-playground/validator.v9 v9.29.1 h1:SvGtYmN60a5CVKTOzMSyfzWDeZRxRuGvRQyEAKbw1xc= +gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWdVbfP1avr/N00E2vyQ= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= From 8d36019d3fac67ffc6ad4c5f8cdb4e943573ae58 Mon Sep 17 00:00:00 2001 From: zhangwei Date: Mon, 6 Jul 2020 22:39:38 +0800 Subject: [PATCH 4/7] add plugin doc --- .github/workflows/ci.yaml | 5 +- README.md | 2 +- gear/README.md | 4 +- gin/v2/README.md | 6 +- gin/v3/README.md | 6 +- go_resty/README.md | 40 ++++++++++ {resty => go_resty}/doc.go | 4 +- {resty => go_resty}/example_go_resty_test.go | 4 +- {resty => go_resty}/go.mod | 0 {resty => go_resty}/go.sum | 0 {resty => go_resty}/go_resty.go | 4 +- http/README.md | 79 +++++++++++++++++++- resty/README.md | 1 - 13 files changed, 138 insertions(+), 17 deletions(-) create mode 100644 go_resty/README.md rename {resty => go_resty}/doc.go (88%) rename {resty => go_resty}/example_go_resty_test.go (97%) rename {resty => go_resty}/go.mod (100%) rename {resty => go_resty}/go.sum (100%) rename {resty => go_resty}/go_resty.go (94%) delete mode 100644 resty/README.md diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index e901aaf..4f9a57e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -28,12 +28,11 @@ jobs: name: Build & Test runs-on: ubuntu-latest steps: - - name: Set up 1.12 - uses: actions/setup-go@v0 + - name: Set up Go 1.12 + uses: actions/setup-go@v1 with: go-version: 1.12 id: go - - name: Check out code into the Go module directory uses: actions/checkout@v2 diff --git a/README.md b/README.md index 0e4615a..584825b 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,4 @@ The plugins of go2sky 1. [http server & client](http/README.md) 1. [gin](gin/README.md) 1. [gear](gear/README.md) -1. [go-resty](resty/README.md) \ No newline at end of file +1. [go-resty](go_resty/README.md) \ No newline at end of file diff --git a/gear/README.md b/gear/README.md index 7256fd6..96dd043 100644 --- a/gear/README.md +++ b/gear/README.md @@ -39,4 +39,6 @@ func main() { // do something } -``` \ No newline at end of file +``` + +[See more](example_gear_test.go). \ No newline at end of file diff --git a/gin/v2/README.md b/gin/v2/README.md index 4e7fbe8..d59ff93 100644 --- a/gin/v2/README.md +++ b/gin/v2/README.md @@ -37,7 +37,9 @@ func main() { //Use go2sky middleware with tracing r.Use(v2.Middleware(r, tracer)) - + // do something } -``` \ No newline at end of file +``` + +[See more](example_gin_test.go). \ No newline at end of file diff --git a/gin/v3/README.md b/gin/v3/README.md index 3ee4ded..9fd2890 100644 --- a/gin/v3/README.md +++ b/gin/v3/README.md @@ -37,7 +37,9 @@ func main() { //Use go2sky middleware with tracing r.Use(v3.Middleware(r, tracer)) - + // do something } -``` \ No newline at end of file +``` + +[See more](example_gin_test.go). \ No newline at end of file diff --git a/go_resty/README.md b/go_resty/README.md new file mode 100644 index 0000000..97a2321 --- /dev/null +++ b/go_resty/README.md @@ -0,0 +1,40 @@ +# Go2sky with go-resty(v2.2.0) + +## Installation + +```bash +go get -u github.com/SkyAPM/go2sky-plugins/go_resty +``` + +## Usage +```go +package main + +import ( + "log" + + "github.com/SkyAPM/go2sky" + "github.com/SkyAPM/go2sky-plugins/go_resty" + "github.com/SkyAPM/go2sky/reporter" +) + +func main() { + // Use gRPC reporter for production + re, err := reporter.NewLogReporter() + if err != nil { + log.Fatalf("new reporter error %v \n", err) + } + defer re.Close() + + tracer, err := go2sky.NewTracer("gin-server", go2sky.WithReporter(re)) + if err != nil { + log.Fatalf("create tracer error %v \n", err) + } + + // create go_resty client + client := go_resty.NewGoResty(tracer) + // do something +} +``` + +[See more](example_go_resty_test.go) \ No newline at end of file diff --git a/resty/doc.go b/go_resty/doc.go similarity index 88% rename from resty/doc.go rename to go_resty/doc.go index a8a9dbf..122b19e 100644 --- a/resty/doc.go +++ b/go_resty/doc.go @@ -15,5 +15,5 @@ // specific language governing permissions and limitations // under the License. -// Package resty (go-resty) is a plugin that can be used to trace GO Resty framework. -package resty +// Package go_resty is a plugin that can be used to trace GO Resty framework. +package go_resty diff --git a/resty/example_go_resty_test.go b/go_resty/example_go_resty_test.go similarity index 97% rename from resty/example_go_resty_test.go rename to go_resty/example_go_resty_test.go index dc938ae..4921a35 100644 --- a/resty/example_go_resty_test.go +++ b/go_resty/example_go_resty_test.go @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -package resty +package go_resty import ( "fmt" @@ -51,7 +51,7 @@ func ExampleNewGoResty() { ts := httptest.NewServer(sm(endFunc())) defer ts.Close() - // create resty client + // create go_resty client client := NewGoResty(tracer) resp, err := client.R().Get(fmt.Sprintf("%s/end", ts.URL)) diff --git a/resty/go.mod b/go_resty/go.mod similarity index 100% rename from resty/go.mod rename to go_resty/go.mod diff --git a/resty/go.sum b/go_resty/go.sum similarity index 100% rename from resty/go.sum rename to go_resty/go.sum diff --git a/resty/go_resty.go b/go_resty/go_resty.go similarity index 94% rename from resty/go_resty.go rename to go_resty/go_resty.go index 9985c87..f9161b9 100644 --- a/resty/go_resty.go +++ b/go_resty/go_resty.go @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -package resty +package go_resty import ( "log" @@ -25,7 +25,7 @@ import ( "github.com/go-resty/resty/v2" ) -// NewGoResty returns a resty Client with tracer +// NewGoResty returns a go_resty Client with tracer func NewGoResty(tracer *go2sky.Tracer, options ...http.ClientOption) *resty.Client { hc, err := http.NewClient(tracer, options...) if err != nil { diff --git a/http/README.md b/http/README.md index a446903..956d88b 100644 --- a/http/README.md +++ b/http/README.md @@ -1 +1,78 @@ -# Go2sky with net/http \ No newline at end of file +# Go2sky with net/http + +## Installation + +```bash +go get -u github.com/SkyAPM/go2sky +``` + +## Usage + +### Server +```go +package main + +import ( + "log" + + "github.com/SkyAPM/go2sky" + "github.com/SkyAPM/go2sky/plugins/http" + "github.com/SkyAPM/go2sky/reporter" +) + +func main() { + // Use gRPC reporter for production + re, err := reporter.NewLogReporter() + if err != nil { + log.Fatalf("new reporter error %v \n", err) + } + defer re.Close() + + tracer, err := go2sky.NewTracer("gin-server", go2sky.WithReporter(re)) + if err != nil { + log.Fatalf("create tracer error %v \n", err) + } + + sm, err := http.NewServerMiddleware(tracer) + if err != nil { + log.Fatalf("create server middleware error %v \n", err) + } + // do something +} +``` + +### Client + +```go +package main + +import ( + "log" + + "github.com/SkyAPM/go2sky" + "github.com/SkyAPM/go2sky/plugins/http" + "github.com/SkyAPM/go2sky/reporter" +) + +func main() { + // Use gRPC reporter for production + re, err := reporter.NewLogReporter() + if err != nil { + log.Fatalf("new reporter error %v \n", err) + } + defer re.Close() + + tracer, err := go2sky.NewTracer("gin-server", go2sky.WithReporter(re)) + if err != nil { + log.Fatalf("create tracer error %v \n", err) + } + + sm, err := http.NewClient(tracer) + if err != nil { + log.Fatalf("create client error %v \n", err) + } + // do something +} +``` + +[See more](https://github.com/SkyAPM/go2sky/blob/master/plugins/http/example_http_test.go) \ No newline at end of file diff --git a/resty/README.md b/resty/README.md deleted file mode 100644 index 143a5cd..0000000 --- a/resty/README.md +++ /dev/null @@ -1 +0,0 @@ -# Go2sky with go-resty \ No newline at end of file From 479ccb4062d6d24d78918520cf25193be383735a Mon Sep 17 00:00:00 2001 From: zhangwei Date: Mon, 6 Jul 2020 23:00:32 +0800 Subject: [PATCH 5/7] add faq --- gin/README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/gin/README.md b/gin/README.md index e9d7290..4ab0d8c 100644 --- a/gin/README.md +++ b/gin/README.md @@ -6,3 +6,7 @@ ## FAQ +### What's the difference between v2 and v3 + +As [commented](https://github.com/SkyAPM/go2sky/issues/59#issuecomment-645427674), v2 version will overwrite in some cases. +To avoid this, we get the request route through `gin.context.FullPath()` From b9dab44dc8645b42c064755ace2185feaa831152 Mon Sep 17 00:00:00 2001 From: zhangwei Date: Mon, 6 Jul 2020 23:04:51 +0800 Subject: [PATCH 6/7] fix lint --- README.md | 2 +- {go_resty => resty}/README.md | 8 ++++---- {go_resty => resty}/doc.go | 4 ++-- {go_resty => resty}/example_go_resty_test.go | 4 ++-- {go_resty => resty}/go.mod | 0 {go_resty => resty}/go.sum | 0 {go_resty => resty}/go_resty.go | 4 ++-- 7 files changed, 11 insertions(+), 11 deletions(-) rename {go_resty => resty}/README.md (77%) rename {go_resty => resty}/doc.go (88%) rename {go_resty => resty}/example_go_resty_test.go (97%) rename {go_resty => resty}/go.mod (100%) rename {go_resty => resty}/go.sum (100%) rename {go_resty => resty}/go_resty.go (94%) diff --git a/README.md b/README.md index 584825b..0e4615a 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,4 @@ The plugins of go2sky 1. [http server & client](http/README.md) 1. [gin](gin/README.md) 1. [gear](gear/README.md) -1. [go-resty](go_resty/README.md) \ No newline at end of file +1. [go-resty](resty/README.md) \ No newline at end of file diff --git a/go_resty/README.md b/resty/README.md similarity index 77% rename from go_resty/README.md rename to resty/README.md index 97a2321..cc03068 100644 --- a/go_resty/README.md +++ b/resty/README.md @@ -3,7 +3,7 @@ ## Installation ```bash -go get -u github.com/SkyAPM/go2sky-plugins/go_resty +go get -u github.com/SkyAPM/go2sky-plugins/resty ``` ## Usage @@ -14,7 +14,7 @@ import ( "log" "github.com/SkyAPM/go2sky" - "github.com/SkyAPM/go2sky-plugins/go_resty" + "github.com/SkyAPM/go2sky-plugins/resty" "github.com/SkyAPM/go2sky/reporter" ) @@ -31,8 +31,8 @@ func main() { log.Fatalf("create tracer error %v \n", err) } - // create go_resty client - client := go_resty.NewGoResty(tracer) + // create resty client + client := resty.NewGoResty(tracer) // do something } ``` diff --git a/go_resty/doc.go b/resty/doc.go similarity index 88% rename from go_resty/doc.go rename to resty/doc.go index 122b19e..a8a9dbf 100644 --- a/go_resty/doc.go +++ b/resty/doc.go @@ -15,5 +15,5 @@ // specific language governing permissions and limitations // under the License. -// Package go_resty is a plugin that can be used to trace GO Resty framework. -package go_resty +// Package resty (go-resty) is a plugin that can be used to trace GO Resty framework. +package resty diff --git a/go_resty/example_go_resty_test.go b/resty/example_go_resty_test.go similarity index 97% rename from go_resty/example_go_resty_test.go rename to resty/example_go_resty_test.go index 4921a35..dc938ae 100644 --- a/go_resty/example_go_resty_test.go +++ b/resty/example_go_resty_test.go @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -package go_resty +package resty import ( "fmt" @@ -51,7 +51,7 @@ func ExampleNewGoResty() { ts := httptest.NewServer(sm(endFunc())) defer ts.Close() - // create go_resty client + // create resty client client := NewGoResty(tracer) resp, err := client.R().Get(fmt.Sprintf("%s/end", ts.URL)) diff --git a/go_resty/go.mod b/resty/go.mod similarity index 100% rename from go_resty/go.mod rename to resty/go.mod diff --git a/go_resty/go.sum b/resty/go.sum similarity index 100% rename from go_resty/go.sum rename to resty/go.sum diff --git a/go_resty/go_resty.go b/resty/go_resty.go similarity index 94% rename from go_resty/go_resty.go rename to resty/go_resty.go index f9161b9..9985c87 100644 --- a/go_resty/go_resty.go +++ b/resty/go_resty.go @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -package go_resty +package resty import ( "log" @@ -25,7 +25,7 @@ import ( "github.com/go-resty/resty/v2" ) -// NewGoResty returns a go_resty Client with tracer +// NewGoResty returns a resty Client with tracer func NewGoResty(tracer *go2sky.Tracer, options ...http.ClientOption) *resty.Client { hc, err := http.NewClient(tracer, options...) if err != nil { From ea2948b8a1d109cadab77b264d0886e4ddc5acf7 Mon Sep 17 00:00:00 2001 From: zhangwei Date: Wed, 8 Jul 2020 20:59:42 +0800 Subject: [PATCH 7/7] update doc --- gear/README.md | 1 + gear/gear.go | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/gear/README.md b/gear/README.md index 96dd043..fc258c9 100644 --- a/gear/README.md +++ b/gear/README.md @@ -20,6 +20,7 @@ import ( ) func main() { + // Use gRPC reporter for production re, err := reporter.NewLogReporter() if err != nil { log.Fatalf("new reporter error %v \n", err) diff --git a/gear/gear.go b/gear/gear.go index a8f655b..43b67b8 100644 --- a/gear/gear.go +++ b/gear/gear.go @@ -18,6 +18,7 @@ package gear import ( + "fmt" "strconv" "time" @@ -36,7 +37,7 @@ func Middleware(tracer *go2sky.Tracer) gear.Middleware { return nil } - span, _, err := tracer.CreateEntrySpan(ctx, ctx.Path, func() (string, error) { + span, _, err := tracer.CreateEntrySpan(ctx, operationName(ctx), func() (string, error) { return ctx.GetHeader(propagation.Header), nil }) if err != nil { @@ -59,3 +60,7 @@ func Middleware(tracer *go2sky.Tracer) gear.Middleware { return nil } } + +func operationName(ctx *gear.Context) string { + return fmt.Sprintf("%s/%s", ctx.Method, ctx.Path) +}