Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ FROM gliderlabs/logspout:${LOGSPOUT_VERSION} as logspout
#
# Build stage, build logspout with fluentd adapter
#
FROM golang:1.12.5-alpine3.9 as builder
FROM golang:1.20.3-alpine3.17 as builder
RUN apk add --update go build-base git mercurial ca-certificates git
ENV GO111MODULE=on
WORKDIR /go/src/github.com/gliderlabs/logspout
Expand All @@ -20,6 +20,7 @@ ADD . /go/src/github.com/dsouzajude/logspout-fluentd
RUN cd /go/src/github.com/dsouzajude/logspout-fluentd; go mod download
RUN cd /go/src/github.com/gliderlabs/logspout; go mod download
RUN echo "replace github.com/dsouzajude/logspout-fluentd => /go/src/github.com/dsouzajude/logspout-fluentd" >> go.mod
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.Version=$1" -o /bin/logspout


Expand Down
16 changes: 13 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Optional environment variables:

- `TAG_PREFIX` the *fixed* tag prefix
- `TAG_SUFFIX_LABEL` the docker label key for which to substitue as the *dynamic* tag suffix
- `LOG_LEVEL` [trace|debug|info|warning|error|fatal|panic] Change the log level. Defaults to info.

The other fluentd specific environment variables include the list below and
their explanation can be better read from the fluentd log driver [website](https://docs.docker.com/config/containers/logging/fluentd/).
Expand All @@ -40,7 +41,6 @@ their explanation can be better read from the fluentd log driver [website](https
- `FLUENTD_REQUEST_ACK` [true|false] For reliability. Fluent-bit currently doesn't support this. Defaults to false.
- `FLUENTD_WRITE_TIMEOUT` [int] Write timeout to post to fluentd/fluent-bit. Defaults to 3 seconds.


Configure Logspout to receive forwarded messages, something like this:

```
Expand Down Expand Up @@ -82,9 +82,19 @@ Configure Logspout to receive forwarded messages, something like this:

# Run standalone fluent-bit instance
>> docker run -ti -p 24224:24224 \
fluent/fluent-bit:1.2 /fluent-bit/bin/fluent-bit \
-i forward://0.0.0.0:24224 -o stdout
fluent/fluent-bit:2.0.11 /fluent-bit/bin/fluent-bit \
-i forward -o stdout

# Run custom built of logspout locally:
>> docker run --rm --name="logspout" --network host \
-v /var/run/docker.sock:/var/run/docker.sock \
-e TAG_PREFIX=docker \
-e TAG_SUFFIX_LABEL="com.mycompany.service" \
-e LOG_LEVEL="debug" \
-e FLUENTD_ASYNC_CONNECT="true" \
-e LOGSPOUT="ignore" \
mycustomlogspout \
./logspout fluentd://localhost:24224

# Send sample log from test container, you should view the log entry
# captured in logspout and a similar log entry in fluent-bit.
Expand Down
39 changes: 31 additions & 8 deletions fluentd/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ command after building:
*
*/
import (
"log"
"math"
"net"
"os"
"regexp"
"strconv"
"time"

log "github.com/sirupsen/logrus"
easy "github.com/t-tomalak/logrus-easy-formatter"

"github.com/fluent/fluent-logger-golang/fluent"
"github.com/gliderlabs/logspout/router"
"github.com/pkg/errors"
Expand Down Expand Up @@ -58,11 +60,15 @@ type Adapter struct {

// Stream handles a stream of messages from Logspout. Implements router.logAdapter.
func (ad *Adapter) Stream(logstream chan *router.Message) {
// create a regex pattern to identify empty messages
pattern, err := regexp.Compile("^[[:space:]]*$")
if err != nil {
log.Fatalf("Could not compile regexp: %v", err)
}
for message := range logstream {
// Skip if message is empty
messageIsEmpty, err := regexp.MatchString("^[[:space:]]*$", message.Data)
if messageIsEmpty {
log.Println("Skipping empty message!")
if pattern.MatchString(message.Data) {
log.Debug("Skipping empty message!")
continue
}

Expand All @@ -84,12 +90,12 @@ func (ad *Adapter) Stream(logstream chan *router.Message) {
"container_name": message.Container.Name,
"source": message.Source,
}
log.Println(tag, message.Time, record)
log.Debugf("tag=%s; record=%s", tag, record)

// Send to fluentd
err = ad.writer.PostWithTime(tag, message.Time, record)
if err != nil {
log.Println("fluentd-adapter PostWithTime Error: ", err)
log.Errorf("fluentd-adapter PostWithTime Error: %v", err)
continue
}
}
Expand All @@ -105,15 +111,19 @@ func NewAdapter(route *router.Route) (router.LogAdapter, error) {
if err != nil {
return nil, err
}
log.Println("Connectivity successful to fluentd @ " + route.Address)
log.Infof("Connectivity successful to fluentd @ %s", route.Address)

// Construct fluentd config object
host, port, err := net.SplitHostPort(route.Address)
portNum, err := strconv.Atoi(port)
if err != nil {
return nil, errors.Wrapf(err, "Invalid fluentd-address %s", route.Address)
}

portNum, err := strconv.Atoi(port)
if err != nil {
return nil, errors.Wrapf(err, "Invalid port number in fluentd-address %s", route.Address)
}

bufferLimit, err := strconv.Atoi(getenv("FLUENTD_BUFFER_LIMIT", strconv.Itoa(defaultBufferLimit)))
if err != nil {
return nil, err
Expand Down Expand Up @@ -179,5 +189,18 @@ func NewAdapter(route *router.Route) (router.LogAdapter, error) {
}

func init() {
logLevel, err := log.ParseLevel(getenv("LOG_LEVEL", "info"))
if err != nil {
log.Fatalf("Error parsing log level: %v", err)
}
log.SetFormatter(
&easy.Formatter{
TimestampFormat: "2006-01-02 15:04:05",
LogFormat: "%time% [%lvl%] %msg%\n",
},
)
log.SetLevel(logLevel)
log.Infof("LOG_LEVEL=%s", logLevel)

router.AdapterFactories.Register(NewAdapter, "fluentd")
}
40 changes: 34 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
module github.com/dsouzajude/logspout-fluentd

go 1.12
go 1.20

require (
github.com/Sirupsen/logrus v0.10.1-0.20160601113210-f3cfb454f4c2 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/fluent/fluent-logger-golang v1.4.0
github.com/fsouza/go-dockerclient v1.6.0
github.com/gliderlabs/logspout v3.2.6+incompatible
github.com/kr/pretty v0.2.0 // indirect
github.com/philhofer/fwd v1.0.0 // indirect
github.com/pkg/errors v0.8.1
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.4.0
github.com/tinylib/msgp v1.1.1 // indirect
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816
)

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.15-0.20190919025122-fc70bd9a86b5 // indirect
github.com/Microsoft/hcsshim v0.8.7-0.20191101173118-65519b62243c // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/containerd/containerd v1.3.0 // indirect
github.com/containerd/continuity v0.0.0-20190426062206-aaeac12a7ffc // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v1.4.2-0.20191101170500-ac7306503d23 // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/gogo/protobuf v1.2.1 // indirect
github.com/golang/protobuf v1.3.1 // indirect
github.com/gorilla/mux v1.7.3 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c // indirect
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/opencontainers/runc v1.1.6 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tinylib/msgp v1.1.8 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb // indirect
google.golang.org/grpc v1.22.0 // indirect
gopkg.in/yaml.v2 v2.2.2 // indirect
)
Loading