-
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.
Merge branch 'main' into rarguelloF/AIDM-326/fix-slog
- Loading branch information
Showing
7 changed files
with
306 additions
and
128 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
43 changes: 43 additions & 0 deletions
43
contrib/cloud.google.com/go/pubsub.v1/internal/tracing/config.go
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 2024 Datadog, Inc. | ||
|
||
package tracing | ||
|
||
import ( | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal/namingschema" | ||
) | ||
|
||
type config struct { | ||
serviceName string | ||
publishSpanName string | ||
receiveSpanName string | ||
measured bool | ||
} | ||
|
||
func defaultConfig() *config { | ||
return &config{ | ||
serviceName: namingschema.ServiceNameOverrideV0("", ""), | ||
publishSpanName: namingschema.OpName(namingschema.GCPPubSubOutbound), | ||
receiveSpanName: namingschema.OpName(namingschema.GCPPubSubInbound), | ||
measured: false, | ||
} | ||
} | ||
|
||
// Option is used to customize spans started by WrapReceiveHandler or Publish. | ||
type Option func(cfg *config) | ||
|
||
// WithServiceName sets the service name tag for traces started by WrapReceiveHandler or Publish. | ||
func WithServiceName(serviceName string) Option { | ||
return func(cfg *config) { | ||
cfg.serviceName = serviceName | ||
} | ||
} | ||
|
||
// WithMeasured sets the measured tag for traces started by WrapReceiveHandler or Publish. | ||
func WithMeasured() Option { | ||
return func(cfg *config) { | ||
cfg.measured = true | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
contrib/cloud.google.com/go/pubsub.v1/internal/tracing/tracing.go
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,120 @@ | ||
// 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 2024 Datadog, Inc. | ||
|
||
package tracing | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
"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" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry" | ||
) | ||
|
||
const componentName = "cloud.google.com/go/pubsub.v1" | ||
|
||
func init() { | ||
telemetry.LoadIntegration(componentName) | ||
tracer.MarkIntegrationImported(componentName) | ||
} | ||
|
||
type Message struct { | ||
ID string | ||
Data []byte | ||
OrderingKey string | ||
Attributes map[string]string | ||
DeliveryAttempt *int | ||
PublishTime time.Time | ||
} | ||
|
||
type Topic interface { | ||
String() string | ||
} | ||
|
||
type Subscription interface { | ||
String() string | ||
} | ||
|
||
func TracePublish(ctx context.Context, topic Topic, msg *Message, opts ...Option) (context.Context, func(serverID string, err error)) { | ||
cfg := defaultConfig() | ||
for _, opt := range opts { | ||
opt(cfg) | ||
} | ||
spanOpts := []ddtrace.StartSpanOption{ | ||
tracer.ResourceName(topic.String()), | ||
tracer.SpanType(ext.SpanTypeMessageProducer), | ||
tracer.Tag("message_size", len(msg.Data)), | ||
tracer.Tag("ordering_key", msg.OrderingKey), | ||
tracer.Tag(ext.Component, componentName), | ||
tracer.Tag(ext.SpanKind, ext.SpanKindProducer), | ||
tracer.Tag(ext.MessagingSystem, ext.MessagingSystemGCPPubsub), | ||
} | ||
if cfg.serviceName != "" { | ||
spanOpts = append(spanOpts, tracer.ServiceName(cfg.serviceName)) | ||
} | ||
if cfg.measured { | ||
spanOpts = append(spanOpts, tracer.Measured()) | ||
} | ||
span, ctx := tracer.StartSpanFromContext( | ||
ctx, | ||
cfg.publishSpanName, | ||
spanOpts..., | ||
) | ||
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/trace: failed injecting tracing attributes: %v", err) | ||
} | ||
span.SetTag("num_attributes", len(msg.Attributes)) | ||
|
||
var once sync.Once | ||
closeSpan := func(serverID string, err error) { | ||
once.Do(func() { | ||
span.SetTag("server_id", serverID) | ||
span.Finish(tracer.WithError(err)) | ||
}) | ||
} | ||
return ctx, closeSpan | ||
} | ||
|
||
func TraceReceiveFunc(s Subscription, opts ...Option) func(ctx context.Context, msg *Message) (context.Context, func()) { | ||
cfg := defaultConfig() | ||
for _, opt := range opts { | ||
opt(cfg) | ||
} | ||
log.Debug("contrib/cloud.google.com/go/pubsub.v1/trace: Wrapping Receive Handler: %#v", cfg) | ||
return func(ctx context.Context, msg *Message) (context.Context, func()) { | ||
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.Tag(ext.Component, componentName), | ||
tracer.Tag(ext.SpanKind, ext.SpanKindConsumer), | ||
tracer.Tag(ext.MessagingSystem, ext.MessagingSystemGCPPubsub), | ||
tracer.ChildOf(parentSpanCtx), | ||
} | ||
if cfg.serviceName != "" { | ||
opts = append(opts, tracer.ServiceName(cfg.serviceName)) | ||
} | ||
if cfg.measured { | ||
opts = append(opts, tracer.Measured()) | ||
} | ||
span, ctx := tracer.StartSpanFromContext(ctx, cfg.receiveSpanName, opts...) | ||
if msg.DeliveryAttempt != nil { | ||
span.SetTag("delivery_attempt", *msg.DeliveryAttempt) | ||
} | ||
return ctx, func() { span.Finish() } | ||
} | ||
} |
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
Oops, something went wrong.