apitoolkit

package module
v1.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 9, 2024 License: MIT Imports: 19 Imported by: 1

Documentation

Index

Constants

View Source
const (
	GoDefaultSDKType = "GoBuiltIn"
	GoGinSDKType     = "GoGin"
	GoGorillaMux     = "GoGorillaMux"
	GoOutgoing       = "GoOutgoing"
	GoFiberSDKType   = "GoFiber"
)

Variables

View Source
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 RedactHeaders

func RedactHeaders(headers map[string][]string, redactList []string) map[string][]string

func RedactJSON

func RedactJSON(data []byte, redactList []string) []byte

func ReportError

func ReportError(ctx context.Context, err error)

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

func BuildError added in v1.0.2

func BuildError(err error) ATError

type Client

type Client struct {
	PublishMessage func(ctx context.Context, payload Payload) error
	// contains filtered or unexported fields
}

func NewClient

func NewClient(ctx context.Context, cfg Config) (*Client, error)

NewClient would initialize an APIToolkit client which we can use to push data to apitoolkit.

func (*Client) BuildFastHTTPPayload added in v1.0.4

func (c *Client) BuildFastHTTPPayload(SDKType string, trackingStart time.Time, req *fasthttp.RequestCtx,
	statusCode int, reqBody []byte, respBody []byte, respHeader map[string][]string,
	pathParams map[string]string, urlPath string,
	redactHeadersList,
	redactRequestBodyList, redactResponseBodyList []string,
	errorList []ATError,
	msgID uuid.UUID,
	parentID *uuid.UUID,
	referer string,
) Payload

func (*Client) BuildPayload added in v1.0.4

func (c *Client) BuildPayload(SDKType string, trackingStart time.Time, req *http.Request,
	statusCode int, reqBody []byte, respBody []byte, respHeader map[string][]string,
	pathParams map[string]string, urlPath string,
	redactHeadersList,
	redactRequestBodyList, redactResponseBodyList []string,
	errorList []ATError,
	msgID uuid.UUID,
	parentID *uuid.UUID,
) Payload

func (*Client) Close

func (c *Client) Close() error

Close cleans up the apitoolkit client. It should be called before the app shorts down, ideally as a defer call.

func (*Client) GetConfig added in v1.0.1

func (c *Client) GetConfig() *Config

func (*Client) GetMetadata added in v1.0.1

func (c *Client) GetMetadata() *ClientMetadata

func (*Client) ReportError added in v1.0.2

func (c *Client) ReportError(ctx context.Context, err error)

func (*Client) SetConfig added in v1.0.5

func (c *Client) SetConfig(cfg *Config)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL