Skip to content

Commit

Permalink
added context setters
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelsz-rb committed Sep 23, 2022
1 parent 5298f3b commit 34c2df3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions async_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,7 @@ func (t *AsyncTransport) Close() error {
func (t *AsyncTransport) setContext(ctx context.Context) {
t.ctx = ctx
}

func (t *AsyncTransport) getContext() context.Context {
return t.ctx
}
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ func (c *Client) CaptureTelemetryEvent(eventType, eventlevel string, eventData m
func (c *Client) SetTelemetry(options ...OptionFunc) {
c.Telemetry = NewTelemetry(c.configuration.scrubHeaders, options...)
}
func (c *Client) SetContext(ctx context.Context) {
c.ctx = ctx
c.Transport.setContext(ctx)
}

// SetEnabled sets whether or not Rollbar is enabled.
// If this is true then this library works as normal.
Expand Down
4 changes: 4 additions & 0 deletions rollbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ var DefaultStackTracer StackTracerFunc = func(err error) ([]runtime.Frame, bool)
return nil, false
}

func SetContext(ctx context.Context) {
std.SetContext(ctx)
}

// SetTelemetry sets the telemetry
func SetTelemetry(options ...OptionFunc) {
std.SetTelemetry(options...)
Expand Down
15 changes: 15 additions & 0 deletions rollbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime"
"strings"
"testing"
"time"
)

type CustomError struct {
Expand Down Expand Up @@ -104,6 +105,20 @@ func TestEverything(t *testing.T) {

type someNonstandardTypeForLogFailing struct{}

func TestSetContext(t *testing.T) {
SetToken(os.Getenv("TOKEN"))
SetEnvironment("test")
ctx, _ := context.WithTimeout(context.Background(), 2*time.Second)
SetContext(ctx)
if std.ctx != ctx {
t.Error("Client ctx must be set")
}
tr := std.Transport.(*AsyncTransport)
if tr.getContext() != ctx {
t.Error("Transport ctx must be set")
}

}
func TestEverythingGeneric(t *testing.T) {
SetToken(os.Getenv("TOKEN"))
SetEnvironment("test")
Expand Down

0 comments on commit 34c2df3

Please sign in to comment.