├── .gitignore ├── Dockerfile ├── LICENSE ├── README.md ├── build.sh ├── loggly ├── adapter │ ├── adapter.go │ ├── adapter_test.go │ └── loggly_message.go └── loggly.go └── modules.go /.gitignore: -------------------------------------------------------------------------------- 1 | deploy.sh 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM gliderlabs/logspout:master 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Joseph Feeney 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # logspout-loggly 2 | Logspout container for Docker and Loggly. 3 | 4 | [This repo follows this suggested pattern from logspout](https://github.com/gliderlabs/logspout/tree/master/custom) 5 | 6 | You can build the image yourself 7 | 8 | ```docker build -t your-user-name/logspout-loggly ./``` 9 | 10 | and, optionally, push it to your own hub.docker.com repo 11 | 12 | ```docker push your-user-name/logspout-loggly``` 13 | 14 | or you can pull a prebuilt image 15 | 16 | ```docker pull iamatypeofwalrus/logspout-loggly``` 17 | 18 | ## How to run 19 | 20 | ```sh 21 | docker run -d \ 22 | -e 'LOGGLY_TOKEN=' \ 23 | -e 'LOGGLY_TAGS=' \ 24 | -e 'FILTER_NAME=' \ 25 | --volume /var/run/docker.sock:/tmp/docker.sock \ 26 | iamatypeofwalrus/logspout-loggly 27 | ``` 28 | 29 | ## How it works 30 | Instead of linking containers together or bothering with [syslog or remote syslog](https://www.loggly.com/blog/centralize-logs-docker-containers) this container follows the [12 Factor app logging philosophy](http://12factor.net/logs). If your docker container(s) log(s) to STDOUT this image will pick up that stream from the docker daemon send those events to Loggly. 31 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | set -e 3 | apk add --update go git mercurial build-base ca-certificates 4 | mkdir -p /go/src/github.com/gliderlabs 5 | cp -r /src /go/src/github.com/gliderlabs/logspout 6 | cd /go/src/github.com/gliderlabs/logspout 7 | export GOPATH=/go 8 | go get 9 | go build -ldflags "-X main.Version=$1" -o /bin/logspout 10 | apk del go git mercurial build-base 11 | rm -rf /go 12 | rm -rf /var/cache/apk/* 13 | 14 | # backwards compatibility 15 | ln -fs /tmp/docker.sock /var/run/docker.sock 16 | -------------------------------------------------------------------------------- /loggly/adapter/adapter.go: -------------------------------------------------------------------------------- 1 | package adapter 2 | 3 | import ( 4 | "bytes" 5 | "encoding/json" 6 | "fmt" 7 | "log" 8 | "net/http" 9 | "os" 10 | "time" 11 | 12 | "github.com/gliderlabs/logspout/router" 13 | ) 14 | 15 | const ( 16 | logglyAddr = "https://logs-01.loggly.com" 17 | logglyEventEndpoint = "/bulk" 18 | flushTimeout = 10 * time.Second 19 | ) 20 | 21 | // Adapter satisfies the router.LogAdapter interface by providing Stream which 22 | // passes all messages to loggly. 23 | type Adapter struct { 24 | bufferSize int 25 | log *log.Logger 26 | logglyURL string 27 | queue chan logglyMessage 28 | } 29 | 30 | // New returns an Adapter that receives messages from logspout. Additionally, 31 | // it launches a goroutine to buffer and flush messages to loggly. 32 | func New(logglyToken string, tags string, bufferSize int) *Adapter { 33 | adapter := &Adapter{ 34 | bufferSize: bufferSize, 35 | log: log.New(os.Stdout, "logspout-loggly", log.LstdFlags), 36 | logglyURL: buildLogglyURL(logglyToken, tags), 37 | queue: make(chan logglyMessage), 38 | } 39 | 40 | go adapter.readQueue() 41 | 42 | return adapter 43 | } 44 | 45 | // Stream satisfies the router.LogAdapter interface and passes all messages to 46 | // Loggly 47 | func (l *Adapter) Stream(logstream chan *router.Message) { 48 | for m := range logstream { 49 | l.queue <- logglyMessage{ 50 | Message: m.Data, 51 | ContainerName: m.Container.Name, 52 | ContainerID: m.Container.ID, 53 | ContainerImage: m.Container.Config.Image, 54 | ContainerHostname: m.Container.Config.Hostname, 55 | } 56 | } 57 | } 58 | 59 | func (l *Adapter) readQueue() { 60 | buffer := l.newBuffer() 61 | 62 | timeout := time.NewTimer(flushTimeout) 63 | 64 | for { 65 | select { 66 | case msg := <-l.queue: 67 | if len(buffer) == cap(buffer) { 68 | timeout.Stop() 69 | l.flushBuffer(buffer) 70 | buffer = l.newBuffer() 71 | } 72 | 73 | buffer = append(buffer, msg) 74 | 75 | case <-timeout.C: 76 | if len(buffer) > 0 { 77 | l.flushBuffer(buffer) 78 | buffer = l.newBuffer() 79 | } 80 | } 81 | 82 | timeout.Reset(flushTimeout) 83 | } 84 | } 85 | 86 | func (l *Adapter) newBuffer() []logglyMessage { 87 | return make([]logglyMessage, 0, l.bufferSize) 88 | } 89 | 90 | func (l *Adapter) flushBuffer(buffer []logglyMessage) { 91 | var data bytes.Buffer 92 | 93 | for _, msg := range buffer { 94 | j, _ := json.Marshal(msg) 95 | data.Write(j) 96 | data.WriteString("\n") 97 | } 98 | 99 | req, _ := http.NewRequest( 100 | "POST", 101 | l.logglyURL, 102 | &data, 103 | ) 104 | 105 | go l.sendRequestToLoggly(req) 106 | } 107 | 108 | func (l *Adapter) sendRequestToLoggly(req *http.Request) { 109 | resp, err := http.DefaultClient.Do(req) 110 | 111 | if resp != nil { 112 | defer resp.Body.Close() 113 | } 114 | 115 | if err != nil { 116 | l.log.Println( 117 | fmt.Errorf( 118 | "error from client: %s", 119 | err.Error(), 120 | ), 121 | ) 122 | return 123 | } 124 | 125 | if resp.StatusCode != http.StatusOK { 126 | l.log.Println( 127 | fmt.Errorf( 128 | "received a %s status code when sending message. response: %s", 129 | resp.StatusCode, 130 | resp.Body, 131 | ), 132 | ) 133 | } 134 | } 135 | 136 | func buildLogglyURL(token, tags string) string { 137 | var url string 138 | url = fmt.Sprintf( 139 | "%s%s/%s", 140 | logglyAddr, 141 | logglyEventEndpoint, 142 | token, 143 | ) 144 | 145 | if tags != "" { 146 | url = fmt.Sprintf( 147 | "%s/tag/%s/", 148 | url, 149 | tags, 150 | ) 151 | } 152 | return url 153 | } 154 | -------------------------------------------------------------------------------- /loggly/adapter/adapter_test.go: -------------------------------------------------------------------------------- 1 | package adapter 2 | 3 | import ( 4 | "bytes" 5 | "log" 6 | "net/http" 7 | "net/http/httptest" 8 | "testing" 9 | ) 10 | 11 | func TestNew(t *testing.T) { 12 | a := New( 13 | "notreal", 14 | "development,sandbox", 15 | 1, 16 | ) 17 | 18 | if a.logglyURL == "" { 19 | t.Error("expected New to set logglyURL") 20 | } 21 | } 22 | 23 | func TestBuffer(t *testing.T) { 24 | bufferSize := 10 25 | a := &Adapter{ 26 | bufferSize: bufferSize, 27 | } 28 | buf := a.newBuffer() 29 | 30 | if len(buf) != 0 { 31 | t.Errorf("expected new buffer length to be 0 but it was %s", len(buf)) 32 | } 33 | 34 | if cap(buf) != bufferSize { 35 | t.Errorf("expected buffer capacity to be %s but it was %s", bufferSize, cap(buf)) 36 | } 37 | 38 | buf = append(buf, logglyMessage{}) 39 | 40 | if len(buf) != 1 { 41 | t.Errorf( 42 | "expected buffer length to be 1 after adding item but it was %s", 43 | len(buf), 44 | ) 45 | } 46 | 47 | oldBuffer := buf 48 | newBuffer := a.newBuffer() 49 | 50 | if &oldBuffer == &newBuffer { 51 | t.Error("expected oldBuffer and newBuffer to point to different structs") 52 | } 53 | } 54 | 55 | func TestBuildLogglyURL(t *testing.T) { 56 | var expectedURL string 57 | var actualURL string 58 | 59 | tag := "development" 60 | tags := "development,sandbox" 61 | token := "notreal" 62 | 63 | expectedURL = "https://logs-01.loggly.com/bulk/notreal" 64 | actualURL = buildLogglyURL(token, "") 65 | 66 | if actualURL != expectedURL { 67 | t.Errorf( 68 | "expected URL to be %s but was %s", 69 | expectedURL, 70 | actualURL, 71 | ) 72 | } 73 | 74 | expectedURL = "https://logs-01.loggly.com/bulk/notreal/tag/development/" 75 | actualURL = buildLogglyURL(token, tag) 76 | 77 | if actualURL != expectedURL { 78 | t.Errorf( 79 | "expected actualURL to be %s but was %s", 80 | expectedURL, 81 | actualURL, 82 | ) 83 | } 84 | 85 | expectedURL = "https://logs-01.loggly.com/bulk/notreal/tag/development,sandbox/" 86 | actualURL = buildLogglyURL(token, tags) 87 | 88 | if actualURL != expectedURL { 89 | t.Errorf( 90 | "expected actualURL to be %s but was %s", 91 | expectedURL, 92 | actualURL, 93 | ) 94 | } 95 | } 96 | 97 | func TestSendRequestToLoggly(t *testing.T) { 98 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 99 | http.Error(w, "bad request", http.StatusBadRequest) 100 | })) 101 | defer ts.Close() 102 | 103 | var testBuff bytes.Buffer 104 | 105 | a := &Adapter{ 106 | bufferSize: 1, 107 | log: log.New(&testBuff, "logspout-loggly", log.LstdFlags), 108 | logglyURL: ts.URL, 109 | queue: make(chan logglyMessage), 110 | } 111 | 112 | req, err := http.NewRequest( 113 | "POST", 114 | ts.URL, 115 | bytes.NewBufferString("fake data"), 116 | ) 117 | 118 | if err != nil { 119 | t.Errorf("expected error to be nil when creating request: %s", err.Error()) 120 | } 121 | 122 | a.sendRequestToLoggly(req) 123 | 124 | if !(testBuff.Len() > 0) { 125 | t.Error("expected snedRequestToLoggly to write to log when it receives a non 200 response from loggly") 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /loggly/adapter/loggly_message.go: -------------------------------------------------------------------------------- 1 | package adapter 2 | 3 | type logglyMessage struct { 4 | Message string `json:"message"` 5 | ContainerName string `json:"container_name"` 6 | ContainerID string `json:"container_id"` 7 | ContainerImage string `json:"container_image"` 8 | ContainerHostname string `json:"hostname"` 9 | } 10 | -------------------------------------------------------------------------------- /loggly/loggly.go: -------------------------------------------------------------------------------- 1 | package loggly 2 | 3 | import ( 4 | "errors" 5 | "log" 6 | "os" 7 | 8 | "github.com/gliderlabs/logspout/router" 9 | "github.com/iamatypeofwalrus/logspout-loggly/loggly/adapter" 10 | ) 11 | 12 | const ( 13 | adapterName = "loggly" 14 | filterNameEnvVar = "FILTER_NAME" 15 | logglyTokenEnvVar = "LOGGLY_TOKEN" 16 | logglyTagsEnvVar = "LOGGLY_TAGS" 17 | ) 18 | 19 | func init() { 20 | router.AdapterFactories.Register(NewLogglyAdapter, adapterName) 21 | 22 | r := &router.Route{ 23 | Adapter: "loggly", 24 | FilterName: os.Getenv(filterNameEnvVar), 25 | } 26 | 27 | // It's not documented in the logspout repo but if you want to use an adapter 28 | // without going through the routesapi you must add at #init or via #New... 29 | err := router.Routes.Add(r) 30 | if err != nil { 31 | log.Fatal("could not add route: ", err.Error()) 32 | } 33 | } 34 | 35 | // NewLogglyAdapter returns an Adapter with that uses a loggly token taken from 36 | // the LOGGLY_TOKEN environment variable 37 | func NewLogglyAdapter(route *router.Route) (router.LogAdapter, error) { 38 | token := os.Getenv(logglyTokenEnvVar) 39 | tags := os.Getenv(logglyTagsEnvVar) 40 | 41 | if token == "" { 42 | return nil, errors.New( 43 | "could not find environment variable LOGGLY_TOKEN", 44 | ) 45 | } 46 | 47 | return adapter.New( 48 | token, 49 | tags, 50 | 100, 51 | ), nil 52 | } 53 | -------------------------------------------------------------------------------- /modules.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | _ "github.com/gliderlabs/logspout/adapters/raw" 5 | _ "github.com/gliderlabs/logspout/adapters/syslog" 6 | _ "github.com/gliderlabs/logspout/httpstream" 7 | _ "github.com/gliderlabs/logspout/routesapi" 8 | _ "github.com/gliderlabs/logspout/transports/tcp" 9 | _ "github.com/gliderlabs/logspout/transports/udp" 10 | _ "github.com/gliderlabs/logspout/transports/tls" 11 | _ "github.com/iamatypeofwalrus/logspout-loggly/loggly" 12 | ) 13 | --------------------------------------------------------------------------------