fluentd.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package fluentd
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math"
  6. "net"
  7. "strconv"
  8. "strings"
  9. "text/template"
  10. "github.com/Sirupsen/logrus"
  11. "github.com/docker/docker/daemon/logger"
  12. "github.com/fluent/fluent-logger-golang/fluent"
  13. )
  14. type Fluentd struct {
  15. tag string
  16. containerID string
  17. containerName string
  18. writer *fluent.Fluent
  19. }
  20. type Receiver struct {
  21. ID string
  22. FullID string
  23. Name string
  24. }
  25. const (
  26. name = "fluentd"
  27. defaultHostName = "localhost"
  28. defaultPort = 24224
  29. defaultTagPrefix = "docker"
  30. )
  31. func init() {
  32. if err := logger.RegisterLogDriver(name, New); err != nil {
  33. logrus.Fatal(err)
  34. }
  35. if err := logger.RegisterLogOptValidator(name, ValidateLogOpt); err != nil {
  36. logrus.Fatal(err)
  37. }
  38. }
  39. func parseConfig(ctx logger.Context) (string, int, string, error) {
  40. host := defaultHostName
  41. port := defaultPort
  42. tag := "docker." + ctx.ContainerID[:12]
  43. config := ctx.Config
  44. if address := config["fluentd-address"]; address != "" {
  45. if h, p, err := net.SplitHostPort(address); err != nil {
  46. if !strings.Contains(err.Error(), "missing port in address") {
  47. return "", 0, "", err
  48. }
  49. host = h
  50. } else {
  51. portnum, err := strconv.Atoi(p)
  52. if err != nil {
  53. return "", 0, "", err
  54. }
  55. host = h
  56. port = portnum
  57. }
  58. }
  59. if config["fluentd-tag"] != "" {
  60. receiver := &Receiver{
  61. ID: ctx.ContainerID[:12],
  62. FullID: ctx.ContainerID,
  63. Name: ctx.ContainerName,
  64. }
  65. tmpl, err := template.New("tag").Parse(config["fluentd-tag"])
  66. if err != nil {
  67. return "", 0, "", err
  68. }
  69. buf := new(bytes.Buffer)
  70. if err := tmpl.Execute(buf, receiver); err != nil {
  71. return "", 0, "", err
  72. }
  73. tag = buf.String()
  74. }
  75. return host, port, tag, nil
  76. }
  77. func New(ctx logger.Context) (logger.Logger, error) {
  78. host, port, tag, err := parseConfig(ctx)
  79. if err != nil {
  80. return nil, err
  81. }
  82. logrus.Debugf("logging driver fluentd configured for container:%s, host:%s, port:%d, tag:%s.", ctx.ContainerID, host, port, tag)
  83. // logger tries to recoonect 2**64 - 1 times
  84. // failed (and panic) after 204 years [ 1.5 ** (2**32 - 1) - 1 seconds]
  85. log, err := fluent.New(fluent.Config{FluentPort: port, FluentHost: host, RetryWait: 1000, MaxRetry: math.MaxUint32})
  86. if err != nil {
  87. return nil, err
  88. }
  89. return &Fluentd{
  90. tag: tag,
  91. containerID: ctx.ContainerID,
  92. containerName: ctx.ContainerName,
  93. writer: log,
  94. }, nil
  95. }
  96. func (f *Fluentd) Log(msg *logger.Message) error {
  97. data := map[string]string{
  98. "container_id": f.containerID,
  99. "container_name": f.containerName,
  100. "source": msg.Source,
  101. "log": string(msg.Line),
  102. }
  103. // fluent-logger-golang buffers logs from failures and disconnections,
  104. // and these are transferred again automatically.
  105. return f.writer.PostWithTime(f.tag, msg.Timestamp, data)
  106. }
  107. func ValidateLogOpt(cfg map[string]string) error {
  108. for key := range cfg {
  109. switch key {
  110. case "fluentd-address":
  111. case "fluentd-tag":
  112. default:
  113. return fmt.Errorf("unknown log opt '%s' for fluentd log driver", key)
  114. }
  115. }
  116. return nil
  117. }
  118. func (f *Fluentd) Close() error {
  119. return f.writer.Close()
  120. }
  121. func (f *Fluentd) Name() string {
  122. return name
  123. }