Skip to content

Commit 1da7b6c

Browse files
authored
Include hostname in log (#10)
Output host hostname in log
1 parent b3c6582 commit 1da7b6c

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ Each log message will be written to a single Kafka message. The message within K
113113
"ContainerId": "be2af19df661fb08561a8a99c734f637f9dc1397c43d88379479fceb5fc0666d",
114114
"ContainerImageName": "hello-world",
115115
"ContainerImageId": "sha256:48b5124b2768d2b917edcb640435044a97967015485e812545546cbed5cf0233",
116+
"Hostname": "docker-master",
116117
"Tag": "common",
117118
"Err": null
118119
}
@@ -130,6 +131,7 @@ Each log message will be written to a single Kafka message. The message within K
130131
| ContainerId | Id of the container that generated the log message |
131132
| ContainerImageName | Name of the container's image |
132133
| ContainerImageId | ID of the container's image |
134+
| Hostname | Hostname of the server running the container |
133135
| Tag | The log tag |
134136
| Err | Usually null, otherwise will be a string containing and error from the logdriver |
135137

driver.go

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type LogMessage struct {
3232
ContainerId string
3333
ContainerImageName string
3434
ContainerImageId string
35+
Hostname string
3536
Tag string
3637

3738
// Err is an error associated with a message. Completeness of a message
@@ -188,6 +189,7 @@ func writeLogsToKafka(lf *logPair, topic string, keyStrategy KeyStrategy, tag st
188189
msg.ContainerName = lf.info.ContainerName
189190
msg.ContainerImageName = lf.info.ContainerImageName
190191
msg.ContainerImageId = lf.info.ContainerImageID
192+
msg.Hostname = getHostname()
191193
msg.Tag = tag
192194

193195
err := WriteMessage(topic, msg, lf.info.ContainerID, keyStrategy, lf.producer)

driver_test.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"io/ioutil"
1717
"encoding/json"
1818
"strconv"
19-
19+
"os"
2020
)
2121

2222

@@ -95,6 +95,26 @@ func TestJsonIncludesContainerInformation(t *testing.T) {
9595
assert.Equal(t, expectedContainerImageName, outMsg.ContainerImageName)
9696
}
9797

98+
func TestJsonIncludesHostname(t *testing.T) {
99+
expectedHostname,_ := os.Hostname()
100+
101+
producer := NewProducer(t)
102+
defer producer.Close()
103+
104+
logMsg := newLogEntry("alpha")
105+
106+
stream := createBufferForLogMessages([]logdriver.LogEntry{logMsg})
107+
108+
lf := createLogPair(producer, stream)
109+
110+
producer.ExpectInputAndSucceed()
111+
writeLogsToKafka(&lf, "topic", KEY_BY_TIMESTAMP, TAG)
112+
113+
recvMsg := <-producer.Successes()
114+
outMsg := unmarshallMessage(recvMsg, t)
115+
assert.Equal(t, expectedHostname, outMsg.Hostname)
116+
}
117+
98118
func TestTagCanBeOverridenWithEnvironmentVariable(t *testing.T) {
99119
overrideTag := "overide"
100120
defaultTag := "default"
@@ -169,7 +189,6 @@ func TestReadingSingleLineFromOnePartition(t *testing.T) {
169189
config := sarama.NewConfig()
170190
consumer := mocks.NewConsumer(t, config)
171191

172-
173192
expectedLine := "alpha"
174193
expectedSource := "stdout"
175194
expectedPartial := false

environment.go

+13
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,16 @@ func getEnvVarOrDefault(logCtx logger.Info, envVarName string, defaultValue stri
182182
}
183183
return value
184184
}
185+
186+
func getHostname() string {
187+
hostname, err := os.Hostname()
188+
if err != nil {
189+
//In the event that an error occured, set the hostname to an empty string as we're unable to retrieve it
190+
hostname = ""
191+
192+
//Only log at debug level, we don't want to flood the system if this should happen for a particular reason (permissions perhaps?)
193+
logrus.WithField("err", err).Debug("Unable to retrieve hostname")
194+
}
195+
196+
return hostname
197+
}

0 commit comments

Comments
 (0)