11package datadog.trace.api
22
3- import datadog.trace.test.util.DDSpecification
4-
53import static datadog.trace.api.config.GeneralConfig.ENV
64import static datadog.trace.api.config.GeneralConfig.EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED
75import static datadog.trace.api.config.GeneralConfig.PRIMARY_TAG
86import static datadog.trace.api.config.GeneralConfig.SERVICE_NAME
97
8+ import datadog.trace.test.util.DDSpecification
9+
1010class BaseHashTest extends DDSpecification {
1111
12+
13+ def setup () {
14+ // start with fresh process tags
15+ ProcessTags . reset()
16+ }
17+
18+ def cleanup () {
19+ // restore the default enablement
20+ removeSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED )
21+ ProcessTags . reset()
22+ }
23+
1224 def " Service used in hash calculation" () {
1325 when :
1426 def firstBaseHash = BaseHash . calc(null )
@@ -46,17 +58,54 @@ class BaseHashTest extends DDSpecification {
4658 when :
4759 def firstBaseHash = BaseHash . calc(null )
4860
49- injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED , " true" )
50- ProcessTags . reset()
5161 ProcessTags . addTag(" 000" , " first" )
5262 def secondBaseHash = BaseHash . calc(null )
5363
5464 then :
5565 firstBaseHash != secondBaseHash
5666 assert ProcessTags . getTagsForSerialization(). startsWithAny(" 000:first," )
57- cleanup :
67+ }
68+
69+ def " addTag triggers BaseHash recalculation" () {
70+ given :
71+ BaseHash . recalcBaseHash(" container-hash-1" )
72+ def hashBefore = BaseHash . getBaseHash()
73+
74+ when :
75+ ProcessTags . addTag(" cluster.name" , " new-cluster" )
76+
77+ then :
78+ BaseHash . getBaseHash() != hashBefore
79+ }
80+
81+ def " recalcBaseHash preserves last containerTagsHash across ProcessTags changes" () {
82+ given :
83+ def containerHash = " my-container-hash"
84+ BaseHash . recalcBaseHash(containerHash)
85+ def hashWithContainerTag = BaseHash . getBaseHash()
86+
87+ when : " a process tag is added"
88+ ProcessTags . addTag(" cluster.name" , " new-cluster" )
89+
90+ then : " hash differs from before the tag was added"
91+ BaseHash . getBaseHash() != hashWithContainerTag
92+
93+ and : " hash equals a fresh calc with the same container hash"
94+ BaseHash . getBaseHash() == BaseHash . calc(containerHash)
95+ }
96+
97+ def " addTag does not recalculate BaseHash when ProcessTags disabled" () {
98+ setup :
5899 injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED , " false" )
59100 ProcessTags . reset()
101+ BaseHash . recalcBaseHash(null )
102+ def hashBefore = BaseHash . getBaseHash()
103+
104+ when :
105+ ProcessTags . addTag(" cluster.name" , " ignored" )
106+
107+ then :
108+ BaseHash . getBaseHash() == hashBefore
60109 }
61110
62111 def " ContainerTagsHash used in hash calculation when provided" () {
@@ -76,10 +125,6 @@ class BaseHashTest extends DDSpecification {
76125 assert secondBaseHash == firstBaseHash
77126 }
78127
79- cleanup :
80- injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED , " false" )
81- ProcessTags . reset()
82-
83128 where :
84129 propagateTagsEnabled << [true , false ]
85130 }
0 commit comments