forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loginfo.go
145 lines (129 loc) · 3.41 KB
/
loginfo.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
package logger // import "github.com/docker/docker/daemon/logger"
import (
"fmt"
"os"
"regexp"
"strings"
"time"
)
// Info provides enough information for a logging driver to do its function.
type Info struct {
Config map[string]string
ContainerID string
ContainerName string
ContainerEntrypoint string
ContainerArgs []string
ContainerImageID string
ContainerImageName string
ContainerCreated time.Time
ContainerEnv []string
ContainerLabels map[string]string
LogPath string
DaemonName string
}
// ExtraAttributes returns the user-defined extra attributes (labels,
// environment variables) in key-value format. This can be used by log drivers
// that support metadata to add more context to a log.
func (info *Info) ExtraAttributes(keyMod func(string) string) (map[string]string, error) {
extra := make(map[string]string)
labels, ok := info.Config["labels"]
if ok && len(labels) > 0 {
for _, l := range strings.Split(labels, ",") {
if v, ok := info.ContainerLabels[l]; ok {
if keyMod != nil {
l = keyMod(l)
}
extra[l] = v
}
}
}
labelsRegex, ok := info.Config["labels-regex"]
if ok && len(labelsRegex) > 0 {
re, err := regexp.Compile(labelsRegex)
if err != nil {
return nil, err
}
for k, v := range info.ContainerLabels {
if re.MatchString(k) {
if keyMod != nil {
k = keyMod(k)
}
extra[k] = v
}
}
}
envMapping := make(map[string]string)
for _, e := range info.ContainerEnv {
if kv := strings.SplitN(e, "=", 2); len(kv) == 2 {
envMapping[kv[0]] = kv[1]
}
}
env, ok := info.Config["env"]
if ok && len(env) > 0 {
for _, l := range strings.Split(env, ",") {
if v, ok := envMapping[l]; ok {
if keyMod != nil {
l = keyMod(l)
}
extra[l] = v
}
}
}
envRegex, ok := info.Config["env-regex"]
if ok && len(envRegex) > 0 {
re, err := regexp.Compile(envRegex)
if err != nil {
return nil, err
}
for k, v := range envMapping {
if re.MatchString(k) {
if keyMod != nil {
k = keyMod(k)
}
extra[k] = v
}
}
}
return extra, nil
}
// Hostname returns the hostname from the underlying OS.
func (info *Info) Hostname() (string, error) {
hostname, err := os.Hostname()
if err != nil {
return "", fmt.Errorf("logger: can not resolve hostname: %v", err)
}
return hostname, nil
}
// Command returns the command that the container being logged was
// started with. The Entrypoint is prepended to the container
// arguments.
func (info *Info) Command() string {
terms := []string{info.ContainerEntrypoint}
terms = append(terms, info.ContainerArgs...)
command := strings.Join(terms, " ")
return command
}
// ID Returns the Container ID shortened to 12 characters.
func (info *Info) ID() string {
return info.ContainerID[:12]
}
// FullID is an alias of ContainerID.
func (info *Info) FullID() string {
return info.ContainerID
}
// Name returns the ContainerName without a preceding '/'.
func (info *Info) Name() string {
return strings.TrimPrefix(info.ContainerName, "/")
}
// ImageID returns the ContainerImageID shortened to 12 characters.
func (info *Info) ImageID() string {
return info.ContainerImageID[:12]
}
// ImageFullID is an alias of ContainerImageID.
func (info *Info) ImageFullID() string {
return info.ContainerImageID
}
// ImageName is an alias of ContainerImageName
func (info *Info) ImageName() string {
return info.ContainerImageName
}