Skip to content

Commit

Permalink
feat: skywalking 插件 (#20)
Browse files Browse the repository at this point in the history
Co-authored-by: yuanyou <[email protected]>
  • Loading branch information
elza2 and yuanyou authored Jan 10, 2023
1 parent 45a2458 commit e6757f4
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 6 deletions.
2 changes: 2 additions & 0 deletions interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/SkyAPM/go2sky"
"github.com/SkyAPM/go2sky/propagation"

// nolint:staticcheck
// ignore SA1019 Need to keep deprecated package for compatibility.
"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -175,6 +176,7 @@ func NewUnaryClientSkywalkingInterceptor(tracer *go2sky.Tracer) grpc.UnaryClient
}

// NewUnaryServerSkywalkingInterceptor skywalking server interceptor.
// nolint: govet
func NewUnaryServerSkywalkingInterceptor(tracer *go2sky.Tracer, opts ...Option) grpc.UnaryServerInterceptor {
options := &options{
reportTags: []string{},
Expand Down
96 changes: 95 additions & 1 deletion interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,35 @@ import (
"strings"
"testing"

"github.com/SkyAPM/go2sky"
"github.com/SkyAPM/go2sky/reporter"
grpc_testing "github.com/grpc-ecosystem/go-grpc-middleware/testing"
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
)

var (
goodPing = &pb_testproto.PingRequest{Value: "goodPing", SleepTimeMs: 999}
goodPing = &pb_testproto.PingRequest{Value: "goodPing", SleepTimeMs: 999}
skyClientPing = &pb_testproto.PingRequest{Value: "skyClientPing", SleepTimeMs: 999}
skyServerPing = &pb_testproto.PingRequest{Value: "skyServerPing", SleepTimeMs: 999}
)

type LogTestSuite struct {
*grpc_testing.InterceptorTestSuite
reader, writer *os.File
}

type SkywalkingClientTestSuite struct {
*grpc_testing.InterceptorTestSuite
reader, writer *os.File
}

type SkywalkingServerTestSuite struct {
*grpc_testing.InterceptorTestSuite
reader, writer *os.File
}

func TestLogTestSuite(t *testing.T) {
r, w, _ := os.Pipe()
// 替换原有os.Stdout
Expand Down Expand Up @@ -64,3 +78,83 @@ func (s *LogTestSuite) TestMarshalJSON() {
s.Nil(err)
s.True(strings.Contains(string(buf), "goodPing"))
}

func TestSkywalkingClientTestSuite(t *testing.T) {
r, w, _ := os.Pipe()
// 替换原有os.Stdout
report, err := reporter.NewLogReporter()
if err != nil {
return
}
tracer, err := go2sky.NewTracer("test", go2sky.WithReporter(report))
os.Stdout = w
s := &SkywalkingClientTestSuite{
InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{
ClientOpts: []grpc.DialOption{
grpc.WithUnaryInterceptor(NewUnaryClientSkywalkingInterceptor(tracer)),
},
},
}
s.reader = r
s.writer = w
suite.Run(t, s)
}

func (c *SkywalkingClientTestSuite) TestNewUnaryClientSkywalkingInterceptor() {
_, err := c.Client.Ping(c.SimpleCtx(), skyClientPing)
c.NoError(err)

var buf bytes.Buffer
output := make(chan string, 1)
go func() {
io.Copy(&buf, c.reader)
output <- buf.String()
c.reader.Close()
}()
c.writer.Close()

o := strings.Split(<-output, "\n")
// 输出空行
c.Len(o, 1)
c.Equal(o[0], "")
}

func TestSkywalkingServerTestSuite(t *testing.T) {
r, w, _ := os.Pipe()
// 替换原有os.Stdout
os.Stdout = w
report, err := reporter.NewLogReporter()
if err != nil {
return
}
tracer, err := go2sky.NewTracer("test", go2sky.WithReporter(report))
s := &SkywalkingServerTestSuite{
InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{
ServerOpts: []grpc.ServerOption{
grpc.UnaryInterceptor(NewUnaryServerSkywalkingInterceptor(tracer)),
},
},
}
s.reader = r
s.writer = w
suite.Run(t, s)
}

func (s *SkywalkingServerTestSuite) TestNewUnaryServerSkywalkingInterceptor() {
_, err := s.Client.Ping(s.SimpleCtx(), skyServerPing)
s.NoError(err)

var buf bytes.Buffer
output := make(chan string, 1)
go func() {
io.Copy(&buf, s.reader)
output <- buf.String()
s.reader.Close()
}()
s.writer.Close()

o := strings.Split(<-output, "\n")
// 输出空行
s.Len(o, 1)
s.Equal(o[0], "")
}
12 changes: 7 additions & 5 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ var (

type operation func(name string, r *http.Request) string

// nolint: govet
type handler struct {
tracer *go2sky.Tracer
name string
next http.Handler
extraTags map[string]string
// filter some health check request.
filterURLs []string
next http.Handler
name string
tracer *go2sky.Tracer
extraTags map[string]string
// get operation name.
operationFunc operation
}

// responseWriter is a minimal wrapper for http.ResponseWriter that allows the
// written HTTP status code to be captured for logging.
// nolint: govet,unused
type responseWriter struct {
http.ResponseWriter
status int
Expand Down Expand Up @@ -71,7 +73,7 @@ func FilterURL(filterURLs []string, url string) bool {
return false
}

func NewServerSkywalkingMiddleware(tracer *go2sky.Tracer, opts ...func(*handler)) (func(http.Handler) http.Handler, error) {
func NewServerSkywalkingHTTPMiddleware(tracer *go2sky.Tracer, opts ...func(*handler)) (func(http.Handler) http.Handler, error) {
if tracer == nil {
panic("tracer is nil.")
}
Expand Down
42 changes: 42 additions & 0 deletions middleware_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package hutils

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/SkyAPM/go2sky"
"github.com/SkyAPM/go2sky/reporter"
)

type Ping struct{}

func TestSkywalkingHTTPMiddlewareTestSuite(t *testing.T) {
report, err := reporter.NewLogReporter()
if err != nil {
return
}
tracer, err := go2sky.NewTracer("test", go2sky.WithReporter(report))
if err != nil {
return
}
middleware, err := NewServerSkywalkingHTTPMiddleware(tracer)
if err != nil {
return
}
mux := http.NewServeMux()
mux.Handle("/ping", middleware(&Ping{}))

req := httptest.NewRequest("GET", "/ping", nil)
rw := httptest.NewRecorder()
mux.ServeHTTP(rw, req)

response := rw.Body.String()
if response != "OK" {
t.Errorf("Response gotten was %q", response)
}
}

func (p *Ping) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
}

0 comments on commit e6757f4

Please sign in to comment.