/
pubsub.go
112 lines (101 loc) · 3.84 KB
/
pubsub.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
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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
// Package pubsub provides functions to trace the cloud.google.com/pubsub/go package.
package pubsub
import (
"context"
"sync"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"cloud.google.com/go/pubsub"
)
// Publish publishes a message on the specified topic and returns a PublishResult.
// This function is functionally equivalent to t.Publish(ctx, msg), but it also starts a publish
// span and it ensures that the tracing metadata is propagated as attributes attached to
// the published message.
// It is required to call (*PublishResult).Get(ctx) on the value returned by Publish to complete
// the span.
func Publish(ctx context.Context, t *pubsub.Topic, msg *pubsub.Message) *PublishResult {
span, ctx := tracer.StartSpanFromContext(
ctx,
"pubsub.publish",
tracer.ResourceName(t.String()),
tracer.SpanType(ext.SpanTypeMessageProducer),
tracer.Tag("message_size", len(msg.Data)),
tracer.Tag("ordering_key", msg.OrderingKey),
)
if msg.Attributes == nil {
msg.Attributes = make(map[string]string)
}
if err := tracer.Inject(span.Context(), tracer.TextMapCarrier(msg.Attributes)); err != nil {
log.Debug("contrib/cloud.google.com/go/pubsub.v1/: failed injecting tracing attributes: %v", err)
}
span.SetTag("num_attributes", len(msg.Attributes))
return &PublishResult{
PublishResult: t.Publish(ctx, msg),
span: span,
}
}
// PublishResult wraps *pubsub.PublishResult
type PublishResult struct {
*pubsub.PublishResult
once sync.Once
span tracer.Span
}
// Get wraps (pubsub.PublishResult).Get(ctx). When this function returns the publish
// span created in Publish is completed.
func (r *PublishResult) Get(ctx context.Context) (string, error) {
serverID, err := r.PublishResult.Get(ctx)
r.once.Do(func() {
r.span.SetTag("server_id", serverID)
r.span.Finish(tracer.WithError(err))
})
return serverID, err
}
type config struct {
serviceName string
}
// A ReceiveOption is used to customize spans started by WrapReceiveHandler.
type ReceiveOption func(cfg *config)
// WithServiceName sets the service name tag for traces started by WrapReceiveHandler.
func WithServiceName(serviceName string) ReceiveOption {
return func(cfg *config) {
cfg.serviceName = serviceName
}
}
// WrapReceiveHandler returns a receive handler that wraps the supplied handler,
// extracts any tracing metadata attached to the received message, and starts a
// receive span.
func WrapReceiveHandler(s *pubsub.Subscription, f func(context.Context, *pubsub.Message), opts ...ReceiveOption) func(context.Context, *pubsub.Message) {
var cfg config
for _, opt := range opts {
opt(&cfg)
}
log.Debug("contrib/cloud.google.com/go/pubsub.v1: Wrapping Receive Handler: %#v", cfg)
return func(ctx context.Context, msg *pubsub.Message) {
parentSpanCtx, _ := tracer.Extract(tracer.TextMapCarrier(msg.Attributes))
opts := []ddtrace.StartSpanOption{
tracer.ResourceName(s.String()),
tracer.SpanType(ext.SpanTypeMessageConsumer),
tracer.Tag("message_size", len(msg.Data)),
tracer.Tag("num_attributes", len(msg.Attributes)),
tracer.Tag("ordering_key", msg.OrderingKey),
tracer.Tag("message_id", msg.ID),
tracer.Tag("publish_time", msg.PublishTime.String()),
tracer.ChildOf(parentSpanCtx),
}
if cfg.serviceName != "" {
opts = append(opts, tracer.ServiceName(cfg.serviceName))
}
span, ctx := tracer.StartSpanFromContext(ctx, "pubsub.receive", opts...)
if msg.DeliveryAttempt != nil {
span.SetTag("delivery_attempt", *msg.DeliveryAttempt)
}
defer span.Finish()
f(ctx, msg)
}
}