Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal-api/src/main/java/datadog/trace/api/BaseHash.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
public final class BaseHash {
private static volatile long baseHash;
private static volatile String baseHashStr;
private static volatile String lastContainerTagsHash;

private BaseHash() {}

public static void recalcBaseHash(String containerTagsHash) {
long hash = calc(containerTagsHash);
updateBaseHash(hash);
lastContainerTagsHash = containerTagsHash;
updateBaseHash(calc(containerTagsHash));
}

static void recalcBaseHash() {
updateBaseHash(calc(lastContainerTagsHash));
}

public static void updateBaseHash(long hash) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public static void addTag(String key, String value) {
Lazy.stringListForm = null;
Lazy.utf8ListForm = null;
}
BaseHash.recalcBaseHash();
}
}

Expand Down Expand Up @@ -242,6 +243,7 @@ public static void reset(Config config) {
empty();
enabled = config.isExperimentalPropagateProcessTagsEnabled();
Lazy.TAGS.putAll(Lazy.loadTags(config));
BaseHash.recalcBaseHash();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
package datadog.trace.api

import datadog.trace.test.util.DDSpecification

import static datadog.trace.api.config.GeneralConfig.ENV
import static datadog.trace.api.config.GeneralConfig.EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED
import static datadog.trace.api.config.GeneralConfig.PRIMARY_TAG
import static datadog.trace.api.config.GeneralConfig.SERVICE_NAME

import datadog.trace.test.util.DDSpecification

class BaseHashTest extends DDSpecification {


def setup() {
// start with fresh process tags
ProcessTags.reset()
}

def cleanup() {
// restore the default enablement
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true")
ProcessTags.reset()
}

def "Service used in hash calculation"() {
when:
def firstBaseHash = BaseHash.calc(null)
Expand Down Expand Up @@ -46,17 +58,54 @@ class BaseHashTest extends DDSpecification {
when:
def firstBaseHash = BaseHash.calc(null)

injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true")
ProcessTags.reset()
ProcessTags.addTag("000", "first")
def secondBaseHash = BaseHash.calc(null)

then:
firstBaseHash != secondBaseHash
assert ProcessTags.getTagsForSerialization().startsWithAny("000:first,")
cleanup:
}

def "addTag triggers BaseHash recalculation"() {
given:
BaseHash.recalcBaseHash("container-hash-1")
def hashBefore = BaseHash.getBaseHash()

when:
ProcessTags.addTag("cluster.name", "new-cluster")

then:
BaseHash.getBaseHash() != hashBefore
}

def "recalcBaseHash preserves last containerTagsHash across ProcessTags changes"() {
given:
def containerHash = "my-container-hash"
BaseHash.recalcBaseHash(containerHash)
def hashWithContainerTag = BaseHash.getBaseHash()

when: "a process tag is added"
ProcessTags.addTag("cluster.name", "new-cluster")

then: "hash differs from before the tag was added"
BaseHash.getBaseHash() != hashWithContainerTag

and: "hash equals a fresh calc with the same container hash"
BaseHash.getBaseHash() == BaseHash.calc(containerHash)
}

def "addTag does not recalculate BaseHash when ProcessTags disabled"() {
setup:
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "false")
ProcessTags.reset()
BaseHash.recalcBaseHash(null)
def hashBefore = BaseHash.getBaseHash()

when:
ProcessTags.addTag("cluster.name", "ignored")

then:
BaseHash.getBaseHash() == hashBefore
}

def "ContainerTagsHash used in hash calculation when provided"() {
Expand All @@ -76,10 +125,6 @@ class BaseHashTest extends DDSpecification {
assert secondBaseHash == firstBaseHash
}

cleanup:
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "false")
ProcessTags.reset()

where:
propagateTagsEnabled << [true, false]
}
Expand Down
Loading