-
Notifications
You must be signed in to change notification settings - Fork 435
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
57 changed files
with
3,033 additions
and
212 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
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 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 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,43 @@ | ||
// 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-2020 Datadog, Inc. | ||
|
||
package pubsub_test | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
pubsubtrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/cloud.google.com/go/pubsub.v1" | ||
|
||
"cloud.google.com/go/pubsub" | ||
) | ||
|
||
func ExamplePublish() { | ||
client, err := pubsub.NewClient(context.Background(), "project-id") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
topic := client.Topic("topic") | ||
_, err = pubsubtrace.Publish(context.Background(), topic, &pubsub.Message{Data: []byte("hello world!")}).Get(context.Background()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func ExampleReceive() { | ||
client, err := pubsub.NewClient(context.Background(), "project-id") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
sub := client.Subscription("subscription") | ||
err = sub.Receive(context.Background(), pubsubtrace.WrapReceiveHandler(sub, func(ctx context.Context, msg *pubsub.Message) { | ||
// TODO: Handle message. | ||
})) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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,90 @@ | ||
// 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-2020 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/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 | ||
} | ||
|
||
// 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)) func(context.Context, *pubsub.Message) { | ||
return func(ctx context.Context, msg *pubsub.Message) { | ||
parentSpanCtx, _ := tracer.Extract(tracer.TextMapCarrier(msg.Attributes)) | ||
span, ctx := tracer.StartSpanFromContext( | ||
ctx, | ||
"pubsub.receive", | ||
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 msg.DeliveryAttempt != nil { | ||
span.SetTag("delivery_attempt", *msg.DeliveryAttempt) | ||
} | ||
defer span.Finish() | ||
f(ctx, msg) | ||
} | ||
} |
Oops, something went wrong.