Skip to content

Commit a019391

Browse files
Merge pull request #21 from FireTail-io/dev
Make default batch callback no-op if FIRETAIL_API_TOKEN is unset
2 parents 04a1678 + 5968837 commit a019391

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

logsapi/client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ type Client struct {
1818

1919
func NewClient(options Options) (*Client, error) {
2020
options.setDefaults()
21-
options.loadEnvVars()
21+
err := options.loadEnvVars()
22+
if err != nil {
23+
return nil, err
24+
}
2225

2326
client := &Client{
2427
recordsChannel: make(chan firetail.Record, options.recordsBufferSize),
@@ -29,7 +32,7 @@ func NewClient(options Options) (*Client, error) {
2932
batchCallback: options.BatchCallback,
3033
}
3134

32-
err := subscribeToLogsApi(options.awsLambdaRuntimeAPI, options.ExtensionID)
35+
err = subscribeToLogsApi(options.awsLambdaRuntimeAPI, options.ExtensionID)
3336
if err != nil {
3437
return nil, err
3538
}

logsapi/options.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Options struct {
2929
awsLambdaRuntimeAPI string // The URL of the Lambda Runtime API
3030
maxBatchSize int // The maximum size of a batch to provide to the BatchCallback
3131
recordsBufferSize int // The size of the records channel buffer
32-
firetailApiUrl string // The URL of the Firetail Logging API used by the default BatchCallback
32+
firetailApiUrl string // The URL of the Firetail Logging API used by the default BatchCallback. If it is the empty string, the default BatchCallback is noop
3333
firetailApiToken string // The API token for the Firetail Logging API used by the default BatchCallback
3434
}
3535

@@ -64,12 +64,7 @@ func (o *Options) loadEnvVars() error {
6464
o.maxBatchSize = maxBatchSize
6565
}
6666

67-
firetailApiToken := os.Getenv("FIRETAIL_API_TOKEN")
68-
if firetailApiToken == "" {
69-
return errors.New("FIRETAIL_API_TOKEN not set")
70-
}
71-
o.firetailApiToken = firetailApiToken
72-
67+
o.firetailApiToken = os.Getenv("FIRETAIL_API_TOKEN")
7368
firetailApiUrl := os.Getenv("FIRETAIL_API_URL")
7469
if firetailApiUrl == "" {
7570
firetailApiUrl = DefaultFiretailApiUrl
@@ -82,6 +77,10 @@ func (o *Options) loadEnvVars() error {
8277
func (o *Options) setDefaults() {
8378
if o.BatchCallback == nil {
8479
o.BatchCallback = func(batch []firetail.Record) error {
80+
// If there's no token set, then there's nothing to do - the default is noop
81+
if o.firetailApiToken == "" {
82+
return nil
83+
}
8584
// Try to send the batch to Firetail
8685
log.Printf("Attempting to send batch of %d record(s) to Firetail...", len(batch))
8786
recordsSent, err := firetail.SendRecordsToSaaS(batch, o.firetailApiUrl, o.firetailApiToken)

logsapi/options_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,11 @@ func TestLoadEnvVarsNegativeIntegers(t *testing.T) {
7878
}
7979
}
8080

81-
func TestLoadEnvVarsNoApiToken(t *testing.T) {
82-
testOptions := Options{}
83-
err := testOptions.loadEnvVars()
84-
require.NotNil(t, err)
85-
assert.Equal(t, "FIRETAIL_API_TOKEN not set", err.Error())
86-
}
87-
8881
func TestLoadEnvVarsDefaults(t *testing.T) {
89-
t.Setenv("FIRETAIL_API_TOKEN", "TEST_TOKEN")
9082
testOptions := Options{}
9183
err := testOptions.loadEnvVars()
9284
require.Nil(t, err)
85+
assert.Equal(t, "", testOptions.firetailApiToken)
9386
assert.Equal(t, DefaultRecordsBufferSize, testOptions.recordsBufferSize)
9487
assert.Equal(t, DefaultMaxBatchSize, testOptions.maxBatchSize)
9588
assert.Equal(t, DefaultFiretailApiUrl, testOptions.firetailApiUrl)
@@ -125,7 +118,8 @@ func TestDefaultBatchCallback(t *testing.T) {
125118
defer testServer.Close()
126119

127120
testOptions := &Options{
128-
firetailApiUrl: testServer.URL,
121+
firetailApiToken: "TEST_TOKEN",
122+
firetailApiUrl: testServer.URL,
129123
}
130124
testOptions.setDefaults()
131125

@@ -146,7 +140,8 @@ func TestDefaultBatchCallback(t *testing.T) {
146140

147141
func TestDefaultBatchCallbackFail(t *testing.T) {
148142
testOptions := &Options{
149-
firetailApiUrl: "\n",
143+
firetailApiToken: "TEST_TOKEN",
144+
firetailApiUrl: "\n",
150145
}
151146
testOptions.setDefaults()
152147

0 commit comments

Comments
 (0)