Skip to content

Commit

Permalink
Add method to set fluentbit hook (#3140)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
bathina2 and mergify[bot] authored Sep 24, 2024
1 parent 1b33856 commit 22be170
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"io"
"net/url"
"os"
"strings"
"time"
Expand Down Expand Up @@ -32,6 +33,12 @@ const (
LevelEnvName = "LOG_LEVEL"
)

var (
ErrEndpointNotSet = errors.New("fluentbit endpoint not set")
ErrNonTCPEndpoint = errors.New("fluentbit endpoint scheme must be tcp")
ErrPathSet = errors.New("fluentbit endpoint path is set")
)

// OutputSink describes the current output sink.
type OutputSink uint8

Expand Down Expand Up @@ -79,6 +86,22 @@ func SetOutput(sink OutputSink) error {
}
}

// SetFluentbitOutput sets the fluentbit output
func SetFluentbitOutput(url *url.URL) error {
if url == nil || url.Host == "" {
return ErrEndpointNotSet
}
if url.Scheme != "tcp" {
return ErrNonTCPEndpoint
}
if url.Path != "" {
return ErrPathSet
}
hook := NewFluentbitHook(url.Host)
log.AddHook(hook)
return nil
}

var envVarFields field.Fields

// initEnvVarFields populates envVarFields with values from the host's environment.
Expand Down
47 changes: 47 additions & 0 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"sync"
"testing"
Expand Down Expand Up @@ -226,6 +227,52 @@ func (s *LogSuite) TestCloneGlobalLogger(c *check.C) {
c.Assert(hook.capturedMessages[0].Message, check.Equals, "Test message")
}

func (s *LogSuite) TestSetFluentbitOutput(c *check.C) {
for _, tc := range []struct {
desc string
url *url.URL
err error
}{
{
desc: "valid_url",
url: &url.URL{
Scheme: "tcp",
Host: "something",
},
},
{
desc: "path_is_set",
url: &url.URL{
Scheme: "tcp",
Host: "something",
Path: "something",
},
err: ErrPathSet,
},
{
desc: "non_tcp_endpoint",
url: &url.URL{
Scheme: "http",
Host: "something",
Path: "something",
},
err: ErrNonTCPEndpoint,
},
{
desc: "empty_endpoint",
url: &url.URL{},
err: ErrEndpointNotSet,
},
{
desc: "nil_endpoint",
err: ErrEndpointNotSet,
},
} {
err := SetFluentbitOutput(tc.url)
c.Assert(err, check.Equals, tc.err)
}
}

type logHook struct {
capturedMessages []*logrus.Entry
}
Expand Down

0 comments on commit 22be170

Please sign in to comment.