File: main.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (201 lines) | stat: -rw-r--r-- 4,902 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package main

import (
	"fmt"
	"os"
	"sync"
	"time"

	nats "github.com/nats-io/nats.go"
	"github.com/newrelic/go-agent/v3/integrations/nrnats"
	newrelic "github.com/newrelic/go-agent/v3/newrelic"
)

var app *newrelic.Application

func doAsync(nc *nats.Conn, txn *newrelic.Transaction) {
	wg := sync.WaitGroup{}
	subj := "async"

	// Simple Async Subscriber
	// Use the nrnats.SubWrapper to wrap the nats.MsgHandler and create a
	// newrelic.Transaction with each processed nats.Msg
	_, err := nc.Subscribe(subj, nrnats.SubWrapper(app, func(m *nats.Msg) {
		defer wg.Done()
		fmt.Println("Received async message:", string(m.Data))
	}))
	if nil != err {
		panic(err)
	}

	// Simple Publisher
	wg.Add(1)
	// Use nrnats.StartPublishSegment to create a
	// newrelic.MessageProducerSegment for the call to nc.Publish
	seg := nrnats.StartPublishSegment(txn, nc, subj)
	err = nc.Publish(subj, []byte("Hello World"))
	seg.End()
	if nil != err {
		panic(err)
	}

	wg.Wait()
}

func doQueue(nc *nats.Conn, txn *newrelic.Transaction) {
	wg := sync.WaitGroup{}
	subj := "queue"

	// Queue Subscriber
	// Use the nrnats.SubWrapper to wrap the nats.MsgHandler and create a
	// newrelic.Transaction with each processed nats.Msg
	_, err := nc.QueueSubscribe(subj, "myQueueName", nrnats.SubWrapper(app, func(m *nats.Msg) {
		defer wg.Done()
		fmt.Println("Received queue message:", string(m.Data))
	}))
	if nil != err {
		panic(err)
	}

	wg.Add(1)
	// Use nrnats.StartPublishSegment to create a
	// newrelic.MessageProducerSegment for the call to nc.Publish
	seg := nrnats.StartPublishSegment(txn, nc, subj)
	err = nc.Publish(subj, []byte("Hello World"))
	seg.End()
	if nil != err {
		panic(err)
	}

	wg.Wait()
}

func doSync(nc *nats.Conn, txn *newrelic.Transaction) {
	subj := "sync"

	// Simple Sync Subscriber
	sub, err := nc.SubscribeSync(subj)
	if nil != err {
		panic(err)
	}
	// Use nrnats.StartPublishSegment to create a
	// newrelic.MessageProducerSegment for the call to nc.Publish
	seg := nrnats.StartPublishSegment(txn, nc, subj)
	err = nc.Publish(subj, []byte("Hello World"))
	seg.End()
	if nil != err {
		panic(err)
	}
	m, err := sub.NextMsg(time.Second)
	if nil != err {
		panic(err)
	}
	fmt.Println("Received sync message:", string(m.Data))
}

func doChan(nc *nats.Conn, txn *newrelic.Transaction) {
	subj := "chan"

	// Channel Subscriber
	ch := make(chan *nats.Msg)
	_, err := nc.ChanSubscribe(subj, ch)
	if nil != err {
		panic(err)
	}

	// Use nrnats.StartPublishSegment to create a
	// newrelic.MessageProducerSegment for the call to nc.Publish
	seg := nrnats.StartPublishSegment(txn, nc, subj)
	err = nc.Publish(subj, []byte("Hello World"))
	seg.End()
	if nil != err {
		panic(err)
	}

	m := <-ch
	fmt.Println("Received chan message:", string(m.Data))
}

func doReply(nc *nats.Conn, txn *newrelic.Transaction) {
	subj := "reply"

	// Replies
	nc.Subscribe(subj, func(m *nats.Msg) {
		// Use nrnats.StartPublishSegment to create a
		// newrelic.MessageProducerSegment for the call to nc.Publish
		seg := nrnats.StartPublishSegment(txn, nc, m.Reply)
		nc.Publish(m.Reply, []byte("Hello World"))
		seg.End()
	})

	// Requests
	// Use nrnats.StartPublishSegment to create a
	// newrelic.MessageProducerSegment for the call to nc.Request
	seg := nrnats.StartPublishSegment(txn, nc, subj)
	m, err := nc.Request(subj, []byte("request"), time.Second)
	seg.End()
	if nil != err {
		panic(err)
	}
	fmt.Println("Received reply message:", string(m.Data))
}

func doRespond(nc *nats.Conn, txn *newrelic.Transaction) {
	subj := "respond"
	// Respond
	nc.Subscribe(subj, func(m *nats.Msg) {
		// Use nrnats.StartPublishSegment to create a
		// newrelic.MessageProducerSegment for the call to m.Respond
		seg := nrnats.StartPublishSegment(txn, nc, m.Reply)
		m.Respond([]byte("Hello World"))
		seg.End()
	})

	// Requests
	// Use nrnats.StartPublishSegment to create a
	// newrelic.MessageProducerSegment for the call to nc.Request
	seg := nrnats.StartPublishSegment(txn, nc, subj)
	m, err := nc.Request(subj, []byte("request"), time.Second)
	seg.End()
	if nil != err {
		panic(err)
	}
	fmt.Println("Received respond message:", string(m.Data))
}

func main() {
	// Initialize agent
	var err error
	app, err = newrelic.NewApplication(
		newrelic.ConfigAppName("NATS App"),
		newrelic.ConfigLicense(os.Getenv("NEW_RELIC_LICENSE_KEY")),
		newrelic.ConfigDebugLogger(os.Stdout),
	)
	if nil != err {
		panic(err)
	}
	defer app.Shutdown(10 * time.Second)
	err = app.WaitForConnection(5 * time.Second)
	if nil != err {
		panic(err)
	}
	txn := app.StartTransaction("main")
	defer txn.End()

	// Connect to a server
	nc, err := nats.Connect(nats.DefaultURL)
	if nil != err {
		panic(err)
	}
	defer nc.Drain()

	doAsync(nc, txn)
	doQueue(nc, txn)
	doSync(nc, txn)
	doChan(nc, txn)
	doReply(nc, txn)
	doRespond(nc, txn)
}