Skip to content

Commit

Permalink
Merge branch 'main' into eliott.bouhana/remove-oldtelemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
eliottness committed Feb 20, 2025
2 parents a71acf1 + 3e85356 commit 955e279
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ variables:
INDEX_FILE: index.txt
KUBERNETES_SERVICE_ACCOUNT_OVERWRITE: dd-trace-go
FF_USE_LEGACY_KUBERNETES_EXECUTION_STRATEGY: "true"
BENCHMARK_TARGETS: "BenchmarkStartRequestSpan|BenchmarkHttpServeTrace|BenchmarkTracerAddSpans|BenchmarkStartSpan|BenchmarkSingleSpanRetention|BenchmarkOTelApiWithCustomTags|BenchmarkInjectW3C|BenchmarkExtractW3C|BenchmarkPartialFlushing|BenchmarkGraphQL|BenchmarkSampleWAFContext|BenchmarkCaptureStackTrace|BenchmarkSetTagString|BenchmarkSetTagStringPtr|BenchmarkSetTagMetric|BenchmarkSetTagStringer|BenchmarkSerializeSpanLinksInMeta|BenchmarkLogs|BenchmarkWorstCaseScenarioFloodLogging|BenchmarkMetrics|BenchmarkWorstCaseScenarioFloodMetrics"
BENCHMARK_TARGETS: "BenchmarkStartRequestSpan|BenchmarkHttpServeTrace|BenchmarkTracerAddSpans|BenchmarkStartSpan|BenchmarkSingleSpanRetention|BenchmarkOTelApiWithCustomTags|BenchmarkInjectW3C|BenchmarkExtractW3C|BenchmarkPartialFlushing|BenchmarkGraphQL|BenchmarkSampleWAFContext|BenchmarkCaptureStackTrace|BenchmarkSetTagString|BenchmarkSetTagStringPtr|BenchmarkSetTagMetric|BenchmarkSetTagStringer|BenchmarkSerializeSpanLinksInMeta|BenchmarkLogs|BenchmarkParallelLogs|BenchmarkMetrics|BenchmarkParallelMetrics"
include:
- ".gitlab/benchmarks.yml"
- ".gitlab/macrobenchmarks.yml"
Expand Down
1 change: 1 addition & 0 deletions appsec/appsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func TrackCustomEvent(ctx context.Context, name string, md map[string]string) {
}

