Skip to content
This repository was archived by the owner on Jun 14, 2023. It is now read-only.

Commit d691d0a

Browse files
authored
Migration plugins (#2)
1 parent 5e42c45 commit d691d0a

33 files changed

+1671
-3
lines changed

.github/workflows/ci.yaml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ on:
2525

2626
jobs:
2727
build:
28-
name: Build
28+
name: Build & Test
2929
runs-on: ubuntu-latest
3030
steps:
3131
- name: Set up Go 1.12
@@ -35,5 +35,27 @@ jobs:
3535
id: go
3636
- name: Check out code into the Go module directory
3737
uses: actions/checkout@v2
38+
39+
- name: Get dependencies
40+
run: make deps
41+
42+
- name: Lint
43+
run: make lint
44+
3845
- name: Test
39-
run: echo "test"
46+
run: make test
47+
48+
license:
49+
name: Check License
50+
runs-on: ubuntu-latest
51+
steps:
52+
- name: Set up Python 3.7
53+
uses: actions/setup-python@v2
54+
with:
55+
python-version: 3.7
56+
57+
- name: Check out code into the Go module directory
58+
uses: actions/checkout@v2
59+
60+
- name: Check license header
61+
run: make license

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
# IDE
18+
.idea/

Makefile

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# Licensed to the SkyAPM org under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
TEST_SHELL="./tools/test.sh"
19+
20+
.PHONY: test
21+
test:
22+
${TEST_SHELL} test
23+
24+
.PHONY: deps
25+
deps:
26+
${TEST_SHELL} deps
27+
28+
LINTER := bin/golangci-lint
29+
$(LINTER):
30+
wget -q -O- https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.20.1
31+
32+
.PHONY: lint
33+
lint: $(LINTER)
34+
${TEST_SHELL} lint
35+
36+
.PHONY: fix
37+
fix: $(LINTER)
38+
${TEST_SHELL} fix
39+
40+
.PHONY: license
41+
license:
42+
python3 tools/check-license-header.py
43+

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# go2sky-plugins
22

3-
43
[![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)
54

65
The plugins of go2sky
6+
7+
### Plugin Summary
8+
9+
1. [http server & client](http/README.md)
10+
1. [gin](gin/README.md)
11+
1. [gear](gear/README.md)
12+
1. [go-resty](resty/README.md)

gear/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Go2sky with gear (v1.21.2)
2+
3+
## Installation
4+
5+
```bash
6+
go get -u github.com/SkyAPM/go2sky-plugins/gear
7+
```
8+
9+
## Usage
10+
```go
11+
package main
12+
13+
import (
14+
"log"
15+
16+
"github.com/SkyAPM/go2sky"
17+
gearplugin "github.com/SkyAPM/go2sky-plugins/gear"
18+
"github.com/SkyAPM/go2sky/reporter"
19+
"github.com/teambition/gear"
20+
)
21+
22+
func main() {
23+
// Use gRPC reporter for production
24+
re, err := reporter.NewLogReporter()
25+
if err != nil {
26+
log.Fatalf("new reporter error %v \n", err)
27+
}
28+
29+
defer re.Close()
30+
31+
tracer, err := go2sky.NewTracer("gear", go2sky.WithReporter(re))
32+
if err != nil {
33+
log.Fatalf("create tracer error %v \n", err)
34+
}
35+
36+
app := gear.New()
37+
38+
//Use go2sky middleware with tracing
39+
app.Use(gearplugin.Middleware(tracer))
40+
41+
// do something
42+
}
43+
```
44+
45+
[See more](example_gear_test.go).

gear/doc.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Licensed to SkyAPM org under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. SkyAPM org licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
// Package gear is a plugin that can be used to trace Gear framework.
19+
package gear

gear/example_gear_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Licensed to SkyAPM org under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. SkyAPM org licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package gear
19+
20+
import (
21+
"fmt"
22+
"log"
23+
"net/http"
24+
"sync"
25+
"time"
26+
27+
"github.com/SkyAPM/go2sky"
28+
h "github.com/SkyAPM/go2sky/plugins/http"
29+
"github.com/SkyAPM/go2sky/reporter"
30+
"github.com/teambition/gear"
31+
)
32+
33+
func ExampleMiddleware() {
34+
re, err := reporter.NewLogReporter()
35+
if err != nil {
36+
log.Fatalf("new reporter error %v \n", err)
37+
}
38+
39+
defer re.Close()
40+
41+
tracer, err := go2sky.NewTracer("gear", go2sky.WithReporter(re))
42+
if err != nil {
43+
log.Fatalf("create tracer error %v \n", err)
44+
}
45+
46+
app := gear.New()
47+
app.Use(Middleware(tracer))
48+
49+
router := gear.NewRouter()
50+
router.Get("/user", func(ctx *gear.Context) error {
51+
name := ctx.Param("name")
52+
return ctx.End(http.StatusOK, []byte(fmt.Sprintf("Hello %s", name)))
53+
})
54+
55+
app.UseHandler(router)
56+
57+
go func() {
58+
app.Error(app.Listen(":8080"))
59+
}()
60+
// Wait for the server to start
61+
time.Sleep(time.Second)
62+
63+
wg := sync.WaitGroup{}
64+
wg.Add(1)
65+
66+
go func() {
67+
defer wg.Done()
68+
request(tracer)
69+
}()
70+
wg.Wait()
71+
// Output:
72+
}
73+
74+
func request(tracer *go2sky.Tracer) {
75+
client, err := h.NewClient(tracer)
76+
if err != nil {
77+
log.Fatalf("create client error %v \n", err)
78+
}
79+
80+
request, err := http.NewRequest("GET", "http://127.0.0.1:8080/user?name=gear", nil)
81+
if err != nil {
82+
log.Fatalf("unable to create http request: %+v\n", err)
83+
}
84+
85+
res, err := client.Do(request)
86+
if err != nil {
87+
log.Fatalf("unable to do http request: %+v\n", err)
88+
}
89+
90+
_ = res.Body.Close()
91+
}

gear/gear.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Licensed to SkyAPM org under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. SkyAPM org licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package gear
19+
20+
import (
21+
"fmt"
22+
"strconv"
23+
"time"
24+
25+
"github.com/SkyAPM/go2sky"
26+
"github.com/SkyAPM/go2sky/propagation"
27+
v3 "github.com/SkyAPM/go2sky/reporter/grpc/language-agent"
28+
"github.com/teambition/gear"
29+
)
30+
31+
const componentIDGearServer = 5007
32+
33+
//Middleware gear middleware return HandlerFunc with tracing.
34+
func Middleware(tracer *go2sky.Tracer) gear.Middleware {
35+
return func(ctx *gear.Context) error {
36+
if tracer == nil {
37+
return nil
38+
}
39+
40+
span, _, err := tracer.CreateEntrySpan(ctx, operationName(ctx), func() (string, error) {
41+
return ctx.GetHeader(propagation.Header), nil
42+
})
43+
if err != nil {
44+
return nil
45+
}
46+
47+
span.SetComponent(componentIDGearServer)
48+
span.Tag(go2sky.TagHTTPMethod, ctx.Method)
49+
span.Tag(go2sky.TagURL, ctx.Host+ctx.Path)
50+
span.SetSpanLayer(v3.SpanLayer_Http)
51+
52+
ctx.OnEnd(func() {
53+
code := ctx.Res.Status()
54+
span.Tag(go2sky.TagStatusCode, strconv.Itoa(code))
55+
if code >= 400 {
56+
span.Error(time.Now(), string(ctx.Res.Body()))
57+
}
58+
span.End()
59+
})
60+
return nil
61+
}
62+
}
63+
64+
func operationName(ctx *gear.Context) string {
65+
return fmt.Sprintf("%s/%s", ctx.Method, ctx.Path)
66+
}

gear/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/SkyAPM/go2sky-plugins/gear
2+
3+
go 1.12
4+
5+
require (
6+
github.com/SkyAPM/go2sky v0.4.0
7+
github.com/teambition/gear v1.21.2
8+
)

0 commit comments

Comments
 (0)