- 0.115.1 (latest)
- 0.115.0
- 0.114.0
- 0.113.0
- 0.112.2
- 0.111.0
- 0.110.10
- 0.109.0
- 0.108.0
- 0.107.0
- 0.106.0
- 0.105.0
- 0.104.0
- 0.103.0
- 0.102.1
- 0.101.1
- 0.100.2
- 0.99.0
- 0.98.0
- 0.97.0
- 0.96.0
- 0.95.0
- 0.94.1
- 0.93.3
- 0.92.3
- 0.91.1
- 0.90.0
- 0.89.0
- 0.88.0
- 0.87.0
- 0.86.0
- 0.85.0
- 0.84.0
- 0.83.0
- 0.82.0
- 0.81.0
- 0.80.0
- 0.79.0
- 0.78.0
- 0.77.0
- 0.76.0
- 0.75.0
Package errorreporting is a Google Cloud Error Reporting library.
Any provided stacktraces must match the format produced by https://golang.org/pkg/runtime/#Stack or as per https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/report#ReportedErrorEvent for language specific stacktrace formats.
This package is still experimental and subject to change.
See https://cloud.google.com/error-reporting/ for more information.
Client
type Client struct {
// contains filtered or unexported fields
}
Client represents a Google Cloud Error Reporting client.
Example
Example
package main
import (
"context"
"errors"
"log"
"cloud.google.com/go/errorreporting"
)
func main() {
// Create the client.
ctx := context.Background()
ec, err := errorreporting.NewClient(ctx, "my-gcp-project", errorreporting.Config{
ServiceName: "myservice",
ServiceVersion: "v1.0",
})
if err != nil {
// TODO: handle error
}
defer func() {
if err := ec.Close(); err != nil {
log.Printf("failed to report errors to Cloud Error Reporting: %v", err)
}
}()
// Report an error.
err = doSomething()
if err != nil {
ec.Report(errorreporting.Entry{
Error: err,
})
}
}
func doSomething() error {
return errors.New("something went wrong")
}
func NewClient
func NewClient(ctx context.Context, projectID string, cfg Config, opts ...option.ClientOption) (*Client, error)
NewClient returns a new error reporting client. Generally you will want to create a client on program initialization and use it through the lifetime of the process.
Example
package main
import (
"context"
"errors"
"log"
"cloud.google.com/go/errorreporting"
)
func main() {
// Create the client.
ctx := context.Background()
ec, err := errorreporting.NewClient(ctx, "my-gcp-project", errorreporting.Config{
ServiceName: "myservice",
ServiceVersion: "v1.0",
})
if err != nil {
// TODO: handle error
}
defer func() {
if err := ec.Close(); err != nil {
log.Printf("failed to report errors to Cloud Error Reporting: %v", err)
}
}()
// Report an error.
err = doSomething()
if err != nil {
ec.Report(errorreporting.Entry{
Error: err,
})
}
}
func doSomething() error {
return errors.New("something went wrong")
}
func (*Client) Close
Close calls Flush, then closes any resources held by the client. Close should be called when the client is no longer needed.
Example
package main
import (
"context"
"errors"
"log"
"cloud.google.com/go/errorreporting"
)
func main() {
// Create the client.
ctx := context.Background()
ec, err := errorreporting.NewClient(ctx, "my-gcp-project", errorreporting.Config{
ServiceName: "myservice",
ServiceVersion: "v1.0",
})
if err != nil {
// TODO: handle error
}
defer func() {
if err := ec.Close(); err != nil {
log.Printf("failed to report errors to Cloud Error Reporting: %v", err)
}
}()
// Report an error.
err = doSomething()
if err != nil {
ec.Report(errorreporting.Entry{
Error: err,
})
}
}
func doSomething() error {
return errors.New("something went wrong")
}
func (*Client) Flush
func (c *Client) Flush()
Flush blocks until all currently buffered error reports are sent.
If any errors occurred since the last call to Flush, or the creation of the client if this is the first call, then Flush reports the error via the Config.OnError handler.
Example
package main
import (
"context"
"errors"
"log"
"cloud.google.com/go/errorreporting"
)
func main() {
// Create the client.
ctx := context.Background()
ec, err := errorreporting.NewClient(ctx, "my-gcp-project", errorreporting.Config{
ServiceName: "myservice",
ServiceVersion: "v1.0",
})
if err != nil {
// TODO: handle error
}
defer func() {
if err := ec.Close(); err != nil {
log.Printf("failed to report errors to Cloud Error Reporting: %v", err)
}
}()
// Report an error.
err = doSomething()
if err != nil {
ec.Report(errorreporting.Entry{
Error: err,
})
}
}
func doSomething() error {
return errors.New("something went wrong")
}
func (*Client) Report
Report writes an error report. It doesn't block. Errors in writing the error report can be handled via Config.OnError.
Example
package main
import (
"context"
"errors"
"log"
"cloud.google.com/go/errorreporting"
)
func main() {
// Create the client.
ctx := context.Background()
ec, err := errorreporting.NewClient(ctx, "my-gcp-project", errorreporting.Config{
ServiceName: "myservice",
ServiceVersion: "v1.0",
})
if err != nil {
// TODO: handle error
}
defer func() {
if err := ec.Close(); err != nil {
log.Printf("failed to report errors to Cloud Error Reporting: %v", err)
}
}()
// Report an error.
err = doSomething()
if err != nil {
ec.Report(errorreporting.Entry{
Error: err,
})
}
}
func doSomething() error {
return errors.New("something went wrong")
}
func (*Client) ReportSync
ReportSync writes an error report. It blocks until the entry is written.
Example
package main
import (
"context"
"errors"
"log"
"cloud.google.com/go/errorreporting"
)
func main() {
// Create the client.
ctx := context.Background()
ec, err := errorreporting.NewClient(ctx, "my-gcp-project", errorreporting.Config{
ServiceName: "myservice",
ServiceVersion: "v1.0",
})
if err != nil {
// TODO: handle error
}
defer func() {
if err := ec.Close(); err != nil {
log.Printf("failed to report errors to Cloud Error Reporting: %v", err)
}
}()
// Report an error.
err = doSomething()
if err != nil {
ec.Report(errorreporting.Entry{
Error: err,
})
}
}
func doSomething() error {
return errors.New("something went wrong")
}
Config
type Config struct {
// ServiceName identifies the running program and is included in the error reports.
// Optional.
ServiceName string
// ServiceVersion identifies the version of the running program and is
// included in the error reports.
// Optional.
ServiceVersion string
// OnError is the function to call if any background
// tasks errored. By default, errors are logged.
OnError func(err error)
}
Config is additional configuration for Client.
Example
package main
import (
"context"
"errors"
"log"
"cloud.google.com/go/errorreporting"
)
func main() {
// Create the client.
ctx := context.Background()
ec, err := errorreporting.NewClient(ctx, "my-gcp-project", errorreporting.Config{
ServiceName: "myservice",
ServiceVersion: "v1.0",
})
if err != nil {
// TODO: handle error
}
defer func() {
if err := ec.Close(); err != nil {
log.Printf("failed to report errors to Cloud Error Reporting: %v", err)
}
}()
// Report an error.
err = doSomething()
if err != nil {
ec.Report(errorreporting.Entry{
Error: err,
})
}
}
func doSomething() error {
return errors.New("something went wrong")
}
Entry
type Entry struct {
Error error
Req *http.Request // if error is associated with a request.
User string // an identifier for the user affected by the error
// Stack specifies the stacktrace and call sequence correlated with
// the error. Stack's content must match the format specified by
// https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/report#ReportedErrorEvent.message
// or at least for Go programs, it must match the format produced
// by https://golang.org/pkg/runtime/debug/#Stack.
//
// If Stack is blank, the result of runtime.Stack will be used instead.
Stack []byte
}
Entry holds information about the reported error.
Example
package main
import (
"context"
"errors"
"log"
"cloud.google.com/go/errorreporting"
)
func main() {
// Create the client.
ctx := context.Background()
ec, err := errorreporting.NewClient(ctx, "my-gcp-project", errorreporting.Config{
ServiceName: "myservice",
ServiceVersion: "v1.0",
})
if err != nil {
// TODO: handle error
}
defer func() {
if err := ec.Close(); err != nil {
log.Printf("failed to report errors to Cloud Error Reporting: %v", err)
}
}()
// Report an error.
err = doSomething()
if err != nil {
ec.Report(errorreporting.Entry{
Error: err,
})
}
}
func doSomething() error {
return errors.New("something went wrong")
}