pkg/splunk_logger: handle potentially dangling goroutine
This commit is contained in:
parent
43f43c84f6
commit
13d642bb46
3 changed files with 78 additions and 5 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package logger
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
|
@ -11,7 +12,7 @@ type SplunkHook struct {
|
||||||
sl *SplunkLogger
|
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)
|
url := fmt.Sprintf("https://%s:%s/services/collector/event", host, port)
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -19,7 +20,7 @@ func NewSplunkHook(host, port, token, source string) (*SplunkHook, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return &SplunkHook{
|
return &SplunkHook{
|
||||||
sl: NewSplunkLogger(url, token, source, hostname),
|
sl: NewSplunkLogger(context, url, token, source, hostname),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -40,7 +41,7 @@ type SplunkEvent struct {
|
||||||
Host string `json:"host"`
|
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{
|
sl := &SplunkLogger{
|
||||||
client: retryablehttp.NewClient().StandardClient(),
|
client: retryablehttp.NewClient().StandardClient(),
|
||||||
url: url,
|
url: url,
|
||||||
|
|
@ -52,15 +53,21 @@ func NewSplunkLogger(url, token, source, hostname string) *SplunkLogger {
|
||||||
ticker := time.NewTicker(time.Second * SendFrequency)
|
ticker := time.NewTicker(time.Second * SendFrequency)
|
||||||
sl.payloads = make(chan *SplunkPayload, PayloadsChannelSize)
|
sl.payloads = make(chan *SplunkPayload, PayloadsChannelSize)
|
||||||
|
|
||||||
go sl.flushPayloads(ticker.C)
|
go sl.flushPayloads(context, ticker.C)
|
||||||
|
|
||||||
return sl
|
return sl
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sl *SplunkLogger) flushPayloads(ticker <-chan time.Time) {
|
func (sl *SplunkLogger) flushPayloads(context context.Context, ticker <-chan time.Time) {
|
||||||
var payloads []*SplunkPayload
|
var payloads []*SplunkPayload
|
||||||
for {
|
for {
|
||||||
select {
|
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:
|
case p := <-sl.payloads:
|
||||||
if p != nil {
|
if p != nil {
|
||||||
payloads = append(payloads, p)
|
payloads = append(payloads, p)
|
||||||
|
|
|
||||||
65
pkg/splunk_logger/splunk_logger_test.go
Normal file
65
pkg/splunk_logger/splunk_logger_test.go
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue