2018-02-05 21:05:59 +00:00
|
|
|
package gcplogs // import "github.com/docker/docker/daemon/logger/gcplogs"
|
2015-12-18 17:43:32 +00:00
|
|
|
|
|
|
|
import (
|
2018-05-19 11:38:54 +00:00
|
|
|
"context"
|
2015-12-18 17:43:32 +00:00
|
|
|
"fmt"
|
2016-03-17 14:58:23 +00:00
|
|
|
"sync"
|
2015-12-18 17:43:32 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
|
|
|
|
2016-12-16 05:18:14 +00:00
|
|
|
"cloud.google.com/go/compute/metadata"
|
|
|
|
"cloud.google.com/go/logging"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-04-29 01:48:51 +00:00
|
|
|
mrpb "google.golang.org/genproto/googleapis/api/monitoredres"
|
2015-12-18 17:43:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
name = "gcplogs"
|
|
|
|
|
2016-11-09 00:34:47 +00:00
|
|
|
projectOptKey = "gcp-project"
|
|
|
|
logLabelsKey = "labels"
|
|
|
|
logEnvKey = "env"
|
|
|
|
logEnvRegexKey = "env-regex"
|
|
|
|
logCmdKey = "gcp-log-cmd"
|
|
|
|
logZoneKey = "gcp-meta-zone"
|
|
|
|
logNameKey = "gcp-meta-name"
|
|
|
|
logIDKey = "gcp-meta-id"
|
2015-12-18 17:43:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// The number of logs the gcplogs driver has dropped.
|
|
|
|
droppedLogs uint64
|
|
|
|
|
2016-03-17 14:58:23 +00:00
|
|
|
onGCE bool
|
2015-12-18 17:43:32 +00:00
|
|
|
|
|
|
|
// instance metadata populated from the metadata server if available
|
|
|
|
projectID string
|
|
|
|
zone string
|
|
|
|
instanceName string
|
|
|
|
instanceID string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
if err := logger.RegisterLogDriver(name, New); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := logger.RegisterLogOptValidator(name, ValidateLogOpts); err != nil {
|
|
|
|
logrus.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type gcplogs struct {
|
2016-12-16 05:18:14 +00:00
|
|
|
logger *logging.Logger
|
2015-12-18 17:43:32 +00:00
|
|
|
instance *instanceInfo
|
|
|
|
container *containerInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type dockerLogEntry struct {
|
|
|
|
Instance *instanceInfo `json:"instance,omitempty"`
|
|
|
|
Container *containerInfo `json:"container,omitempty"`
|
2017-05-11 23:40:13 +00:00
|
|
|
Message string `json:"message,omitempty"`
|
2015-12-18 17:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type instanceInfo struct {
|
|
|
|
Zone string `json:"zone,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type containerInfo struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
ImageName string `json:"imageName,omitempty"`
|
|
|
|
ImageID string `json:"imageId,omitempty"`
|
|
|
|
Created time.Time `json:"created,omitempty"`
|
|
|
|
Command string `json:"command,omitempty"`
|
|
|
|
Metadata map[string]string `json:"metadata,omitempty"`
|
|
|
|
}
|
|
|
|
|
2016-03-17 14:58:23 +00:00
|
|
|
var initGCPOnce sync.Once
|
|
|
|
|
|
|
|
func initGCP() {
|
|
|
|
initGCPOnce.Do(func() {
|
|
|
|
onGCE = metadata.OnGCE()
|
|
|
|
if onGCE {
|
|
|
|
// These will fail on instances if the metadata service is
|
|
|
|
// down or the client is compiled with an API version that
|
|
|
|
// has been removed. Since these are not vital, let's ignore
|
2017-02-16 12:08:57 +00:00
|
|
|
// them and make their fields in the dockerLogEntry ,omitempty
|
2016-03-17 14:58:23 +00:00
|
|
|
projectID, _ = metadata.ProjectID()
|
|
|
|
zone, _ = metadata.Zone()
|
|
|
|
instanceName, _ = metadata.InstanceName()
|
|
|
|
instanceID, _ = metadata.InstanceID()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-18 17:43:32 +00:00
|
|
|
// New creates a new logger that logs to Google Cloud Logging using the application
|
|
|
|
// default credentials.
|
|
|
|
//
|
|
|
|
// See https://developers.google.com/identity/protocols/application-default-credentials
|
2016-11-26 05:08:34 +00:00
|
|
|
func New(info logger.Info) (logger.Logger, error) {
|
2016-03-17 14:58:23 +00:00
|
|
|
initGCP()
|
2015-12-18 17:43:32 +00:00
|
|
|
|
|
|
|
var project string
|
|
|
|
if projectID != "" {
|
|
|
|
project = projectID
|
|
|
|
}
|
2016-11-26 05:08:34 +00:00
|
|
|
if projectID, found := info.Config[projectOptKey]; found {
|
2015-12-18 17:43:32 +00:00
|
|
|
project = projectID
|
|
|
|
}
|
|
|
|
if project == "" {
|
2017-02-16 12:08:57 +00:00
|
|
|
return nil, fmt.Errorf("No project was specified and couldn't read project from the metadata server. Please specify a project")
|
2015-12-18 17:43:32 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 09:50:14 +00:00
|
|
|
// Issue #29344: gcplogs segfaults (static binary)
|
|
|
|
// If HOME is not set, logging.NewClient() will call os/user.Current() via oauth2/google.
|
|
|
|
// However, in static binary, os/user.Current() leads to segfault due to a glibc issue that won't be fixed
|
|
|
|
// in a short term. (golang/go#13470, https://sourceware.org/bugzilla/show_bug.cgi?id=19341)
|
|
|
|
// So we forcibly set HOME so as to avoid call to os/user/Current()
|
|
|
|
if err := ensureHomeIfIAmStatic(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:18:14 +00:00
|
|
|
c, err := logging.NewClient(context.Background(), project)
|
2015-12-18 17:43:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-29 01:48:51 +00:00
|
|
|
var instanceResource *instanceInfo
|
|
|
|
if onGCE {
|
|
|
|
instanceResource = &instanceInfo{
|
|
|
|
Zone: zone,
|
|
|
|
Name: instanceName,
|
|
|
|
ID: instanceID,
|
|
|
|
}
|
|
|
|
} else if info.Config[logZoneKey] != "" || info.Config[logNameKey] != "" || info.Config[logIDKey] != "" {
|
|
|
|
instanceResource = &instanceInfo{
|
|
|
|
Zone: info.Config[logZoneKey],
|
|
|
|
Name: info.Config[logNameKey],
|
|
|
|
ID: info.Config[logIDKey],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
options := []logging.LoggerOption{}
|
|
|
|
if instanceResource != nil {
|
|
|
|
vmMrpb := logging.CommonResource(
|
|
|
|
&mrpb.MonitoredResource{
|
|
|
|
Type: "gce_instance",
|
|
|
|
Labels: map[string]string{
|
|
|
|
"instance_id": instanceResource.ID,
|
|
|
|
"zone": instanceResource.Zone,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
options = []logging.LoggerOption{vmMrpb}
|
|
|
|
}
|
|
|
|
lg := c.Logger("gcplogs-docker-driver", options...)
|
2015-12-18 17:43:32 +00:00
|
|
|
|
2016-12-16 05:18:14 +00:00
|
|
|
if err := c.Ping(context.Background()); err != nil {
|
2015-12-18 17:43:32 +00:00
|
|
|
return nil, fmt.Errorf("unable to connect or authenticate with Google Cloud Logging: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-11-09 00:34:47 +00:00
|
|
|
extraAttributes, err := info.ExtraAttributes(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-18 17:43:32 +00:00
|
|
|
l := &gcplogs{
|
2016-12-16 05:18:14 +00:00
|
|
|
logger: lg,
|
2015-12-18 17:43:32 +00:00
|
|
|
container: &containerInfo{
|
2016-11-26 05:08:34 +00:00
|
|
|
Name: info.ContainerName,
|
|
|
|
ID: info.ContainerID,
|
|
|
|
ImageName: info.ContainerImageName,
|
|
|
|
ImageID: info.ContainerImageID,
|
|
|
|
Created: info.ContainerCreated,
|
2016-11-09 00:34:47 +00:00
|
|
|
Metadata: extraAttributes,
|
2015-12-18 17:43:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-26 05:08:34 +00:00
|
|
|
if info.Config[logCmdKey] == "true" {
|
|
|
|
l.container.Command = info.Command()
|
2015-12-18 17:43:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-29 01:48:51 +00:00
|
|
|
if instanceResource != nil {
|
|
|
|
l.instance = instanceResource
|
2015-12-18 17:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The logger "overflows" at a rate of 10,000 logs per second and this
|
|
|
|
// overflow func is called. We want to surface the error to the user
|
|
|
|
// without overly spamming /var/log/docker.log so we log the first time
|
|
|
|
// we overflow and every 1000th time after.
|
2016-12-16 05:18:14 +00:00
|
|
|
c.OnError = func(err error) {
|
|
|
|
if err == logging.ErrOverflow {
|
|
|
|
if i := atomic.AddUint64(&droppedLogs, 1); i%1000 == 1 {
|
|
|
|
logrus.Errorf("gcplogs driver has dropped %v logs", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logrus.Error(err)
|
2015-12-18 17:43:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateLogOpts validates the opts passed to the gcplogs driver. Currently, the gcplogs
|
|
|
|
// driver doesn't take any arguments.
|
|
|
|
func ValidateLogOpts(cfg map[string]string) error {
|
|
|
|
for k := range cfg {
|
|
|
|
switch k {
|
2016-11-09 00:34:47 +00:00
|
|
|
case projectOptKey, logLabelsKey, logEnvKey, logEnvRegexKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
|
2015-12-18 17:43:32 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("%q is not a valid option for the gcplogs driver", k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *gcplogs) Log(m *logger.Message) error {
|
2017-05-11 23:40:13 +00:00
|
|
|
message := string(m.Line)
|
2016-12-12 14:54:20 +00:00
|
|
|
ts := m.Timestamp
|
|
|
|
logger.PutMessage(m)
|
|
|
|
|
2016-12-16 05:18:14 +00:00
|
|
|
l.logger.Log(logging.Entry{
|
2016-12-12 14:54:20 +00:00
|
|
|
Timestamp: ts,
|
2015-12-18 17:43:32 +00:00
|
|
|
Payload: &dockerLogEntry{
|
|
|
|
Instance: l.instance,
|
|
|
|
Container: l.container,
|
2017-05-11 23:40:13 +00:00
|
|
|
Message: message,
|
2015-12-18 17:43:32 +00:00
|
|
|
},
|
|
|
|
})
|
2016-12-16 05:18:14 +00:00
|
|
|
return nil
|
2015-12-18 17:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *gcplogs) Close() error {
|
2016-12-16 05:18:14 +00:00
|
|
|
l.logger.Flush()
|
|
|
|
return nil
|
2015-12-18 17:43:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *gcplogs) Name() string {
|
|
|
|
return name
|
|
|
|
}
|