Skip to content

Commit caf8966

Browse files
committed
Add containerId and containerName to json
1 parent c6ef859 commit caf8966

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

driver.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ import (
1919
"github.com/tonistiigi/fifo"
2020

2121
"github.com/Shopify/sarama"
22-
23-
"github.com/docker/docker/api/types/backend"
2422
)
2523

2624
// An mapped version of logger.Message where Line is a String, not a byte array
2725
type LogMessage struct {
2826
Line string
2927
Source string
3028
Timestamp time.Time
31-
Attrs backend.LogAttributes
3229
Partial bool
30+
ContainerName string
31+
ContainerId string
3332

3433
// Err is an error associated with a message. Completeness of a message
3534
// with Err is not expected, tho it may be partially complete (fields may
@@ -157,6 +156,8 @@ func ConsumeLog(lf *logPair, topic string, keyStrategy KeyStrategy) {
157156
msg.Source = buf.Source
158157
msg.Partial = buf.Partial
159158
msg.Timestamp = time.Unix(0, buf.TimeNano)
159+
msg.ContainerId = lf.info.ContainerID
160+
msg.ContainerName = lf.info.ContainerName
160161

161162
err := WriteMessage(topic, msg, lf.info.ContainerID, keyStrategy, lf.producer)
162163
if err != nil {

driver_test.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io"
1212
"github.com/Shopify/sarama/mocks"
1313
"github.com/docker/docker/daemon/logger"
14+
"github.com/stretchr/testify/assert"
1415
)
1516

1617

@@ -57,14 +58,35 @@ func TestConsumesMultipleLogMessagesFromDocker(t *testing.T) {
5758
assertLineMatch(t, "delta", <-producer.Successes())
5859
}
5960

61+
func TestJsonIncludesContainerInformation(t *testing.T) {
62+
expectedContainerId := "containerid1"
63+
expectedContainerName := "containername1"
6064

65+
producer := NewProducer(t)
66+
defer producer.Close()
67+
68+
logMsg := newLogEntry("alpha")
6169

70+
stream := createBufferForLogMessages([]logdriver.LogEntry{logMsg})
71+
72+
lf := createLogPair(producer, stream)
73+
lf.info.ContainerID = expectedContainerId
74+
lf.info.ContainerName = expectedContainerName
75+
76+
producer.ExpectInputAndSucceed()
77+
ConsumeLog(&lf, "topic", KEY_BY_TIMESTAMP)
78+
79+
recvMsg := <-producer.Successes()
80+
outMsg := unmarshallMessage(recvMsg, t)
81+
assert.Equal(t, expectedContainerId, outMsg.ContainerId)
82+
assert.Equal(t, expectedContainerName, outMsg.ContainerName)
83+
}
6284

6385
func createLogPair(producer *mocks.AsyncProducer, stream io.ReadCloser) logPair {
6486
var lf logPair
6587
lf.producer = producer
6688
lf.stream = stream
67-
lf.info = logger.Info{}
89+
lf.info = logger.Info{ContainerName: "mycontainer", ContainerID: "abcdefg"}
6890
return lf
6991
}
7092

kakfa_test.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,27 @@ func assertTopic(t *testing.T, expectedTopic string, message *sarama.ProducerMes
135135
}
136136

137137
func assertLineMatch(t *testing.T, expectedLine string, message *sarama.ProducerMessage) {
138+
outputJson := unmarshallMessage(message, t)
139+
assert.Equal(t, expectedLine, outputJson.Line)
140+
}
141+
142+
143+
func unmarshallMessage(message *sarama.ProducerMessage, t *testing.T) (LogMessage) {
138144
msgContentBytes, err := message.Value.Encode()
139145
if err != nil {
140146
t.Error(err)
141147
t.FailNow()
142148
}
143-
144149
var outputJson LogMessage
145150
jErr := json.Unmarshal(msgContentBytes, &outputJson)
146151
if jErr != nil {
147152
t.Error(jErr)
148153
t.FailNow()
149154
}
150-
151-
assert.Equal(t, expectedLine, outputJson.Line)
155+
return outputJson
152156
}
153157

158+
154159
func newMessage(expectedTime time.Time, expectedSource string, expectedLine string) (LogMessage) {
155160
var msg LogMessage
156161
msg.Timestamp = expectedTime

0 commit comments

Comments
 (0)