tagPrefix := "appsec.events." + name + "."
span.SetTag("_dd."+tagPrefix+"sdk", "true")
span.SetTag(tagPrefix+"track", "true")
span.SetTag(ext.SamplingPriority, ext.PriorityUserKeep)
for k, v := range md {
Expand Down
4 changes: 4 additions & 0 deletions appsec/appsec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestTrackUserLoginSuccessEvent(t *testing.T) {
require.Len(t, mt.FinishedSpans(), 1)
finished := mt.FinishedSpans()[0]
expectedEventPrefix := "appsec.events.users.login.success."
require.Equal(t, "true", finished.Tag("_dd."+expectedEventPrefix+"sdk"))
require.Equal(t, "true", finished.Tag(expectedEventPrefix+"track"))
require.Equal(t, ext.PriorityUserKeep, finished.Tag(ext.SamplingPriority))
require.Equal(t, "user id", finished.Tag("usr.id"))
Expand Down Expand Up @@ -82,6 +83,7 @@ func TestTrackUserLoginSuccess(t *testing.T) {
require.Len(t, mt.FinishedSpans(), 1)
finished := mt.FinishedSpans()[0]
expectedEventPrefix := "appsec.events.users.login.success."
require.Equal(t, "true", finished.Tag("_dd."+expectedEventPrefix+"sdk"))
require.Equal(t, "true", finished.Tag(expectedEventPrefix+"track"))
require.Equal(t, ext.PriorityUserKeep, finished.Tag(ext.SamplingPriority))
require.Equal(t, "user id", finished.Tag("usr.id"))
Expand Down Expand Up @@ -135,6 +137,7 @@ func TestTrackUserLoginFailureEvent(t *testing.T) {
require.Len(t, mt.FinishedSpans(), 1)
finished := mt.FinishedSpans()[0]
expectedEventPrefix := "appsec.events.users.login.failure."
require.Equal(t, "true", finished.Tag("_dd."+expectedEventPrefix+"sdk"))
require.Equal(t, "true", finished.Tag(expectedEventPrefix+"track"))
require.Equal(t, ext.PriorityUserKeep, finished.Tag(ext.SamplingPriority))
require.Equal(t, "user id", finished.Tag(expectedEventPrefix+"usr.id"))
Expand Down Expand Up @@ -174,6 +177,7 @@ func TestTrackUserLoginFailure(t *testing.T) {
require.Len(t, mt.FinishedSpans(), 1)
finished := mt.FinishedSpans()[0]
expectedEventPrefix := "appsec.events.users.login.failure."
require.Equal(t, "true", finished.Tag("_dd."+expectedEventPrefix+"sdk"))
require.Equal(t, "true", finished.Tag(expectedEventPrefix+"track"))
require.Equal(t, "user login", finished.Tag(expectedEventPrefix+"usr.login"))
require.Equal(t, strconv.FormatBool(userExists), finished.Tag(expectedEventPrefix+"usr.exists"))
Expand Down
9 changes: 8 additions & 1 deletion contrib/99designs/gqlgen/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (t *gqlTracer) InterceptOperation(ctx context.Context, next graphql.Operati

func (t *gqlTracer) InterceptField(ctx context.Context, next graphql.Resolver) (res any, err error) {
opCtx := graphql.GetOperationContext(ctx)
if t.cfg.withoutTraceIntrospectionQuery && opCtx.OperationName == "IntrospectionQuery" {
if t.cfg.withoutTraceIntrospectionQuery && isIntrospectionQuery(opCtx) {
res, err = next(ctx)
return
}
Expand Down Expand Up @@ -244,6 +244,13 @@ func serverSpanName(octx *graphql.OperationContext) string {
return namingschema.OpNameOverrideV0(namingschema.GraphqlServer, nameV0)
}

func isIntrospectionQuery(octx *graphql.OperationContext) bool {
if octx.Operation != nil {
return octx.Operation.Name == "IntrospectionQuery"
}
return octx.OperationName == "IntrospectionQuery"
}

// Ensure all of these interfaces are implemented.
var _ interface {
graphql.HandlerExtension
Expand Down
32 changes: 20 additions & 12 deletions contrib/99designs/gqlgen/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,30 +110,38 @@ func TestOptions(t *testing.T) {

// WithoutTraceIntrospectionQuery tested here since we are specifically checking against an IntrosepctionQuery operation.
query = `query IntrospectionQuery { __schema { queryType { name } } }`
testFunc := func(assert *assert.Assertions, spans []mocktracer.Span) {
var hasFieldSpan bool
for _, span := range spans {
if span.OperationName() == fieldOp {
hasFieldSpan = true
break
}
}
assert.Equal(false, hasFieldSpan)
}
for name, tt := range map[string]struct {
tracerOpts []Option
clientOpts []client.Option
test func(assert *assert.Assertions, spans []mocktracer.Span)
}{
"WithoutTraceIntrospectionQuery": {
"WithoutTraceIntrospectionQuery with OperationName": {
tracerOpts: []Option{WithoutTraceIntrospectionQuery()},
test: func(assert *assert.Assertions, spans []mocktracer.Span) {
var hasFieldSpan bool
for _, span := range spans {
if span.OperationName() == fieldOp {
hasFieldSpan = true
break
}
}
assert.Equal(false, hasFieldSpan)
},
clientOpts: []client.Option{client.Operation("IntrospectionQuery")},
test: testFunc,
},
"WithoutTraceIntrospectionQuery without OperationName": {
tracerOpts: []Option{WithoutTraceIntrospectionQuery()},
clientOpts: []client.Option{},
test: testFunc,
},
} {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
mt := mocktracer.Start()
defer mt.Stop()
c := newTestClient(t, testserver.New(), NewTracer(tt.tracerOpts...))
c.MustPost(query, &testServerResponse{}, client.Operation("IntrospectionQuery"))
c.MustPost(query, &testServerResponse{}, tt.clientOpts...)
tt.test(assert, mt.FinishedSpans())
})
}
Expand Down
18 changes: 16 additions & 2 deletions internal/telemetry/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,14 @@ func TestClientFlush(t *testing.T) {
{
name: "extended-heartbeat-config",
clientConfig: ClientConfig{
HeartbeatInterval: time.Nanosecond,
ExtendedHeartbeatInterval: time.Nanosecond,
},
when: func(c *client) {
c.RegisterAppConfig("key", "value", OriginDefault)

// Make sure the limiter of the heartbeat is triggered
time.Sleep(time.Microsecond)
runtime.Gosched()
},
expect: func(t *testing.T, payloads []transport.Payload) {
payload := payloads[0]
Expand All @@ -148,11 +151,14 @@ func TestClientFlush(t *testing.T) {
{
name: "extended-heartbeat-integrations",
clientConfig: ClientConfig{
HeartbeatInterval: time.Nanosecond,
ExtendedHeartbeatInterval: time.Nanosecond,
},
when: func(c *client) {
c.MarkIntegrationAsLoaded(Integration{Name: "test-integration", Version: "1.0.0"})

// Make sure the limiter of the heartbeat is triggered
time.Sleep(time.Microsecond)
runtime.Gosched()
},
expect: func(t *testing.T, payloads []transport.Payload) {
payload := payloads[0]
Expand Down Expand Up @@ -344,6 +350,10 @@ func TestClientFlush(t *testing.T) {
when: func(c *client) {
c.ProductStarted("test-product")
c.MarkIntegrationAsLoaded(Integration{Name: "test-integration", Version: "1.0.0"})

// Make sure the limiter of the heartbeat is triggered
time.Sleep(time.Microsecond)
runtime.Gosched()
},
expect: func(t *testing.T, payloads []transport.Payload) {
payload := payloads[0]
Expand Down Expand Up @@ -442,6 +452,10 @@ func TestClientFlush(t *testing.T) {
},
when: func(c *client) {
c.AppStart()

// Make sure the limiter of the heartbeat is triggered
time.Sleep(time.Microsecond)
runtime.Gosched()
},
expect: func(t *testing.T, payloads []transport.Payload) {
payload := payloads[0]
Expand Down
21 changes: 10 additions & 11 deletions internal/telemetry/internal/mapper/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
func NewDefaultMapper(heartbeatInterval, extendedHeartBeatInterval time.Duration) Mapper {
mapper := &defaultMapper{
heartbeatEnricher: heartbeatEnricher{
RL: rate.NewLimiter(rate.Every(heartbeatInterval), 1),
heartbeatRL: rate.NewLimiter(rate.Every(heartbeatInterval), 1),
extendedHeartbeatRL: rate.NewLimiter(rate.Every(extendedHeartBeatInterval), 1),
},
}

// The rate limiter is initialized with a token, but we want the first heartbeat to be sent in one minute, so we consume the token
mapper.heartbeatEnricher.RL.Allow()
mapper.heartbeatEnricher.heartbeatRL.Allow()
mapper.heartbeatEnricher.extendedHeartbeatRL.Allow()
return mapper
}
Expand Down Expand Up @@ -59,11 +59,11 @@ func (t *messageBatchReducer) Transform(payloads []transport.Payload) ([]transpo
}

type heartbeatEnricher struct {
RL *rate.Limiter
heartbeatRL *rate.Limiter
extendedHeartbeatRL *rate.Limiter

extendedHeartbeat transport.AppExtendedHeartbeat
heartBeat transport.AppHeartbeat
heartbeat transport.AppHeartbeat
}

func (t *heartbeatEnricher) Transform(payloads []transport.Payload) ([]transport.Payload, Mapper) {
Expand All @@ -85,15 +85,14 @@ func (t *heartbeatEnricher) Transform(payloads []transport.Payload) ([]transport
}
}

if !t.RL.Allow() {
// We don't send anything
return payloads, t
}

if t.extendedHeartbeatRL.Allow() {
// We have an extended heartbeat to send
return append(payloads, t.extendedHeartbeat), t
}

return append(payloads, t.heartBeat), t
if t.heartbeatRL.Allow() {
return append(payloads, t.heartbeat), t
}

// We don't send anything
return payloads, t
}

0 comments on commit 955e279

Please sign in to comment.