Documentation ¶
Overview ¶
APIToolkit: The API Toolkit golang client is an sdk used to integrate golang web services with APIToolkit. It monitors incoming traffic, gathers the requests and sends the request to the apitoolkit servers.
APIToolkit go sdk can be used with most popular Golang routers off the box. And if your routing library of choice is not supported, feel free to leave an issue on github, or send in a pull request.
Here's how the SDK can be used with a gin server: ```go
func main(){ // Initialize the client using your apitoolkit.io generated apikey apitoolkitClient, err := apitoolkit.NewClient(context.Background(), apitoolkit.Config{APIKey: "<APIKEY>"}) if err != nil { panic(err) } router := gin.New() // Register with the corresponding middleware of your choice. For Gin router, we use the GinMiddleware method. router.Use(apitoolkitClient.GinMiddleware) // Register your handlers as usual and run the gin server as usual. router.POST("/:slug/test", func(c *gin.Context) {c.String(200, "ok")}) }
```
Index ¶
- Constants
- Variables
- func HTTPClient(ctx context.Context, opts ...RoundTripperOption) *http.Client
- func ReportError(ctx context.Context, err error)
- type ATError
- type Client
- func (c *Client) ChiMiddleware(next http.Handler) http.Handler
- func (c *Client) Close() error
- func (c *Client) EchoMiddleware(next echo.HandlerFunc) echo.HandlerFunc
- func (c *Client) FiberMiddleware(ctx *fiber.Ctx) error
- func (c *Client) GinMiddleware(ctx *gin.Context)
- func (c *Client) GorillaMuxMiddleware(next http.Handler) http.Handler
- func (c *Client) Middleware(next http.Handler) http.Handler
- func (c *Client) WrapRoundTripper(ctx context.Context, rt http.RoundTripper, opts ...RoundTripperOption) http.RoundTripper
- type ClientMetadata
- type Config
- type Payload
- type RoundTripperOption
Constants ¶
const ( GoDefaultSDKType = "GoBuiltIn" GoGinSDKType = "GoGin" GoGorillaMux = "GoGorillaMux" GoOutgoing = "GoOutgoing" GoFiberSDKType = "GoFiber" )
Variables ¶
var ( ErrorListCtxKey = ctxKey("error-list") CurrentRequestMessageID = ctxKey("current-req-msg-id") CurrentClient = ctxKey("current=apitoolkit-client") )
Functions ¶
func HTTPClient ¶
func HTTPClient(ctx context.Context, opts ...RoundTripperOption) *http.Client
func ReportError ¶
ReportError Allows you to report an error from your server to APIToolkit. This error would be associated with a given request, and helps give a request more context especially when investigating incidents
Types ¶
type ATError ¶
type ATError struct { When time.Time `json:"when,omitempty"` ErrorType string `json:"error_type,omitempty"` RootErrorType string `json:"root_error_type,omitempty"` Message string `json:"message,omitempty"` RootErrorMessage string `json:"root_error_message,omitempty"` StackTrace string `json:"stack_trace,omitempty"` }
ATError is the Apitoolkit error type/object
type Client ¶
type Client struct { PublishMessage func(ctx context.Context, payload Payload) error // contains filtered or unexported fields }
func NewClient ¶
NewClient would initialize an APIToolkit client which we can use to push data to apitoolkit.
func (*Client) ChiMiddleware ¶
ChiMiddleware is for the Golang Chi router and collects request, response parameters and publishes the payload
func (*Client) Close ¶
Close cleans up the apitoolkit client. It should be called before the app shorts down, ideally as a defer call.
func (*Client) EchoMiddleware ¶
func (c *Client) EchoMiddleware(next echo.HandlerFunc) echo.HandlerFunc
EchoMiddleware middleware for echo framework, collects requests, response and publishes the payload
func (*Client) GinMiddleware ¶
func (*Client) GorillaMuxMiddleware ¶
GorillaMuxMiddleware is for the gorilla mux routing library and collects request, response parameters and publishes the payload
func (*Client) Middleware ¶
Middleware collects request, response parameters and publishes the payload
func (*Client) WrapRoundTripper ¶
func (c *Client) WrapRoundTripper(ctx context.Context, rt http.RoundTripper, opts ...RoundTripperOption) http.RoundTripper
WrapRoundTripper returns a new RoundTripper which traces all requests sent over the transport.
type ClientMetadata ¶
type ClientMetadata struct { ProjectId string `json:"project_id"` PubsubProjectId string `json:"pubsub_project_id"` TopicID string `json:"topic_id"` PubsubPushServiceAccount json.RawMessage `json:"pubsub_push_service_account"` }
type Config ¶
type Config struct { Debug bool // VerboseDebug should never be enabled in production // and logs entire message body which gets sent to APIToolkit VerboseDebug bool RootURL string APIKey string ProjectID string // ServiceVersion is an identifier to help you track deployments. This could be a semver version or a git hash or anything you like. ServiceVersion string // A list of field headers whose values should never be sent to apitoolkit RedactHeaders []string RedactRequestBody []string RedactResponseBody []string // Tags are arbitrary identifiers for service being tracked, and can be used as filters on apitoolkit. Tags []string `json:"tags"` }
type Payload ¶
type Payload struct { Timestamp time.Time `json:"timestamp"` RequestHeaders map[string][]string `json:"request_headers"` QueryParams map[string][]string `json:"query_params"` PathParams map[string]string `json:"path_params"` ResponseHeaders map[string][]string `json:"response_headers"` Method string `json:"method"` SdkType string `json:"sdk_type"` Host string `json:"host"` RawURL string `json:"raw_url"` Referer string `json:"referer"` ProjectID string `json:"project_id"` URLPath string `json:"url_path"` ResponseBody []byte `json:"response_body"` RequestBody []byte `json:"request_body"` ProtoMinor int `json:"proto_minor"` StatusCode int `json:"status_code"` ProtoMajor int `json:"proto_major"` Duration time.Duration `json:"duration"` Errors []ATError `json:"errors"` ServiceVersion *string `json:"service_version"` Tags []string `json:"tags"` MsgID string `json:"msg_id"` ParentID *string `json:"parent_id"` }
Payload represents request and response details FIXME: How would we handle errors from background processes (Not web requests)
type RoundTripperOption ¶
type RoundTripperOption func(*roundTripperConfig)
func WithHTTPClient ¶
func WithHTTPClient(httpClient *http.Client) RoundTripperOption
WithHTTPClient allows you supply your own custom http client
func WithRedactHeaders ¶
func WithRedactHeaders(headers ...string) RoundTripperOption
func WithRedactRequestBody ¶
func WithRedactRequestBody(fields ...string) RoundTripperOption
func WithRedactResponseBody ¶
func WithRedactResponseBody(fields ...string) RoundTripperOption