diff --git a/common/log_record.go b/common/log_record.go index 5d168c7..5adb51e 100644 --- a/common/log_record.go +++ b/common/log_record.go @@ -26,7 +26,8 @@ type LogRecord struct { LineNo int `json:"lineno"` Message string `json:"message"` Timestamp Number `json:"timestamp"` - PID int `json:"pid"` + CID string `json:"cid,omitempty"` + PID int `json:"pid,omitempty"` TID string `json:"tid,omitempty"` ExcType string `json:"excType,omitempty"` ExcText string `json:"excText,omitempty"` diff --git a/common/log_record_easyjson.go b/common/log_record_easyjson.go index 084bc26..740f5cb 100644 --- a/common/log_record_easyjson.go +++ b/common/log_record_easyjson.go @@ -1,4 +1,4 @@ -// AUTOGENERATED FILE: easyjson marshaler/unmarshalers. +// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT. package common @@ -53,7 +53,7 @@ func easyjson500470b6DecodeGithubComMailgunLogrusHooksCommon(in *jlexer.Lexer, o if m, ok := v1.(easyjson.Unmarshaler); ok { m.UnmarshalEasyJSON(in) } else if m, ok := v1.(json.Unmarshaler); ok { - m.UnmarshalJSON(in.Raw()) + _ = m.UnmarshalJSON(in.Raw()) } else { v1 = in.Interface() } @@ -80,6 +80,8 @@ func easyjson500470b6DecodeGithubComMailgunLogrusHooksCommon(in *jlexer.Lexer, o out.Message = string(in.String()) case "timestamp": out.Timestamp = Number(in.Float64()) + case "cid": + out.CID = string(in.String()) case "pid": out.PID = int(in.Int()) case "tid": @@ -105,21 +107,22 @@ func easyjson500470b6EncodeGithubComMailgunLogrusHooksCommon(out *jwriter.Writer first := true _ = first if len(in.Context) != 0 { - if !first { - out.RawByte(',') - } - first = false - out.RawString("\"context\":") - if in.Context == nil && (out.Flags&jwriter.NilMapAsEmpty) == 0 { - out.RawString(`null`) + const prefix string = ",\"context\":" + if first { + first = false + out.RawString(prefix[1:]) } else { + out.RawString(prefix) + } + { out.RawByte('{') v2First := true for v2Name, v2Value := range in.Context { - if !v2First { + if v2First { + v2First = false + } else { out.RawByte(',') } - v2First = false out.String(string(v2Name)) out.RawByte(':') if m, ok := v2Value.(easyjson.Marshaler); ok { @@ -134,97 +137,153 @@ func easyjson500470b6EncodeGithubComMailgunLogrusHooksCommon(out *jwriter.Writer } } if in.Category != "" { - if !first { - out.RawByte(',') + const prefix string = ",\"category\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) } - first = false - out.RawString("\"category\":") out.String(string(in.Category)) } - if !first { - out.RawByte(',') - } - first = false - out.RawString("\"appname\":") - out.String(string(in.AppName)) - if !first { - out.RawByte(',') - } - first = false - out.RawString("\"hostname\":") - out.String(string(in.HostName)) - if !first { - out.RawByte(',') - } - first = false - out.RawString("\"logLevel\":") - out.String(string(in.LogLevel)) - if !first { - out.RawByte(',') - } - first = false - out.RawString("\"filename\":") - out.String(string(in.FileName)) - if !first { - out.RawByte(',') - } - first = false - out.RawString("\"funcName\":") - out.String(string(in.FuncName)) - if !first { - out.RawByte(',') - } - first = false - out.RawString("\"lineno\":") - out.Int(int(in.LineNo)) - if !first { - out.RawByte(',') - } - first = false - out.RawString("\"message\":") - out.String(string(in.Message)) - if !first { - out.RawByte(',') - } - first = false - out.RawString("\"timestamp\":") - out.Raw((in.Timestamp).MarshalJSON()) - if !first { - out.RawByte(',') - } - first = false - out.RawString("\"pid\":") - out.Int(int(in.PID)) + { + const prefix string = ",\"appname\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.AppName)) + } + { + const prefix string = ",\"hostname\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.HostName)) + } + { + const prefix string = ",\"logLevel\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.LogLevel)) + } + { + const prefix string = ",\"filename\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.FileName)) + } + { + const prefix string = ",\"funcName\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.FuncName)) + } + { + const prefix string = ",\"lineno\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int(int(in.LineNo)) + } + { + const prefix string = ",\"message\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.Message)) + } + { + const prefix string = ",\"timestamp\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Raw((in.Timestamp).MarshalJSON()) + } + if in.CID != "" { + const prefix string = ",\"cid\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.String(string(in.CID)) + } + if in.PID != 0 { + const prefix string = ",\"pid\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) + } + out.Int(int(in.PID)) + } if in.TID != "" { - if !first { - out.RawByte(',') + const prefix string = ",\"tid\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) } - first = false - out.RawString("\"tid\":") out.String(string(in.TID)) } if in.ExcType != "" { - if !first { - out.RawByte(',') + const prefix string = ",\"excType\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) } - first = false - out.RawString("\"excType\":") out.String(string(in.ExcType)) } if in.ExcText != "" { - if !first { - out.RawByte(',') + const prefix string = ",\"excText\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) } - first = false - out.RawString("\"excText\":") out.String(string(in.ExcText)) } if in.ExcValue != "" { - if !first { - out.RawByte(',') + const prefix string = ",\"excValue\":" + if first { + first = false + out.RawString(prefix[1:]) + } else { + out.RawString(prefix) } - first = false - out.RawString("\"excValue\":") out.String(string(in.ExcValue)) } out.RawByte('}') diff --git a/kafkahook/kafkahook.go b/kafkahook/kafkahook.go index 9e489c1..c573d35 100644 --- a/kafkahook/kafkahook.go +++ b/kafkahook/kafkahook.go @@ -1,7 +1,9 @@ package kafkahook import ( + "bufio" "bytes" + "context" "fmt" "io" "os" @@ -16,18 +18,20 @@ import ( "github.com/mailgun/logrus-hooks/common" "github.com/mailru/easyjson/jwriter" "github.com/sirupsen/logrus" - "context" ) const bufferSize = 150 type KafkaHook struct { - produce chan []byte + produce chan []byte + conf Config + debug bool + + // Process identity metadata hostName string appName string - conf Config + cid string pid int - debug bool // Sync stuff wg sync.WaitGroup @@ -98,11 +102,13 @@ func New(conf Config) (*KafkaHook, error) { }() if h.hostName, err = os.Hostname(); err != nil { - h.hostName = "unknown_host" + h.hostName = "unknown" } h.appName = filepath.Base(os.Args[0]) - h.pid = os.Getpid() - + if h.pid = os.Getpid(); h.pid == 1 { + h.pid = 0 + } + h.cid = getDockerCID() return &h, nil } @@ -123,6 +129,7 @@ func (h *KafkaHook) Fire(entry *logrus.Entry) error { Message: entry.Message, Context: nil, Timestamp: common.Number(float64(entry.Time.UnixNano()) / 1000000000), + CID: h.cid, PID: h.pid, } rec.FromFields(entry.Data) @@ -195,3 +202,24 @@ func (h *KafkaHook) Close() error { }) return err } + +func getDockerCID() string { + f, err := os.Open("/proc/self/cgroup") + if err != nil { + return "" + } + defer f.Close() + + scanner := bufio.NewScanner(f) + for scanner.Scan() { + line := scanner.Text() + parts := strings.Split(line, "/docker/") + if len(parts) != 2 { + continue + } + + fullDockerCID := parts[1] + return fullDockerCID[:12] + } + return "" +} diff --git a/version b/version index 781dcb0..26aaba0 100644 --- a/version +++ b/version @@ -1 +1 @@ -1.1.3 +1.2.0