Agent插件

云眼About 1 min

Agent插件

使用插件扩展云眼Agent。

Agent插件

云眼 Agent可以通过使用插件进行扩展。插件不同于为自定义逻辑提供命名空间环境的标准Agent包。插件必须作为Agent分发的一部分进行编译,并通过配置启用。

拦截器插件

拦截器定义示例

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"

Last update:
Contributors: “zhangweixue”