Skip to content

Commit aaa2f8e

Browse files
author
Matthew Fisher
committed
Merge pull request #3524 from bacongobbler/logger-enhancements
feat(logger): expose config as flags
2 parents 053eb4e + b2ecf87 commit aaa2f8e

5 files changed

Lines changed: 51 additions & 42 deletions

File tree

logger/README.md

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,9 @@ the [Deis Project](https://github.com/deis/deis).
1010

1111
## Usage
1212

13-
Please consult the [Makefile](Makefile) for current instructions on how to build, test, push,
14-
install, and start **deis/logger**.
15-
16-
## Environment Variables
17-
18-
* **DEBUG** enables verbose output if set
19-
* **ETCD_PORT** sets the TCP port on which to connect to the local etcd
20-
daemon (default: *4001*)
21-
* **ETCD_PATH** sets the etcd directory where the logger announces
22-
its configuration (default: */deis/logs*)
23-
* **ETCD_TTL** sets the time-to-live before etcd purges a configuration
24-
value, in seconds (default: *10*)
25-
* **PORT** sets the TCP port on which the logger listens (default: *514*)
13+
```bash
14+
$ docker run deis/logger --help
15+
```
2616

2717
## License
2818

logger/image/Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM ubuntu-debootstrap:14.04
22

3-
CMD ["/bin/logger"]
3+
ENTRYPOINT ["/bin/logger"]
4+
CMD ["--enable-publish"]
45
EXPOSE 514
56

67
ADD . /

logger/main.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,67 @@
11
package main
22

33
import (
4+
"flag"
5+
"fmt"
46
"log"
57
"os"
68
"os/signal"
9+
"strconv"
710
"syscall"
811
"time"
912

1013
"github.com/coreos/go-etcd/etcd"
11-
1214
"github.com/deis/deis/logger/syslogd"
1315
)
1416

15-
const (
16-
timeout time.Duration = 10 * time.Second
17-
ttl time.Duration = timeout * 2
17+
var (
18+
logAddr string
19+
logPort int
20+
enablePublish bool
21+
publishHost string
22+
publishPath string
23+
publishPort string
24+
publishInterval int
25+
publishTTL int
1826
)
1927

20-
func main() {
21-
host := getopt("HOST", "127.0.0.1")
22-
23-
etcdPort := getopt("ETCD_PORT", "4001")
24-
etcdPath := getopt("ETCD_PATH", "/deis/logs")
28+
func init() {
29+
flag.StringVar(&logAddr, "log-addr", "0.0.0.0", "bind address for the logger")
30+
flag.IntVar(&logPort, "log-port", 514, "bind port for the logger")
31+
flag.StringVar(&syslogd.LogRoot, "log-root", "/data/logs", "log path to store logs")
32+
flag.BoolVar(&enablePublish, "enable-publish", false, "enable publishing to service discovery")
33+
flag.StringVar(&publishHost, "publish-host", getopt("HOST", "127.0.0.1"), "service discovery hostname")
34+
flag.IntVar(&publishInterval, "publish-interval", 10, "publish interval in seconds")
35+
flag.StringVar(&publishPath, "publish-path", getopt("ETCD_PATH", "/deis/logs"), "path to publish host/port information")
36+
flag.StringVar(&publishPort, "publish-port", getopt("ETCD_PORT", "4001"), "service discovery port")
37+
flag.IntVar(&publishTTL, "publish-ttl", publishInterval*2, "publish TTL in seconds")
38+
}
2539

26-
externalPort := getopt("EXTERNAL_PORT", "514")
40+
func main() {
41+
flag.Parse()
2742

28-
client := etcd.NewClient([]string{"http://" + host + ":" + etcdPort})
43+
client := etcd.NewClient([]string{"http://" + publishHost + ":" + publishPort})
2944

3045
// Wait for terminating signal
3146
exitChan := make(chan os.Signal, 2)
3247
cleanupChan := make(chan bool)
3348
signal.Notify(exitChan, syscall.SIGTERM, syscall.SIGINT)
3449

35-
go syslogd.Listen(exitChan, cleanupChan)
50+
go syslogd.Listen(exitChan, cleanupChan, fmt.Sprintf("%s:%d", logAddr, logPort))
3651

37-
go publishService(client, host, etcdPath, externalPort, uint64(ttl.Seconds()))
52+
if enablePublish {
53+
go publishService(client, publishHost, publishPath, strconv.Itoa(logPort), uint64(time.Duration(publishTTL).Seconds()))
54+
}
3855

3956
// Wait for the proper shutdown of the syslog server before exit
4057
<-cleanupChan
4158
}
4259

43-
func publishService(client *etcd.Client, host string, etcdPath string, externalPort string, ttl uint64) {
60+
func publishService(client *etcd.Client, host string, etcdPath string, port string, ttl uint64) {
4461
for {
4562
setEtcd(client, etcdPath+"/host", host, ttl)
46-
setEtcd(client, etcdPath+"/port", externalPort, ttl)
47-
time.Sleep(timeout)
63+
setEtcd(client, etcdPath+"/port", port, ttl)
64+
time.Sleep(time.Duration(publishInterval))
4865
}
4966
}
5067

logger/syslogd/syslogd.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/deis/deis/logger/drain"
1414
)
1515

16-
const logRoot = "/data/logs"
16+
var LogRoot string
1717

1818
type handler struct {
1919
// To simplify implementation of our handler we embed helper
@@ -51,7 +51,7 @@ func getLogFile(message string) (io.Writer, error) {
5151
return nil, fmt.Errorf("Could not find app name in message: %s", message)
5252
}
5353
appName := match[1]
54-
filePath := path.Join(logRoot, appName+".log")
54+
filePath := path.Join(LogRoot, appName+".log")
5555
// check if file exists
5656
exists, err := fileExists(filePath)
5757
if err != nil {
@@ -97,19 +97,19 @@ func (h *handler) mainLoop() {
9797
}
9898

9999
// Listen starts a new syslog server which runs until it receives a signal.
100-
func Listen(signalChan chan os.Signal, cleanupDone chan bool) {
100+
func Listen(signalChan chan os.Signal, cleanupDone chan bool, bindAddr string) {
101101
fmt.Println("Starting syslog...")
102-
// If logRoot doesn't exist, create it
102+
// If LogRoot doesn't exist, create it
103103
// equivalent to Python's `if not os.path.exists(filename)`
104-
if _, err := os.Stat(logRoot); os.IsNotExist(err) {
105-
if err = os.MkdirAll(logRoot, 0777); err != nil {
106-
log.Fatalf("unable to create logRoot at %s", logRoot)
104+
if _, err := os.Stat(LogRoot); os.IsNotExist(err) {
105+
if err = os.MkdirAll(LogRoot, 0777); err != nil {
106+
log.Fatalf("unable to create LogRoot at %s: %v", LogRoot, err)
107107
}
108108
}
109109
// Create a server with one handler and run one listen gorutine
110110
s := syslog.NewServer()
111111
s.AddHandler(newHandler())
112-
s.Listen("0.0.0.0:514")
112+
s.Listen(bindAddr)
113113
fmt.Println("Syslog server started...")
114114
fmt.Println("deis-logger running")
115115

logger/tests/logger_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ func TestLogger(t *testing.T) {
3131
"--name", name,
3232
"--rm",
3333
"-p", port+":514/udp",
34-
"-e", "EXTERNAL_PORT="+port,
35-
"-e", "HOST="+host,
36-
"-e", "ETCD_PORT="+etcdPort,
37-
imageName)
34+
imageName,
35+
"--enable-publish",
36+
"--log-port="+port,
37+
"--publish-host="+host,
38+
"--publish-port="+etcdPort)
3839
}()
3940
dockercli.PrintToStdout(t, stdout, stdoutPipe, "deis-logger running")
4041
if err != nil {

0 commit comments

Comments
 (0)