mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 11:03:34 +00:00
logs: stream corrosion logs with 'uc machine logs corrosion' from container instead of journal (fixes #392)
This commit is contained in:
+11
-1
@@ -16,6 +16,16 @@ const (
|
||||
LogStreamHeartbeat
|
||||
)
|
||||
|
||||
// System service names whose logs can be streamed via client.MachineLogs.
|
||||
const (
|
||||
SystemServiceCorrosion = "corrosion"
|
||||
SystemServiceDocker = "docker"
|
||||
SystemServiceUncloud = "uncloud"
|
||||
)
|
||||
|
||||
// SystemServices lists all system services that support log streaming.
|
||||
var SystemServices = []string{SystemServiceCorrosion, SystemServiceDocker, SystemServiceUncloud}
|
||||
|
||||
type LogStreamType int
|
||||
|
||||
// LogStreamTypeFromProto converts a protobuf LogEntry.StreamType to the internal LogStreamType.
|
||||
@@ -61,7 +71,7 @@ type ServiceLogsOptions struct {
|
||||
Machines []string
|
||||
}
|
||||
|
||||
// ServiceLogEntry represents a single log entry from a service container or systemd service.
|
||||
// ServiceLogEntry represents a single log entry from a service container or system service.
|
||||
type ServiceLogEntry struct {
|
||||
// Metadata may not be set if an error occurred (Err is not nil).
|
||||
Metadata ServiceLogEntryMetadata
|
||||
|
||||
+12
-12
@@ -156,11 +156,11 @@ func (cli *Client) ContainerLogs(
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
// MachineLogs streams journal logs for the given systemd service across one or more machines in
|
||||
// MachineLogs streams logs for the given system service across one or more machines in
|
||||
// chronological order based on timestamps. If opts.Machines is empty, logs are streamed from all
|
||||
// machines in the cluster.
|
||||
func (cli *Client) MachineLogs(
|
||||
ctx context.Context, unit string, opts api.ServiceLogsOptions,
|
||||
ctx context.Context, service string, opts api.ServiceLogsOptions,
|
||||
) (<-chan api.ServiceLogEntry, error) {
|
||||
machines, err := cli.ListMachines(ctx, &api.MachineFilter{NamesOrIDs: opts.Machines})
|
||||
if err != nil {
|
||||
@@ -172,18 +172,18 @@ func (cli *Client) MachineLogs(
|
||||
|
||||
streams := make([]<-chan api.ServiceLogEntry, 0, len(machines))
|
||||
for _, m := range machines {
|
||||
ch, err := cli.systemdServiceLogs(ctx, m.Machine.Id, unit, opts)
|
||||
ch, err := cli.systemServiceLogs(ctx, m.Machine.Id, service, opts)
|
||||
if err != nil {
|
||||
// TODO: cancel already-opened streams. Currently they leak until ctx is cancelled which could
|
||||
// be critical when used as SDK.
|
||||
return nil, fmt.Errorf("stream logs from systemd service '%s' on machine '%s': %w",
|
||||
unit, m.Machine.Name, err)
|
||||
return nil, fmt.Errorf("stream logs from system service '%s' on machine '%s': %w",
|
||||
service, m.Machine.Name, err)
|
||||
}
|
||||
|
||||
// Enrich journal log entries with the systemd service name and machine metadata.
|
||||
// Enrich log entries with the system service name and machine metadata.
|
||||
metadata := api.ServiceLogEntryMetadata{
|
||||
ServiceID: unit,
|
||||
ServiceName: unit,
|
||||
ServiceID: service,
|
||||
ServiceName: service,
|
||||
MachineID: m.Machine.Id,
|
||||
MachineName: m.Machine.Name,
|
||||
}
|
||||
@@ -194,14 +194,14 @@ func (cli *Client) MachineLogs(
|
||||
return merger.Stream(), nil
|
||||
}
|
||||
|
||||
// systemdServiceLogs streams log entries from a single systemd service on the specified machine.
|
||||
func (cli *Client) systemdServiceLogs(
|
||||
ctx context.Context, machineID, unit string, opts api.ServiceLogsOptions,
|
||||
// systemServiceLogs streams log entries from a single system service on the specified machine.
|
||||
func (cli *Client) systemServiceLogs(
|
||||
ctx context.Context, machineID, service string, opts api.ServiceLogsOptions,
|
||||
) (<-chan api.LogEntry, error) {
|
||||
proxyCtx := cli.ProxySingleMachineContext(ctx, machineID)
|
||||
|
||||
req := &pb.LogsRequest{
|
||||
Id: unit,
|
||||
Id: service,
|
||||
Follow: opts.Follow,
|
||||
Tail: int32(opts.Tail),
|
||||
Since: opts.Since,
|
||||
|
||||
Reference in New Issue
Block a user