Skip to content

Commit 87e2aaa

Browse files
committed
adding more tests
1 parent 22631c5 commit 87e2aaa

File tree

3 files changed

+110
-3
lines changed

3 files changed

+110
-3
lines changed

dd-java-agent/instrumentation/okhttp-3/src/test/groovy/OkHttp3Test.groovy

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,65 @@ abstract class OkHttp3Test extends HttpClientTest {
7878
method = "GET"
7979
url = server.address.resolve(path)
8080
}
81+
82+
def "baggage span tags are properly added"() {
83+
setup:
84+
// W3C Baggage header format: key1=value1,key2=value2,key3=value3
85+
def baggageHeader = "user.id=bark,session.id=test-sess1,account.id=fetch,language=en"
86+
def sentHeaders = [:]
87+
88+
when:
89+
def status
90+
// Capture the headers that OkHttp3 sends
91+
def interceptor = new Interceptor() {
92+
@Override
93+
Response intercept(Chain chain) throws IOException {
94+
def request = chain.request()
95+
// Capture all headers sent
96+
request.headers().names().each { headerName ->
97+
sentHeaders[headerName] = request.header(headerName)
98+
}
99+
return chain.proceed(request)
100+
}
101+
}
102+
103+
def testClient = new OkHttpClient.Builder()
104+
.connectTimeout(CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
105+
.readTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
106+
.writeTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
107+
.addInterceptor(interceptor)
108+
.build()
109+
110+
def request = new Request.Builder()
111+
.url(server.address.resolve("/success").toURL())
112+
.header("baggage", baggageHeader) // Pass baggage directly in header
113+
.get()
114+
.build()
115+
def response = testClient.newCall(request).execute()
116+
status = response.code()
117+
118+
then:
119+
status == 200
120+
// Verify baggage header was sent
121+
sentHeaders["baggage"] == baggageHeader
122+
sentHeaders["baggage"].contains("user.id=bark")
123+
sentHeaders["baggage"].contains("session.id=test-sess1")
124+
sentHeaders["baggage"].contains("account.id=fetch")
125+
sentHeaders["baggage"].contains("language=en")
126+
127+
// Verify the resulting span has the correct baggage tags (only default configured keys)
128+
assertTraces(1) {
129+
trace(1) {
130+
clientSpan(it, null, "GET", false, false, server.address.resolve("/success"), 200, false, null, false, [
131+
// Should have baggage tags for keys in default configuration
132+
"baggage.user.id": "bark",
133+
"baggage.session.id": "test-sess1",
134+
"baggage.account.id": "fetch",
135+
// "baggage.language" should NOT be present since it's not in default config
136+
])
137+
}
138+
}
139+
}
81140
}
82141

83142
@Timeout(5)

dd-trace-core/src/test/groovy/datadog/trace/core/CoreSpanBuilderTest.groovy

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import static datadog.trace.api.DDTags.DJM_ENABLED
44
import static datadog.trace.api.DDTags.DSM_ENABLED
55
import static datadog.trace.api.DDTags.PROFILING_ENABLED
66
import static datadog.trace.api.DDTags.SCHEMA_VERSION_TAG_KEY
7+
import static datadog.trace.api.config.TracerConfig.TRACE_BAGGAGE_TAG_KEYS
78

89
import datadog.trace.api.Config
910
import datadog.trace.api.DDSpanId
1011
import datadog.trace.api.DDTraceId
1112
import datadog.trace.api.gateway.RequestContextSlot
13+
import datadog.trace.api.sampling.PrioritySampling
1214
import datadog.trace.api.naming.SpanNaming
1315
import datadog.trace.api.sampling.PrioritySampling
1416
import datadog.trace.bootstrap.instrumentation.api.AgentScope
@@ -491,6 +493,31 @@ class CoreSpanBuilderTest extends DDCoreSpecification {
491493
span1.finish()
492494
}
493495

496+
def "buildSpan should add baggage tags with different configurations"() {
497+
setup:
498+
injectSysConfig(TRACE_BAGGAGE_TAG_KEYS, baggageTagKeysConfig)
499+
def baggage = ["user.id": "alice", "session.id": "123", "region": "us-west-1", "env": "production"]
500+
def tagContext = new TagContext(null, null, null, baggage, PrioritySampling.UNSET, null, DATADOG, DDTraceId.ZERO)
501+
502+
when:
503+
def span = tracer.buildSpan("test", "test-op")
504+
.asChildOf(tagContext)
505+
.start()
506+
507+
then:
508+
// Filter span tags to only check baggage tags (those starting with "baggage.")
509+
def actualBaggageTags = span.tags.findAll { key, value -> key.startsWith("baggage.") }
510+
actualBaggageTags == expectedBaggageTags
511+
512+
cleanup:
513+
span.finish()
514+
515+
where:
516+
baggageTagKeysConfig | expectedBaggageTags
517+
"user.id" | ["baggage.user.id": "alice"]
518+
"user.id,session.id" | ["baggage.user.id": "alice", "baggage.session.id": "123"]
519+
}
520+
494521
def productTags() {
495522
def productTags = [
496523
(PROFILING_ENABLED) : Config.get().isProfilingEnabled() ? 1 : 0

dd-trace-core/src/test/groovy/datadog/trace/core/CoreTracerTest.groovy

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import static datadog.trace.api.config.TracerConfig.HEADER_TAGS
3838
import static datadog.trace.api.config.TracerConfig.PRIORITY_SAMPLING
3939
import static datadog.trace.api.config.TracerConfig.SERVICE_MAPPING
4040
import static datadog.trace.api.config.TracerConfig.SPAN_TAGS
41+
import static datadog.trace.api.config.TracerConfig.TRACE_BAGGAGE_TAG_KEYS
4142
import static datadog.trace.api.config.TracerConfig.WRITER_TYPE
4243

4344
@Timeout(10)
@@ -640,11 +641,31 @@ class CoreTracerTest extends DDCoreSpecification {
640641
"service" | "env" | "service_2" | "env_2"
641642
}
642643

643-
def "test mapBaggageTags"() {
644+
def "test mapBaggageTags default"() {
644645
when:
645-
def tags = CoreTracer.mapBaggageTags(["user.id": "doggo", "foo": "bar"])
646+
def tags = CoreTracer.mapBaggageTags(["user.id": "doggo", "account.id": "test", "session.id":"1234", "region":"us-east-1"])
646647
then:
647-
tags == ["baggage.user.id": "doggo"]
648+
tags == ["baggage.user.id": "doggo", "baggage.account.id": "test", "baggage.session.id": "1234"]
649+
}
650+
651+
def "test mapBaggageTags with different configurations"() {
652+
setup:
653+
injectSysConfig(TRACE_BAGGAGE_TAG_KEYS, baggageTagKeysConfig)
654+
655+
when:
656+
def tags = CoreTracer.mapBaggageTags(["user.id": "doggo", "foo": "bar", "language":"en"])
657+
658+
then:
659+
tags == expectedTags
660+
661+
where:
662+
baggageTagKeysConfig | expectedTags
663+
"*" | ["baggage.user.id": "doggo", "baggage.foo": "bar", "baggage.language": "en"]
664+
" " | [:]
665+
"system.id" | [:]
666+
"user.id" | ["baggage.user.id": "doggo"]
667+
"foo" | ["baggage.foo": "bar"]
668+
"user.id,foo" | ["baggage.user.id": "doggo", "baggage.foo": "bar"]
648669
}
649670
}
650671

0 commit comments

Comments
 (0)