-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
183 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package nramqp | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
amqp "github.com/rabbitmq/amqp091-go" | ||
|
||
"github.com/newrelic/go-agent/v3/newrelic" | ||
) | ||
|
||
type Channel struct { | ||
amqpChan *amqp.Channel | ||
txn *newrelic.Transaction | ||
// exchangeCache stores the name and the type of an exchange | ||
exchangeCache map[string]string | ||
} | ||
|
||
func WrappedAMQPChannel(ch *amqp.Channel, txn *newrelic.Transaction) *Channel { | ||
return &Channel{ | ||
amqpChan: ch, | ||
txn: txn, | ||
exchangeCache: map[string]string{}, | ||
} | ||
} | ||
|
||
func (ch *Channel) ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp.Table) error { | ||
txn := ch.txn | ||
|
||
segmentName := "MessageBroker/RabbitMQ/Exchange/Declare/Named/Default" | ||
if name != "" { | ||
segmentName = "MessageBroker/RabbitMQ/Exchange/Declare/Named/" + name | ||
} | ||
excSegment := txn.StartSegment(segmentName) | ||
defer excSegment.End() | ||
|
||
ch.exchangeCache[name] = kind | ||
return ch.amqpChan.ExchangeDeclare(name, kind, durable, autoDelete, internal, noWait, args) | ||
} | ||
|
||
func (ch *Channel) QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args amqp.Table) (amqp.Queue, error) { | ||
txn := ch.txn | ||
|
||
segmentName := "MessageBroker/RabbitMQ/Queue/Declare/Named/Default" | ||
if name != "" { | ||
segmentName = "MessageBroker/RabbitMQ/Queue/Declare/Named/" + name | ||
} | ||
excSegment := txn.StartSegment(segmentName) | ||
defer excSegment.End() | ||
|
||
return ch.amqpChan.QueueDeclare(name, durable, autoDelete, exclusive, noWait, args) | ||
} | ||
|
||
func (ch *Channel) QueueBind(name, key, exchange string, noWait bool, args amqp.Table) error { | ||
txn := ch.txn | ||
|
||
segmentName := "MessageBroker/RabbitMQ/Queue/Bind/Named/Default" | ||
if name != "" { | ||
segmentName = "MessageBroker/RabbitMQ/Queue/Bind/Named/" + name | ||
} | ||
|
||
excSegment := txn.StartSegment(segmentName) | ||
defer excSegment.End() | ||
|
||
err := ch.amqpChan.QueueBind(name, key, exchange, noWait, args) | ||
|
||
txn.SetWebRequest(newrelic.WebRequest{ | ||
MessageBrokerAttributes: newrelic.MessageBrokerAttributes{ | ||
RoutingKey: key, | ||
}, | ||
}) | ||
|
||
return err | ||
} | ||
|
||
// Consume creates a named segment that captures telemetry about this specific consume operation to the transaction. | ||
func (ch *Channel) Consume(queue, consumer string, autoAck, exclusive, noLocal, noWait bool, args amqp.Table) (<-chan amqp.Delivery, error) { | ||
txn := ch.txn | ||
|
||
name := "MessageBroker/RabbitMQ/Queue/Consume/Named/Default" | ||
if queue != "" { | ||
if consumer != "" { | ||
name = fmt.Sprintf("MessageBroker/RabbitMQ/Queue/Consume/Named/%s/%s", queue, consumer) | ||
} else { | ||
name = fmt.Sprintf("MessageBroker/RabbitMQ/Queue/Consume/Named/%s", queue) | ||
} | ||
} | ||
|
||
consumeSegment := txn.StartSegment(name) | ||
defer consumeSegment.End() | ||
|
||
txn.SetWebRequest(newrelic.WebRequest{ | ||
Transport: newrelic.TransportAMQP, | ||
MessageBrokerAttributes: newrelic.MessageBrokerAttributes{ | ||
QueueName: queue, | ||
}, | ||
}) | ||
|
||
return ch.amqpChan.Consume(queue, consumer, autoAck, exclusive, noLocal, noWait, args) | ||
} | ||
|
||
func (ch *Channel) PublishWithContext(ctx context.Context, exchange, key string, mandatory, immediate bool, msg amqp.Publishing) error { | ||
txn := ch.txn | ||
if txn != nil { | ||
// Create copy of headers so that we can inject DT headers into original object | ||
copyHeaders := amqp.Table{} | ||
for k, v := range msg.Headers { | ||
copyHeaders[k] = v | ||
} | ||
|
||
injectDtHeaders(txn, msg.Headers) | ||
|
||
exchangeName := "Default" | ||
if exchange != "" { | ||
exchangeName = exchange | ||
} | ||
consumeSegment := txn.StartSegment("MessageBroker/RabbitMQ/Exchange/Produce/Named/" + exchangeName) | ||
defer consumeSegment.End() | ||
|
||
txn.SetWebRequest(newrelic.WebRequest{ | ||
Transport: newrelic.TransportAMQP, | ||
Body: msg.Body, | ||
MessageBrokerAttributes: newrelic.MessageBrokerAttributes{ | ||
RoutingKey: key, | ||
Headers: copyHeaders, | ||
ExchangeType: ch.exchangeCache[exchange], | ||
ReplyTo: msg.ReplyTo, | ||
CorrelationID: msg.CorrelationId, | ||
}, | ||
}) | ||
} | ||
|
||
return ch.amqpChan.PublishWithContext(ctx, exchange, key, mandatory, immediate, msg) | ||
} | ||
|
||
// Adds Distributed Tracing headers to the amqp table object | ||
func injectDtHeaders(txn *newrelic.Transaction, headers amqp.Table) { | ||
dummyHeaders := http.Header{} | ||
txn.InsertDistributedTraceHeaders(dummyHeaders) | ||
|
||
dtHeaders := dummyHeaders.Get(newrelic.DistributedTraceNewRelicHeader) | ||
if dtHeaders != "" { | ||
headers[newrelic.DistributedTraceNewRelicHeader] = dtHeaders | ||
} | ||
traceParent := dummyHeaders.Get(newrelic.DistributedTraceW3CTraceParentHeader) | ||
if traceParent != "" { | ||
headers[newrelic.DistributedTraceW3CTraceParentHeader] = traceParent | ||
} | ||
traceState := dummyHeaders.Get(newrelic.DistributedTraceW3CTraceStateHeader) | ||
if traceState != "" { | ||
headers[newrelic.DistributedTraceW3CTraceStateHeader] = traceState | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters