Documentation
¶
Overview ¶
Package requestlog is the Request log (C-23): the recent HTTP requests Muster sent for a Connection or an outgoing webhook Destination, and the requests that reached a Connection's callbacks, each with its answer. The outbound package hands each attempt to the Writer already masked of the Secrets of its client and request, and Inbound each request of a callback masked of the values of outbound.MaskedHeaders; the Writer applies the shape masking — signed action ids, `/start` tokens, the token of an account_link event — and the truncation of bodies, URLs and headers, and inserts one row with the business time on the main pool, outside any transaction of the caller, never failing or retrying the request it records. A record's owner is its Connection, else its outgoing webhook Destination, else nobody. The Reader shows only the records newer than request_log.max_age among the newest request_log.max_records of their owner, and Prune deletes the rest.
Index ¶
- Constants
- Variables
- func Annotate(ctx context.Context, l Links)
- func Inbound(rec InboundRecorder, realClock clock.Clock, operation string, ...) http.Handler
- func Prune(ctx context.Context, db dbgen.DBTX, orgID int64, now time.Time) (int64, error)
- type AlertGroupRef
- type Body
- type Config
- type DestinationRef
- type EnvelopeRecord
- type EnvelopeRecorder
- type Filter
- type InboundRecord
- type InboundRecorder
- type Links
- type OwnerKind
- type Page
- type Position
- type Queries
- type Reader
- type Record
- type Summary
- type Writer
Constants ¶
const ( OperationPress = "press" OperationUpdate = "update" OperationPush = "push" OperationInteractive = "interactive" )
The operations of inbound records (C-23.FR-2): a Mattermost button press, a Telegram update in the webhook mode, a pushed Bridge event, and a Slack interaction — a press in http mode, and the envelope of the type interactive in Socket Mode, whose record takes the operation of its type as every envelope's does (C-24.FR-14).
const ( // MaxRecords is request_log.max_records: the records kept of each owner. MaxRecords = 500 // MaxAge is request_log.max_age, on the business clock. MaxAge = 7 * 24 * time.Hour // BodyMax is request_log.body_max: the bytes kept of a body. BodyMax = 16384 )
The bounds of the Request log (defaults.md, P-55 to P-57).
const ( DirectionOutbound = "outbound" DirectionInbound = "inbound" )
The directions of a record.
const ( Status2xx = "2xx" Status3xx = "3xx" Status4xx = "4xx" Status5xx = "5xx" StatusNoAnswer = "none" )
The status classes of Filter.StatusClass.
const MethodWebSocket = "WEBSOCKET"
MethodWebSocket is the method of the record of an envelope received over a WebSocket, which has no HTTP method.
const PruneBatch = 5000
PruneBatch is the most records one statement of the pruning deletes.
const WriteTimeout = 2 * time.Second
WriteTimeout bounds the write of one record.
Variables ¶
var ErrNotFound = errors.New("no such request record, connection or destination")
ErrNotFound is a record, a Connection or a Destination that does not exist, is deleted or is past a bound.
var ErrOneOfRequired = errors.New("exactly one of a connection and a destination, or a check run, is needed")
ErrOneOfRequired is a list that names neither a Connection, a Destination nor a check run, or both a Connection and a Destination.
Functions ¶
func Annotate ¶
Annotate tells the Request log what the request of a callback wrapped by Inbound concerns: its Connection, without which the request is not recorded, and, once matched, its Destination and Alert Group. A zero field keeps what an earlier call set; outside Inbound it does nothing.
func Inbound ¶
func Inbound(rec InboundRecorder, realClock clock.Clock, operation string, next http.Handler) http.Handler
Inbound wraps the callback next of operation (C-23.FR-1, FR-3): it keeps the request's headers and the body as the handler reads it, holds Muster's answer back while the handler runs and times it on the real clock. Once the handler returned it sends the answer with its length and flushes it, so that the caller has all of it, and then, when the handler named the request's Connection with Annotate, hands the record — the values of outbound.MaskedHeaders [redacted] — to rec, which masks it further and writes it. A request without a Connection, for an unknown or deleted one, leaves no trace; a record that cannot be written leaves the answer as it was. The answer is not flushed, hijacked or unwrapped while the handler runs: a callback that needs to would need answerWriter to allow it.
func Prune ¶
Prune deletes the records of the Organization orgID older than request_log.max_age at now and then, for each Connection, each outgoing webhook Destination and the records without an owner, those past the newest request_log.max_records, PruneBatch rows per statement until one comes back short (C-23.FR-4). It deletes only what is past a bound, so two overlapping runs are harmless. It returns how many it deleted; an owner that fails does not stop the others.
Types ¶
type AlertGroupRef ¶
AlertGroupRef names the Alert Group a record concerns.
type Body ¶
Body is a body as Inbound kept it: Data is all of it when Whole, nothing otherwise; Size is its full size, the declared length when its handler stopped before the end.
type Config ¶
type Config struct {
OrgID int64
// DB is the main pool, never a transaction of the caller: a record is written whatever becomes of the caller's.
DB dbgen.DBTX
// Business dates the records.
Business clock.Clock
Log *logging.Logger
// Timeout bounds each write; 0 is WriteTimeout.
Timeout time.Duration
}
Config is what a Writer needs.
type DestinationRef ¶
type DestinationRef struct {
PublicID string
Name string
Type string
Health string
BrokenSince *time.Time
BrokenReason *string
}
DestinationRef names the Destination a record concerns, with its health.
type EnvelopeRecord ¶
type EnvelopeRecord struct {
Links Links
Operation string
URL string
Envelope []byte
Confirmation []byte
Via string
Latency time.Duration
}
EnvelopeRecord is an envelope that reached a Connection over a WebSocket and was confirmed on it — an envelope of Slack's Socket Mode (C-24.FR-14, C-23.FR-1): its Connection, its type as the operation, the socket's address without its query, the envelope as received, Muster's confirmation, the path of the socket — outbound.ViaDirect or outbound.ViaProxy — and the time from its receipt to the confirmation on the real clock.
type EnvelopeRecorder ¶
type EnvelopeRecorder interface {
RecordEnvelope(ctx context.Context, r EnvelopeRecord)
}
EnvelopeRecorder keeps the records of envelopes; *Writer implements it.
type Filter ¶
type Filter struct {
Connection string
Destination string
Run string
Direction string
StatusClass string
Operation string
AlertGroup string
After *Position
Limit int
}
Filter is what a list shows: the records of exactly one owner, a Connection or a Destination by public_id — all those of an outgoing webhook, and those of its Connection that name a messenger Destination — or, with Run alone, of a check run by public_id, those whose owner is a Connection or nobody; Run beside an owner narrows its list. Then those of a direction, status class, operation and Alert Group when set, after a position, at most Limit of them (1 to MaxRecords).
type InboundRecord ¶
type InboundRecord struct {
Links Links
Operation string
Method string
URL string
RequestHeader http.Header
Request Body
Status int
ResponseHeader http.Header
Response Body
Latency time.Duration
}
InboundRecord is a request that reached a callback, as Inbound hands it over: what it concerns, its method, path and query as received, its headers and Muster's answer with the values of outbound.MaskedHeaders [redacted], both bodies, and the handler's time on the real clock.
type InboundRecorder ¶
type InboundRecorder interface {
RecordInbound(ctx context.Context, r InboundRecord)
}
InboundRecorder keeps the records of the requests that reach the callbacks; *Writer implements it.
type Links ¶
Links are what an inbound request concerns, as its callback tells Annotate: its Connection by internal id and public_id, and the Destination and the Alert Group it was matched to, 0 for none.
type OwnerKind ¶
type OwnerKind string
OwnerKind is whose Request log a record is in, which decides the Permission that reads it.
type Page ¶
Page is one page of a list, newest first; Next is where the next one starts, nil after the last.
type Queries ¶
type Queries interface {
InsertRecord(ctx context.Context, arg dbgen.InsertRecordParams) error
FindConnection(ctx context.Context, arg dbgen.FindConnectionParams) (int64, error)
FindDestination(ctx context.Context, arg dbgen.FindDestinationParams) (dbgen.FindDestinationRow, error)
ListConnectionRecords(ctx context.Context, arg dbgen.ListConnectionRecordsParams) (
[]dbgen.ListConnectionRecordsRow, error)
ListDestinationRecords(ctx context.Context, arg dbgen.ListDestinationRecordsParams) (
[]dbgen.ListDestinationRecordsRow, error)
ListRunRecords(ctx context.Context, arg dbgen.ListRunRecordsParams) ([]dbgen.ListRunRecordsRow, error)
GetRecord(ctx context.Context, arg dbgen.GetRecordParams) (dbgen.GetRecordRow, error)
CountNewerOfConnection(ctx context.Context, arg dbgen.CountNewerOfConnectionParams) (int64, error)
CountNewerOfDestination(ctx context.Context, arg dbgen.CountNewerOfDestinationParams) (int64, error)
CountNewerUnowned(ctx context.Context, arg dbgen.CountNewerUnownedParams) (int64, error)
PruneOlder(ctx context.Context, arg dbgen.PruneOlderParams) (int64, error)
ListConnectionIDs(ctx context.Context, orgID int64) ([]int64, error)
ListWebhookDestinationIDs(ctx context.Context, orgID int64) ([]int64, error)
PruneConnectionExcess(ctx context.Context, arg dbgen.PruneConnectionExcessParams) (int64, error)
PruneDestinationExcess(ctx context.Context, arg dbgen.PruneDestinationExcessParams) (int64, error)
PruneUnownedExcess(ctx context.Context, arg dbgen.PruneUnownedExcessParams) (int64, error)
}
Queries are the queries of the package; *dbgen.Queries implements them.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader reads the Request log of one Organization within its bounds (C-23.FR-4, FR-5).
func NewReader ¶
NewReader returns the Reader of the Organization orgID over db; business tells the age bound.
func (*Reader) Get ¶
Get reads the record publicID with its headers and bodies, and whose Request log it is in; ErrNotFound when it does not exist, its owner is deleted, or it is past either bound (C-23.FR-4, FR-5).
func (*Reader) List ¶
List lists the records f names, newest first, within the bounds (C-23.FR-5): ErrOneOfRequired unless it names exactly one owner or a check run alone, ErrNotFound for an owner that does not exist or is deleted. A check run or an Alert Group that is no public_id of its kind matches nothing.
type Record ¶
type Record struct {
Summary
Owner OwnerKind
RequestHeaders map[string][]string
RequestBody string
RequestBodyBytes int64
ResponseHeaders map[string][]string
ResponseBody *string
ResponseBodyBytes int64
Error *string
}
Record is one record with its headers and bodies; the answer's are nil without an answer.
type Summary ¶
type Summary struct {
ID int64
PublicID string
At time.Time
Direction string
Operation string
Method string
URL string
Status *int
LatencyMS int64
Via string
Attempt int
ErrorClass *string
ErrorCode *string
CheckKind *string
CheckRun *string
Connection *string
Destination *DestinationRef
AlertGroup *AlertGroupRef
RequestBodyTruncated bool
ResponseBodyTruncated bool
}
Summary is a record as a list shows it.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer writes the records of the Request log of one Organization; it is the outbound package's Recorder and the InboundRecorder of the callbacks.
func (*Writer) Record ¶
Record writes the outbound record r (C-23.FR-1, FR-2, FR-4): shape masking, truncation and one INSERT with the business time, under a context that does not end with the caller's and ends after the timeout. A record that cannot be written is logged without its content and counted, never returned: the request it records is unchanged.
func (*Writer) RecordEnvelope ¶
func (w *Writer) RecordEnvelope(ctx context.Context, r EnvelopeRecord)
RecordEnvelope writes the record of an envelope (C-23.FR-1, FR-2, C-24.FR-14) as RecordInbound writes the record of a callback's request: the direction inbound, the attempt 1, no error; the method WEBSOCKET, no request headers, the envelope as the request body and the confirmation as the answer's body without a status or answer headers, shape-masked and cut as every record. Its operation is the envelope's type, sanitized and cut to what a record stores, envelope when it has none. Like Record it fails nothing; a nil Writer records nothing.
func (*Writer) RecordInbound ¶
func (w *Writer) RecordInbound(ctx context.Context, r InboundRecord)
RecordInbound writes the record of a request that reached a callback (C-23.FR-1 to FR-4): the direction inbound, the path direct, the attempt 1, no error, Muster's answer as its status, shape-masked and cut as an outbound record; a body that Inbound did not keep whole is stored empty, marked truncated, with its size, and a method longer than a record stores is cut. Like Record it fails nothing; a nil Writer records nothing.