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

Migration plugins #2

Merged
merged 7 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ on:

jobs:
build:
name: Build
name: Build & Test
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.12
Expand All @@ -35,5 +35,27 @@ jobs:
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"
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# IDE
.idea/
43 changes: 43 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
# 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.
#

TEST_SHELL="./tools/test.sh"

.PHONY: test
test:
${TEST_SHELL} test

.PHONY: deps
deps:
${TEST_SHELL} deps

LINTER := bin/golangci-lint
$(LINTER):
wget -q -O- https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.20.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wget is not built-in in MacOS, curl is preferable so that we can run the check locally before pushing to the remote


.PHONY: lint
lint: $(LINTER)
${TEST_SHELL} lint

.PHONY: fix
fix: $(LINTER)
${TEST_SHELL} fix

.PHONY: license
license:
python3 tools/check-license-header.py

8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
45 changes: 45 additions & 0 deletions gear/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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() {
// 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("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
}
```

[See more](example_gear_test.go).
19 changes: 19 additions & 0 deletions gear/doc.go
Original file line number Diff line number Diff line change
@@ -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
91 changes: 91 additions & 0 deletions gear/example_gear_test.go
Original file line number Diff line number Diff line change
@@ -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()
}
66 changes: 66 additions & 0 deletions gear/gear.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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"
"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, operationName(ctx), 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
}
}

func operationName(ctx *gear.Context) string {
return fmt.Sprintf("%s/%s", ctx.Method, ctx.Path)
}
8 changes: 8 additions & 0 deletions gear/go.mod
Original file line number Diff line number Diff line change
@@ -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
)
Loading