2015-12-18 12:43:32 -05:00
|
|
|
package gcplogs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-17 10:58:23 -04:00
|
|
|
"sync"
|
2015-12-18 12:43:32 -05:00
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/daemon/logger"
|
|
|
|
|
2016-12-16 00:18:14 -05:00
|
|
|
"cloud.google.com/go/compute/metadata"
|
|
|
|
"cloud.google.com/go/logging"
|
2015-12-18 12:43:32 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"golang.org/x/net/context"
|
2017-04-28 21:48:51 -04:00
|
|
|
mrpb "google.golang.org/genproto/googleapis/api/monitoredres"
|
2015-12-18 12:43:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
name = "gcplogs"
|
|
|
|
|
2016-11-08 19:34:47 -05: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 12:43:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// The number of logs the gcplogs driver has dropped.
|
|
|
|
droppedLogs uint64
|
|
|
|
|
2016-03-17 10:58:23 -04:00
|
|
|
onGCE bool
|
2015-12-18 12:43:32 -05: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 00:18:14 -05:00
|
|
|
logger *logging.Logger
|
2015-12-18 12:43:32 -05:00
|
|
|
instance *instanceInfo
|
|
|
|
container *containerInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
type dockerLogEntry struct {
|
|
|
|
Instance *instanceInfo `json:"instance,omitempty"`
|
|
|
|
Container *containerInfo `json:"container,omitempty"`
|
|
|
|
Data string `json:"data,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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 10:58:23 -04: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 07:08:57 -05:00
|
|
|
// them and make their fields in the dockerLogEntry ,omitempty
|
2016-03-17 10:58:23 -04:00
|
|
|
projectID, _ = metadata.ProjectID()
|
|
|
|
zone, _ = metadata.Zone()
|
|
|
|
instanceName, _ = metadata.InstanceName()
|
|
|
|
instanceID, _ = metadata.InstanceID()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-12-18 12:43:32 -05: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 00:08:34 -05:00
|
|
|
func New(info logger.Info) (logger.Logger, error) {
|
2016-03-17 10:58:23 -04:00
|
|
|
initGCP()
|
2015-12-18 12:43:32 -05:00
|
|
|
|
|
|
|
var project string
|
|
|
|
if projectID != "" {
|
|
|
|
project = projectID
|
|
|
|
}
|
2016-11-26 00:08:34 -05:00
|
|
|
if projectID, found := info.Config[projectOptKey]; found {
|
2015-12-18 12:43:32 -05:00
|
|
|
project = projectID
|
|
|
|
}
|
|
|
|
if project == "" {
|
2017-02-16 07:08:57 -05: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 12:43:32 -05:00
|
|
|
}
|
|
|
|
|
2016-12-16 04:50:14 -05: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 00:18:14 -05:00
|
|
|
c, err := logging.NewClient(context.Background(), project)
|
2015-12-18 12:43:32 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-28 21:48:51 -04: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 12:43:32 -05:00
|
|
|
|
2016-12-16 00:18:14 -05:00
|
|
|
if err := c.Ping(context.Background()); err != nil {
|
2015-12-18 12:43:32 -05:00
|
|
|
return nil, fmt.Errorf("unable to connect or authenticate with Google Cloud Logging: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-11-08 19:34:47 -05:00
|
|
|
extraAttributes, err := info.ExtraAttributes(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-12-18 12:43:32 -05:00
|
|
|
l := &gcplogs{
|
2016-12-16 00:18:14 -05:00
|
|
|
logger: lg,
|
2015-12-18 12:43:32 -05:00
|
|
|
container: &containerInfo{
|
2016-11-26 00:08:34 -05:00
|
|
|
Name: info.ContainerName,
|
|
|
|
ID: info.ContainerID,
|
|
|
|
ImageName: info.ContainerImageName,
|
|
|
|
ImageID: info.ContainerImageID,
|
|
|
|
Created: info.ContainerCreated,
|
2016-11-08 19:34:47 -05:00
|
|
|
Metadata: extraAttributes,
|
2015-12-18 12:43:32 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-26 00:08:34 -05:00
|
|
|
if info.Config[logCmdKey] == "true" {
|
|
|
|
l.container.Command = info.Command()
|
2015-12-18 12:43:32 -05:00
|
|
|
}
|
|
|
|
|
2017-04-28 21:48:51 -04:00
|
|
|
if instanceResource != nil {
|
|
|
|
l.instance = instanceResource
|
2015-12-18 12:43:32 -05: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 00:18:14 -05: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 12:43:32 -05: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-08 19:34:47 -05:00
|
|
|
case projectOptKey, logLabelsKey, logEnvKey, logEnvRegexKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
|
2015-12-18 12:43:32 -05: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 {
|
2016-12-12 09:54:20 -05:00
|
|
|
data := string(m.Line)
|
|
|
|
ts := m.Timestamp
|
|
|
|
logger.PutMessage(m)
|
|
|
|
|
2016-12-16 00:18:14 -05:00
|
|
|
l.logger.Log(logging.Entry{
|
2016-12-12 09:54:20 -05:00
|
|
|
Timestamp: ts,
|
2015-12-18 12:43:32 -05:00
|
|
|
Payload: &dockerLogEntry{
|
|
|
|
Instance: l.instance,
|
|
|
|
Container: l.container,
|
2016-12-12 09:54:20 -05:00
|
|
|
Data: data,
|
2015-12-18 12:43:32 -05:00
|
|
|
},
|
|
|
|
})
|
2016-12-16 00:18:14 -05:00
|
|
|
return nil
|
2015-12-18 12:43:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *gcplogs) Close() error {
|
2016-12-16 00:18:14 -05:00
|
|
|
l.logger.Flush()
|
|
|
|
return nil
|
2015-12-18 12:43:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *gcplogs) Name() string {
|
|
|
|
return name
|
|
|
|
}
|