Skip to content

Commit

Permalink
feat:add goframe plugins config
Browse files Browse the repository at this point in the history
  • Loading branch information
lidiwei committed Sep 20, 2024
1 parent b538914 commit b2723e9
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 12 deletions.
21 changes: 12 additions & 9 deletions docs/en/agent/plugin-configurations.md
Original file line number Diff line number Diff line change
@@ -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. |
| 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. |
25 changes: 25 additions & 0 deletions plugins/goframe/net/ghttp/config.go
Original file line number Diff line number Diff line change
@@ -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"`
}
30 changes: 29 additions & 1 deletion plugins/goframe/net/ghttp/intercepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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})
Expand All @@ -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)
}
4 changes: 4 additions & 0 deletions test/plugins/scenarios/goframe/bin/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 4 additions & 1 deletion test/plugins/scenarios/goframe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion tools/go-agent/config/agent.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
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}


0 comments on commit b2723e9

Please sign in to comment.