Documentation
¶
Overview ¶
Package logsmonitor finds the CloudWatch Logs log groups of deployed stacks and tails them: upstream's api/logs-monitor.
FindLogGroups finds a stack's log groups from its deployed resources and its template. A Monitor reads the new events of the log groups added to it while it runs, and sends each to the host as CDK_TOOLKIT_I5033, a line of the form "[<log group>] <time> <message>". Monitor.AddStack finds a stack's log groups, adds them to a monitor, and sends CDK_TOOLKIT_I5031 naming them, as upstream's deploy does for each stack it deploys when it traces logs. Upstream's watch starts its monitor between deployments and stops it while deploying, so that log events and deployment events don't interleave.
It differs from upstream in these ways:
- A failure to find a stack's log groups is sent by Monitor.AddStack as CDK_TOOLKIT_E5035, the code of the monitor's own failures, and never becomes the result of the stack's deployment. Upstream's deploy finds them in the finally of the stack's deployment, so the failure replaces the deployment's own error, or fails a deployment that succeeded.
- Monitor.Stop cancels a read in flight, then reads the log groups once more, for at most 30 seconds, before it sends CDK_TOOLKIT_I5034, so that the events logged since the last read are sent. Upstream's deactivate stops reading at once and forgets the log groups, so those events are never read.
- FindLogGroups makes no STS call for the partition. Upstream's finder asks STS for it, to build a template evaluator whose functions it never calls, so a failure of that call fails the finder.
- A function's LoggingConfig that refers by Ref to a resource whose LogGroupName isn't a string, such as an Fn::Join, gives no log group. Upstream's gives the value itself as the log group's name, and every read of it fails. The log group the Ref names is found anyway by its own resource, unless an ignored resource refers to it.
- CDK_TOOLKIT_I5033 gives its time as 15:04:05, in local time, without colour, as stack events do. Upstream's form depends on the machine's locale.
- A function without a physical ID gives the log group "/aws/lambda/", which no function has. Upstream's gives "/aws/lambda/undefined", the log group of any function named undefined, so it can tail another function's logs.
Index ¶
Constants ¶
const PollingInterval = 2 * time.Second
PollingInterval is how long a Monitor waits after reading its log groups before it reads them again.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Found ¶
type Found struct {
// Environment is the stack's environment, resolved.
Environment cloudassembly.Environment
// Logs reads the log groups.
Logs Logs
// Groups are the names of the log groups, in the order the stack's resources are listed; a name may appear more than once.
Groups []string
}
Found is the log groups of one deployed stack: upstream's FoundLogGroupsResult.
func FindLogGroups ¶
func FindLogGroups(ctx context.Context, io *iohelper.Helper, access *awsauth.Access, s *cloudassembly.Stack) (Found, error)
FindLogGroups finds the log groups of s's deployed resources, read with access: upstream's findCloudWatchLogGroups.
They are each AWS::Logs::LogGroup's, by its physical ID, unless a resource of type AWS::EC2::FlowLog, AWS::CloudTrail::Trail or AWS::CodeBuild::Project refers to it in s's template, by any string equal to its logical ID; and each AWS::Lambda::Function's: the log group its LoggingConfig names, as a string or by a Ref to a resource whose LogGroupName is a string, or else /aws/lambda/ followed by its physical ID. The resources are listed, every page, through s's lookup role, or, when that fails, with a debug message, through the base credentials. It fails when s's environment can't be resolved, when the base credentials can't be used either, when s's template can't be read, or when the resources can't be listed; when ctx is done and the lookup role fails, it returns that failure without trying the base credentials.
type Logs ¶
type Logs interface {
FilterLogEvents(ctx context.Context, in *cloudwatchlogs.FilterLogEventsInput, opts ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.FilterLogEventsOutput, error)
}
Logs is the part of the CloudWatch Logs API a Monitor reads with, which *cloudwatchlogs.Client satisfies.
type Monitor ¶
type Monitor struct {
// contains filtered or unexported fields
}
Monitor reads the new events of CloudWatch Logs log groups while it runs, and sends each to the host as CDK_TOOLKIT_I5033: upstream's CloudWatchLogEventMonitor.
Monitor.Start sends CDK_TOOLKIT_I5032, reads every log group added to it, then reads them again every PollingInterval until Monitor.Stop, which reads them once more, sends CDK_TOOLKIT_I5034 and forgets them. A monitor can be started again after it is stopped, with the log groups added since. A log group is read from the time the monitor was made with, or, for one added after the monitor was stopped, from when it was stopped; after each read, from just after the newest event read.
Its methods are safe for concurrent use, except that Start must not be called again before Stop.
func New ¶
New returns a monitor that sends its messages to io and reads its log groups from start, or from now when start is the zero time.
func (*Monitor) Add ¶
func (m *Monitor) Add(env cloudassembly.Environment, logs Logs, groups []string)
Add adds log groups, in env, read with logs, to those the monitor reads: upstream's addLogGroups. Logs replaces the client of the log groups already added in env, whose environment is identified by its account and region. A log group already added is read again from the monitor's start time.
func (*Monitor) AddStack ¶
func (m *Monitor) AddStack(ctx context.Context, access *awsauth.Access, s *cloudassembly.Stack) error
AddStack finds the log groups of s's deployed resources with FindLogGroups, adds them to m, and sends CDK_TOOLKIT_I5031 naming them, as upstream's deploy does for each stack it deploys with traceLogs.
A failure to find them is sent as CDK_TOOLKIT_E5035, so that it never becomes the result of the stack's deployment, and nothing is added; upstream's deploy throws it in place of the deployment's own result. AddStack returns an error only when ctx is done, or when the host fails to take E5035 or I5031.
func (*Monitor) Start ¶
Start sends CDK_TOOLKIT_I5032, reads the log groups once, and then reads them every PollingInterval in a goroutine, until ctx is done or Monitor.Stop is called: upstream's activate. Stop must be called once Start has returned without error.
A log group that doesn't exist yet, as a function's doesn't until it logs, has no events. A read that is throttled is a debug message, and the log group is read again next time; any other failure to read a log group is sent as CDK_TOOLKIT_E5035, and the other log groups' events are still sent. Start returns an error only when the host does, and the monitor is then not running.
func (*Monitor) Stop ¶
Stop stops reading, cancelling a read in flight, Start's first read included, and waiting for the events it had read to be sent, then reads the log groups once more, sends CDK_TOOLKIT_I5034, and forgets the log groups: upstream's deactivate. A log group whose read was cancelled is read by the last read from where that read began, so that its events are still sent. Log groups added from then on are read from the time Stop returns.
The last read runs under context.WithoutCancel, so that the events logged since the previous read are sent even when ctx is cancelled. It gives up on a log group not read within 30 seconds, whose failure is then sent as CDK_TOOLKIT_E5035, so that a stalled connection can't hold it. A monitor that isn't running isn't read, but still sends CDK_TOOLKIT_I5034 and forgets its log groups. Stop returns an error only when the host does.