logentries.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Package logentries provides the log driver for forwarding server logs
  2. // to logentries endpoints.
  3. package logentries // import "github.com/docker/docker/daemon/logger/logentries"
  4. import (
  5. "fmt"
  6. "strconv"
  7. "github.com/bsphere/le_go"
  8. "github.com/docker/docker/daemon/logger"
  9. "github.com/pkg/errors"
  10. "github.com/sirupsen/logrus"
  11. )
  12. type logentries struct {
  13. tag string
  14. containerID string
  15. containerName string
  16. writer *le_go.Logger
  17. extra map[string]string
  18. lineOnly bool
  19. }
  20. const (
  21. name = "logentries"
  22. token = "logentries-token"
  23. lineonly = "line-only"
  24. )
  25. func init() {
  26. if err := logger.RegisterLogDriver(name, New); err != nil {
  27. logrus.Fatal(err)
  28. }
  29. if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
  30. logrus.Fatal(err)
  31. }
  32. }
  33. // New creates a logentries logger using the configuration passed in on
  34. // the context. The supported context configuration variable is
  35. // logentries-token.
  36. func New(info logger.Info) (logger.Logger, error) {
  37. logrus.WithField("container", info.ContainerID).
  38. WithField("token", info.Config[token]).
  39. WithField("line-only", info.Config[lineonly]).
  40. Debug("logging driver logentries configured")
  41. log, err := le_go.Connect(info.Config[token])
  42. if err != nil {
  43. return nil, errors.Wrap(err, "error connecting to logentries")
  44. }
  45. var lineOnly bool
  46. if info.Config[lineonly] != "" {
  47. if lineOnly, err = strconv.ParseBool(info.Config[lineonly]); err != nil {
  48. return nil, errors.Wrap(err, "error parsing lineonly option")
  49. }
  50. }
  51. return &logentries{
  52. containerID: info.ContainerID,
  53. containerName: info.ContainerName,
  54. writer: log,
  55. lineOnly: lineOnly,
  56. }, nil
  57. }
  58. func (f *logentries) Log(msg *logger.Message) error {
  59. if !f.lineOnly {
  60. data := map[string]string{
  61. "container_id": f.containerID,
  62. "container_name": f.containerName,
  63. "source": msg.Source,
  64. "log": string(msg.Line),
  65. }
  66. for k, v := range f.extra {
  67. data[k] = v
  68. }
  69. ts := msg.Timestamp
  70. logger.PutMessage(msg)
  71. f.writer.Println(f.tag, ts, data)
  72. } else {
  73. line := string(msg.Line)
  74. logger.PutMessage(msg)
  75. f.writer.Println(line)
  76. }
  77. return nil
  78. }
  79. func (f *logentries) Close() error {
  80. return f.writer.Close()
  81. }
  82. func (f *logentries) Name() string {
  83. return name
  84. }
  85. // ValidateLogOpt looks for logentries specific log option logentries-address.
  86. func ValidateLogOpt(cfg map[string]string) error {
  87. for key := range cfg {
  88. switch key {
  89. case "env":
  90. case "env-regex":
  91. case "labels":
  92. case "labels-regex":
  93. case "tag":
  94. case key:
  95. default:
  96. return fmt.Errorf("unknown log opt '%s' for logentries log driver", key)
  97. }
  98. }
  99. if cfg[token] == "" {
  100. return fmt.Errorf("Missing logentries token")
  101. }
  102. return nil
  103. }