Skip to content

Commit

Permalink
Merge branch 'main' into ichinaski/fix-peer-tags-config
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio authored Feb 3, 2025
2 parents f31e3aa + b143b60 commit 7164af2
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 13 deletions.
16 changes: 15 additions & 1 deletion contrib/gorm.io/gorm.v1/orchestrion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,18 @@ aspects:
join-point:
function-call: gorm.io/gorm.Open
advice:
- replace-function: gopkg.in/DataDog/dd-trace-go.v1/contrib/gorm.io/gorm.v1.Open
- wrap-expression:
imports:
gorm: gorm.io/gorm
gormtrace: gopkg.in/DataDog/dd-trace-go.v1/contrib/gorm.io/gorm.v1
template: |-
func() (*gorm.DB, error) {
db, err := {{ . }}
if err != nil {
return nil, err
}
if err := db.Use(gormtrace.NewTracePlugin()); err != nil {
return nil, err
}
return db, nil
}()
7 changes: 6 additions & 1 deletion ddtrace/tracer/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,17 @@ func newConcentrator(c *config, bucketSize int64, statsdClient internal.StatsdCl
env = "unknown-env"
log.Debug("No DD Env found, normally the agent should have one")
}
gitCommitSha := ""
if c.ciVisibilityEnabled {
// We only have this data if we're in CI Visibility
gitCommitSha = utils.GetCITags()[constants.GitCommitSHA]
}
aggKey := stats.PayloadAggregationKey{
Hostname: c.hostname,
Env: env,
Version: c.version,
ContainerID: "", // This intentionally left empty as the Agent will attach the container ID only in certain situations.
GitCommitSha: utils.GetCITags()[constants.GitCommitSHA],
GitCommitSha: gitCommitSha,
ImageTag: "",
}
spanConcentrator := stats.NewSpanConcentrator(sCfg, time.Now())
Expand Down
2 changes: 1 addition & 1 deletion ddtrace/tracer/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestConcentrator(t *testing.T) {
t.Run("ciGitSha", func(t *testing.T) {
utils.AddCITags(constants.GitCommitSHA, "DEADBEEF")
transport := newDummyTransport()
c := newConcentrator(&config{transport: transport, env: "someEnv"}, (10 * time.Second).Nanoseconds(), &statsd.NoOpClientDirect{})
c := newConcentrator(&config{transport: transport, env: "someEnv", ciVisibilityEnabled: true}, (10 * time.Second).Nanoseconds(), &statsd.NoOpClientDirect{})
assert.Len(t, transport.Stats(), 0)
ss1, ok := c.newTracerStatSpan(&s1, nil)
assert.True(t, ok)
Expand Down
15 changes: 11 additions & 4 deletions ddtrace/tracer/textmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,22 @@ func (p *chainedPropagator) Inject(spanCtx ddtrace.SpanContext, carrier interfac
func (p *chainedPropagator) Extract(carrier interface{}) (ddtrace.SpanContext, error) {
var ctx ddtrace.SpanContext
var links []ddtrace.SpanLink

for _, v := range p.extractors {
firstExtract := (ctx == nil) // ctx stores the most recently extracted ctx across iterations; if it's nil, no extractor has run yet
extractedCtx, err := v.Extract(carrier)

if firstExtract {
if err != nil && err != ErrSpanContextNotFound { // We only care if the first extraction returns an error because this breaks distributed tracing
return nil, err
if err != nil {
if p.onlyExtractFirst { // Every error is relevant when we are relying on the first extractor
return nil, err
}
if err != ErrSpanContextNotFound { // We don't care about ErrSpanContextNotFound because we could find a span context in a subsequent extractor
return nil, err
}
}
if p.onlyExtractFirst { // Return early if only performing one extraction
return extractedCtx.(*spanContext), nil
if p.onlyExtractFirst {
return extractedCtx, nil
}
ctx = extractedCtx
} else { // A local trace context has already been extracted
Expand Down
43 changes: 43 additions & 0 deletions ddtrace/tracer/textmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2244,6 +2244,49 @@ func TestOtelPropagator(t *testing.T) {
}
}

// Assert that extraction returns a ErrSpanContextNotFound error when no trace context headers are found
func TestExtractNoHeaders(t *testing.T) {
tests := []struct {
name string
extractEnv string
extractFirst bool
}{
{
name: "single header",
extractEnv: "datadog",
extractFirst: false,
},
{
name: "single header - extractFirst",
extractEnv: "datadog",
extractFirst: true,
},
{
name: "multi header",
extractEnv: "datadog,tracecontext",
extractFirst: false,
},
{
name: "multi header - extractFirst",
extractEnv: "datadog,tracecontext",
extractFirst: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv(headerPropagationStyleExtract, tt.extractEnv)
if tt.extractFirst {
t.Setenv("DD_TRACE_PROPAGATION_EXTRACT_FIRST", "true")
}
tracer := newTracer()
defer tracer.Stop()
ctx, err := tracer.Extract(TextMapCarrier{})
assert.Equal(t, ErrSpanContextNotFound, err)
assert.Nil(t, ctx)
})
}
}

func BenchmarkInjectDatadog(b *testing.B) {
b.Setenv(headerPropagationStyleInject, "datadog")
tracer := newTracer()
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module gopkg.in/DataDog/dd-trace-go.v1

go 1.22.0

// This replace is a temporary workaround to deal with a breaking change here that is used by the datadog-agent
// It can safely be removed once this PR is released: https://github.com/DataDog/datadog-agent/pull/33370
replace github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes => github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.20.0

require (
cloud.google.com/go/pubsub v1.37.0
github.com/99designs/gqlgen v0.17.36
Expand Down
4 changes: 2 additions & 2 deletions internal/civisibility/utils/net/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ import (

const (
// DefaultMaxRetries is the default number of retries for a request.
DefaultMaxRetries int = 5
DefaultMaxRetries int = 3
// DefaultBackoff is the default backoff time for a request.
DefaultBackoff time.Duration = 150 * time.Millisecond
DefaultBackoff time.Duration = 100 * time.Millisecond
)

type (
Expand Down
2 changes: 1 addition & 1 deletion internal/civisibility/utils/net/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func decompressData(data []byte) ([]byte, error) {

// exponentialBackoff performs an exponential backoff with retries.
func exponentialBackoff(retryCount int, initialDelay time.Duration) {
maxDelay := 30 * time.Second
maxDelay := 10 * time.Second
delay := initialDelay * (1 << uint(retryCount)) // Exponential backoff
if delay > maxDelay {
delay = maxDelay
Expand Down
4 changes: 2 additions & 2 deletions internal/civisibility/utils/net/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,9 @@ func TestSendRequestWithInvalidRetryAfterHeader(t *testing.T) {

func TestExponentialBackoffWithMaxDelay(t *testing.T) {
start := time.Now()
exponentialBackoff(10, 1*time.Second) // Should be limited to maxDelay (30s)
exponentialBackoff(10, 1*time.Second) // Should be limited to maxDelay (10s)
duration := time.Since(start)
assert.LessOrEqual(t, duration, 31*time.Second)
assert.LessOrEqual(t, duration, 11*time.Second)
}

func TestSendRequestWithContextTimeout(t *testing.T) {
Expand Down
17 changes: 16 additions & 1 deletion internal/orchestrion/_integration/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type TestCase struct {

func (tc *TestCase) Setup(_ context.Context, t *testing.T) {
var err error
tc.DB, err = gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{})
tc.DB, err = gorm.Open(sqlite.Open("file::memory:"))
require.NoError(t, err)

require.NoError(t, tc.DB.AutoMigrate(&Note{}))
Expand Down Expand Up @@ -63,6 +63,21 @@ func (*TestCase) ExpectedTraces() trace.Traces {
Meta: map[string]string{
"component": "gorm.io/gorm.v1",
},
Children: trace.Traces{
{
Tags: map[string]any{
"resource": "SELECT * FROM `notes` WHERE user_id = ? AND `notes`.`deleted_at` IS NULL ORDER BY `notes`.`id` LIMIT 1",
"type": "sql",
"name": "sqlite3.query",
"service": "sqlite3.db",
},
Meta: map[string]string{
"component": "database/sql",
"span.kind": "client",
"db.system": "other_sql",
},
},
},
},
},
},
Expand Down

0 comments on commit 7164af2

Please sign in to comment.