github.com/newrelic/go-agent@v3.26.0+incompatible/examples/short-lived-process/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  	"github.com/newrelic/go-agent"
    12  )
    13  
    14  func mustGetEnv(key string) string {
    15  	if val := os.Getenv(key); "" != val {
    16  		return val
    17  	}
    18  	panic(fmt.Sprintf("environment variable %s unset", key))
    19  }
    20  
    21  func main() {
    22  	cfg := newrelic.NewConfig("Short Lived App", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
    23  	cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
    24  	app, err := newrelic.NewApplication(cfg)
    25  	if nil != err {
    26  		fmt.Println(err)
    27  		os.Exit(1)
    28  	}
    29  
    30  	// Wait for the application to connect.
    31  	if err := app.WaitForConnection(5 * time.Second); nil != err {
    32  		fmt.Println(err)
    33  	}
    34  
    35  	// Do the tasks at hand.  Perhaps record them using transactions and/or
    36  	// custom events.
    37  	tasks := []string{"white", "black", "red", "blue", "green", "yellow"}
    38  	for _, task := range tasks {
    39  		txn := app.StartTransaction("task", nil, nil)
    40  		time.Sleep(10 * time.Millisecond)
    41  		txn.End()
    42  		app.RecordCustomEvent("task", map[string]interface{}{
    43  			"color": task,
    44  		})
    45  	}
    46  
    47  	// Shut down the application to flush data to New Relic.
    48  	app.Shutdown(10 * time.Second)
    49  }