Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy events and log log_event failures #31

Open
wants to merge 3 commits into
base: 1-12-2
Choose a base branch
from
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
25 changes: 20 additions & 5 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"fmt"
"net/http/httputil"
"strconv"
"sync"
"time"
Expand All @@ -23,7 +24,7 @@
Time int64 `json:"time"`
}

type diagnosticsEvent struct {

Check failure on line 27 in logger.go

View workflow job for this annotation

GitHub Actions / lint

type `diagnosticsEvent` is unused (unused)
EventName string `json:"eventName"`
Metadata map[string]interface{} `json:"metadata"`
Time int64 `json:"time"`
Expand Down Expand Up @@ -205,7 +206,6 @@
}

func (l *logger) flush(closing bool) {
l.logDiagnosticsEvents(l.diagnostics)
Copy link
Contributor Author

@tore-statsig tore-statsig Jan 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this causes issues potentially

l.mu.Lock()
defer l.mu.Unlock()

Expand All @@ -220,10 +220,13 @@
return
}

eventsCopy := make([]interface{}, len(l.events))
copy(eventsCopy, l.events)

if closing {
l.sendEvents(l.events)
l.sendEvents(eventsCopy)
} else {
go l.sendEvents(l.events)
go l.sendEvents(eventsCopy)
}

l.events = make([]interface{}, 0)
Expand All @@ -234,17 +237,29 @@
Events: events,
StatsigMetadata: l.transport.metadata,
}
var res logEventResponse
_, _ = l.transport.retryablePostRequest("/log_event", input, &res, maxRetries)
var result logEventResponse
global.Logger().Log(fmt.Sprintf("logging %d events\n", len(events)), nil)
res, err := l.transport.retryablePostRequest("/log_event", input, &result, maxRetries)

if err != nil {
global.Logger().LogError(err)
}

respDump, err := httputil.DumpResponse(res, true)
if err != nil {
global.Logger().LogError(err)
}

global.Logger().Log(fmt.Sprintf("log_event response:\n%s", string(respDump)), nil)
}

func (l *logger) logDiagnosticsEvents(d *diagnostics) {

Check failure on line 256 in logger.go

View workflow job for this annotation

GitHub Actions / lint

func `(*logger).logDiagnosticsEvents` is unused (unused)
l.logDiagnosticsEvent(d.initDiagnostics)
l.logDiagnosticsEvent(d.syncDiagnostics)
l.logDiagnosticsEvent(d.apiDiagnostics)
}

func (l *logger) logDiagnosticsEvent(d *diagnosticsBase) {

Check failure on line 262 in logger.go

View workflow job for this annotation

GitHub Actions / lint

func `(*logger).logDiagnosticsEvent` is unused (unused)
if l.options.StatsigLoggerOptions.DisableInitDiagnostics && d.context == InitializeContext {
return
}
Expand Down
10 changes: 10 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (
)

func TestLog(t *testing.T) {
InitializeGlobalOutputLogger(OutputLoggerOptions{
LogCallback: func(message string, err error) {
t.Log(message)
t.Log(err)
},
DisableInitDiagnostics: false,
DisableSyncDiagnostics: true,
})
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {}))
defer testServer.Close()
opt := &Options{
Expand Down Expand Up @@ -92,4 +100,6 @@ func TestLog(t *testing.T) {
if evt3.Time/1000 < nowSecond-2 || evt3.Time/1000 > nowSecond+2 {
t.Errorf("Config exposure event time not set correctly.")
}

logger.flush(true)
}
Loading