github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrstan/examples/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  	"sync"
    10  	"time"
    11  
    12  	"github.com/nats-io/stan.go"
    13  	newrelic "github.com/newrelic/go-agent"
    14  	"github.com/newrelic/go-agent/_integrations/nrnats"
    15  	"github.com/newrelic/go-agent/_integrations/nrstan"
    16  )
    17  
    18  var app newrelic.Application
    19  
    20  func doAsync(sc stan.Conn, txn newrelic.Transaction) {
    21  	wg := sync.WaitGroup{}
    22  	subj := "async"
    23  
    24  	// Simple Async Subscriber
    25  	// Use the nrstan.StreamingSubWrapper to wrap the stan.MsgHandler and
    26  	// create a newrelic.Transaction with each processed stan.Msg
    27  	_, err := sc.Subscribe(subj, nrstan.StreamingSubWrapper(app, func(m *stan.Msg) {
    28  		defer wg.Done()
    29  		fmt.Println("Received async message:", string(m.Data))
    30  	}))
    31  	if nil != err {
    32  		panic(err)
    33  	}
    34  
    35  	// Simple Publisher
    36  	wg.Add(1)
    37  	// Use nrnats.StartPublishSegment to create a newrelic.ExternalSegment for
    38  	// the call to sc.Publish
    39  	seg := nrnats.StartPublishSegment(txn, sc.NatsConn(), subj)
    40  	err = sc.Publish(subj, []byte("Hello World"))
    41  	seg.End()
    42  	if nil != err {
    43  		panic(err)
    44  	}
    45  
    46  	wg.Wait()
    47  }
    48  
    49  func doQueue(sc stan.Conn, txn newrelic.Transaction) {
    50  	wg := sync.WaitGroup{}
    51  	subj := "queue"
    52  
    53  	// Queue Subscriber
    54  	// Use the nrstan.StreamingSubWrapper to wrap the stan.MsgHandler and
    55  	// create a newrelic.Transaction with each processed stan.Msg
    56  	_, err := sc.QueueSubscribe(subj, "myqueue", nrstan.StreamingSubWrapper(app, func(m *stan.Msg) {
    57  		defer wg.Done()
    58  		fmt.Println("Received queue message:", string(m.Data))
    59  	}))
    60  	if nil != err {
    61  		panic(err)
    62  	}
    63  
    64  	wg.Add(1)
    65  	// Use nrnats.StartPublishSegment to create a newrelic.ExternalSegment for
    66  	// the call to sc.Publish
    67  	seg := nrnats.StartPublishSegment(txn, sc.NatsConn(), subj)
    68  	err = sc.Publish(subj, []byte("Hello World"))
    69  	seg.End()
    70  	if nil != err {
    71  		panic(err)
    72  	}
    73  
    74  	wg.Wait()
    75  }
    76  
    77  func mustGetEnv(key string) string {
    78  	if val := os.Getenv(key); "" != val {
    79  		return val
    80  	}
    81  	panic(fmt.Sprintf("environment variable %s unset", key))
    82  }
    83  
    84  func main() {
    85  	// Initialize agent
    86  	cfg := newrelic.NewConfig("STAN App", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
    87  	cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
    88  	var err error
    89  	app, err = newrelic.NewApplication(cfg)
    90  	if nil != err {
    91  		panic(err)
    92  	}
    93  	defer app.Shutdown(10 * time.Second)
    94  	err = app.WaitForConnection(5 * time.Second)
    95  	if nil != err {
    96  		panic(err)
    97  	}
    98  	txn := app.StartTransaction("main", nil, nil)
    99  	defer txn.End()
   100  
   101  	// Connect to a server
   102  	sc, err := stan.Connect("test-cluster", "clientid")
   103  	if nil != err {
   104  		panic(err)
   105  	}
   106  	defer sc.Close()
   107  
   108  	doAsync(sc, txn)
   109  	doQueue(sc, txn)
   110  }