From b2723e913ba1d20a9a82a17927ba705e08ed7464 Mon Sep 17 00:00:00 2001 From: lidiwei Date: Fri, 20 Sep 2024 14:31:05 +0800 Subject: [PATCH] feat:add goframe plugins config --- docs/en/agent/plugin-configurations.md | 21 +++++++------ plugins/goframe/net/ghttp/config.go | 25 ++++++++++++++++ plugins/goframe/net/ghttp/intercepter.go | 30 ++++++++++++++++++- test/plugins/scenarios/goframe/bin/startup.sh | 4 +++ test/plugins/scenarios/goframe/main.go | 5 +++- tools/go-agent/config/agent.default.yaml | 11 ++++++- 6 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 plugins/goframe/net/ghttp/config.go diff --git a/docs/en/agent/plugin-configurations.md b/docs/en/agent/plugin-configurations.md index b4c73183..2024c0bc 100644 --- a/docs/en/agent/plugin-configurations.md +++ b/docs/en/agent/plugin-configurations.md @@ -1,11 +1,14 @@ # Plugin Configurations -| key | environment key | default value | description | -|--------------------------------|-------------------------------------------------------|---------------|----------------------------------------------------------------| -| 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. | -| redis.max_args_bytes | SW_AGENT_PLUGIN_CONFIG_REDIS_MAX_ARGS_BYTES | 1024 | Limit the bytes size of redis args request. | -| reporter.discard | SW_AGENT_REPORTER_DISCARD | false | Discard the reporter. | -| gin.collect_request_headers | SW_AGENT_PLUGIN_CONFIG_GIN_COLLECT_REQUEST_HEADERS | | Collect the http header of gin request. | -| gin.header_length_threshold | SW_AGENT_PLUGIN_CONFIG_GIN_HEADER_LENGTH_THRESHOLD | 2048 | Controlling the length limitation of all header values. | \ No newline at end of file +| key | environment key | default value | description | +|------------------------------------|-----------------------------------------------------------|---------------|----------------------------------------------------------------| +| 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. | +| redis.max_args_bytes | SW_AGENT_PLUGIN_CONFIG_REDIS_MAX_ARGS_BYTES | 1024 | Limit the bytes size of redis args request. | +| reporter.discard | SW_AGENT_REPORTER_DISCARD | false | Discard the reporter. | +| gin.collect_request_headers | SW_AGENT_PLUGIN_CONFIG_GIN_COLLECT_REQUEST_HEADERS | | Collect the http header of gin request. | +| gin.header_length_threshold | SW_AGENT_PLUGIN_CONFIG_GIN_HEADER_LENGTH_THRESHOLD | 2048 | Controlling the length limitation of all header values. | +| goframe.collect_request_parameters | SW_AGENT_PLUGIN_CONFIG_GOFRAME_COLLECT_REQUEST_PARAMETERS | false | Collect the parameters of the HTTP request on the server side. | +| goframe.collect_request_headers | SW_AGENT_PLUGIN_CONFIG_GOFRAME_COLLECT_REQUEST_HEADERS | | Collect the http header of goframe request. | +| goframe.header_length_threshold | SW_AGENT_PLUGIN_CONFIG_GOFRAME_HEADER_LENGTH_THRESHOLD | 2048 | Controlling the length limitation of all header values. | \ No newline at end of file diff --git a/plugins/goframe/net/ghttp/config.go b/plugins/goframe/net/ghttp/config.go new file mode 100644 index 00000000..bacb2733 --- /dev/null +++ b/plugins/goframe/net/ghttp/config.go @@ -0,0 +1,25 @@ +// 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 ghttp + +//skywalking:config goframe +var config struct { + CollectRequestParameters bool `config:"collect_request_parameters"` + CollectRequestHeaders []string `config:"collect_request_headers"` + HeaderLengthThreshold int `config:"header_length_threshold"` +} diff --git a/plugins/goframe/net/ghttp/intercepter.go b/plugins/goframe/net/ghttp/intercepter.go index ceda0f86..96eda925 100644 --- a/plugins/goframe/net/ghttp/intercepter.go +++ b/plugins/goframe/net/ghttp/intercepter.go @@ -21,6 +21,7 @@ package ghttp import ( "fmt" "net/http" + "strings" "github.com/apache/skywalking-go/plugins/core/operator" "github.com/apache/skywalking-go/plugins/core/tracing" @@ -42,7 +43,12 @@ func (h *GoFrameServerInterceptor) BeforeInvoke(invocation operator.Invocation) return err } - s.Tag(tracing.TagHTTPParams, request.URL.RawQuery) + if config.CollectRequestParameters && request.URL != nil { + s.Tag(tracing.TagHTTPParams, request.URL.RawQuery) + } + if len(config.CollectRequestHeaders) > 0 { + collectRequestHeaders(s, request.Header) + } writer := invocation.Args()[0].(http.ResponseWriter) invocation.ChangeArg(0, &writerWrapper{ResponseWriter: writer, statusCode: http.StatusOK}) @@ -67,3 +73,25 @@ type writerWrapper struct { http.ResponseWriter statusCode int } + +func collectRequestHeaders(span tracing.Span, requestHeaders http.Header) { + var headerTagValues []string + for _, header := range config.CollectRequestHeaders { + var headerValue = requestHeaders.Get(header) + if headerValue != "" { + headerTagValues = append(headerTagValues, header+"="+headerValue) + } + } + + if len(headerTagValues) == 0 { + return + } + + tagValue := strings.Join(headerTagValues, "\n") + if len(tagValue) > config.HeaderLengthThreshold { + maxLen := config.HeaderLengthThreshold + tagValue = tagValue[:maxLen] + } + + span.Tag(tracing.TagHTTPHeaders, tagValue) +} diff --git a/test/plugins/scenarios/goframe/bin/startup.sh b/test/plugins/scenarios/goframe/bin/startup.sh index b00cdfc7..7d283705 100755 --- a/test/plugins/scenarios/goframe/bin/startup.sh +++ b/test/plugins/scenarios/goframe/bin/startup.sh @@ -19,4 +19,8 @@ home="$(cd "$(dirname $0)"; pwd)" go build ${GO_BUILD_OPTS} -o goframe +export SW_AGENT_PLUGIN_CONFIG_GOFRAME_COLLECT_REQUEST_PARAMETERS=true +export SW_AGENT_PLUGIN_CONFIG_GOFRAME_COLLECT_REQUEST_HEADERS=h1,h2 +export SW_AGENT_PLUGIN_CONFIG_GOFRAME_HEADER_LENGTH_THRESHOLD=17 + ./goframe \ No newline at end of file diff --git a/test/plugins/scenarios/goframe/main.go b/test/plugins/scenarios/goframe/main.go index 78237cb9..9cdb9680 100644 --- a/test/plugins/scenarios/goframe/main.go +++ b/test/plugins/scenarios/goframe/main.go @@ -43,7 +43,10 @@ func main() { r.Response.Write("success") }) s.BindHandler("/consumer", func(r *ghttp.Request) { - var resp, err = g.Client().Get(gctx.GetInitCtx(), "http://localhost:8080/provider?test=1") + client := g.Client() + client.SetHeader("h1", "h1") + client.SetHeader("h2", "h2") + var resp, err = client.Get(gctx.GetInitCtx(), "http://localhost:8080/provider?test=1") if err != nil { r.Response.Write(err.Error()) return diff --git a/tools/go-agent/config/agent.default.yaml b/tools/go-agent/config/agent.default.yaml index c7e3f81e..8e14390d 100644 --- a/tools/go-agent/config/agent.default.yaml +++ b/tools/go-agent/config/agent.default.yaml @@ -110,4 +110,13 @@ plugin: # Collect the http header of gin request collect_request_headers: ${SW_AGENT_PLUGIN_CONFIG_GIN_COLLECT_REQUEST_HEADERS:} # Controlling the length limitation of all header values - header_length_threshold: ${SW_AGENT_PLUGIN_CONFIG_GIN_HEADER_LENGTH_THRESHOLD:2048} \ No newline at end of file + header_length_threshold: ${SW_AGENT_PLUGIN_CONFIG_GIN_HEADER_LENGTH_THRESHOLD:2048} + goframe: + # Collect the parameters of the HTTP request on the server side + collect_request_parameters: ${SW_AGENT_PLUGIN_CONFIG_GOFRAME_COLLECT_REQUEST_PARAMETERS:false} + # Collect the http header of goframe request + collect_request_headers: ${SW_AGENT_PLUGIN_CONFIG_GOFRAME_COLLECT_REQUEST_HEADERS:} + # Controlling the length limitation of all header values + header_length_threshold: ${SW_AGENT_PLUGIN_CONFIG_GOFRAME_HEADER_LENGTH_THRESHOLD:2048} + +