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

add the app engine support #3

Open
wants to merge 21 commits into
base: master
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
14 changes: 13 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ matrix:
allow_failures:
- go: tip
install:

- export FILE=$(curl https://storage.googleapis.com/appengine-sdks/ | grep -o 'featured/go_appengine_sdk_linux_amd64-[^\<]*' | head -1)
- curl -O https://storage.googleapis.com/appengine-sdks/featured/$FILE
- unzip -q $FILE

- go get github.com/smartystreets/goconvey
- go get code.google.com/p/go-uuid/uuid
- go get "github.com/pborman/uuid"
- go get github.com/kaeuferportal/stack2struct
- go get golang.org/x/net/context
- go get google.golang.org/appengine/urlfetch
- go get google.golang.org/appengine

script:
- ./go_appengine/goapp test ./tests;
- ./go_appengine/goapp build ./raygun4goGAE;
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Beko Käuferportal GmbH
Copyright (c) 2015 Miki haiat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# raygun4go
[![Build Status](https://travis-ci.org/MindscapeHQ/raygun4go.svg?branch=master)](https://travis-ci.org/MindscapeHQ/raygun4go)
# raygun4goGAE
[![Build Status](https://travis-ci.org/miko-code/raygun4goGAE.svg?branch=master)](https://travis-ci.org/miko-code/raygun4goGAE)
[![Build status](https://ci.appveyor.com/api/projects/status/9pqk769jaxfxp0bb/branch/master?svg=true)](https://ci.appveyor.com/project/kaeuferportal-oss/raygun4go/branch/master)
[![Coverage](http://gocover.io/_badge/github.com/MindscapeHQ/raygun4go)](http://gocover.io/github.com/MindscapeHQ/raygun4go)
[![GoDoc](https://godoc.org/github.com/MindscapeHQ/raygun4go?status.svg)](http://godoc.org/github.com/MindscapeHQ/raygun4go)
[![GoReportcard](http://goreportcard.com/badge/MindscapeHQ/raygun4go)](http://goreportcard.com/report/MindscapeHQ/raygun4go)
[![Coverage](http://gocover.io/_badge/github.com/miko-code/raygun4goGAE)](http://gocover.io/github.com/miko-code/raygun4goGAE)
[![GoDoc](https://godoc.org/github.com/miko-code/raygun4goGAE?status.svg)](http://godoc.org/github.com/miko-code/raygun4goGAE)
[![GoReportcard](http://goreportcard.com/badge/miko-code/raygun4goGAE)](http://goreportcard.com/report/miko-code/raygun4goGAE)


raygun4go adds Raygun-based error handling to your golang code. It catches all
occuring errors, extracts as much information as possible and sends the error
Expand All @@ -13,7 +14,7 @@ to Raygun via their REST-API.

### Installation
```
$ go get github.com/MindscapeHQ/raygun4go
$ go get github.com/miko-code/raygun4goGAE
```

### Basic Usage
Expand All @@ -23,13 +24,21 @@ in a context as global as possible. In webservers, this will probably be your
request handling method, in all other programs it should be your main-method.
Having found the right spot, just add the following example code:


```
//generate new appengine context
ctx := appengine.NewContext(r)

raygun, err := raygun4go.New("appName", "apiKey")
if err != nil {
log.Println("Unable to create Raygun client:", err.Error())
}
raygun.Silent(true)
defer raygun.HandleError()
defer raygun.HandleError(ctx)

raygun.CreateError("this is error", ctx)


```

where ``appName`` is the name of your app and ``apiKey`` is your
Expand Down Expand Up @@ -57,7 +66,7 @@ Method | Description
## Bugs and feature requests

Have a bug or a feature request? Please first check the list of
[issues](https://github.com/MindscapeHQ/raygun4go/issues).
[issues](https://github.com/miko-code/raygun4goGAE/issues).

If your problem or idea is not addressed yet, [please open a new
issue](https://github.com/MindscapeHQ/raygun4go/issues/new).
issue](https://github.com/miko-code/raygun4goGAE/issues/new).
25 changes: 14 additions & 11 deletions raygun4go.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ import (
"log"
"net/http"

"code.google.com/p/go-uuid/uuid"
"github.com/pborman/uuid"
gcontext "golang.org/x/net/context" //google context
"google.golang.org/appengine/urlfetch"
)

// Client is the struct holding your Raygun configuration and context
Expand Down Expand Up @@ -78,7 +80,7 @@ func (ci *contextInformation) Identifier() string {
return ci.identifier
}

// New creates and returns a Client, needing an appName and an apiKey. It also
// New creates and. returns a Client, needing an appName and an apiKey. It also
// creates a unique identifier for you program.
func New(appName, apiKey string) (c *Client, err error) {
context := contextInformation{identifier: uuid.New()}
Expand Down Expand Up @@ -135,17 +137,18 @@ func (c *Client) User(u string) *Client {
// defer c.HandleError()
//
// to handle all panics inside the calling function and all calls made from it.
// Be sure to call this in your main function or (if it is webserver) in your
// Be sure to call .this in your main function or (if it is webserver) in your
// request handler as soon as possible.
func (c *Client) HandleError() {
func (c *Client) HandleError(ctx gcontext.Context) {
if e := recover(); e != nil {
err, ok := e.(error)
if !ok {
err = errors.New(e.(string))
}
log.Println("Recovering from:", err.Error())
post := c.createPost(err, currentStack())
c.submit(post)

c.submit(post, ctx)
}
}

Expand All @@ -155,15 +158,16 @@ func (c *Client) createPost(err error, stack stackTrace) postData {
}

// CreateError is a simple wrapper to manually post messages (errors) to raygun
func (c *Client) CreateError(message string) {
func (c *Client) CreateError(message string, ctx gcontext.Context) {
err := errors.New(message)
post := c.createPost(err, currentStack())
c.submit(post)
c.submit(post, ctx)

}

// submit takes care of actually sending the error to Raygun unless the silent
// option is set.
func (c *Client) submit(post postData) {
func (c *Client) submit(post postData, ctx gcontext.Context) {
if c.silent {
enc, _ := json.MarshalIndent(post, "", "\t")
fmt.Println(string(enc))
Expand All @@ -175,15 +179,14 @@ func (c *Client) submit(post postData) {
log.Printf("Unable to convert to JSON (%s): %#v\n", err.Error(), post)
return
}

client := urlfetch.Client(ctx)
r, err := http.NewRequest("POST", raygunEndpoint+"/entries", bytes.NewBuffer(json))
if err != nil {
log.Printf("Unable to create request (%s)\n", err.Error())
return
}
r.Header.Add("X-ApiKey", c.apiKey)
httpClient := http.Client{}
resp, err := httpClient.Do(r)
resp, err := client.Do(r)
if err != nil {
log.Println(err.Error())
}
Expand Down
25 changes: 17 additions & 8 deletions raygun4go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package raygun4go

import (
"fmt"
"github.com/pborman/uuid"

. "github.com/smartystreets/goconvey/convey"
"google.golang.org/appengine"
"google.golang.org/appengine/aetest"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"code.google.com/p/go-uuid/uuid"

. "github.com/smartystreets/goconvey/convey"
)

func TestClient(t *testing.T) {
Expand Down Expand Up @@ -66,8 +67,10 @@ func TestClient(t *testing.T) {
})

Convey("#HandleError", func() {
u := "http://www.example.com?foo=bar&fizz[]=buzz&fizz[]=buzz2"
r, _ := http.NewRequest("GET", u, nil)
inst, _ := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: false})
r, _ := inst.NewRequest("GET", "/", nil)

ctx := appengine.NewContext(r)
r.RemoteAddr = "1.2.3.4"
r.PostForm = url.Values{
"foo": []string{"bar"},
Expand All @@ -80,20 +83,26 @@ func TestClient(t *testing.T) {
c.context.Tags = []string{"golang", "test"}
c.context.CustomData = map[string]string{"foo": "bar"}
c.context.User = "Test User"
defer c.HandleError()
defer c.HandleError(ctx)
panic("Test: See if this works with Raygun")
})

Convey("#CreateError", func() {
inst, _ := aetest.NewInstance(&aetest.Options{StronglyConsistentDatastore: false})
r, _ := inst.NewRequest("GET", "/", nil)
ctx := appengine.NewContext(r)
ts := raygunEndpointStub()
defer ts.Close()
raygunEndpoint = ts.URL
c, _ := New("app", "key")
c.Silent(false)
c.apiKey = "key"
c.CreateError("Test: See if this works with Raygun")
c.CreateError("Test: See if this works with Raygun", ctx)

})

})

}

func raygunEndpointStub() *httptest.Server {
Expand Down