-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.go
75 lines (61 loc) · 1.7 KB
/
channel.go
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
package amqptrace
import (
"context"
"github.com/streadway/amqp"
"go.opentelemetry.io/otel/api/kv"
"go.opentelemetry.io/otel/api/trace"
)
// Channel is trace channel
type Channel struct {
Ch *amqp.Channel
tracer trace.Tracer
opts *channelOptions
}
// ChannelOption is channel option
type ChannelOption func(*channelOptions)
func ChannelComopenentName(componentName string) ChannelOption {
return func(o *channelOptions) {
o.componentName = componentName
}
}
type channelOptions struct {
componentName string
}
func applyChannelOptions(opts ...ChannelOption) *channelOptions {
o := &channelOptions{componentName: "net/amqp"}
for _, opt := range opts {
opt(o)
}
return o
}
// NewChannel create a trace channel
func NewChannel(tracer trace.Tracer, channel *amqp.Channel, opts ...ChannelOption) *Channel {
o := applyChannelOptions(opts...)
ch := &Channel{tracer: tracer, Ch: channel, opts: o}
return ch
}
// Publish sends a Publishing from the client to an exchange on the server.
func (ch *Channel) Publish(ctx context.Context, exchange, key string, mandatory, immediate bool, msg amqp.Publishing) error {
var span trace.Span
ctx, span = ch.tracer.Start(
ctx,
exchange+"."+key+" - Rabbitmq Publisher",
trace.WithSpanKind(trace.SpanKindClient),
trace.WithAttributes(
kv.String("exchange", exchange),
kv.String("routekey", key),
kv.String("content-type", msg.ContentType),
kv.Key("component").String(ch.opts.componentName),
),
)
defer span.End()
if msg.Headers == nil {
msg.Headers = amqp.Table{}
}
Inject(ctx, msg.Headers)
if err := ch.Ch.Publish(exchange, key, mandatory, immediate, msg); err != nil {
span.SetAttribute("error", err)
return err
}
return nil
}