-
Notifications
You must be signed in to change notification settings - Fork 0
/
journald.go
199 lines (169 loc) · 5.21 KB
/
journald.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//go:build linux
// +build linux
package journald // import "github.com/fdurand/moby/daemon/logger/journald"
import (
"fmt"
"strconv"
"sync/atomic"
"time"
"unicode"
"github.com/coreos/go-systemd/v22/journal"
"github.com/fdurand/moby/daemon/logger"
"github.com/fdurand/moby/daemon/logger/loggerutils"
"github.com/fdurand/moby/pkg/stringid"
)
const name = "journald"
// 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"
fieldLogEpoch = "CONTAINER_LOG_EPOCH"
fieldLogOrdinal = "CONTAINER_LOG_ORDINAL"
)
var waitUntilFlushed func(*journald) error
type journald struct {
// 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
vars map[string]string // additional variables and values to send to the journal along with the log message
closed chan struct{}
// Overrides for unit tests.
sendToJournal func(message string, priority journal.Priority, vars map[string]string) error
journalReadDir string //nolint:structcheck,unused // Referenced in read.go, which has more restrictive build constraints.
readSyncTimeout time.Duration
}
func init() {
if err := logger.RegisterLogDriver(name, New); err != nil {
panic(err)
}
if err := logger.RegisterLogOptValidator(name, validateLogOpt); err != nil {
panic(err)
}
}
// sanitizeKeyMode returns the sanitized string so that it could be used in journald.
// 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
}
// New creates a journald logger using the configuration passed in on
// the context.
func New(info logger.Info) (logger.Logger, error) {
if !journal.Enabled() {
return nil, fmt.Errorf("journald is not enabled on this host")
}
return new(info)
}
func new(info logger.Info) (*journald, error) {
// parse log tag
tag, err := loggerutils.ParseLogTag(info, loggerutils.DefaultTemplate)
if err != nil {
return nil, err
}
epoch := stringid.GenerateRandomID()
vars := map[string]string{
fieldContainerID: info.ContainerID[:12],
fieldContainerIDFull: info.ContainerID,
fieldContainerName: info.Name(),
fieldContainerTag: tag,
fieldImageName: info.ImageName(),
fieldSyslogIdentifier: tag,
fieldLogEpoch: epoch,
}
extraAttrs, err := info.ExtraAttributes(sanitizeKeyMod)
if err != nil {
return nil, err
}
for k, v := range extraAttrs {
vars[k] = v
}
return &journald{
epoch: epoch,
vars: vars,
closed: make(chan struct{}),
sendToJournal: journal.Send,
}, nil
}
// 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 {
case "labels":
case "labels-regex":
case "env":
case "env-regex":
case "tag":
default:
return fmt.Errorf("unknown log opt '%s' for journald log driver", key)
}
}
return nil
}
func (s *journald) Log(msg *logger.Message) error {
vars := map[string]string{}
for k, v := range s.vars {
vars[k] = v
}
if !msg.Timestamp.IsZero() {
vars[fieldSyslogTimestamp] = msg.Timestamp.Format(time.RFC3339Nano)
}
if msg.PLogMetaData != nil {
vars[fieldPLogID] = msg.PLogMetaData.ID
vars[fieldPLogOrdinal] = strconv.Itoa(msg.PLogMetaData.Ordinal)
vars[fieldPLogLast] = strconv.FormatBool(msg.PLogMetaData.Last)
if !msg.PLogMetaData.Last {
vars[fieldPartialMessage] = "true"
}
}
line := string(msg.Line)
source := msg.Source
logger.PutMessage(msg)
seq := atomic.AddUint64(&s.ordinal, 1)
vars[fieldLogOrdinal] = strconv.FormatUint(seq, 10)
if source == "stderr" {
return s.sendToJournal(line, journal.PriErr, vars)
}
return s.sendToJournal(line, journal.PriInfo, vars)
}
func (s *journald) Name() string {
return name
}
func (s *journald) Close() error {
close(s.closed)
if waitUntilFlushed != nil {
return waitUntilFlushed(s)
}
return nil
}