github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrstan/nrstan.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package nrstan
     5  
     6  import (
     7  	stan "github.com/nats-io/stan.go"
     8  	newrelic "github.com/newrelic/go-agent"
     9  	"github.com/newrelic/go-agent/internal"
    10  	"github.com/newrelic/go-agent/internal/integrationsupport"
    11  )
    12  
    13  // StreamingSubWrapper can be used to wrap the function for STREAMING stan.Subscribe and stan.QueueSubscribe
    14  // (https://godoc.org/github.com/nats-io/stan.go#Conn)
    15  // If the `newrelic.Application` parameter is non-nil, it will create a `newrelic.Transaction` and end the transaction
    16  // when the passed function is complete.
    17  func StreamingSubWrapper(app newrelic.Application, f func(msg *stan.Msg)) func(msg *stan.Msg) {
    18  	if app == nil {
    19  		return f
    20  	}
    21  	return func(msg *stan.Msg) {
    22  		namer := internal.MessageMetricKey{
    23  			Library:         "STAN",
    24  			DestinationType: string(newrelic.MessageTopic),
    25  			DestinationName: msg.MsgProto.Subject,
    26  			Consumer:        true,
    27  		}
    28  		txn := app.StartTransaction(namer.Name(), nil, nil)
    29  		defer txn.End()
    30  
    31  		integrationsupport.AddAgentAttribute(txn, internal.AttributeMessageRoutingKey, msg.MsgProto.Subject, nil)
    32  		integrationsupport.AddAgentAttribute(txn, internal.AttributeMessageReplyTo, msg.MsgProto.Reply, nil)
    33  
    34  		f(msg)
    35  	}
    36  }