Skip to content

Commit a7255b0

Browse files
authored
Merge pull request #77 from rchicoli/development
set time zone
2 parents 918f87a + 8534e1c commit a7255b0

File tree

8 files changed

+57
-30
lines changed

8 files changed

+57
-30
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ RUN CGO_ENABLED=0 go build -v -a -installsuffix cgo -o /usr/bin/docker-log-elast
1515
FROM alpine:3.7
1616

1717
# RUN apk --no-cache add ca-certificates
18+
RUN apk --no-cache add tzdata
19+
# TZ required to set the localtime
20+
# TZ can be set with docker plugin command
21+
1822
COPY --from=builder /usr/bin/docker-log-elasticsearch /usr/bin/
1923
WORKDIR /usr/bin
2024
ENTRYPOINT [ "/usr/bin/docker-log-elasticsearch" ]

README.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ This application is under active development and will continue to be modified an
3131

3232
You need to install Docker Engine >= 1.12 and Elasticsearch application. Additional information about Docker plugins [can be found here](https://docs.docker.com/engine/extend/plugins_logging/).
3333

34-
## Incompatible docker version
35-
36-
It was found a bug with docker version `17.09.0~ce`. Currently I've been developing this plugin using the docker version `17.05.0~ce`.
37-
Before going stable I will add a cross test for multiple docker versions.
38-
3934
### How to install
4035

4136
The following command will download and enable the plugin.
@@ -44,6 +39,15 @@ The following command will download and enable the plugin.
4439
docker plugin install rchicoli/docker-log-elasticsearch:latest --alias elasticsearch
4540
```
4641

42+
### How to configure
43+
44+
The plugin must be disabled in order to change the [plugin settings](https://docs.docker.com/engine/reference/commandline/plugin_set/)
45+
46+
| Environment | Description | Default Value |
47+
| ----- | ----------- | -------------- |
48+
| LOG_LEVEL | log level to output for plugin logs (debug, info, warn, error) | info |
49+
| TZ | time zone to generate new indexes at midnight | Europe/Berlin |
50+
4751
### How to use
4852

4953
#### Prerequisites
@@ -55,7 +59,7 @@ Before creating a docker container, a healthy instance of Elasticsearch service
5559
| Key | Default Value | Required |
5660
| --- | ------------- | -------- |
5761
| elasticsearch-fields | containerID,containerName,containerImageName,containerCreated | no |
58-
| elasticsearch-index | docker | no |
62+
| elasticsearch-index | docker-%Y.%m.%d | no |
5963
| elasticsearch-insecure | false | no |
6064
| elasticsearch-password | no | no | |
6165
| elasticsearch-sniff | yes | no | |
@@ -87,17 +91,18 @@ Before creating a docker container, a healthy instance of Elasticsearch service
8791
- *index* to write log messages to
8892
- *examples*: docker, logging-%F, docker-%Y.%m.%d
8993

90-
```bash
91-
# FORMAT controls the output. Interpreted sequences are:
92-
%b locale's abbreviated month name (Jan)
93-
%B locale's full month name (January)
94-
%d day of month (01)
95-
%F full date; same as %Y.%m.%d
96-
%j day of year (001..366)
97-
%m month (01..12)
98-
%y last two digits of year (00..99)
99-
%Y year (2018)
100-
```
94+
Interpreted sequences are:
95+
96+
| Regex | Description |
97+
| ----- | ----------- |
98+
| %b | locale's abbreviated month name (Jan) |
99+
| %B | locale's full month name (January) |
100+
| %d | day of month (01) |
101+
| %F | full date; same as %Y.%m.%d |
102+
| %j | day of year (001..366) |
103+
| %m | month (01..12) |
104+
| %y | last two digits of year (00..99) |
105+
| %Y | year (2018) |
101106

102107
###### elasticsearch-username ######
103108
- *username* to authenticate to a secure Elasticsearch cluster
@@ -344,8 +349,8 @@ docker run -ti --rm --log-driver rchicoli/docker-log-elasticsearch:development -
344349

345350
There are some limitations so far, which will be improved at some point.
346351

347-
- grok parses everything to a string field, convertion type is not possible at the moment
348-
- grok-pattern-from requires the file to be inside the plugin's rootfs
352+
- grok parses everything to a string field, convertion type is only possible if a template has been added to elasticsearch
353+
- grok-pattern-from requires the file to be inside the plugin's rootfs. Alternative is the plugin's mount source.
349354

350355
### Description of fields
351356

pkg/docker/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ type Grok struct {
4949

5050
func defaultLogOpt() LogOpt {
5151
return LogOpt{
52-
index: "docker",
53-
// index: "docker-%F",
52+
index: "docker-%Y.%m.%d",
5453
tzpe: "log",
5554
timeout: 1,
5655
fields: "containerID,containerName,containerImageName,containerCreated",

pkg/docker/driver.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,18 @@ func (d *Driver) StartLogging(file string, info logger.Info) error {
171171
c.pipeline.inputCh = make(chan logdriver.LogEntry)
172172
c.pipeline.outputCh = make(chan LogMessage)
173173

174-
c.cron = cron.New()
175-
c.indexName = indexRegex(time.Now(), config.index)
176-
c.cron.AddFunc("@daily", func() {
177-
d.mu.Lock()
174+
if indexFlag(config.index) {
178175
c.indexName = indexRegex(time.Now(), config.index)
179-
d.mu.Unlock()
180-
})
181-
c.cron.Start()
176+
c.cron = cron.New()
177+
c.cron.AddFunc("@daily", func() {
178+
d.mu.Lock()
179+
c.indexName = indexRegex(time.Now(), config.index)
180+
d.mu.Unlock()
181+
})
182+
c.cron.Start()
183+
} else {
184+
c.indexName = config.index
185+
}
182186

183187
c.pipeline.group.Go(func() error {
184188

pkg/docker/index.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import (
1414
// to replace with a defined regular expression
1515
var percent = regexp.MustCompile("%.")
1616

17+
func indexFlag(indexRegex string) bool {
18+
if strings.Contains(indexRegex, "%") {
19+
return true
20+
}
21+
return false
22+
}
23+
1724
func indexRegex(now time.Time, indexRegex string) string {
1825

1926
// %b locale's abbreviated month name (Jan)

plugin/config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
"Settable": [
1919
"value"
2020
]
21+
},
22+
{
23+
"Name": "TZ",
24+
"Description": "Set time zone to generate new indexes at midnight",
25+
"Value": "Europe/Berlin",
26+
"Settable": [
27+
"value"
28+
]
2129
}
2230
],
2331
"Network": {

tests/acceptance-tests/01-defaults.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ function test_default_fields_are_filled_out(){
7070
basht_assert "echo '${output}' | jq -r '.hits.hits[0]._source.message'" == "$message"
7171
basht_assert "echo '${output}' | jq -r '.hits.hits[0]._source.source'" == "stdout"
7272
basht_assert "echo '${output}' | jq -r '.hits.hits[0]._source.partial'" == "true"
73-
basht_assert "echo '${output}' | jq -r '.hits.hits[0]._source.timestamp'" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]+Z$
73+
basht_assert "echo '${output}' | jq -r '.hits.hits[0]._source.timestamp'" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]+(\+02:00|Z)$
7474
basht_assert "echo '${output}' | jq -r '.hits.hits[0]._source[]' | wc -l" == 8
7575

7676
}

tests/helpers.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
ELASTICSEARCH_IP="172.31.0.2"
44
ELASTICSEARCH_PORT="9200"
55

6-
ELASTICSEARCH_INDEX="docker"
6+
ELASTICSEARCH_INDEX="docker-$(date +%Y.%m.%d)"
77
ELASTICSEARCH_TYPE="log"
88

99
WEBAPPER_IP="172.31.0.3"

0 commit comments

Comments
 (0)