File: nrnats.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
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (66 lines) | stat: -rw-r--r-- 2,646 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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package nrnats

import (
	"strings"

	nats "github.com/nats-io/nats.go"
	"github.com/newrelic/go-agent/v3/internal"
	"github.com/newrelic/go-agent/v3/internal/integrationsupport"
	newrelic "github.com/newrelic/go-agent/v3/newrelic"
)

// StartPublishSegment creates and starts a `newrelic.MessageProducerSegment`
// (https://godoc.org/github.com/newrelic/go-agent#MessageProducerSegment) for NATS
// publishers.  Call this function before calling any method that publishes or
// responds to a NATS message.  Call `End()`
// (https://godoc.org/github.com/newrelic/go-agent#MessageProducerSegment.End) on the
// returned newrelic.MessageProducerSegment when the publish is complete.  The
// `newrelic.Transaction` and `nats.Conn` parameters are required.  The subject
// parameter is the subject of the publish call and is used in metric and span
// names.
func StartPublishSegment(txn *newrelic.Transaction, nc *nats.Conn, subject string) *newrelic.MessageProducerSegment {
	if nil == txn {
		return nil
	}
	if nil == nc {
		return nil
	}
	return &newrelic.MessageProducerSegment{
		StartTime:            txn.StartSegmentNow(),
		Library:              "NATS",
		DestinationType:      newrelic.MessageTopic,
		DestinationName:      subject,
		DestinationTemporary: strings.HasPrefix(subject, "_INBOX"),
	}
}

// SubWrapper can be used to wrap the function for nats.Subscribe (https://godoc.org/github.com/nats-io/go-nats#Conn.Subscribe
// or https://godoc.org/github.com/nats-io/go-nats#EncodedConn.Subscribe)
// and nats.QueueSubscribe (https://godoc.org/github.com/nats-io/go-nats#Conn.QueueSubscribe or
// https://godoc.org/github.com/nats-io/go-nats#EncodedConn.QueueSubscribe)
// If the `newrelic.Application` parameter is non-nil, it will create a `newrelic.Transaction` and end the transaction
// when the passed function is complete.
func SubWrapper(app *newrelic.Application, f func(msg *nats.Msg)) func(msg *nats.Msg) {
	if app == nil {
		return f
	}
	return func(msg *nats.Msg) {
		namer := internal.MessageMetricKey{
			Library:         "NATS",
			DestinationType: string(newrelic.MessageTopic),
			DestinationName: msg.Subject,
			Consumer:        true,
		}
		txn := app.StartTransaction(namer.Name())
		defer txn.End()

		integrationsupport.AddAgentAttribute(txn, newrelic.AttributeMessageRoutingKey, msg.Sub.Subject, nil)
		integrationsupport.AddAgentAttribute(txn, newrelic.AttributeMessageQueueName, msg.Sub.Queue, nil)
		integrationsupport.AddAgentAttribute(txn, newrelic.AttributeMessageReplyTo, msg.Reply, nil)

		f(msg)
	}
}