gcplogging.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package gcplogs
  2. import (
  3. "fmt"
  4. "sync"
  5. "sync/atomic"
  6. "time"
  7. "github.com/docker/docker/daemon/logger"
  8. "github.com/Sirupsen/logrus"
  9. "golang.org/x/net/context"
  10. "google.golang.org/cloud/compute/metadata"
  11. "google.golang.org/cloud/logging"
  12. )
  13. const (
  14. name = "gcplogs"
  15. projectOptKey = "gcp-project"
  16. logLabelsKey = "labels"
  17. logEnvKey = "env"
  18. logCmdKey = "gcp-log-cmd"
  19. logZoneKey = "gcp-meta-zone"
  20. logNameKey = "gcp-meta-name"
  21. logIDKey = "gcp-meta-id"
  22. )
  23. var (
  24. // The number of logs the gcplogs driver has dropped.
  25. droppedLogs uint64
  26. onGCE bool
  27. // instance metadata populated from the metadata server if available
  28. projectID string
  29. zone string
  30. instanceName string
  31. instanceID string
  32. )
  33. func init() {
  34. if err := logger.RegisterLogDriver(name, New); err != nil {
  35. logrus.Fatal(err)
  36. }
  37. if err := logger.RegisterLogOptValidator(name, ValidateLogOpts); err != nil {
  38. logrus.Fatal(err)
  39. }
  40. }
  41. type gcplogs struct {
  42. client *logging.Client
  43. instance *instanceInfo
  44. container *containerInfo
  45. }
  46. type dockerLogEntry struct {
  47. Instance *instanceInfo `json:"instance,omitempty"`
  48. Container *containerInfo `json:"container,omitempty"`
  49. Data string `json:"data,omitempty"`
  50. }
  51. type instanceInfo struct {
  52. Zone string `json:"zone,omitempty"`
  53. Name string `json:"name,omitempty"`
  54. ID string `json:"id,omitempty"`
  55. }
  56. type containerInfo struct {
  57. Name string `json:"name,omitempty"`
  58. ID string `json:"id,omitempty"`
  59. ImageName string `json:"imageName,omitempty"`
  60. ImageID string `json:"imageId,omitempty"`
  61. Created time.Time `json:"created,omitempty"`
  62. Command string `json:"command,omitempty"`
  63. Metadata map[string]string `json:"metadata,omitempty"`
  64. }
  65. var initGCPOnce sync.Once
  66. func initGCP() {
  67. initGCPOnce.Do(func() {
  68. onGCE = metadata.OnGCE()
  69. if onGCE {
  70. // These will fail on instances if the metadata service is
  71. // down or the client is compiled with an API version that
  72. // has been removed. Since these are not vital, let's ignore
  73. // them and make their fields in the dockeLogEntry ,omitempty
  74. projectID, _ = metadata.ProjectID()
  75. zone, _ = metadata.Zone()
  76. instanceName, _ = metadata.InstanceName()
  77. instanceID, _ = metadata.InstanceID()
  78. }
  79. })
  80. }
  81. // New creates a new logger that logs to Google Cloud Logging using the application
  82. // default credentials.
  83. //
  84. // See https://developers.google.com/identity/protocols/application-default-credentials
  85. func New(ctx logger.Context) (logger.Logger, error) {
  86. initGCP()
  87. var project string
  88. if projectID != "" {
  89. project = projectID
  90. }
  91. if projectID, found := ctx.Config[projectOptKey]; found {
  92. project = projectID
  93. }
  94. if project == "" {
  95. return nil, fmt.Errorf("No project was specified and couldn't read project from the meatadata server. Please specify a project")
  96. }
  97. c, err := logging.NewClient(context.Background(), project, "gcplogs-docker-driver")
  98. if err != nil {
  99. return nil, err
  100. }
  101. if err := c.Ping(); err != nil {
  102. return nil, fmt.Errorf("unable to connect or authenticate with Google Cloud Logging: %v", err)
  103. }
  104. l := &gcplogs{
  105. client: c,
  106. container: &containerInfo{
  107. Name: ctx.ContainerName,
  108. ID: ctx.ContainerID,
  109. ImageName: ctx.ContainerImageName,
  110. ImageID: ctx.ContainerImageID,
  111. Created: ctx.ContainerCreated,
  112. Metadata: ctx.ExtraAttributes(nil),
  113. },
  114. }
  115. if ctx.Config[logCmdKey] == "true" {
  116. l.container.Command = ctx.Command()
  117. }
  118. if onGCE {
  119. l.instance = &instanceInfo{
  120. Zone: zone,
  121. Name: instanceName,
  122. ID: instanceID,
  123. }
  124. } else if ctx.Config[logZoneKey] != "" || ctx.Config[logNameKey] != "" || ctx.Config[logIDKey] != "" {
  125. l.instance = &instanceInfo{
  126. Zone: ctx.Config[logZoneKey],
  127. Name: ctx.Config[logNameKey],
  128. ID: ctx.Config[logIDKey],
  129. }
  130. }
  131. // The logger "overflows" at a rate of 10,000 logs per second and this
  132. // overflow func is called. We want to surface the error to the user
  133. // without overly spamming /var/log/docker.log so we log the first time
  134. // we overflow and every 1000th time after.
  135. c.Overflow = func(_ *logging.Client, _ logging.Entry) error {
  136. if i := atomic.AddUint64(&droppedLogs, 1); i%1000 == 1 {
  137. logrus.Errorf("gcplogs driver has dropped %v logs", i)
  138. }
  139. return nil
  140. }
  141. return l, nil
  142. }
  143. // ValidateLogOpts validates the opts passed to the gcplogs driver. Currently, the gcplogs
  144. // driver doesn't take any arguments.
  145. func ValidateLogOpts(cfg map[string]string) error {
  146. for k := range cfg {
  147. switch k {
  148. case projectOptKey, logLabelsKey, logEnvKey, logCmdKey, logZoneKey, logNameKey, logIDKey:
  149. default:
  150. return fmt.Errorf("%q is not a valid option for the gcplogs driver", k)
  151. }
  152. }
  153. return nil
  154. }
  155. func (l *gcplogs) Log(m *logger.Message) error {
  156. return l.client.Log(logging.Entry{
  157. Time: m.Timestamp,
  158. Payload: &dockerLogEntry{
  159. Instance: l.instance,
  160. Container: l.container,
  161. Data: string(m.Line),
  162. },
  163. })
  164. }
  165. func (l *gcplogs) Close() error {
  166. return l.client.Flush()
  167. }
  168. func (l *gcplogs) Name() string {
  169. return name
  170. }