This is an automated email from the ASF dual-hosted git repository. liuhan 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 ea728c7 feat: support setting a discard type of reporter (#134) ea728c7 is described below commit ea728c7d8ab60144c7ac562dec32bad95cbd8850 Author: Rick <1450685+linuxsu...@users.noreply.github.com> AuthorDate: Tue Oct 24 12:37:56 2023 +0800 feat: support setting a discard type of reporter (#134) --- .github/workflows/plugin-tests.yaml | 1 + CHANGES.md | 1 + docs/en/agent/plugin-configurations.md | 1 + plugins/core/reporter/discard_reporter.go | 46 +++++++++++++++++ plugins/core/reporter/discard_reporter_test.go | 38 ++++++++++++++ .../{grpc => discard-reporter}/bin/startup.sh | 27 ++-------- .../config/excepted.yml} | 28 +---------- test/plugins/scenarios/discard-reporter/go.mod | 3 ++ test/plugins/scenarios/discard-reporter/go.sum | 0 test/plugins/scenarios/discard-reporter/main.go | 58 ++++++++++++++++++++++ .../bin/startup.sh => discard-reporter/plugin.yml} | 37 ++++---------- test/plugins/scenarios/grpc/bin/startup.sh | 4 +- tools/go-agent/Makefile | 2 +- tools/go-agent/config/agent.default.yaml | 1 + tools/go-agent/config/loader.go | 3 +- tools/go-agent/instrument/reporter/instrument.go | 3 ++ 16 files changed, 173 insertions(+), 80 deletions(-) diff --git a/.github/workflows/plugin-tests.yaml b/.github/workflows/plugin-tests.yaml index bb25241..bb747ea 100644 --- a/.github/workflows/plugin-tests.yaml +++ b/.github/workflows/plugin-tests.yaml @@ -89,6 +89,7 @@ jobs: - irisv12 - trace-activation - fasthttp + - discard-reporter steps: - uses: actions/checkout@v2 with: diff --git a/CHANGES.md b/CHANGES.md index f2c7283..81056e9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,7 @@ Release Notes. #### Features #### Plugins +* Support setting a discard type of reporter. #### Documentation diff --git a/docs/en/agent/plugin-configurations.md b/docs/en/agent/plugin-configurations.md index 088dd89..b68fbb1 100644 --- a/docs/en/agent/plugin-configurations.md +++ b/docs/en/agent/plugin-configurations.md @@ -5,3 +5,4 @@ | http.server_collect_parameters | SW_AGENT_PLUGIN_CONFIG_HTTP_SERVER_COLLECT_PARAMETERS | false | Collect the parameters of the HTTP request on the server side. | | mongo.collect_statement | SW_AGENT_PLUGIN_CONFIG_MONGO_COLLECT_STATEMENT | false | Collect the statement of the MongoDB request. | | sql.collect_parameter | SW_AGENT_PLUGIN_CONFIG_SQL_COLLECT_PARAMETER | false | Collect the parameter of the SQL request. | +| reporter.discard | SW_AGENT_REPORTER_DISCARD | false | Discard the reporter. | diff --git a/plugins/core/reporter/discard_reporter.go b/plugins/core/reporter/discard_reporter.go new file mode 100644 index 0000000..3531d46 --- /dev/null +++ b/plugins/core/reporter/discard_reporter.go @@ -0,0 +1,46 @@ +// 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 logv3 "skywalking.apache.org/repo/goapi/collect/logging/v3" + +type discardReporter struct{} + +func NewDiscardReporter() Reporter { + return &discardReporter{} +} + +func (r *discardReporter) Boot(entity *Entity, cdsWatchers []AgentConfigChangeWatcher) { + // do nothing +} +func (r *discardReporter) SendTracing(spans []ReportedSpan) { + // do nothing +} +func (r *discardReporter) SendMetrics(metrics []ReportedMeter) { + // do nothing +} +func (r *discardReporter) SendLog(log *logv3.LogData) { + // do nothing +} +func (r *discardReporter) ConnectionStatus() ConnectionStatus { + // do nothing + return 0 +} +func (r *discardReporter) Close() { + // do nothing +} diff --git a/plugins/core/reporter/discard_reporter_test.go b/plugins/core/reporter/discard_reporter_test.go new file mode 100644 index 0000000..795ae7b --- /dev/null +++ b/plugins/core/reporter/discard_reporter_test.go @@ -0,0 +1,38 @@ +// 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_test + +import ( + "testing" + + "github.com/apache/skywalking-go/plugins/core/reporter" +) + +func TestDiscardReporter(t *testing.T) { + r := reporter.NewDiscardReporter() + + // make sure no panic + r.Boot(nil, nil) + r.SendTracing(nil) + r.SendMetrics(nil) + r.SendLog(nil) + if status := r.ConnectionStatus(); status != 0 { + t.Errorf("expect 0, actual is %d", status) + } + r.Close() +} diff --git a/test/plugins/scenarios/grpc/bin/startup.sh b/test/plugins/scenarios/discard-reporter/bin/startup.sh old mode 100644 new mode 100755 similarity index 53% copy from test/plugins/scenarios/grpc/bin/startup.sh copy to test/plugins/scenarios/discard-reporter/bin/startup.sh index 83256a8..a598f53 --- a/test/plugins/scenarios/grpc/bin/startup.sh +++ b/test/plugins/scenarios/discard-reporter/bin/startup.sh @@ -16,28 +16,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -home="$(cd "$(dirname $0)";cd ..; pwd)" -cd $home +home="$(cd "$(dirname $0)"; pwd)" -echo "Installing protoc" -apt update && apt install -y protobuf-compiler -go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 -go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2 -export PATH="$PATH:$(go env GOPATH)/bin" +go build ${GO_BUILD_OPTS} -o http -echo "building API" -protoc -I=${home}/api --go_out=${home}/api --go-grpc_out=${home}/api ${home}/api/api.proto +export SW_AGENT_REPORTER_DISCARD=true +export SW_AGENT_PLUGIN_CONFIG_HTTP_SERVER_COLLECT_PARAMETERS=true -echo "building applications" -go build ${GO_BUILD_OPTS} -o server ./grpc_server/server.go -go build ${GO_BUILD_OPTS} -o client ./grpc_client/client.go - - -echo "starting server" -export SW_AGENT_NAME=grpc-server -./server & -sleep 2 - -echo "starting client" -export SW_AGENT_NAME=grpc-client -./client +./http \ No newline at end of file diff --git a/test/plugins/scenarios/grpc/bin/startup.sh b/test/plugins/scenarios/discard-reporter/config/excepted.yml similarity index 52% copy from test/plugins/scenarios/grpc/bin/startup.sh copy to test/plugins/scenarios/discard-reporter/config/excepted.yml index 83256a8..19366a4 100644 --- a/test/plugins/scenarios/grpc/bin/startup.sh +++ b/test/plugins/scenarios/discard-reporter/config/excepted.yml @@ -1,5 +1,3 @@ -#!/bin/bash -# # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -16,28 +14,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -home="$(cd "$(dirname $0)";cd ..; pwd)" -cd $home - -echo "Installing protoc" -apt update && apt install -y protobuf-compiler -go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 -go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2 -export PATH="$PATH:$(go env GOPATH)/bin" - -echo "building API" -protoc -I=${home}/api --go_out=${home}/api --go-grpc_out=${home}/api ${home}/api/api.proto - -echo "building applications" -go build ${GO_BUILD_OPTS} -o server ./grpc_server/server.go -go build ${GO_BUILD_OPTS} -o client ./grpc_client/client.go - - -echo "starting server" -export SW_AGENT_NAME=grpc-server -./server & -sleep 2 - -echo "starting client" -export SW_AGENT_NAME=grpc-client -./client +segmentItems: [] \ No newline at end of file diff --git a/test/plugins/scenarios/discard-reporter/go.mod b/test/plugins/scenarios/discard-reporter/go.mod new file mode 100644 index 0000000..c845935 --- /dev/null +++ b/test/plugins/scenarios/discard-reporter/go.mod @@ -0,0 +1,3 @@ +module test/plugins/scenarios/discard-reporter + +go 1.18 diff --git a/test/plugins/scenarios/discard-reporter/go.sum b/test/plugins/scenarios/discard-reporter/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/test/plugins/scenarios/discard-reporter/main.go b/test/plugins/scenarios/discard-reporter/main.go new file mode 100644 index 0000000..b59ef37 --- /dev/null +++ b/test/plugins/scenarios/discard-reporter/main.go @@ -0,0 +1,58 @@ +// 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 main + +import ( + "io" + "log" + "net/http" + + _ "github.com/apache/skywalking-go" +) + +func providerHandler(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte("success")) +} + +func consumerHandler(w http.ResponseWriter, r *http.Request) { + resp, err := http.Get("http://localhost:8080/provider?test=1") + if err != nil { + log.Printf("request provider error: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Print(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + _, _ = w.Write(body) +} + +func main() { + http.HandleFunc("/provider", providerHandler) + http.HandleFunc("/consumer", consumerHandler) + + http.HandleFunc("/health", func(writer http.ResponseWriter, request *http.Request) { + writer.WriteHeader(http.StatusOK) + }) + + _ = http.ListenAndServe(":8080", nil) +} diff --git a/test/plugins/scenarios/grpc/bin/startup.sh b/test/plugins/scenarios/discard-reporter/plugin.yml similarity index 52% copy from test/plugins/scenarios/grpc/bin/startup.sh copy to test/plugins/scenarios/discard-reporter/plugin.yml index 83256a8..401b195 100644 --- a/test/plugins/scenarios/grpc/bin/startup.sh +++ b/test/plugins/scenarios/discard-reporter/plugin.yml @@ -1,5 +1,3 @@ -#!/bin/bash -# # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information @@ -16,28 +14,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -home="$(cd "$(dirname $0)";cd ..; pwd)" -cd $home - -echo "Installing protoc" -apt update && apt install -y protobuf-compiler -go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28 -go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2 -export PATH="$PATH:$(go env GOPATH)/bin" - -echo "building API" -protoc -I=${home}/api --go_out=${home}/api --go-grpc_out=${home}/api ${home}/api/api.proto - -echo "building applications" -go build ${GO_BUILD_OPTS} -o server ./grpc_server/server.go -go build ${GO_BUILD_OPTS} -o client ./grpc_client/client.go - - -echo "starting server" -export SW_AGENT_NAME=grpc-server -./server & -sleep 2 - -echo "starting client" -export SW_AGENT_NAME=grpc-client -./client +entry-service: http://${HTTP_HOST}:${HTTP_PORT}/consumer +health-checker: http://${HTTP_HOST}:${HTTP_PORT}/health +start-script: ./bin/startup.sh +framework: go +export-port: 8080 +support-version: + - go: 1.17 + - go: 1.18 + - go: 1.19 + - go: 1.20 diff --git a/test/plugins/scenarios/grpc/bin/startup.sh b/test/plugins/scenarios/grpc/bin/startup.sh index 83256a8..096174e 100644 --- a/test/plugins/scenarios/grpc/bin/startup.sh +++ b/test/plugins/scenarios/grpc/bin/startup.sh @@ -29,8 +29,8 @@ echo "building API" protoc -I=${home}/api --go_out=${home}/api --go-grpc_out=${home}/api ${home}/api/api.proto echo "building applications" -go build ${GO_BUILD_OPTS} -o server ./grpc_server/server.go -go build ${GO_BUILD_OPTS} -o client ./grpc_client/client.go +CGO_ENABLED=0 go build ${GO_BUILD_OPTS} -o server ./grpc_server/server.go +CGO_ENABLED=0 go build ${GO_BUILD_OPTS} -o client ./grpc_client/client.go echo "starting server" diff --git a/tools/go-agent/Makefile b/tools/go-agent/Makefile index 3279245..3de51a1 100644 --- a/tools/go-agent/Makefile +++ b/tools/go-agent/Makefile @@ -51,4 +51,4 @@ build: linux darwin windows .PHONY: $(PLATFORMS) $(PLATFORMS): mkdir -p $(OUT_DIR) - GOOS=$(os) GOARCH=$(ARCH) $(GO_BUILD) $(GO_BUILD_FLAGS) -ldflags "$(GO_BUILD_LDFLAGS)" -o $(OUT_DIR)/$(BINARY)-$(VERSION)-$(os)-$(ARCH) ./cmd + CGO_ENABLED=0 GOOS=$(os) GOARCH=$(ARCH) $(GO_BUILD) $(GO_BUILD_FLAGS) -ldflags "$(GO_BUILD_LDFLAGS)" -o $(OUT_DIR)/$(BINARY)-$(VERSION)-$(os)-$(ARCH) ./cmd diff --git a/tools/go-agent/config/agent.default.yaml b/tools/go-agent/config/agent.default.yaml index 328f0a4..3045499 100644 --- a/tools/go-agent/config/agent.default.yaml +++ b/tools/go-agent/config/agent.default.yaml @@ -32,6 +32,7 @@ agent: max_value_size: ${SW_AGENT_CORRELATION_MAX_VALUE_SIZE:128} reporter: + discard: ${SW_AGENT_REPORTER_DISCARD:false} grpc: # The gRPC server address of the backend service. backend_service: ${SW_AGENT_REPORTER_GRPC_BACKEND_SERVICE:127.0.0.1:11800} diff --git a/tools/go-agent/config/loader.go b/tools/go-agent/config/loader.go index 8eae924..fa9c56b 100644 --- a/tools/go-agent/config/loader.go +++ b/tools/go-agent/config/loader.go @@ -53,7 +53,8 @@ type Agent struct { } type Reporter struct { - GRPC GRPCReporter `yaml:"grpc"` + Discard StringValue `yaml:"discard"` + GRPC GRPCReporter `yaml:"grpc"` } type Log struct { diff --git a/tools/go-agent/instrument/reporter/instrument.go b/tools/go-agent/instrument/reporter/instrument.go index 30ed284..2d83bb7 100644 --- a/tools/go-agent/instrument/reporter/instrument.go +++ b/tools/go-agent/instrument/reporter/instrument.go @@ -120,6 +120,9 @@ import ( ) func {{.InitFuncName}}(logger operator.LogOperator) (Reporter, error) { + if {{.Config.Reporter.Discard.ToGoBoolValue}} { + return NewDiscardReporter(), nil + } var opts []ReporterOption checkIntervalVal := {{.Config.Reporter.GRPC.CheckInterval.ToGoIntValue "the GRPC reporter check interval must be number"}} opts = append(opts, WithCheckInterval(time.Second * time.Duration(checkIntervalVal)))