This repository has been archived by the owner on Jun 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,6 @@ | |
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# IDE | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
.PHONY: lint | ||
lint: $(LINTER) | ||
${TEST_SHELL} lint | ||
|
||
.PHONY: fix | ||
fix: $(LINTER) | ||
${TEST_SHELL} fix | ||
|
||
.PHONY: license | ||
license: | ||
python3 tools/check-license-header.py | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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