Skip to content

Commit

Permalink
pkg/splunk_logger: handle potentially dangling goroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
diaasami authored and ondrejbudai committed Nov 10, 2023
1 parent 43f43c8 commit 13d642b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pkg/splunk_logger/splunk_hook.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"context"
"fmt"
"os"

Expand All @@ -11,15 +12,15 @@ type SplunkHook struct {
sl *SplunkLogger
}

func NewSplunkHook(host, port, token, source string) (*SplunkHook, error) {
func NewSplunkHook(context context.Context, host, port, token, source string) (*SplunkHook, error) {
url := fmt.Sprintf("https://%s:%s/services/collector/event", host, port)
hostname, err := os.Hostname()
if err != nil {
return nil, err
}

return &SplunkHook{
sl: NewSplunkLogger(url, token, source, hostname),
sl: NewSplunkLogger(context, url, token, source, hostname),
}, nil
}

Expand Down
13 changes: 10 additions & 3 deletions pkg/splunk_logger/splunk_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logger

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -40,7 +41,7 @@ type SplunkEvent struct {
Host string `json:"host"`
}

func NewSplunkLogger(url, token, source, hostname string) *SplunkLogger {
func NewSplunkLogger(context context.Context, url, token, source, hostname string) *SplunkLogger {
sl := &SplunkLogger{
client: retryablehttp.NewClient().StandardClient(),
url: url,
Expand All @@ -52,15 +53,21 @@ func NewSplunkLogger(url, token, source, hostname string) *SplunkLogger {
ticker := time.NewTicker(time.Second * SendFrequency)
sl.payloads = make(chan *SplunkPayload, PayloadsChannelSize)

go sl.flushPayloads(ticker.C)
go sl.flushPayloads(context, ticker.C)

return sl
}

func (sl *SplunkLogger) flushPayloads(ticker <-chan time.Time) {
func (sl *SplunkLogger) flushPayloads(context context.Context, ticker <-chan time.Time) {
var payloads []*SplunkPayload
for {
select {
case <-context.Done():
err := sl.SendPayloads(payloads)
if err != nil {
fmt.Fprintf(os.Stderr, "Splunk logger unable to send payloads: %v", err)
}
return
case p := <-sl.payloads:
if p != nil {
payloads = append(payloads, p)
Expand Down
65 changes: 65 additions & 0 deletions pkg/splunk_logger/splunk_logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package logger

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestSplunkLogger(t *testing.T) {
ch := make(chan bool)
time.AfterFunc(time.Second*10, func() {
ch <- false
})
count := 0
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// make sure the logger retries requests
if count == 0 {
count += 1
w.WriteHeader(http.StatusInternalServerError)
return
}
require.Equal(t, "Splunk", r.Header.Get("Authorization"))
require.Equal(t, "application/json", r.Header.Get("Content-Type"))
var sp SplunkPayload
err := json.NewDecoder(r.Body).Decode(&sp)
require.NoError(t, err)
require.Equal(t, "test-host", sp.Host)
require.Equal(t, "test-host", sp.Event.Host)
require.Equal(t, "image-builder", sp.Event.Ident)
require.Equal(t, "message", sp.Event.Message)
ch <- true
}))
sl := NewSplunkLogger(context.Background(), srv.URL, "", "image-builder", "test-host")
require.NoError(t, sl.LogWithTime(time.Now(), "message"))
require.True(t, <-ch)
}

func TestSplunkLoggerContext(t *testing.T) {
ch := make(chan bool)
time.AfterFunc(time.Second*10, func() {
ch <- false
})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "Splunk", r.Header.Get("Authorization"))
require.Equal(t, "application/json", r.Header.Get("Content-Type"))
var sp SplunkPayload
err := json.NewDecoder(r.Body).Decode(&sp)
require.NoError(t, err)
require.Equal(t, "test-host", sp.Host)
require.Equal(t, "test-host", sp.Event.Host)
require.Equal(t, "image-builder", sp.Event.Ident)
require.Equal(t, "message", sp.Event.Message)
ch <- true
}))
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
defer cancel()
sl := NewSplunkLogger(ctx, srv.URL, "", "image-builder", "test-host")
require.NoError(t, sl.LogWithTime(time.Now(), "message"))
require.True(t, <-ch)
}

0 comments on commit 13d642b

Please sign in to comment.