Skip to content

Commit 1bf62b0

Browse files
authored
Merge pull request #87 from rchicoli/organize-code
Organize code
2 parents 1a496bb + cda5b23 commit 1bf62b0

File tree

8 files changed

+442
-421
lines changed

8 files changed

+442
-421
lines changed

pkg/docker/config.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"github.com/docker/docker/daemon/logger"
1212
)
1313

14-
// LogOpt ...
15-
type LogOpt struct {
14+
// Configuration is a type to all log-opt provided
15+
type Configuration struct {
1616
index string
1717
tzpe string
1818
url string
@@ -38,7 +38,7 @@ type Bulk struct {
3838
stats bool
3939
}
4040

41-
// Grok ...
41+
// Grok filter
4242
type Grok struct {
4343
grokPattern string
4444
grokPatternFrom string
@@ -47,8 +47,8 @@ type Grok struct {
4747
grokNamedCapture bool
4848
}
4949

50-
func defaultLogOpt() LogOpt {
51-
return LogOpt{
50+
func newConfiguration() Configuration {
51+
return Configuration{
5252
index: "docker-%Y.%m.%d",
5353
tzpe: "log",
5454
timeout: 1,
@@ -95,7 +95,7 @@ func parseAddress(address string) error {
9595
}
9696

9797
// ValidateLogOpt looks for es specific log option es-address.
98-
func (c *LogOpt) validateLogOpt(cfg map[string]string) error {
98+
func (c *Configuration) validateLogOpt(cfg map[string]string) error {
9999
for key, v := range cfg {
100100
switch key {
101101
case "elasticsearch-url":
@@ -205,9 +205,6 @@ func (c *LogOpt) validateLogOpt(cfg map[string]string) error {
205205
}
206206
c.grokNamedCapture = s
207207

208-
// case "tag":
209-
// case "labels":
210-
// case "env":
211208
default:
212209
return fmt.Errorf("error: unknown log-opt: %q", v)
213210
}
@@ -216,7 +213,7 @@ func (c *LogOpt) validateLogOpt(cfg map[string]string) error {
216213
return nil
217214
}
218215

219-
func getLogOptFields(fields string, info logger.Info) LogMessage {
216+
func getLogMessageFields(fields string, info logger.Info) LogMessage {
220217
var l LogMessage
221218
for _, v := range strings.Split(fields, ",") {
222219
switch v {

pkg/docker/container.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package docker
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io"
7+
"path"
8+
"syscall"
9+
10+
log "github.com/Sirupsen/logrus"
11+
"github.com/docker/docker/api/types/plugins/logdriver"
12+
"github.com/docker/docker/daemon/logger"
13+
"github.com/rchicoli/docker-log-elasticsearch/pkg/elasticsearch"
14+
"github.com/robfig/cron"
15+
"github.com/tonistiigi/fifo"
16+
"golang.org/x/sync/errgroup"
17+
)
18+
19+
type container struct {
20+
cron *cron.Cron
21+
esClient elasticsearch.Client
22+
indexName string
23+
info logger.Info
24+
logger *log.Entry
25+
pipeline pipeline
26+
stream io.ReadCloser
27+
}
28+
29+
type pipeline struct {
30+
group *errgroup.Group
31+
inputCh chan logdriver.LogEntry
32+
outputCh chan LogMessage
33+
}
34+
35+
// newContainer stores the container's configuration in memory
36+
// and returns a pointer to the container
37+
func (d *Driver) newContainer(ctx context.Context, file string) (*container, error) {
38+
39+
filename := path.Base(file)
40+
log.WithField("fifo", file).Debug("created fifo file")
41+
42+
d.mu.Lock()
43+
if _, exists := d.logs[filename]; exists {
44+
return nil, fmt.Errorf("error: a logger for this container already exists: %s", filename)
45+
}
46+
d.mu.Unlock()
47+
48+
f, err := fifo.OpenFifo(ctx, file, syscall.O_RDONLY, 0700)
49+
if err != nil {
50+
return nil, fmt.Errorf("could not open fifo: %q", err)
51+
}
52+
53+
d.mu.Lock()
54+
c := &container{stream: f}
55+
d.logs[filename] = c
56+
d.mu.Unlock()
57+
58+
return c, nil
59+
}
60+
61+
// getContainer retrieves the container's configuration from memory
62+
func (d *Driver) getContainer(file string) (*container, error) {
63+
64+
filename := path.Base(file)
65+
66+
d.mu.Lock()
67+
defer d.mu.Unlock()
68+
69+
c, exists := d.logs[filename]
70+
if !exists {
71+
return nil, fmt.Errorf("error: logger not found for socket ID: %v", file)
72+
}
73+
74+
return c, nil
75+
}

0 commit comments

Comments
 (0)