2021-08-23 13:14:53 +00:00
|
|
|
//go:build linux
|
2015-05-14 17:20:00 +00:00
|
|
|
|
2018-02-05 21:05:59 +00:00
|
|
|
package journald // import "github.com/docker/docker/daemon/logger/journald"
|
2015-04-20 18:26:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-01 10:31:12 +00:00
|
|
|
"strconv"
|
2022-02-10 23:53:59 +00:00
|
|
|
"sync/atomic"
|
2022-02-02 21:39:35 +00:00
|
|
|
"time"
|
2016-06-19 04:30:33 +00:00
|
|
|
"unicode"
|
2015-04-20 18:26:39 +00:00
|
|
|
|
2020-02-28 07:52:14 +00:00
|
|
|
"github.com/coreos/go-systemd/v22/journal"
|
2022-02-10 23:53:59 +00:00
|
|
|
|
2015-04-20 18:26:39 +00:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
2016-01-21 21:30:48 +00:00
|
|
|
"github.com/docker/docker/daemon/logger/loggerutils"
|
2022-02-10 23:53:59 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-04-20 18:26:39 +00:00
|
|
|
)
|
|
|
|
|
2015-04-09 04:23:30 +00:00
|
|
|
const name = "journald"
|
|
|
|
|
2022-02-02 21:39:35 +00:00
|
|
|
// Well-known user journal fields.
|
|
|
|
// https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html
|
|
|
|
const (
|
|
|
|
fieldSyslogIdentifier = "SYSLOG_IDENTIFIER"
|
|
|
|
fieldSyslogTimestamp = "SYSLOG_TIMESTAMP"
|
|
|
|
)
|
|
|
|
|
|
|
|
// User journal fields used by the log driver.
|
|
|
|
const (
|
|
|
|
fieldContainerID = "CONTAINER_ID"
|
|
|
|
fieldContainerIDFull = "CONTAINER_ID_FULL"
|
|
|
|
fieldContainerName = "CONTAINER_NAME"
|
|
|
|
fieldContainerTag = "CONTAINER_TAG"
|
|
|
|
fieldImageName = "IMAGE_NAME"
|
|
|
|
|
|
|
|
// Fields used to serialize PLogMetaData.
|
|
|
|
|
|
|
|
fieldPLogID = "CONTAINER_PARTIAL_ID"
|
|
|
|
fieldPLogOrdinal = "CONTAINER_PARTIAL_ORDINAL"
|
|
|
|
fieldPLogLast = "CONTAINER_PARTIAL_LAST"
|
|
|
|
fieldPartialMessage = "CONTAINER_PARTIAL_MESSAGE"
|
2022-02-10 23:53:59 +00:00
|
|
|
|
|
|
|
fieldLogEpoch = "CONTAINER_LOG_EPOCH"
|
|
|
|
fieldLogOrdinal = "CONTAINER_LOG_ORDINAL"
|
2022-02-02 21:39:35 +00:00
|
|
|
)
|
|
|
|
|
2022-02-10 23:53:59 +00:00
|
|
|
var waitUntilFlushed func(*journald) error
|
|
|
|
|
2015-07-21 22:26:52 +00:00
|
|
|
type journald struct {
|
2022-02-10 23:53:59 +00:00
|
|
|
// Sequence number of the most recent message sent by this instance of
|
|
|
|
// the log driver, starting from 1. Corollary: ordinal == 0 implies no
|
|
|
|
// messages have been sent by this instance.
|
|
|
|
ordinal uint64 // Placed first in struct to ensure 8-byte alignment for atomic ops.
|
|
|
|
// Epoch identifier to distinguish sequence numbers from this instance
|
|
|
|
// vs. other instances.
|
|
|
|
epoch string
|
|
|
|
|
2022-03-02 23:38:16 +00:00
|
|
|
vars map[string]string // additional variables and values to send to the journal along with the log message
|
|
|
|
|
|
|
|
closed chan struct{}
|
2022-02-02 21:39:35 +00:00
|
|
|
|
|
|
|
// Overrides for unit tests.
|
|
|
|
|
2022-02-10 23:53:59 +00:00
|
|
|
sendToJournal func(message string, priority journal.Priority, vars map[string]string) error
|
2022-09-03 21:20:23 +00:00
|
|
|
journalReadDir string //nolint:unused // Referenced in read.go, which has more restrictive build constraints.
|
2022-02-10 23:53:59 +00:00
|
|
|
readSyncTimeout time.Duration
|
Add log reading to the journald log driver
If a logdriver doesn't register a callback function to validate log
options, it won't be usable. Fix the journald driver by adding a dummy
validator.
Teach the client and the daemon's "logs" logic that the server can also
supply "logs" data via the "journald" driver. Update documentation and
tests that depend on error messages.
Add support for reading log data from the systemd journal to the
journald log driver. The internal logic uses a goroutine to scan the
journal for matching entries after any specified cutoff time, formats
the messages from those entries as JSONLog messages, and stuffs the
results down a pipe whose reading end we hand back to the caller.
If we are missing any of the 'linux', 'cgo', or 'journald' build tags,
however, we don't implement a reader, so the 'logs' endpoint will still
return an error.
Make the necessary changes to the build setup to ensure that support for
reading container logs from the systemd journal is built.
Rename the Jmap member of the journald logdriver's struct to "vars" to
make it non-public, and to make it easier to tell that it's just there
to hold additional variable values that we want journald to record along
with log data that we're sending to it.
In the client, don't assume that we know which logdrivers the server
implements, and remove the check that looks at the server. It's
redundant because the server already knows, and the check also makes
using older clients with newer servers (which may have new logdrivers in
them) unnecessarily hard.
When we try to "logs" and have to report that the container's logdriver
doesn't support reading, send the error message through the
might-be-a-multiplexer so that clients which are expecting multiplexed
data will be able to properly display the error, instead of tripping
over the data and printing a less helpful "Unrecognized input header"
error.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2015-07-23 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 04:23:30 +00:00
|
|
|
func init() {
|
|
|
|
if err := logger.RegisterLogDriver(name, New); err != nil {
|
2022-04-21 09:50:37 +00:00
|
|
|
panic(err)
|
2015-04-09 04:23:30 +00:00
|
|
|
}
|
Add log reading to the journald log driver
If a logdriver doesn't register a callback function to validate log
options, it won't be usable. Fix the journald driver by adding a dummy
validator.
Teach the client and the daemon's "logs" logic that the server can also
supply "logs" data via the "journald" driver. Update documentation and
tests that depend on error messages.
Add support for reading log data from the systemd journal to the
journald log driver. The internal logic uses a goroutine to scan the
journal for matching entries after any specified cutoff time, formats
the messages from those entries as JSONLog messages, and stuffs the
results down a pipe whose reading end we hand back to the caller.
If we are missing any of the 'linux', 'cgo', or 'journald' build tags,
however, we don't implement a reader, so the 'logs' endpoint will still
return an error.
Make the necessary changes to the build setup to ensure that support for
reading container logs from the systemd journal is built.
Rename the Jmap member of the journald logdriver's struct to "vars" to
make it non-public, and to make it easier to tell that it's just there
to hold additional variable values that we want journald to record along
with log data that we're sending to it.
In the client, don't assume that we know which logdrivers the server
implements, and remove the check that looks at the server. It's
redundant because the server already knows, and the check also makes
using older clients with newer servers (which may have new logdrivers in
them) unnecessarily hard.
When we try to "logs" and have to report that the container's logdriver
doesn't support reading, send the error message through the
might-be-a-multiplexer so that clients which are expecting multiplexed
data will be able to properly display the error, instead of tripping
over the data and printing a less helpful "Unrecognized input header"
error.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2015-07-23 15:02:56 +00:00
|
|
|
if err := logger.RegisterLogOptValidator(name, validateLogOpt); err != nil {
|
2022-04-21 09:50:37 +00:00
|
|
|
panic(err)
|
Add log reading to the journald log driver
If a logdriver doesn't register a callback function to validate log
options, it won't be usable. Fix the journald driver by adding a dummy
validator.
Teach the client and the daemon's "logs" logic that the server can also
supply "logs" data via the "journald" driver. Update documentation and
tests that depend on error messages.
Add support for reading log data from the systemd journal to the
journald log driver. The internal logic uses a goroutine to scan the
journal for matching entries after any specified cutoff time, formats
the messages from those entries as JSONLog messages, and stuffs the
results down a pipe whose reading end we hand back to the caller.
If we are missing any of the 'linux', 'cgo', or 'journald' build tags,
however, we don't implement a reader, so the 'logs' endpoint will still
return an error.
Make the necessary changes to the build setup to ensure that support for
reading container logs from the systemd journal is built.
Rename the Jmap member of the journald logdriver's struct to "vars" to
make it non-public, and to make it easier to tell that it's just there
to hold additional variable values that we want journald to record along
with log data that we're sending to it.
In the client, don't assume that we know which logdrivers the server
implements, and remove the check that looks at the server. It's
redundant because the server already knows, and the check also makes
using older clients with newer servers (which may have new logdrivers in
them) unnecessarily hard.
When we try to "logs" and have to report that the container's logdriver
doesn't support reading, send the error message through the
might-be-a-multiplexer so that clients which are expecting multiplexed
data will be able to properly display the error, instead of tripping
over the data and printing a less helpful "Unrecognized input header"
error.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2015-07-23 15:02:56 +00:00
|
|
|
}
|
2015-04-09 04:23:30 +00:00
|
|
|
}
|
|
|
|
|
2023-04-25 05:39:28 +00:00
|
|
|
// sanitizeKeyMod returns the sanitized string so that it could be used in journald.
|
2016-06-19 04:30:33 +00:00
|
|
|
// In journald log, there are special requirements for fields.
|
|
|
|
// Fields must be composed of uppercase letters, numbers, and underscores, but must
|
|
|
|
// not start with an underscore.
|
|
|
|
func sanitizeKeyMod(s string) string {
|
|
|
|
n := ""
|
|
|
|
for _, v := range s {
|
|
|
|
if 'a' <= v && v <= 'z' {
|
|
|
|
v = unicode.ToUpper(v)
|
|
|
|
} else if ('Z' < v || v < 'A') && ('9' < v || v < '0') {
|
|
|
|
v = '_'
|
|
|
|
}
|
|
|
|
// If (n == "" && v == '_'), then we will skip as this is the beginning with '_'
|
|
|
|
if !(n == "" && v == '_') {
|
|
|
|
n += string(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2015-07-21 22:26:52 +00:00
|
|
|
// New creates a journald logger using the configuration passed in on
|
2015-08-10 03:12:42 +00:00
|
|
|
// the context.
|
2016-11-26 05:08:34 +00:00
|
|
|
func New(info logger.Info) (logger.Logger, error) {
|
2015-04-20 18:26:39 +00:00
|
|
|
if !journal.Enabled() {
|
|
|
|
return nil, fmt.Errorf("journald is not enabled on this host")
|
|
|
|
}
|
2015-10-04 21:06:19 +00:00
|
|
|
|
2022-02-02 21:39:35 +00:00
|
|
|
return new(info)
|
|
|
|
}
|
|
|
|
|
|
|
|
func new(info logger.Info) (*journald, error) {
|
2016-01-21 21:30:48 +00:00
|
|
|
// parse log tag
|
2016-11-26 05:08:34 +00:00
|
|
|
tag, err := loggerutils.ParseLogTag(info, loggerutils.DefaultTemplate)
|
2016-01-21 21:30:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-10 23:53:59 +00:00
|
|
|
epoch := stringid.GenerateRandomID()
|
|
|
|
|
Add log reading to the journald log driver
If a logdriver doesn't register a callback function to validate log
options, it won't be usable. Fix the journald driver by adding a dummy
validator.
Teach the client and the daemon's "logs" logic that the server can also
supply "logs" data via the "journald" driver. Update documentation and
tests that depend on error messages.
Add support for reading log data from the systemd journal to the
journald log driver. The internal logic uses a goroutine to scan the
journal for matching entries after any specified cutoff time, formats
the messages from those entries as JSONLog messages, and stuffs the
results down a pipe whose reading end we hand back to the caller.
If we are missing any of the 'linux', 'cgo', or 'journald' build tags,
however, we don't implement a reader, so the 'logs' endpoint will still
return an error.
Make the necessary changes to the build setup to ensure that support for
reading container logs from the systemd journal is built.
Rename the Jmap member of the journald logdriver's struct to "vars" to
make it non-public, and to make it easier to tell that it's just there
to hold additional variable values that we want journald to record along
with log data that we're sending to it.
In the client, don't assume that we know which logdrivers the server
implements, and remove the check that looks at the server. It's
redundant because the server already knows, and the check also makes
using older clients with newer servers (which may have new logdrivers in
them) unnecessarily hard.
When we try to "logs" and have to report that the container's logdriver
doesn't support reading, send the error message through the
might-be-a-multiplexer so that clients which are expecting multiplexed
data will be able to properly display the error, instead of tripping
over the data and printing a less helpful "Unrecognized input header"
error.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2015-07-23 15:02:56 +00:00
|
|
|
vars := map[string]string{
|
2022-02-02 21:39:35 +00:00
|
|
|
fieldContainerID: info.ContainerID[:12],
|
|
|
|
fieldContainerIDFull: info.ContainerID,
|
|
|
|
fieldContainerName: info.Name(),
|
|
|
|
fieldContainerTag: tag,
|
|
|
|
fieldImageName: info.ImageName(),
|
|
|
|
fieldSyslogIdentifier: tag,
|
2022-02-10 23:53:59 +00:00
|
|
|
fieldLogEpoch: epoch,
|
2015-10-04 21:06:19 +00:00
|
|
|
}
|
2016-11-09 00:34:47 +00:00
|
|
|
extraAttrs, err := info.ExtraAttributes(sanitizeKeyMod)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-04 21:06:19 +00:00
|
|
|
for k, v := range extraAttrs {
|
|
|
|
vars[k] = v
|
|
|
|
}
|
2022-02-10 23:53:59 +00:00
|
|
|
return &journald{
|
|
|
|
epoch: epoch,
|
|
|
|
vars: vars,
|
|
|
|
closed: make(chan struct{}),
|
|
|
|
sendToJournal: journal.Send,
|
|
|
|
}, nil
|
2015-04-20 18:26:39 +00:00
|
|
|
}
|
|
|
|
|
Add log reading to the journald log driver
If a logdriver doesn't register a callback function to validate log
options, it won't be usable. Fix the journald driver by adding a dummy
validator.
Teach the client and the daemon's "logs" logic that the server can also
supply "logs" data via the "journald" driver. Update documentation and
tests that depend on error messages.
Add support for reading log data from the systemd journal to the
journald log driver. The internal logic uses a goroutine to scan the
journal for matching entries after any specified cutoff time, formats
the messages from those entries as JSONLog messages, and stuffs the
results down a pipe whose reading end we hand back to the caller.
If we are missing any of the 'linux', 'cgo', or 'journald' build tags,
however, we don't implement a reader, so the 'logs' endpoint will still
return an error.
Make the necessary changes to the build setup to ensure that support for
reading container logs from the systemd journal is built.
Rename the Jmap member of the journald logdriver's struct to "vars" to
make it non-public, and to make it easier to tell that it's just there
to hold additional variable values that we want journald to record along
with log data that we're sending to it.
In the client, don't assume that we know which logdrivers the server
implements, and remove the check that looks at the server. It's
redundant because the server already knows, and the check also makes
using older clients with newer servers (which may have new logdrivers in
them) unnecessarily hard.
When we try to "logs" and have to report that the container's logdriver
doesn't support reading, send the error message through the
might-be-a-multiplexer so that clients which are expecting multiplexed
data will be able to properly display the error, instead of tripping
over the data and printing a less helpful "Unrecognized input header"
error.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2015-07-23 15:02:56 +00:00
|
|
|
// We don't actually accept any options, but we have to supply a callback for
|
|
|
|
// the factory to pass the (probably empty) configuration map to.
|
|
|
|
func validateLogOpt(cfg map[string]string) error {
|
|
|
|
for key := range cfg {
|
|
|
|
switch key {
|
2015-10-04 21:06:19 +00:00
|
|
|
case "labels":
|
2019-01-16 21:52:22 +00:00
|
|
|
case "labels-regex":
|
2015-10-04 21:06:19 +00:00
|
|
|
case "env":
|
2016-11-09 00:34:47 +00:00
|
|
|
case "env-regex":
|
2016-01-21 21:30:48 +00:00
|
|
|
case "tag":
|
Add log reading to the journald log driver
If a logdriver doesn't register a callback function to validate log
options, it won't be usable. Fix the journald driver by adding a dummy
validator.
Teach the client and the daemon's "logs" logic that the server can also
supply "logs" data via the "journald" driver. Update documentation and
tests that depend on error messages.
Add support for reading log data from the systemd journal to the
journald log driver. The internal logic uses a goroutine to scan the
journal for matching entries after any specified cutoff time, formats
the messages from those entries as JSONLog messages, and stuffs the
results down a pipe whose reading end we hand back to the caller.
If we are missing any of the 'linux', 'cgo', or 'journald' build tags,
however, we don't implement a reader, so the 'logs' endpoint will still
return an error.
Make the necessary changes to the build setup to ensure that support for
reading container logs from the systemd journal is built.
Rename the Jmap member of the journald logdriver's struct to "vars" to
make it non-public, and to make it easier to tell that it's just there
to hold additional variable values that we want journald to record along
with log data that we're sending to it.
In the client, don't assume that we know which logdrivers the server
implements, and remove the check that looks at the server. It's
redundant because the server already knows, and the check also makes
using older clients with newer servers (which may have new logdrivers in
them) unnecessarily hard.
When we try to "logs" and have to report that the container's logdriver
doesn't support reading, send the error message through the
might-be-a-multiplexer so that clients which are expecting multiplexed
data will be able to properly display the error, instead of tripping
over the data and printing a less helpful "Unrecognized input header"
error.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2015-07-23 15:02:56 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown log opt '%s' for journald log driver", key)
|
|
|
|
}
|
2015-04-20 18:26:39 +00:00
|
|
|
}
|
Add log reading to the journald log driver
If a logdriver doesn't register a callback function to validate log
options, it won't be usable. Fix the journald driver by adding a dummy
validator.
Teach the client and the daemon's "logs" logic that the server can also
supply "logs" data via the "journald" driver. Update documentation and
tests that depend on error messages.
Add support for reading log data from the systemd journal to the
journald log driver. The internal logic uses a goroutine to scan the
journal for matching entries after any specified cutoff time, formats
the messages from those entries as JSONLog messages, and stuffs the
results down a pipe whose reading end we hand back to the caller.
If we are missing any of the 'linux', 'cgo', or 'journald' build tags,
however, we don't implement a reader, so the 'logs' endpoint will still
return an error.
Make the necessary changes to the build setup to ensure that support for
reading container logs from the systemd journal is built.
Rename the Jmap member of the journald logdriver's struct to "vars" to
make it non-public, and to make it easier to tell that it's just there
to hold additional variable values that we want journald to record along
with log data that we're sending to it.
In the client, don't assume that we know which logdrivers the server
implements, and remove the check that looks at the server. It's
redundant because the server already knows, and the check also makes
using older clients with newer servers (which may have new logdrivers in
them) unnecessarily hard.
When we try to "logs" and have to report that the container's logdriver
doesn't support reading, send the error message through the
might-be-a-multiplexer so that clients which are expecting multiplexed
data will be able to properly display the error, instead of tripping
over the data and printing a less helpful "Unrecognized input header"
error.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2015-07-23 15:02:56 +00:00
|
|
|
return nil
|
2015-04-20 18:26:39 +00:00
|
|
|
}
|
|
|
|
|
Add log reading to the journald log driver
If a logdriver doesn't register a callback function to validate log
options, it won't be usable. Fix the journald driver by adding a dummy
validator.
Teach the client and the daemon's "logs" logic that the server can also
supply "logs" data via the "journald" driver. Update documentation and
tests that depend on error messages.
Add support for reading log data from the systemd journal to the
journald log driver. The internal logic uses a goroutine to scan the
journal for matching entries after any specified cutoff time, formats
the messages from those entries as JSONLog messages, and stuffs the
results down a pipe whose reading end we hand back to the caller.
If we are missing any of the 'linux', 'cgo', or 'journald' build tags,
however, we don't implement a reader, so the 'logs' endpoint will still
return an error.
Make the necessary changes to the build setup to ensure that support for
reading container logs from the systemd journal is built.
Rename the Jmap member of the journald logdriver's struct to "vars" to
make it non-public, and to make it easier to tell that it's just there
to hold additional variable values that we want journald to record along
with log data that we're sending to it.
In the client, don't assume that we know which logdrivers the server
implements, and remove the check that looks at the server. It's
redundant because the server already knows, and the check also makes
using older clients with newer servers (which may have new logdrivers in
them) unnecessarily hard.
When we try to "logs" and have to report that the container's logdriver
doesn't support reading, send the error message through the
might-be-a-multiplexer so that clients which are expecting multiplexed
data will be able to properly display the error, instead of tripping
over the data and printing a less helpful "Unrecognized input header"
error.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2015-07-23 15:02:56 +00:00
|
|
|
func (s *journald) Log(msg *logger.Message) error {
|
Improve logging of long log lines
This change updates how we handle long lines of output from the
container. The previous logic used a bufio reader to read entire lines
of output from the container through an intermediate BytesPipe, and that
allowed the container to cause dockerd to consume an unconstrained
amount of memory as it attempted to collect a whole line of output, by
outputting data without newlines.
To avoid that, we replace the bufio reader with our own buffering scheme
that handles log lines up to 16k in length, breaking up anything longer
than that into multiple chunks. If we can dispense with noting this
detail properly at the end of output, we can switch from using
ReadBytes() to using ReadLine() instead. We add a field ("Partial") to
the log message structure to flag when we pass data to the log driver
that did not end with a newline.
The Line member of Message structures that we pass to log drivers is now
a slice into data which can be overwritten between calls to the log
driver's Log() method, so drivers which batch up Messages before
processing them need to take additional care: we add a function
(logger.CopyMessage()) that can be used to create a deep copy of a
Message structure, and modify the awslogs driver to use it.
We update the jsonfile log driver to append a "\n" to the data that it
logs to disk only when the Partial flag is false (it previously did so
unconditionally), to make its "logs" output correctly reproduce the data
as we received it.
Likewise, we modify the journald log driver to add a data field with
value CONTAINER_PARTIAL_MESSAGE=true to entries when the Partial flag is
true, and update its "logs" reader to refrain from appending a "\n" to
the data that it retrieves if it does not see this field/value pair (it
also previously did this unconditionally).
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2016-05-24 18:12:47 +00:00
|
|
|
vars := map[string]string{}
|
|
|
|
for k, v := range s.vars {
|
|
|
|
vars[k] = v
|
|
|
|
}
|
2022-02-02 21:39:35 +00:00
|
|
|
if !msg.Timestamp.IsZero() {
|
|
|
|
vars[fieldSyslogTimestamp] = msg.Timestamp.Format(time.RFC3339Nano)
|
|
|
|
}
|
2020-09-01 10:31:12 +00:00
|
|
|
if msg.PLogMetaData != nil {
|
2022-02-02 21:39:35 +00:00
|
|
|
vars[fieldPLogID] = msg.PLogMetaData.ID
|
|
|
|
vars[fieldPLogOrdinal] = strconv.Itoa(msg.PLogMetaData.Ordinal)
|
|
|
|
vars[fieldPLogLast] = strconv.FormatBool(msg.PLogMetaData.Last)
|
2020-09-01 10:31:12 +00:00
|
|
|
if !msg.PLogMetaData.Last {
|
2022-02-02 21:39:35 +00:00
|
|
|
vars[fieldPartialMessage] = "true"
|
2020-09-01 10:31:12 +00:00
|
|
|
}
|
Improve logging of long log lines
This change updates how we handle long lines of output from the
container. The previous logic used a bufio reader to read entire lines
of output from the container through an intermediate BytesPipe, and that
allowed the container to cause dockerd to consume an unconstrained
amount of memory as it attempted to collect a whole line of output, by
outputting data without newlines.
To avoid that, we replace the bufio reader with our own buffering scheme
that handles log lines up to 16k in length, breaking up anything longer
than that into multiple chunks. If we can dispense with noting this
detail properly at the end of output, we can switch from using
ReadBytes() to using ReadLine() instead. We add a field ("Partial") to
the log message structure to flag when we pass data to the log driver
that did not end with a newline.
The Line member of Message structures that we pass to log drivers is now
a slice into data which can be overwritten between calls to the log
driver's Log() method, so drivers which batch up Messages before
processing them need to take additional care: we add a function
(logger.CopyMessage()) that can be used to create a deep copy of a
Message structure, and modify the awslogs driver to use it.
We update the jsonfile log driver to append a "\n" to the data that it
logs to disk only when the Partial flag is false (it previously did so
unconditionally), to make its "logs" output correctly reproduce the data
as we received it.
Likewise, we modify the journald log driver to add a data field with
value CONTAINER_PARTIAL_MESSAGE=true to entries when the Partial flag is
true, and update its "logs" reader to refrain from appending a "\n" to
the data that it retrieves if it does not see this field/value pair (it
also previously did this unconditionally).
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2016-05-24 18:12:47 +00:00
|
|
|
}
|
2016-12-12 14:54:20 +00:00
|
|
|
|
|
|
|
line := string(msg.Line)
|
2017-06-26 21:34:51 +00:00
|
|
|
source := msg.Source
|
2016-12-12 14:54:20 +00:00
|
|
|
logger.PutMessage(msg)
|
|
|
|
|
2022-02-10 23:53:59 +00:00
|
|
|
seq := atomic.AddUint64(&s.ordinal, 1)
|
|
|
|
vars[fieldLogOrdinal] = strconv.FormatUint(seq, 10)
|
|
|
|
|
2017-06-26 21:34:51 +00:00
|
|
|
if source == "stderr" {
|
2022-02-02 21:39:35 +00:00
|
|
|
return s.sendToJournal(line, journal.PriErr, vars)
|
Add log reading to the journald log driver
If a logdriver doesn't register a callback function to validate log
options, it won't be usable. Fix the journald driver by adding a dummy
validator.
Teach the client and the daemon's "logs" logic that the server can also
supply "logs" data via the "journald" driver. Update documentation and
tests that depend on error messages.
Add support for reading log data from the systemd journal to the
journald log driver. The internal logic uses a goroutine to scan the
journal for matching entries after any specified cutoff time, formats
the messages from those entries as JSONLog messages, and stuffs the
results down a pipe whose reading end we hand back to the caller.
If we are missing any of the 'linux', 'cgo', or 'journald' build tags,
however, we don't implement a reader, so the 'logs' endpoint will still
return an error.
Make the necessary changes to the build setup to ensure that support for
reading container logs from the systemd journal is built.
Rename the Jmap member of the journald logdriver's struct to "vars" to
make it non-public, and to make it easier to tell that it's just there
to hold additional variable values that we want journald to record along
with log data that we're sending to it.
In the client, don't assume that we know which logdrivers the server
implements, and remove the check that looks at the server. It's
redundant because the server already knows, and the check also makes
using older clients with newer servers (which may have new logdrivers in
them) unnecessarily hard.
When we try to "logs" and have to report that the container's logdriver
doesn't support reading, send the error message through the
might-be-a-multiplexer so that clients which are expecting multiplexed
data will be able to properly display the error, instead of tripping
over the data and printing a less helpful "Unrecognized input header"
error.
Signed-off-by: Nalin Dahyabhai <nalin@redhat.com> (github: nalind)
2015-07-23 15:02:56 +00:00
|
|
|
}
|
2022-02-02 21:39:35 +00:00
|
|
|
return s.sendToJournal(line, journal.PriInfo, vars)
|
2015-04-20 18:26:39 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 22:26:52 +00:00
|
|
|
func (s *journald) Name() string {
|
2015-04-09 04:23:30 +00:00
|
|
|
return name
|
|
|
|
}
|
2022-03-02 23:38:16 +00:00
|
|
|
|
|
|
|
func (s *journald) Close() error {
|
|
|
|
close(s.closed)
|
2022-02-10 23:53:59 +00:00
|
|
|
if waitUntilFlushed != nil {
|
|
|
|
return waitUntilFlushed(s)
|
|
|
|
}
|
2022-03-02 23:38:16 +00:00
|
|
|
return nil
|
|
|
|
}
|