Skip to content

Commit

Permalink
feat: HTTPTransport implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed May 8, 2019
1 parent f057b6f commit e2974f3
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 43 deletions.
9 changes: 5 additions & 4 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sentry

import (
"context"
"time"
)

func Init(options ClientOptions) error {
Expand Down Expand Up @@ -82,12 +83,12 @@ func PopScope() {
hub.PopScope()
}

func Flush(timeout int) {
func Flush(timeout time.Duration) bool {
hub := CurrentHub()
hub.Flush(timeout)
return hub.Flush(timeout)
}

func LastEventID() {
func LastEventID() string {
hub := CurrentHub()
hub.LastEventID()
return hub.LastEventID()
}
29 changes: 19 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sentry

import (
"context"
"fmt"
"crypto/x509"
"io"
"io/ioutil"
"log"
Expand Down Expand Up @@ -34,6 +34,11 @@ type ClientOptions struct {
Environment string
MaxBreadcrumbs int
DebugWriter io.Writer

HTTPTransport *http.Transport
HTTPProxy string
HTTPSProxy string
CaCerts *x509.CertPool
}

type Client struct {
Expand Down Expand Up @@ -126,10 +131,7 @@ func (client *Client) CaptureException(exception error, hint *EventHint, scope E
}

func (client *Client) CaptureEvent(event *Event, hint *EventHint, scope EventModifier) {
// TODO: Handle return values
if _, err := client.processEvent(event, hint, scope); err != nil {
debugger.Println(err)
}
client.processEvent(event, hint, scope)
}

func (client *Client) Recover(recoveredErr interface{}, hint *EventHint, scope EventModifier) {
Expand Down Expand Up @@ -168,6 +170,10 @@ func (client *Client) RecoverWithContext(ctx context.Context, err interface{}, h
}
}

func (client *Client) Flush(timeout time.Duration) bool {
return client.Transport.Flush(timeout)
}

func (client *Client) eventFromMessage(message string) *Event {
return &Event{
Message: message,
Expand All @@ -182,7 +188,7 @@ func (client *Client) eventFromException(exception error) *Event {
}

// TODO: Should return some sort of SentryResponse instead of http.Response
func (client *Client) processEvent(event *Event, hint *EventHint, scope EventModifier) (*http.Response, error) {
func (client *Client) processEvent(event *Event, hint *EventHint, scope EventModifier) {
options := client.Options()

// TODO: Reconsider if its worth going away from default implementation
Expand All @@ -192,12 +198,14 @@ func (client *Client) processEvent(event *Event, hint *EventHint, scope EventMod
if options.SampleRate != 0.0 {
randomFloat := rand.New(rand.NewSource(time.Now().UnixNano())).Float32()
if randomFloat > options.SampleRate {
return nil, fmt.Errorf("event dropped due to SampleRate hit")
debugger.Println("event dropped due to SampleRate hit")
return
}
}

if event = client.prepareEvent(event, hint, scope); event == nil {
return nil, fmt.Errorf("event dropped by one of the EventProcessors")
debugger.Println("event dropped by one of the EventProcessors")
return
}

if options.BeforeSend != nil {
Expand All @@ -206,11 +214,12 @@ func (client *Client) processEvent(event *Event, hint *EventHint, scope EventMod
h = hint
}
if event = options.BeforeSend(event, h); event == nil {
return nil, fmt.Errorf("event dropped due to BeforeSend callback")
debugger.Println("event dropped due to BeforeSend callback")
return
}
}

return client.Transport.SendEvent(event)
client.Transport.SendEvent(event)
}

func (client *Client) prepareEvent(event *Event, hint *EventHint, scope EventModifier) *Event {
Expand Down
9 changes: 6 additions & 3 deletions examples/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"sentry"
sentryIntegrations "sentry/integrations"
"strconv"
"time"
)

func prettyPrint(v interface{}) string {
Expand All @@ -24,9 +24,12 @@ func (t *DevNullTransport) Configure(options sentry.ClientOptions) {
fmt.Println("Headers:", dsn.RequestHeaders())
fmt.Println()
}
func (t *DevNullTransport) SendEvent(event *sentry.Event) (*http.Response, error) {
func (t *DevNullTransport) SendEvent(event *sentry.Event) {
fmt.Println("Faked Transport")
return nil, nil
}

func (t *DevNullTransport) Flush(timeout time.Duration) bool {
return true
}

func recoverHandler() {
Expand Down
7 changes: 5 additions & 2 deletions examples/http/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ func (t *DevNullTransport) Configure(options sentry.ClientOptions) {
fmt.Println("Headers:", dsn.RequestHeaders())
fmt.Println()
}
func (t *DevNullTransport) SendEvent(event *sentry.Event) (*http.Response, error) {
func (t *DevNullTransport) SendEvent(event *sentry.Event) {
fmt.Println("Faked Transport")
log.Println(prettyPrint(event))
return nil, nil
}

func (t *DevNullTransport) Flush(timeout time.Duration) bool {
return true
}

func customHandlerFunc(w http.ResponseWriter, r *http.Request) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module sentry
go 1.12

require (
github.com/certifi/gocertifi v0.0.0-20190506164543-d2eda7129713
github.com/go-errors/errors v1.0.1
github.com/pingcap/errors v0.11.1
github.com/pkg/errors v0.8.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/certifi/gocertifi v0.0.0-20190506164543-d2eda7129713 h1:UNOqI3EKhvbqV8f1Vm3NIwkrhq388sGCeAH2Op7w0rc=
github.com/certifi/gocertifi v0.0.0-20190506164543-d2eda7129713/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4=
github.com/go-errors/errors v1.0.1 h1:LUHzmkK3GUKUrL/1gfBUxAHzcev3apQlezX/+O7ma6w=
github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q=
github.com/pingcap/errors v0.11.1 h1:BXFZ6MdDd2U1uJUa2sRAWTmm+nieEzuyYM0R4aUTcC8=
Expand Down
11 changes: 9 additions & 2 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sentry

import (
"context"
"time"
)

// Default maximum number of breadcrumbs added to an event. Can be overwritten `maxBreadcrumbs` option.
Expand Down Expand Up @@ -173,8 +174,14 @@ func (hub *Hub) RecoverWithContext(ctx context.Context, err interface{}, hint *E
})
}

func (hub *Hub) Flush(timeout int) {
panic("Implement Flush redirect to the Client")
func (hub *Hub) Flush(timeout time.Duration) bool {
client := hub.Client()

if client == nil {
return false
}

return client.Flush(timeout)
}

func (hub *Hub) GetIntegration(name string) Integration {
Expand Down
8 changes: 5 additions & 3 deletions mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sentry

import (
"context"
"net/http"
"time"
)

type ScopeMock struct {
Expand All @@ -26,9 +26,11 @@ type TransportMock struct {
}

func (t *TransportMock) Configure(options ClientOptions) {}
func (t *TransportMock) SendEvent(event *Event) (*http.Response, error) {
func (t *TransportMock) SendEvent(event *Event) {
t.lastEvent = event
return nil, nil
}
func (t *TransportMock) Flush(timeout time.Duration) bool {
return true
}

type ClientMock struct {
Expand Down
Loading

0 comments on commit e2974f3

Please sign in to comment.