This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-go.git


The following commit(s) were added to refs/heads/main by this push:
     new af5a9bd  Move the gRPC reporter generate in the agent tool (#25)
af5a9bd is described below

commit af5a9bdfa7f621a458c763b28fdc06b4d42a5193
Author: mrproliu <[email protected]>
AuthorDate: Fri May 5 13:10:16 2023 +0800

    Move the gRPC reporter generate in the agent tool (#25)
---
 agent/core/compile.go                              |  2 +-
 agent/core/tracing/compile.go                      |  2 +-
 agent/reporter/imports.go                          | 64 +++++++++++++++++++
 go.mod                                             |  1 +
 go.sum                                             |  1 +
 go.work.sum                                        | 32 ++++++++++
 imports.go                                         |  1 +
 .../reporter.go => plugins/core/reporter/api.go    | 18 ------
 .../core/reporter}/config_discovery.go             |  0
 {reporter => plugins/core/reporter/grpc}/grpc.go   | 21 ++++---
 .../core/reporter/{ => grpc}/grpc_opts_enhance.go  | 16 +++--
 plugins/core/reporter/grpc_struct.go               | 62 -------------------
 plugins/core/sampler.go                            |  2 +-
 plugins/core/span_default.go                       |  2 +-
 plugins/core/span_tracing.go                       |  2 +-
 plugins/core/test_base.go                          |  2 +-
 plugins/core/tracer.go                             |  2 +-
 plugins/core/tracer_test.go                        |  2 +-
 plugins/core/tracing.go                            |  2 +-
 reporter/doc.go                                    | 21 -------
 reporter/grpc_opts.go                              | 22 -------
 reporter/imports.go                                | 25 --------
 .../plugins/runner-helper/templates/dockerfile.tpl |  3 +
 test/plugins/scenarios/gin/go.mod                  |  2 +-
 test/plugins/scenarios/gin/go.sum                  |  4 +-
 tools/go-agent/instrument/agentcore/instrument.go  | 12 ++--
 .../go-agent/instrument/plugins/rewrite/rewrite.go | 11 +---
 tools/go-agent/instrument/reporter/instrument.go   | 25 ++++++--
 tools/go-agent/tools/copy.go                       |  4 +-
 tools/go-agent/tools/dst.go                        | 71 ++++++++++++++++++++++
 30 files changed, 238 insertions(+), 196 deletions(-)

diff --git a/agent/core/compile.go b/agent/core/compile.go
index 599458c..c041368 100644
--- a/agent/core/compile.go
+++ b/agent/core/compile.go
@@ -37,8 +37,8 @@ import (
        //go:nolint
        _ "github.com/apache/skywalking-go/agent/core/operator"
        _ "github.com/apache/skywalking-go/agent/core/tracing"
+       _ "github.com/apache/skywalking-go/agent/reporter"
        _ "github.com/apache/skywalking-go/log"
-       _ "github.com/apache/skywalking-go/reporter"
 
        //go:nolint
        _ "github.com/google/uuid"
diff --git a/agent/core/tracing/compile.go b/agent/core/tracing/compile.go
index a7a5458..6668809 100644
--- a/agent/core/tracing/compile.go
+++ b/agent/core/tracing/compile.go
@@ -27,7 +27,7 @@ import (
 
        //go:nolint
        _ "github.com/apache/skywalking-go/agent/core/operator"
-       _ "github.com/apache/skywalking-go/reporter"
+       _ "github.com/apache/skywalking-go/agent/reporter"
 
        //go:nolint
        _ "skywalking.apache.org/repo/goapi/collect/common/v3"
diff --git a/agent/reporter/imports.go b/agent/reporter/imports.go
new file mode 100644
index 0000000..55a6739
--- /dev/null
+++ b/agent/reporter/imports.go
@@ -0,0 +1,64 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (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.
+
+package reporter
+
+import (
+       // imports required packages for gRPC reporter
+       _ "context"
+       _ "fmt"
+       _ "io"
+       _ "os"
+       _ "strconv"
+       _ "time"
+
+       // imports the logs for reporter
+       _ "github.com/apache/skywalking-go/log"
+
+       // imports configuration and starter for gRPC
+       _ "google.golang.org/grpc"
+       _ "google.golang.org/grpc/balancer"
+       _ "google.golang.org/grpc/balancer/grpclb"
+       _ "google.golang.org/grpc/balancer/roundrobin"
+       _ "google.golang.org/grpc/balancer/weightedroundrobin"
+       _ "google.golang.org/grpc/balancer/weightedtarget"
+       _ "google.golang.org/grpc/codes"
+       _ "google.golang.org/grpc/connectivity"
+       _ "google.golang.org/grpc/credentials"
+       _ "google.golang.org/grpc/credentials/insecure"
+       _ "google.golang.org/grpc/encoding"
+       _ "google.golang.org/grpc/encoding/gzip"
+       _ "google.golang.org/grpc/grpclog"
+       _ "google.golang.org/grpc/keepalive"
+       _ "google.golang.org/grpc/metadata"
+       _ "google.golang.org/grpc/resolver"
+       _ "google.golang.org/grpc/resolver/manual"
+       _ "google.golang.org/grpc/stats"
+       _ "google.golang.org/grpc/status"
+
+       // imports protocols between agent and backend
+       _ "skywalking.apache.org/repo/goapi/collect/agent/configuration/v3"
+       _ "skywalking.apache.org/repo/goapi/collect/common/v3"
+       _ "skywalking.apache.org/repo/goapi/collect/ebpf/profiling/process/v3"
+       _ "skywalking.apache.org/repo/goapi/collect/ebpf/profiling/v3"
+       _ "skywalking.apache.org/repo/goapi/collect/event/v3"
+       _ "skywalking.apache.org/repo/goapi/collect/language/agent/v3"
+       _ "skywalking.apache.org/repo/goapi/collect/language/profile/v3"
+       _ "skywalking.apache.org/repo/goapi/collect/logging/v3"
+       _ "skywalking.apache.org/repo/goapi/collect/management/v3"
+       _ "skywalking.apache.org/repo/goapi/collect/servicemesh/v3"
+)
diff --git a/go.mod b/go.mod
index 79c92f5..e4ce261 100644
--- a/go.mod
+++ b/go.mod
@@ -11,6 +11,7 @@ require (
 
 require (
        github.com/golang/protobuf v1.5.2 // indirect
+       github.com/google/go-cmp v0.5.9 // indirect
        golang.org/x/net v0.8.0 // indirect
        golang.org/x/sys v0.6.0 // indirect
        golang.org/x/text v0.8.0 // indirect
diff --git a/go.sum b/go.sum
index 45c8fb0..7710935 100644
--- a/go.sum
+++ b/go.sum
@@ -42,6 +42,7 @@ github.com/google/go-cmp v0.5.0/go.mod 
h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
 github.com/google/go-cmp v0.5.5/go.mod 
h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/google/go-cmp v0.5.6/go.mod 
h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
+github.com/google/go-cmp v0.5.9/go.mod 
h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
 github.com/google/uuid v1.1.2/go.mod 
h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
 github.com/google/uuid v1.3.0/go.mod 
h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
diff --git a/go.work.sum b/go.work.sum
index 321f6a5..d18b9d3 100644
--- a/go.work.sum
+++ b/go.work.sum
@@ -49,8 +49,10 @@ cloud.google.com/go/clouddms v1.5.0/go.mod 
h1:QSxQnhikCLUw13iAbffF2CZxAER3xDGNHj
 cloud.google.com/go/cloudtasks v1.8.0 
h1:faUiUgXjW8yVZ7XMnKHKm1WE4OldPBUWWfIRN/3z1dc=
 cloud.google.com/go/cloudtasks v1.10.0/go.mod 
h1:NDSoTLkZ3+vExFEWu2UJV1arUyzVDAiZtdWcsUyNwBs=
 cloud.google.com/go/compute v1.15.1 
h1:7UGq3QknM33pw5xATlpzeoomNxsacIVvTqTTvbfajmE=
+cloud.google.com/go/compute v1.18.0/go.mod 
h1:1X7yHxec2Ga+Ss6jPyjxRxpu2uu7PLgsOVXvgU0yacs=
 cloud.google.com/go/compute v1.19.0/go.mod 
h1:rikpw2y+UMidAe9tISo04EHNOIf42RLYF/q8Bs93scU=
 cloud.google.com/go/compute/metadata v0.2.3 
h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY=
+cloud.google.com/go/compute/metadata v0.2.3/go.mod 
h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
 cloud.google.com/go/contactcenterinsights v1.4.0 
h1:tTQLI/ZvguUf9Hv+36BkG2+/PeC8Ol1q4pBW+tgCx0A=
 cloud.google.com/go/contactcenterinsights v1.6.0/go.mod 
h1:IIDlT6CLcDoyv79kDv8iWxMSTZhLxSCofVV5W6YFM/w=
 cloud.google.com/go/container v1.7.0 
h1:nbEK/59GyDRKKlo1SqpohY1TK8LmJ2XNcvS9Gyom2A0=
@@ -244,32 +246,62 @@ github.com/OneOfOne/xxhash v1.2.2 
h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE
 github.com/antihax/optional v1.0.0 
h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
 github.com/apache/skywalking-go v0.0.0-20230411034404-b9270e98036b/go.mod 
h1:K0KR1RwXnGRlhw/ANpJ4Ex2CzhT4ST78MxmgHNi3/Z4=
 github.com/census-instrumentation/opencensus-proto v0.4.1 
h1:iKLQ0xPNFxR/2hzXZMrBo8f1j86j5WHzznCCQxV/b8g=
+github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod 
h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw=
 github.com/cespare/xxhash v1.1.0 
h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
 github.com/cespare/xxhash/v2 v2.2.0 
h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
+github.com/cespare/xxhash/v2 v2.2.0/go.mod 
h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
 github.com/client9/misspell v0.3.4 
h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
 github.com/cncf/udpa/go v0.0.0-20220112060539-c52dc94e7fbe 
h1:QQ3GSy+MqSHxm/d8nCtnAiZdYFd45cYZPs8vOOIYKfk=
+github.com/cncf/udpa/go v0.0.0-20220112060539-c52dc94e7fbe/go.mod 
h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI=
+github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod 
h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
 github.com/cncf/xds/go v0.0.0-20230105202645-06c439db220b 
h1:ACGZRIr7HsgBKHsueQ1yM4WaVaXh21ynwqsF8M8tXhA=
+github.com/cncf/xds/go v0.0.0-20230310173818-32f1caf87195/go.mod 
h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
 github.com/envoyproxy/go-control-plane v0.10.3 
h1:xdCVXxEe0Y3FQith+0cj2irwZudqGYvecuLB1HtdexY=
+github.com/envoyproxy/go-control-plane v0.11.0/go.mod 
h1:VnHyVMpzcLvCFt9yUz1UnCwHLhwx1WguiVDV7pTG/tI=
 github.com/envoyproxy/protoc-gen-validate v0.9.1 
h1:PS7VIOgmSVhWUEeZwTe7z7zouA22Cr590PzXKbZHOVY=
+github.com/envoyproxy/protoc-gen-validate v0.10.0/go.mod 
h1:DRjgyB0I43LtJapqN6NiRwroiAU2PaFuvk/vjgh61ss=
 github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
 github.com/golang/glog v1.0.0 h1:nfP3RFugxnNRyKgeWd4oI1nYvXpxrx8ck8ZrcizshdQ=
+github.com/golang/glog v1.1.0/go.mod 
h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ=
+github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod 
h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
 github.com/golang/mock v1.1.1 h1:G5FRp8JnTd7RQH5kemVNlMeyXQAztQ3mOWV95KxsXH8=
+github.com/golang/protobuf v1.3.1/go.mod 
h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
+github.com/google/go-cmp v0.5.3/go.mod 
h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
 github.com/google/go-cmp v0.5.9/go.mod 
h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
 github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
+github.com/googleapis/enterprise-certificate-proxy v0.2.3/go.mod 
h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k=
+github.com/googleapis/gax-go/v2 v2.7.1/go.mod 
h1:4orTrqY6hXxxaUL4LHIPl6lGo8vAE38/qKbhSAKP6QI=
 github.com/grpc-ecosystem/grpc-gateway v1.16.0 
h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
 github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 
h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM=
 github.com/rogpeppe/fastuuid v1.2.0 
h1:Ppwyp6VYCF1nvBTXL3trRso7mXMlRrw9ooo375wvi2s=
 github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 
h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
 github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
 github.com/yuin/goldmark v1.4.13 
h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
+go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
 go.opentelemetry.io/proto/otlp v0.7.0 
h1:rwOQPCuKAKmwGKq2aVNnYIibI6wnV7EvzgfTCzcdGg8=
 golang.org/x/exp v0.0.0-20190121172915-509febef88a4 
h1:c2HOrn5iMezYjSlGPncknSEr/8x5LELb/ilJbXi9DEA=
 golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 
h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug=
+golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
+golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod 
h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
+golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod 
h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
 golang.org/x/oauth2 v0.4.0 h1:NF0gk8LVPg1Ml7SSbGyySuoxdsXitj7TvgvuRxIMc/M=
+golang.org/x/oauth2 v0.6.0/go.mod 
h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw=
+golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod 
h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
 golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
+golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
+golang.org/x/tools v0.7.0/go.mod 
h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
 golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 
h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
+google.golang.org/api v0.114.0/go.mod 
h1:ifYI2ZsFK6/uGddGfAD5BMxlnkBqCmqHSDUVi45N5Yg=
 google.golang.org/appengine v1.6.7 
h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
+google.golang.org/appengine v1.6.7/go.mod 
h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
+google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4/go.mod 
h1:NWraEVixdDnqcqQ30jipen1STv2r/n24Wb7twVTGR4s=
+google.golang.org/genproto v0.0.0-20230320184635-7606e756e683/go.mod 
h1:NWraEVixdDnqcqQ30jipen1STv2r/n24Wb7twVTGR4s=
+google.golang.org/grpc v1.33.2/go.mod 
h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
+google.golang.org/grpc v1.53.0/go.mod 
h1:OnIrk0ipVdj4N5d9IUoFUx72/VlD7+jUsHwZgwSMQpw=
+google.golang.org/protobuf v1.29.1/go.mod 
h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
 gopkg.in/src-d/go-billy.v4 v4.3.2 
h1:0SQA1pRztfTFx2miS8sA97XvooFeNOmvUenF4o0EcVg=
 gopkg.in/yaml.v2 v2.2.3 h1:fvjTMHxHEw/mxHbtzPi3JCcKXQRAnQTBRo6YCJSVHKI=
 honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc 
h1:/hemPrYIhOhy8zYrNj+069zDB68us2sMGsfkFJO0iZs=
diff --git a/imports.go b/imports.go
index 5f0f76b..c5c235c 100644
--- a/imports.go
+++ b/imports.go
@@ -26,4 +26,5 @@ import (
        _ "github.com/apache/skywalking-go/agent/core"
        _ "github.com/apache/skywalking-go/agent/core/operator"
        _ "github.com/apache/skywalking-go/agent/core/tracing"
+       _ "github.com/apache/skywalking-go/agent/reporter"
 )
diff --git a/reporter/reporter.go b/plugins/core/reporter/api.go
similarity index 86%
rename from reporter/reporter.go
rename to plugins/core/reporter/api.go
index 90a303d..0b546bf 100644
--- a/reporter/reporter.go
+++ b/plugins/core/reporter/api.go
@@ -18,8 +18,6 @@
 package reporter
 
 import (
-       "time"
-
        commonv3 "skywalking.apache.org/repo/goapi/collect/common/v3"
        agentv3 "skywalking.apache.org/repo/goapi/collect/language/agent/v3"
 )
@@ -29,22 +27,6 @@ import (
 // particular meanings.
 type Tag string
 
-// Span interface as commonv3 span specification
-type Span interface {
-       SetOperationName(string)
-       GetOperationName() string
-       SetPeer(string)
-       SetSpanLayer(agentv3.SpanLayer)
-       SetComponent(int32)
-       Tag(Tag, string)
-       Log(time.Time, ...string)
-       Error(time.Time, ...string)
-       End()
-       IsEntry() bool
-       IsExit() bool
-       IsValid() bool
-}
-
 // SegmentContext is the context in a segment
 type SegmentContext interface {
        GetTraceID() string
diff --git a/reporter/config_discovery.go 
b/plugins/core/reporter/config_discovery.go
similarity index 100%
rename from reporter/config_discovery.go
rename to plugins/core/reporter/config_discovery.go
diff --git a/reporter/grpc.go b/plugins/core/reporter/grpc/grpc.go
similarity index 94%
rename from reporter/grpc.go
rename to plugins/core/reporter/grpc/grpc.go
index d2962bb..18e9272 100644
--- a/reporter/grpc.go
+++ b/plugins/core/reporter/grpc/grpc.go
@@ -15,7 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
-package reporter
+package grpc
 
 import (
        "context"
@@ -24,16 +24,17 @@ import (
 
        "github.com/apache/skywalking-go/log"
 
-       "google.golang.org/grpc/credentials/insecure"
-
        "google.golang.org/grpc"
        "google.golang.org/grpc/connectivity"
        "google.golang.org/grpc/credentials"
+       "google.golang.org/grpc/credentials/insecure"
        "google.golang.org/grpc/metadata"
 
        configuration 
"skywalking.apache.org/repo/goapi/collect/agent/configuration/v3"
        agentv3 "skywalking.apache.org/repo/goapi/collect/language/agent/v3"
        managementv3 "skywalking.apache.org/repo/goapi/collect/management/v3"
+
+       "github.com/apache/skywalking-go/plugins/core/reporter"
 )
 
 const (
@@ -43,7 +44,7 @@ const (
 )
 
 // NewGRPCReporter create a new reporter to send data to gRPC oap server. Only 
one backend address is allowed.
-func NewGRPCReporter(logger log.Logger, serverAddr string, opts 
...GRPCReporterOption) (Reporter, error) {
+func NewGRPCReporter(logger log.Logger, serverAddr string, opts 
...ReporterOption) (reporter.Reporter, error) {
        r := &gRPCReporter{
                logger:        logger,
                sendCh:        make(chan *agentv3.SegmentObject, 
maxSendQueueSize),
@@ -71,13 +72,13 @@ func NewGRPCReporter(logger log.Logger, serverAddr string, 
opts ...GRPCReporterO
        r.managementClient = managementv3.NewManagementServiceClient(r.conn)
        if r.cdsInterval > 0 {
                r.cdsClient = 
configuration.NewConfigurationDiscoveryServiceClient(r.conn)
-               r.cdsService = NewConfigDiscoveryService()
+               r.cdsService = reporter.NewConfigDiscoveryService()
        }
        return r, nil
 }
 
 type gRPCReporter struct {
-       entity           *Entity
+       entity           *reporter.Entity
        logger           log.Logger
        sendCh           chan *agentv3.SegmentObject
        conn             *grpc.ClientConn
@@ -85,7 +86,7 @@ type gRPCReporter struct {
        managementClient managementv3.ManagementServiceClient
        checkInterval    time.Duration
        cdsInterval      time.Duration
-       cdsService       *ConfigDiscoveryService
+       cdsService       *reporter.ConfigDiscoveryService
        cdsClient        configuration.ConfigurationDiscoveryServiceClient
 
        md    metadata.MD
@@ -95,7 +96,7 @@ type gRPCReporter struct {
        bootFlag bool
 }
 
-func (r *gRPCReporter) Boot(entity *Entity, cdsWatchers 
[]AgentConfigChangeWatcher) {
+func (r *gRPCReporter) Boot(entity *reporter.Entity, cdsWatchers 
[]reporter.AgentConfigChangeWatcher) {
        r.entity = entity
        r.initSendPipeline()
        r.check()
@@ -103,7 +104,7 @@ func (r *gRPCReporter) Boot(entity *Entity, cdsWatchers 
[]AgentConfigChangeWatch
        r.bootFlag = true
 }
 
-func (r *gRPCReporter) Send(spans []ReportedSpan) {
+func (r *gRPCReporter) Send(spans []reporter.ReportedSpan) {
        spanSize := len(spans)
        if spanSize < 1 {
                return
@@ -217,7 +218,7 @@ func (r *gRPCReporter) initSendPipeline() {
        }()
 }
 
-func (r *gRPCReporter) initCDS(cdsWatchers []AgentConfigChangeWatcher) {
+func (r *gRPCReporter) initCDS(cdsWatchers 
[]reporter.AgentConfigChangeWatcher) {
        if r.cdsClient == nil {
                return
        }
diff --git a/plugins/core/reporter/grpc_opts_enhance.go 
b/plugins/core/reporter/grpc/grpc_opts_enhance.go
similarity index 80%
rename from plugins/core/reporter/grpc_opts_enhance.go
rename to plugins/core/reporter/grpc/grpc_opts_enhance.go
index 4338b21..f9231f4 100644
--- a/plugins/core/reporter/grpc_opts_enhance.go
+++ b/plugins/core/reporter/grpc/grpc_opts_enhance.go
@@ -15,7 +15,7 @@
 // specific language governing permissions and limitations
 // under the License.
 
-package reporter
+package grpc
 
 import (
        "time"
@@ -28,36 +28,40 @@ import (
 
 var authKey = "Authentication"
 
+// ReporterOption allows for functional options to adjust behavior
+// of a gRPC reporter to be created by NewGRPCReporter
+type ReporterOption func(r *gRPCReporter)
+
 // WithCheckInterval setup service and endpoint registry check interval
-func WithCheckInterval(interval time.Duration) GRPCReporterOption {
+func WithCheckInterval(interval time.Duration) ReporterOption {
        return func(r *gRPCReporter) {
                r.checkInterval = interval
        }
 }
 
 // WithMaxSendQueueSize setup send span queue buffer length
-func WithMaxSendQueueSize(maxSendQueueSize int) GRPCReporterOption {
+func WithMaxSendQueueSize(maxSendQueueSize int) ReporterOption {
        return func(r *gRPCReporter) {
                r.sendCh = make(chan *agentv3.SegmentObject, maxSendQueueSize)
        }
 }
 
 // WithTransportCredentials setup transport layer security
-func WithTransportCredentials(creds credentials.TransportCredentials) 
GRPCReporterOption {
+func WithTransportCredentials(creds credentials.TransportCredentials) 
ReporterOption {
        return func(r *gRPCReporter) {
                r.creds = creds
        }
 }
 
 // WithAuthentication used Authentication for gRPC
-func WithAuthentication(auth string) GRPCReporterOption {
+func WithAuthentication(auth string) ReporterOption {
        return func(r *gRPCReporter) {
                r.md = metadata.New(map[string]string{authKey: auth})
        }
 }
 
 // WithCDS setup Configuration Discovery Service to dynamic config
-func WithCDS(interval time.Duration) GRPCReporterOption {
+func WithCDS(interval time.Duration) ReporterOption {
        return func(r *gRPCReporter) {
                r.cdsInterval = interval
        }
diff --git a/plugins/core/reporter/grpc_struct.go 
b/plugins/core/reporter/grpc_struct.go
deleted file mode 100644
index 294a638..0000000
--- a/plugins/core/reporter/grpc_struct.go
+++ /dev/null
@@ -1,62 +0,0 @@
-// Licensed to Apache Software Foundation (ASF) under one or more contributor
-// license agreements. See the NOTICE file distributed with
-// this work for additional information regarding copyright
-// ownership. Apache Software Foundation (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.
-
-package reporter
-
-import (
-       "time"
-
-       "google.golang.org/grpc"
-       "google.golang.org/grpc/credentials"
-       "google.golang.org/grpc/metadata"
-
-       configuration 
"skywalking.apache.org/repo/goapi/collect/agent/configuration/v3"
-       commonv3 "skywalking.apache.org/repo/goapi/collect/common/v3"
-       agentv3 "skywalking.apache.org/repo/goapi/collect/language/agent/v3"
-       managementv3 "skywalking.apache.org/repo/goapi/collect/management/v3"
-)
-
-//skywalking:nocopy
-
-// All struct are from the reporter package in the library, copy these files 
is works for compiler
-
-// nolint
-type Entity struct {
-       ServiceName         string
-       ServiceInstanceName string
-       Props               []*commonv3.KeyStringValuePair
-       Layer               string
-}
-
-// nolint
-type gRPCReporter struct {
-       entity           Entity
-       sendCh           chan *agentv3.SegmentObject
-       conn             *grpc.ClientConn
-       traceClient      agentv3.TraceSegmentReportServiceClient
-       managementClient managementv3.ManagementServiceClient
-       checkInterval    time.Duration
-       cdsInterval      time.Duration
-       cdsClient        configuration.ConfigurationDiscoveryServiceClient
-
-       md    metadata.MD
-       creds credentials.TransportCredentials
-}
-
-// GRPCReporterOption allows for functional options to adjust behavior
-// of a gRPC reporter to be created by NewGRPCReporter
-type GRPCReporterOption func(r *gRPCReporter)
diff --git a/plugins/core/sampler.go b/plugins/core/sampler.go
index 539a05b..c9fb6ba 100644
--- a/plugins/core/sampler.go
+++ b/plugins/core/sampler.go
@@ -24,7 +24,7 @@ import (
        "sync"
        "time"
 
-       "github.com/apache/skywalking-go/reporter"
+       "github.com/apache/skywalking-go/plugins/core/reporter"
 )
 
 type Sampler interface {
diff --git a/plugins/core/span_default.go b/plugins/core/span_default.go
index f887883..31aeb4c 100644
--- a/plugins/core/span_default.go
+++ b/plugins/core/span_default.go
@@ -21,7 +21,7 @@ import (
        "math"
        "time"
 
-       "github.com/apache/skywalking-go/reporter"
+       "github.com/apache/skywalking-go/plugins/core/reporter"
 
        commonv3 "skywalking.apache.org/repo/goapi/collect/common/v3"
        agentv3 "skywalking.apache.org/repo/goapi/collect/language/agent/v3"
diff --git a/plugins/core/span_tracing.go b/plugins/core/span_tracing.go
index 3e0d8f6..cc2ab69 100644
--- a/plugins/core/span_tracing.go
+++ b/plugins/core/span_tracing.go
@@ -21,7 +21,7 @@ import (
        "fmt"
        "sync/atomic"
 
-       "github.com/apache/skywalking-go/reporter"
+       "github.com/apache/skywalking-go/plugins/core/reporter"
 
        commonv3 "skywalking.apache.org/repo/goapi/collect/common/v3"
        agentv3 "skywalking.apache.org/repo/goapi/collect/language/agent/v3"
diff --git a/plugins/core/test_base.go b/plugins/core/test_base.go
index bdadee4..8ca5abe 100644
--- a/plugins/core/test_base.go
+++ b/plugins/core/test_base.go
@@ -19,7 +19,7 @@ package core
 
 import (
        "github.com/apache/skywalking-go/plugins/core/operator"
-       "github.com/apache/skywalking-go/reporter"
+       "github.com/apache/skywalking-go/plugins/core/reporter"
 )
 
 var tlsData interface{}
diff --git a/plugins/core/tracer.go b/plugins/core/tracer.go
index 9b9585d..3adb86b 100644
--- a/plugins/core/tracer.go
+++ b/plugins/core/tracer.go
@@ -25,7 +25,7 @@ import (
 
        "github.com/apache/skywalking-go/log"
        "github.com/apache/skywalking-go/plugins/core/operator"
-       "github.com/apache/skywalking-go/reporter"
+       "github.com/apache/skywalking-go/plugins/core/reporter"
 )
 
 // nolint
diff --git a/plugins/core/tracer_test.go b/plugins/core/tracer_test.go
index 4648d2e..75acecb 100644
--- a/plugins/core/tracer_test.go
+++ b/plugins/core/tracer_test.go
@@ -21,7 +21,7 @@ import (
        "os"
        "testing"
 
-       "github.com/apache/skywalking-go/reporter"
+       "github.com/apache/skywalking-go/plugins/core/reporter"
 
        "github.com/stretchr/testify/assert"
 )
diff --git a/plugins/core/tracing.go b/plugins/core/tracing.go
index 930b0d9..1dc1a97 100644
--- a/plugins/core/tracing.go
+++ b/plugins/core/tracing.go
@@ -22,8 +22,8 @@ import (
 
        "github.com/pkg/errors"
 
+       "github.com/apache/skywalking-go/plugins/core/reporter"
        "github.com/apache/skywalking-go/plugins/core/tracing"
-       "github.com/apache/skywalking-go/reporter"
 )
 
 func (t *Tracer) Tracing() interface{} {
diff --git a/reporter/doc.go b/reporter/doc.go
deleted file mode 100644
index 4eeeffe..0000000
--- a/reporter/doc.go
+++ /dev/null
@@ -1,21 +0,0 @@
-// Licensed to Apache Software Foundation (ASF) under one or more contributor
-// license agreements. See the NOTICE file distributed with
-// this work for additional information regarding copyright
-// ownership. Apache Software Foundation (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.
-
-/*
-Package reporter holds reporters contain official reporter implementations.
-*/
-package reporter
diff --git a/reporter/grpc_opts.go b/reporter/grpc_opts.go
deleted file mode 100644
index 74c9db4..0000000
--- a/reporter/grpc_opts.go
+++ /dev/null
@@ -1,22 +0,0 @@
-// Licensed to Apache Software Foundation (ASF) under one or more contributor
-// license agreements. See the NOTICE file distributed with
-// this work for additional information regarding copyright
-// ownership. Apache Software Foundation (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.
-
-package reporter
-
-// GRPCReporterOption allows for functional options to adjust behavior
-// of a gRPC reporter to be created by NewGRPCReporter
-type GRPCReporterOption func(r *gRPCReporter)
diff --git a/reporter/imports.go b/reporter/imports.go
deleted file mode 100644
index 87c2170..0000000
--- a/reporter/imports.go
+++ /dev/null
@@ -1,25 +0,0 @@
-// Licensed to Apache Software Foundation (ASF) under one or more contributor
-// license agreements. See the NOTICE file distributed with
-// this work for additional information regarding copyright
-// ownership. Apache Software Foundation (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.
-
-package reporter
-
-import (
-       // imports required packages for gRPC reporter
-       _ "fmt"
-       _ "os"
-       _ "strconv"
-)
diff --git a/test/plugins/runner-helper/templates/dockerfile.tpl 
b/test/plugins/runner-helper/templates/dockerfile.tpl
index e360a09..73d7397 100644
--- a/test/plugins/runner-helper/templates/dockerfile.tpl
+++ b/test/plugins/runner-helper/templates/dockerfile.tpl
@@ -24,9 +24,12 @@ RUN mkdir -p /gotmp
 {{ end -}}
 {{ if .GreaterThanGo18 -}}
 RUN go work use 
test/plugins/workspace/{{.Context.ScenarioName}}/{{.Context.CaseName}}
+{{ else }}
+RUN echo "replace github.com/apache/skywalking-go => ../../../../../" >> 
test/plugins/workspace/{{.Context.ScenarioName}}/{{.Context.CaseName}}/go.mod
 {{ end -}}
 
 WORKDIR 
/skywalking-go/test/plugins/workspace/{{.Context.ScenarioName}}/{{.Context.CaseName}}/
+RUN go mod tidy
 
 ENV GO_BUILD_OPTS=" -toolexec \"/skywalking-go{{.ToolExecPath}}\" -a -work "
 
diff --git a/test/plugins/scenarios/gin/go.mod 
b/test/plugins/scenarios/gin/go.mod
index 88e650f..b954e97 100644
--- a/test/plugins/scenarios/gin/go.mod
+++ b/test/plugins/scenarios/gin/go.mod
@@ -3,7 +3,7 @@ module test
 go 1.18
 
 require (
-       github.com/apache/skywalking-go v0.0.0-20230426040646-191ee520f376
+       github.com/apache/skywalking-go v0.0.0-20230504123708-834b8ff1dbd0
        github.com/gin-gonic/gin v1.9.0
 )
 
diff --git a/test/plugins/scenarios/gin/go.sum 
b/test/plugins/scenarios/gin/go.sum
index 789ef09..aae5283 100644
--- a/test/plugins/scenarios/gin/go.sum
+++ b/test/plugins/scenarios/gin/go.sum
@@ -3,8 +3,8 @@ cloud.google.com/go v0.34.0/go.mod 
h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
 github.com/BurntSushi/toml v0.3.1/go.mod 
h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 github.com/OneOfOne/xxhash v1.2.2/go.mod 
h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
 github.com/antihax/optional v1.0.0/go.mod 
h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
-github.com/apache/skywalking-go v0.0.0-20230426040646-191ee520f376 
h1:25yu3g28hn1N2Cvk+1Xz8ilPowttKiZ/oDUk4xzdhw0=
-github.com/apache/skywalking-go v0.0.0-20230426040646-191ee520f376/go.mod 
h1:MCI8mjRhuedphUbUeqU/AXwR8P6ACfN/1Mq1WWnnRdk=
+github.com/apache/skywalking-go v0.0.0-20230504123708-834b8ff1dbd0 
h1:iX5uVpASnf6tagIDEhcdvIXIHIClYNFz3l7YWN2xa8s=
+github.com/apache/skywalking-go v0.0.0-20230504123708-834b8ff1dbd0/go.mod 
h1:MCI8mjRhuedphUbUeqU/AXwR8P6ACfN/1Mq1WWnnRdk=
 github.com/bytedance/sonic v1.5.0/go.mod 
h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
 github.com/bytedance/sonic v1.8.0 
h1:ea0Xadu+sHlu7x5O3gKhRpQ1IKiMrSiHttPF0ybECuA=
 github.com/bytedance/sonic v1.8.0/go.mod 
h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U=
diff --git a/tools/go-agent/instrument/agentcore/instrument.go 
b/tools/go-agent/instrument/agentcore/instrument.go
index 37b4fd3..aae1090 100644
--- a/tools/go-agent/instrument/agentcore/instrument.go
+++ b/tools/go-agent/instrument/agentcore/instrument.go
@@ -36,8 +36,11 @@ import (
 )
 
 var (
-       EnhanceBasePackage     = "github.com/apache/skywalking-go/agent/core"
-       EnhanceFromBasePackage = "github.com/apache/skywalking-go/plugins/core"
+       ProjectBasePackage      = "github.com/apache/skywalking-go/"
+       EnhanceBasePackage      = ProjectBasePackage + "agent/core"
+       EnhanceFromBasePackage  = ProjectBasePackage + "plugins/core"
+       ReporterFromBasePackage = "reporter"
+       ReporterBasePackage     = "agent/reporter"
 
        CopiedBasePackage = `skywalking-go(@[\d\w\.\-]+)?\/agent\/core`
        CopiedSubPackages = []string{"", "tracing", "operator"}
@@ -91,6 +94,7 @@ func (i *Instrument) WriteExtraFiles(dir string) ([]string, 
error) {
        for _, p := range CopiedSubPackages {
                pkgUpdates[filepath.Join(EnhanceFromBasePackage, p)] = 
filepath.Join(EnhanceBasePackage, p)
        }
+       pkgUpdates[filepath.Join(EnhanceFromBasePackage, 
ReporterFromBasePackage)] = filepath.Join(ProjectBasePackage, 
ReporterBasePackage)
        copiedFiles, err := tools.CopyGoFiles(core.FS, sub, dir, 
i.buildDSTDebugInfo, func(file *dst.File) {
                tools.ChangePackageImportPath(file, pkgUpdates)
        })
@@ -117,7 +121,7 @@ func (i *Instrument) WriteExtraFiles(dir string) ([]string, 
error) {
        return results, nil
 }
 
-func (i *Instrument) buildDSTDebugInfo(entry fs.DirEntry) (*tools.DebugInfo, 
error) {
+func (i *Instrument) buildDSTDebugInfo(entry fs.DirEntry, _ *dst.File) 
(*tools.DebugInfo, error) {
        if i.compileOpts.DebugDir == "" {
                return nil, nil
        }
@@ -129,7 +133,7 @@ func (i *Instrument) writeTracerInitLink(dir string) 
(string, error) {
        return tools.WriteFile(dir, "tracer_init.go", 
html.UnescapeString(tools.ExecuteTemplate(`package core
 
 import (
-       "github.com/apache/skywalking-go/reporter"
+       "github.com/apache/skywalking-go/agent/reporter"
        "fmt"
        "os"
        "strconv"
diff --git a/tools/go-agent/instrument/plugins/rewrite/rewrite.go 
b/tools/go-agent/instrument/plugins/rewrite/rewrite.go
index db8c7e3..f194612 100644
--- a/tools/go-agent/instrument/plugins/rewrite/rewrite.go
+++ b/tools/go-agent/instrument/plugins/rewrite/rewrite.go
@@ -20,7 +20,6 @@ package rewrite
 import (
        "fmt"
        "go/parser"
-       "go/token"
        "path/filepath"
 
        "github.com/apache/skywalking-go/tools/go-agent/tools"
@@ -103,13 +102,5 @@ func (c *Context) processSingleFile(file *dst.File, 
fromPackage string) {
        })
 
        // remove the import decl if empty
-       dstutil.Apply(file, func(cursor *dstutil.Cursor) bool {
-               if decl, ok := cursor.Node().(*dst.GenDecl); ok && decl.Tok == 
token.IMPORT && len(decl.Specs) == 0 {
-                       cursor.Delete()
-                       return false
-               }
-               return true
-       }, func(cursor *dstutil.Cursor) bool {
-               return true
-       })
+       tools.RemoveImportDefineIfNoPackage(file)
 }
diff --git a/tools/go-agent/instrument/reporter/instrument.go 
b/tools/go-agent/instrument/reporter/instrument.go
index c05caf8..4c36327 100644
--- a/tools/go-agent/instrument/reporter/instrument.go
+++ b/tools/go-agent/instrument/reporter/instrument.go
@@ -44,7 +44,7 @@ func NewGRPCInstrument() *GRPCInstrument {
 
 func (i *GRPCInstrument) CouldHandle(opts *api.CompileOptions) bool {
        i.compileOpts = opts
-       return opts.Package == "github.com/apache/skywalking-go/reporter"
+       return opts.Package == "github.com/apache/skywalking-go/agent/reporter"
 }
 
 func (i *GRPCInstrument) FilterAndEdit(path string, cursor *dstutil.Cursor, 
allFiles []*dst.File) bool {
@@ -60,13 +60,13 @@ func (i *GRPCInstrument) AfterEnhanceFile(fromPath, newPath 
string) error {
 }
 
 func (i *GRPCInstrument) WriteExtraFiles(dir string) ([]string, error) {
-       // copy gRPC reporter enhance files
+       // copy reporter api files
        results := make([]string, 0)
-       copiedFiles, err := tools.CopyGoFiles(core.FS, "reporter", dir, 
func(entry fs.DirEntry) (*tools.DebugInfo, error) {
+       copiedFiles, err := tools.CopyGoFiles(core.FS, "reporter", dir, 
func(entry fs.DirEntry, f *dst.File) (*tools.DebugInfo, error) {
                if i.compileOpts.DebugDir == "" {
                        return nil, nil
                }
-               debugPath := filepath.Join(i.compileOpts.DebugDir, "reporter", 
entry.Name())
+               debugPath := filepath.Join(i.compileOpts.DebugDir, "plugins", 
"core", "reporter", entry.Name())
                return tools.BuildDSTDebugInfo(debugPath, nil)
        }, func(file *dst.File) {
        })
@@ -75,6 +75,23 @@ func (i *GRPCInstrument) WriteExtraFiles(dir string) 
([]string, error) {
        }
        results = append(results, copiedFiles...)
 
+       // copy reporter implementations
+       reporterDirName := filepath.Join("reporter", "grpc")
+       copiedFiles, err = tools.CopyGoFiles(core.FS, reporterDirName, dir, 
func(entry fs.DirEntry, f *dst.File) (*tools.DebugInfo, error) {
+               if i.compileOpts.DebugDir == "" {
+                       return nil, nil
+               }
+               debugPath := filepath.Join(i.compileOpts.DebugDir, "plugins", 
"core", reporterDirName, entry.Name())
+               return tools.BuildDSTDebugInfo(debugPath, f)
+       }, func(file *dst.File) {
+               file.Name = dst.NewIdent("reporter")
+               tools.DeletePackageImports(file, 
"github.com/apache/skywalking-go/plugins/core/reporter")
+       })
+       if err != nil {
+               return nil, err
+       }
+       results = append(results, copiedFiles...)
+
        // generate the file for export the reporter
        file, err := i.generateReporterInitFile(dir)
        if err != nil {
diff --git a/tools/go-agent/tools/copy.go b/tools/go-agent/tools/copy.go
index 0ceeb1a..226cd52 100644
--- a/tools/go-agent/tools/copy.go
+++ b/tools/go-agent/tools/copy.go
@@ -28,7 +28,7 @@ import (
 )
 
 func CopyGoFiles(fromFS fs.ReadDirFS, fromDir, targetDir string,
-       debugInfoBuilder func(entry fs.DirEntry) (*DebugInfo, error),
+       debugInfoBuilder func(entry fs.DirEntry, file *dst.File) (*DebugInfo, 
error),
        peek func(file *dst.File)) ([]string, error) {
        results := make([]string, 0)
        files, err := fromFS.ReadDir(fromDir)
@@ -57,7 +57,7 @@ func CopyGoFiles(fromFS fs.ReadDirFS, fromDir, targetDir 
string,
                if err != nil {
                        return nil, err
                }
-               debugInfo, err := debugInfoBuilder(f)
+               debugInfo, err := debugInfoBuilder(f, parse)
                if err != nil {
                        return nil, err
                }
diff --git a/tools/go-agent/tools/dst.go b/tools/go-agent/tools/dst.go
index 39b49d3..8fbbde9 100644
--- a/tools/go-agent/tools/dst.go
+++ b/tools/go-agent/tools/dst.go
@@ -25,6 +25,7 @@ import (
        "go/token"
        "io"
        "os"
+       "path/filepath"
        "strings"
 
        "github.com/dave/dst"
@@ -48,6 +49,76 @@ func ChangePackageImportPath(file dst.Node, pkgChanges 
map[string]string) {
        })
 }
 
+func DeletePackageImports(file dst.Node, imports ...string) {
+       containsDeletedImport := false
+       deletedPackages := make(map[string]string)
+       dstutil.Apply(file, func(cursor *dstutil.Cursor) bool {
+               switch n := cursor.Node().(type) {
+               case *dst.ImportSpec:
+                       for _, pkg := range imports {
+                               if n.Path.Value == fmt.Sprintf("%q", pkg) {
+                                       containsDeletedImport = true
+                                       cursor.Delete()
+
+                                       if n.Name != nil {
+                                               deletedPackages[n.Name.Name] = 
pkg
+                                       } else {
+                                               
deletedPackages[filepath.Base(pkg)] = pkg
+                                       }
+                               }
+                       }
+                       return false
+               case *dst.SelectorExpr:
+                       pkgRefName, ok := n.X.(*dst.Ident)
+                       if !ok {
+                               return true
+                       }
+                       if _, ok := deletedPackages[pkgRefName.Name]; ok {
+                               removePackageRef(cursor.Parent(), n)
+                       }
+               }
+               return true
+       }, func(cursor *dstutil.Cursor) bool {
+               return true
+       })
+
+       if containsDeletedImport {
+               RemoveImportDefineIfNoPackage(file)
+       }
+}
+
+func removePackageRef(parent dst.Node, current *dst.SelectorExpr) {
+       switch p := parent.(type) {
+       case *dst.Field:
+               p.Type = dst.NewIdent(current.Sel.Name)
+       case *dst.Ellipsis:
+               p.Elt = dst.NewIdent(current.Sel.Name)
+       case *dst.StarExpr:
+               p.X = dst.NewIdent(current.Sel.Name)
+       case *dst.TypeAssertExpr:
+               p.Type = dst.NewIdent(current.Sel.Name)
+       case *dst.CompositeLit:
+               p.Type = dst.NewIdent(current.Sel.Name)
+       case *dst.ArrayType:
+               p.Elt = dst.NewIdent(current.Sel.Name)
+       case *dst.CallExpr:
+               p.Fun = dst.NewIdent(current.Sel.Name)
+       }
+}
+
+func RemoveImportDefineIfNoPackage(file dst.Node) {
+       // remove the import decl if empty
+       dstutil.Apply(file, func(cursor *dstutil.Cursor) bool {
+               if decl, ok := cursor.Node().(*dst.GenDecl); ok && decl.Tok == 
token.IMPORT && len(decl.Specs) == 0 {
+                       cursor.Delete()
+                       return false
+               }
+               return true
+       }, func(cursor *dstutil.Cursor) bool {
+               return true
+       })
+}
+
 type DebugInfo struct {
        FilePath     string
        Line         int


Reply via email to