Agent插件
Agent插件
使用插件扩展云眼Agent。
Agent插件
云眼 Agent可以通过使用插件进行扩展。插件不同于为自定义逻辑提供命名空间环境的标准Agent包。插件必须作为Agent分发的一部分进行编译,并通过配置启用。
拦截器插件
- 拦截器可以添加到云眼Agent中,以通过实现拦截器接口来自定义请求和/或响应。
- 此接口定义一个基于 http 返回标准 net/HTTP 中间件处理程序的方法。处理程序接口。
Handler()
- 拦截器结构还可以包含一组可以通过文件配置的字段
config.yaml
- HTTPLog 拦截器插件 - 添加基于 go-chi/httplog 的 HTTP 请求日志记录。
拦截器定义示例
Go
package example import ( "context" "net/http" "github.com/eyeofcloud/agent/plugins/interceptors" ) // Example implements the Interceptor plugin interface type Example struct { // set of configuration fields RequestHeader string ResponseHeader string ContextValue string } func (i *Example) Handler() func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Header.Add("X-Example-Request", i.RequestHeader) // Example adding context to the request path ctx := context.WithValue(r.Context(), "example-context", i.ContextValue) // Continuing with the normal serving next.ServeHTTP(w, r.WithContext(ctx)) // Modify the response in some way w.Header().Add("X-Example-Response", i.ResponseHeader) }) } } // Register our interceptor as "example". func init() { interceptors.Add("example", func() interceptors.Interceptor { return &Example{} }) } ``` To make the interceptor available to Agent, add the plugin as an anonymous import into [all.go](./interceptors/all/all.go). ```go package all // Add imports here to trigger the plugin `init()` function import ( _ "github.com/eyeofcloud/agent/plugins/interceptors/example" ) ``` Enable the example interceptor by adding to `server.interceptors` within your `config.yaml`. Note that the yaml fields should match the struct definition of your plugin. ```yaml server: interceptors: example: requestHeader: "example-request" responseHeader: "example-response" contextValue: "example-context"