github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrpkgerrors/example/main.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"os"
     9  	"time"
    10  
    11  	newrelic "github.com/newrelic/go-agent"
    12  	"github.com/newrelic/go-agent/_integrations/nrpkgerrors"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  type sampleError string
    17  
    18  func (e sampleError) Error() string {
    19  	return string(e)
    20  }
    21  
    22  func alpha() error {
    23  	return errors.WithStack(sampleError("alpha is the cause"))
    24  }
    25  
    26  func beta() error {
    27  	return errors.WithStack(alpha())
    28  }
    29  
    30  func gamma() error {
    31  	return errors.Wrap(beta(), "gamma was involved")
    32  }
    33  
    34  func mustGetEnv(key string) string {
    35  	if val := os.Getenv(key); "" != val {
    36  		return val
    37  	}
    38  	panic(fmt.Sprintf("environment variable %s unset", key))
    39  }
    40  
    41  func main() {
    42  	cfg := newrelic.NewConfig("pkg/errors app", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
    43  	cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
    44  	app, err := newrelic.NewApplication(cfg)
    45  	if nil != err {
    46  		fmt.Println(err)
    47  		os.Exit(1)
    48  	}
    49  
    50  	if err := app.WaitForConnection(5 * time.Second); nil != err {
    51  		fmt.Println(err)
    52  	}
    53  
    54  	txn := app.StartTransaction("has-error", nil, nil)
    55  	e := gamma()
    56  	txn.NoticeError(nrpkgerrors.Wrap(e))
    57  	txn.End()
    58  
    59  	app.Shutdown(10 * time.Second)
    60  }