github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrnats/example_test.go (about) 1 // Copyright 2020 New Relic Corporation. All rights reserved. 2 // SPDX-License-Identifier: Apache-2.0 3 4 package nrnats 5 6 import ( 7 "fmt" 8 "time" 9 10 "github.com/nats-io/nats.go" 11 "github.com/nats-io/stan.go" 12 newrelic "github.com/newrelic/go-agent" 13 ) 14 15 func currentTransaction() newrelic.Transaction { return nil } 16 17 func ExampleStartPublishSegment() { 18 nc, _ := nats.Connect(nats.DefaultURL) 19 txn := currentTransaction() 20 subject := "testing.subject" 21 22 // Start the Publish segment 23 seg := StartPublishSegment(txn, nc, subject) 24 err := nc.Publish(subject, []byte("Hello World")) 25 if nil != err { 26 panic(err) 27 } 28 // Manually end the segment 29 seg.End() 30 } 31 32 func ExampleStartPublishSegment_defer() { 33 nc, _ := nats.Connect(nats.DefaultURL) 34 txn := currentTransaction() 35 subject := "testing.subject" 36 37 // Start the Publish segment and defer End till the func returns 38 defer StartPublishSegment(txn, nc, subject).End() 39 m, err := nc.Request(subject, []byte("request"), time.Second) 40 if nil != err { 41 panic(err) 42 } 43 fmt.Println("Received reply message:", string(m.Data)) 44 } 45 46 var clusterID, clientID string 47 48 // StartPublishSegment can be used with a NATS Streamming Connection as well 49 // (https://github.com/nats-io/stan.go). Use the `NatsConn()` method on the 50 // `stan.Conn` interface (https://godoc.org/github.com/nats-io/stan#Conn) to 51 // access the `nats.Conn` object. 52 func ExampleStartPublishSegment_stan() { 53 sc, _ := stan.Connect(clusterID, clientID) 54 txn := currentTransaction() 55 subject := "testing.subject" 56 57 defer StartPublishSegment(txn, sc.NatsConn(), subject).End() 58 sc.Publish(subject, []byte("Hello World")) 59 }