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

feat: add dubbo-go plugins #34

Merged
merged 45 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
b8a8f3f
feat: add dubbo-go plugins
royal-dargon Sep 30, 2021
dcc94d8
fix: fix mockoap address
royal-dargon Sep 30, 2021
1fc65d3
add:lisence
royal-dargon Sep 30, 2021
806b58b
add: add lisence
royal-dargon Sep 30, 2021
a283484
add: test
royal-dargon Sep 30, 2021
861653b
fix: fix the problem of license
royal-dargon Sep 30, 2021
a843960
add:add zk healthcheck
royal-dargon Sep 30, 2021
4a25ff3
fix:fix client.go
royal-dargon Sep 30, 2021
9cfc8be
fix:fix client.go
royal-dargon Sep 30, 2021
7c3cacc
fix:fix client.go
royal-dargon Sep 30, 2021
e866095
Update dubbo-go/test/client/cmd/client.go
arugal Sep 30, 2021
cc1362e
fix:fix client.go
royal-dargon Sep 30, 2021
b55b618
Merge branch 'dubbo-go' of https://github.com/royal-dargon/go2sky-plu…
royal-dargon Sep 30, 2021
b4783d8
Update dubbo-go/test/client/cmd/client.go
royal-dargon Sep 30, 2021
9a6ffaf
Update dubbo-go/dubbo-go.go
royal-dargon Sep 30, 2021
a6e59ce
Update dubbo-go/test/docker-compose.yml
royal-dargon Sep 30, 2021
a4d90cf
Update dubbo-go/test/docker-compose.yml
royal-dargon Sep 30, 2021
0c5917a
Update dubbo-go/test/dubbo_go_plugin_test.yaml
royal-dargon Sep 30, 2021
af9b99d
Merge branch 'dubbo-go' of https://github.com/royal-dargon/go2sky-plu…
royal-dargon Sep 30, 2021
8190e92
docs: update README
royal-dargon Sep 30, 2021
c9eb531
fix: fix docker-compose
royal-dargon Sep 30, 2021
eb1f446
fix: fix docker compose
royal-dargon Sep 30, 2021
813c01a
Update dubbo-go/test/client/cmd/client.go
arugal Sep 30, 2021
30cf9a5
Update dubbo-go/test/docker-compose.yml
arugal Sep 30, 2021
d678b3f
Update dubbo-go/test/server/cmd/server.go
arugal Sep 30, 2021
acbdaf6
fix: fix expected.data
royal-dargon Sep 30, 2021
8887116
Merge branch 'dubbo-go' of https://github.com/royal-dargon/go2sky-plu…
royal-dargon Sep 30, 2021
d3bae49
fix:fix the except.yml
royal-dargon Oct 1, 2021
f605002
fix: fix readme
royal-dargon Oct 1, 2021
409683f
fix: fix the expected.yml
royal-dargon Oct 1, 2021
c1ed6d8
fix: fix expected.yml
royal-dargon Oct 1, 2021
6b2395a
add: add dubbo-go
royal-dargon Oct 1, 2021
3bbba2e
Update dubbo-go/README.md
royal-dargon Oct 1, 2021
100c5ad
Update dubbo-go/README.md
royal-dargon Oct 1, 2021
c3a11ac
Update dubbo-go/dubbo-go.go
royal-dargon Oct 1, 2021
9f4a8fc
fix:fix some problems
royal-dargon Oct 9, 2021
965fdc9
fix:fix the problem about expected.yml
royal-dargon Oct 9, 2021
1786097
fix:fix the expected.yml problems
royal-dargon Oct 10, 2021
c490da0
Merge branch 'master' into dubbo-go
arugal Oct 11, 2021
7971a8d
Update dubbo-go/dubbo-go.go
arugal Oct 11, 2021
bdec0f4
Update dubbo-go/test/client/cmd/client.go
arugal Oct 11, 2021
e95a16b
Update dubbo-go/test/docker-compose.yml
arugal Oct 11, 2021
9500f60
Update dubbo-go/test/dubbo_go_plugin_test.yaml
arugal Oct 11, 2021
fce5dcd
Update dubbo-go/test/expected.data.yml
arugal Oct 11, 2021
66133a1
Update dubbo-go/dubbo-go.go
arugal Oct 11, 2021
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
1 change: 1 addition & 0 deletions .github/workflows/plugin_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
kratos/test/go_kratos_plugin_test.yaml: kratos/**
sql/test/sql_plugin_test.yaml: sql/**
kafkareporter/test/go_kafka_reporter_plugin_test.yaml: kafkareporter/**
dubbo-go/test/dubbo_go_plugin_test.yaml: dubbo-go/**

PluginsTest:
name: Plugin
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The plugins of [go2sky](https://github.com/SkyAPM/go2sky)
1. [go-restful](go-restful/README.md)
1. [go-kratos](kratos/README.md)
1. [sql](sql/README.md)
1. [dubbo-go](dubbo-go/README.md)

### Log Plugins

Expand Down
101 changes: 101 additions & 0 deletions dubbo-go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
### Go2Sky with Dubbo-go

##### Installation

```
go get -u github.com/SkyAMP/go2sky-plugins/dubbo-go
```

##### Usage

Server:

```go
import (
_ "dubbo.apache.org/dubbo-go/v3/cluster/cluster_impl"
_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
"dubbo.apache.org/dubbo-go/v3/common/logger"
_ "dubbo.apache.org/dubbo-go/v3/common/proxy/proxy_factory"
"dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/filter/filter_impl"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo"
_ "dubbo.apache.org/dubbo-go/v3/registry/protocol"
_ "dubbo.apache.org/dubbo-go/v3/registry/zookeeper"
"github.com/SkyAPM/go2sky"
"github.com/SkyAPM/go2sky/reporter"
hessian "github.com/apache/dubbo-go-hessian2"

dubbo_go "github.com/SkyAPM/go2sky-plugins/dubbo-go"
"github.com/SkyAPM/go2sky-plugins/dubbo-go/test/server/pkg"
)

// set dubbogo configs ...

// setup reporter, use gRPC reporter for production
report, err := reporter.NewLogReporter()
if err != nil {
log.Fatalf("new reporter error: %v \n", err)
}

// setup tracer
tracer, err := go2sky.NewTracer(serviceName, go2sky.WithReporter(report))
if err != nil {
log.Fatalf("crate tracer error: %v \n", err)
}

// set dubbogo plugin server tracer
err = dubbo_go.SetServerTracer(tracer)
if err != nil {
log.Fatalf("set tracer error: %v \n", err)
}

// set extra tags and report tags
dubbo_go.SetServerExtraTags("extra-tags", "server")
dubbo_go.SetServerReportTags("release")
```

Client:

```go
import (
_ "dubbo.apache.org/dubbo-go/v3/cluster/cluster_impl"
_ "dubbo.apache.org/dubbo-go/v3/cluster/loadbalance"
_ "dubbo.apache.org/dubbo-go/v3/common/proxy/proxy_factory"
"dubbo.apache.org/dubbo-go/v3/config"
_ "dubbo.apache.org/dubbo-go/v3/filter/filter_impl"
_ "dubbo.apache.org/dubbo-go/v3/protocol/dubbo"
_ "dubbo.apache.org/dubbo-go/v3/registry/protocol"
_ "dubbo.apache.org/dubbo-go/v3/registry/zookeeper"
"github.com/SkyAPM/go2sky"
"github.com/SkyAPM/go2sky/reporter"
hessian "github.com/apache/dubbo-go-hessian2"

dubbo_go "github.com/SkyAPM/go2sky-plugins/dubbo-go"
"github.com/SkyAPM/go2sky-plugins/dubbo-go/test/client/pkg"
)

// set dubbogo configs ...

// setup reporter, use gRPC reporter for production
report, err := reporter.NewLogReporter()
if err != nil {
log.Fatalf("new reporter error: %v \n", err)
}

// setup tracer
tracer, err := go2sky.NewTracer(serviceName, go2sky.WithReporter(report))
if err != nil {
log.Fatalf("crate tracer error: %v \n", err)
}

// set dubbogo plugin client tracer
err = dubbo_go.SetClientTracer(tracer)
if err != nil {
log.Fatalf("set tracer error: %v \n", err)
}

// set extra tags and report tags
dubbo_go.SetClientExtraTags("extra-tags", "client")
dubbo_go.SetClientReportTags("release")
```

209 changes: 209 additions & 0 deletions dubbo-go/dubbo-go.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
//
// Copyright 2021 SkyAPM org
//
// Licensed 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 dubbo_go

import (
"context"
"errors"
"sync"
"time"

"dubbo.apache.org/dubbo-go/v3/common/extension"
"dubbo.apache.org/dubbo-go/v3/filter"
"dubbo.apache.org/dubbo-go/v3/protocol"
agentv3 "skywalking.apache.org/repo/goapi/collect/language/agent/v3"

"github.com/SkyAPM/go2sky"
)

// side record the server or client
type side int

const (
componentID = 3

serverSide side = iota
clientSide side = iota
)

var (
errInvalidTracer = errors.New("invalid tracer")
)

// this should be executed before users set their own Tracer
func init() {
// init default tracer
defaultTracer, _ = go2sky.NewTracer("default-dubbo-go-tracer")

// set filter
// in dubbo go conf:
// client side: filter: "go2sky-tracing-client"
// server side: filter: "go2sky-tracing-server"
// maybe move to constant
extension.SetFilter("go2sky-tracing-client", GetClientTracingFilterSingleton)
extension.SetFilter("go2sky-tracing-server", GetServerTracingFilterSingleton)
}

// default go2sky tracer
var defaultTracer *go2sky.Tracer

type tracingFilter struct {
tracer *go2sky.Tracer
extraTags map[string]string
reportTags []string
side side
}

// SetClientTracer set client tracer with user's tracer.
func SetClientTracer(tracer *go2sky.Tracer) error {
if tracer == nil {
return errInvalidTracer
}

clientFilter.tracer = tracer

return nil
}

// SetServerTracer set server tracer with user's tracer.
func SetServerTracer(tracer *go2sky.Tracer) error {
if tracer == nil {
return errInvalidTracer
}

serverFilter.tracer = tracer

return nil
}

// SetClientExtraTags adds extra tag to client tracer spans.
func SetClientExtraTags(key string, value string) {
if clientFilter.extraTags == nil {
clientFilter.extraTags = make(map[string]string)
}

clientFilter.extraTags[key] = value
}

// SetServerExtraTags adds extra tag to server tracer spans.
func SetServerExtraTags(key string, value string) {
if serverFilter.extraTags == nil {
serverFilter.extraTags = make(map[string]string)
}

serverFilter.extraTags[key] = value
}

// SetClientReportTags adds report tags to client tracer spans.
func SetClientReportTags(tags ...string) {
clientFilter.reportTags = append(clientFilter.reportTags, tags...)
}

// SetServerReportTags adds report tags to server tracer spans.
func SetServerReportTags(tags ...string) {
serverFilter.reportTags = append(serverFilter.reportTags, tags...)
}

// Invoke implements dubbo-go filter interface.
func (cf tracingFilter) Invoke(ctx context.Context, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
operationName := invoker.GetURL().ServiceKey() + "#" + invocation.MethodName()
var span go2sky.Span

if cf.side == clientSide {
span, _ = cf.tracer.CreateExitSpan(ctx, operationName, invoker.GetURL().Location, func(key, value string) error {
invocation.SetAttachments(key, value)
return nil
})

span.SetComponent(componentID)
} else {
// componentIDGo2SkyServer
span, ctx, _ = cf.tracer.CreateEntrySpan(ctx, operationName, func(key string) (string, error) {
return invocation.AttachmentsByKey(key, ""), nil
})

span.SetComponent(componentID)
}
defer span.End()

// add extra tags
for k, v := range cf.extraTags {
span.Tag(go2sky.Tag(k), v)
}

// add report tags
for _, tag := range cf.reportTags {
// from attachments
if v, ok := invocation.Attachments()[tag].(string); ok {
if ok {
span.Tag(go2sky.Tag(tag), v)
}
}
// or from url
if v := invoker.GetURL().GetParam(tag, ""); v != "" {
span.Tag(go2sky.Tag(tag), v)
}
}

// other tags ...
span.SetSpanLayer(agentv3.SpanLayer_RPCFramework)

result := invoker.Invoke(ctx, invocation)

// finish span
// defer span.End()
if result.Error() != nil {
// tag error
span.Error(time.Now(), result.Error().Error())
}
return result
}

// OnResponse implements dubbo-go filter interface.
func (cf tracingFilter) OnResponse(ctx context.Context, result protocol.Result, invoker protocol.Invoker, invocation protocol.Invocation) protocol.Result {
return result
}

var (
serverFilterOnce sync.Once
serverFilter *tracingFilter

clientFilterOnce sync.Once
clientFilter *tracingFilter
)

// GetServerTracingFilterSingleton returns global server filter for server side.
func GetServerTracingFilterSingleton() filter.Filter {
serverFilterOnce.Do(func() {
serverFilter = &tracingFilter{
tracer: defaultTracer,
side: serverSide,
}
})
return serverFilter
}

// GetClientTracingFilterSingleton returns global filter for client side.
func GetClientTracingFilterSingleton() filter.Filter {
clientFilterOnce.Do(func() {
clientFilter = &tracingFilter{
tracer: defaultTracer,
side: clientSide,
}
})
return clientFilter
}
11 changes: 11 additions & 0 deletions dubbo-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/SkyAPM/go2sky-plugins/dubbo-go

go 1.14

require (
dubbo.apache.org/dubbo-go/v3 v3.0.0-rc2
github.com/SkyAPM/go2sky v1.2.0
github.com/apache/dubbo-go-hessian2 v1.9.3
github.com/dubbogo/gost v1.11.17
skywalking.apache.org/repo/goapi v0.0.0-20210820070710-e10b78bbf481
)
Loading