Skip to content

Commit

Permalink
Add container ID to process identity meta
Browse files Browse the repository at this point in the history
  • Loading branch information
horkhe committed Sep 26, 2018
1 parent 7857322 commit 3accd6d
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 94 deletions.
3 changes: 2 additions & 1 deletion common/log_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
229 changes: 144 additions & 85 deletions common/log_record_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 35 additions & 7 deletions kafkahook/kafkahook.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package kafkahook

import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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 ""
}
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.3
1.2.0

0 comments on commit 3accd6d

Please sign in to comment.