cloud.google.com/go/pubsub

cloud.google.com/go/pubsub

Package pubsub provides an easy way to publish and receive Google Cloud Pub/Sub messages, hiding the details of the underlying server RPCs

Modify pubsub.Subscription.Receive to call tracing functions

Join Point
Function body
Advice
Prepend statements produced by the following template:
// Using the following synthetic imports:
import (
	tracing "gopkg.in/DataDog/dd-trace-go.v1/contrib/cloud.google.com/go/pubsub.v1/internal/tracing"
)
{{- $subscription := .Function.Receiver -}}
{{- $handler := .Function.Argument 1 -}}
__dd_traceFn := tracing.TraceReceiveFunc({{ $subscription }})
__dd_wrapHandler := func(h func(ctx context.Context, msg *Message)) func(ctx context.Context, msg *Message) {
	return func(ctx context.Context, msg *Message) {
		__dd_traceMsg := &tracing.Message{
			ID:              msg.ID,
			Data:            msg.Data,
			OrderingKey:     msg.OrderingKey,
			Attributes:      msg.Attributes,
			DeliveryAttempt: msg.DeliveryAttempt,
			PublishTime:     msg.PublishTime,
		}
		ctx, closeSpan := __dd_traceFn(ctx, __dd_traceMsg)
		defer closeSpan()
		h(ctx, msg)
	}
}
{{ $handler }} = __dd_wrapHandler({{ $handler }})

Add struct fields to pubsub.PublishResult

Join Point
Definition of pubsub.PublishResult
Advice
Introduce new declarations:
type DDCloseSpanFunc = func(serverID string, err error)
Advice
Add new field DDCloseSpan of type DDCloseSpanFunc.

Modify pubsub.Topic.Publish to call tracing functions

Join Point
Function body
  • Function declaration
Advice
Prepend statements produced by the following template:
// Using the following synthetic imports:
import (
	tracing "gopkg.in/DataDog/dd-trace-go.v1/contrib/cloud.google.com/go/pubsub.v1/internal/tracing"
)
{{- $topic := .Function.Receiver -}}
{{- $ctx := .Function.Argument 0 -}}
{{- $msg := .Function.Argument 1 -}}
{{- $publishResult := .Function.Result 0 -}}
__dd_traceMsg := &tracing.Message{
	ID:              {{ $msg }}.ID,
	Data:            {{ $msg }}.Data,
	OrderingKey:     {{ $msg }}.OrderingKey,
	Attributes:      {{ $msg }}.Attributes,
	DeliveryAttempt: {{ $msg }}.DeliveryAttempt,
	PublishTime:     {{ $msg }}.PublishTime,
}
__dd_ctx, __dd_closeSpan := tracing.TracePublish({{ $ctx }}, {{ $topic }}, __dd_traceMsg)
{{ $ctx }} = __dd_ctx
{{ $msg }}.Attributes = __dd_traceMsg.Attributes

defer func() {
	{{ $publishResult }}.DDCloseSpan = __dd_closeSpan
}()

Modify pubsub.PublishResult.Get to call tracing functions

Join Point
Function body
Advice
Prepend statements produced by the following template:
{{- $publishResult := .Function.Receiver -}}
{{- $serverID := .Function.Result 0 -}}
{{- $err := .Function.Result 1 -}}
defer func() {
	if {{ $publishResult }}.DDCloseSpan != nil {
		{{ $publishResult }}.DDCloseSpan({{ $serverID }}, {{ $err }})
	}
}()