Skip to content

Commit 52d1e19

Browse files
amarzialidevflow.devflow-routing-intake
andauthored
Update BaseHash when process tags are updated (#10809)
Update BaseHash when process tags are updated suggestion Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>
1 parent fd65c0a commit 52d1e19

File tree

3 files changed

+63
-11
lines changed

3 files changed

+63
-11
lines changed

internal-api/src/main/java/datadog/trace/api/BaseHash.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
public final class BaseHash {
66
private static volatile long baseHash;
77
private static volatile String baseHashStr;
8+
private static volatile String lastContainerTagsHash;
89

910
private BaseHash() {}
1011

1112
public static void recalcBaseHash(String containerTagsHash) {
12-
long hash = calc(containerTagsHash);
13-
updateBaseHash(hash);
13+
lastContainerTagsHash = containerTagsHash;
14+
updateBaseHash(calc(containerTagsHash));
15+
}
16+
17+
static void recalcBaseHash() {
18+
updateBaseHash(calc(lastContainerTagsHash));
1419
}
1520

1621
public static void updateBaseHash(long hash) {

internal-api/src/main/java/datadog/trace/api/ProcessTags.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ public static void addTag(String key, String value) {
182182
Lazy.stringListForm = null;
183183
Lazy.utf8ListForm = null;
184184
}
185+
BaseHash.recalcBaseHash();
185186
}
186187
}
187188

@@ -242,6 +243,7 @@ public static void reset(Config config) {
242243
empty();
243244
enabled = config.isExperimentalPropagateProcessTagsEnabled();
244245
Lazy.TAGS.putAll(Lazy.loadTags(config));
246+
BaseHash.recalcBaseHash();
245247
}
246248
}
247249
}

internal-api/src/test/groovy/datadog/trace/api/BaseHashTest.groovy

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
package datadog.trace.api
22

3-
import datadog.trace.test.util.DDSpecification
4-
53
import static datadog.trace.api.config.GeneralConfig.ENV
64
import static datadog.trace.api.config.GeneralConfig.EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED
75
import static datadog.trace.api.config.GeneralConfig.PRIMARY_TAG
86
import static datadog.trace.api.config.GeneralConfig.SERVICE_NAME
97

8+
import datadog.trace.test.util.DDSpecification
9+
1010
class 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

Comments
 (0)