Skip to content

Commit 74633af

Browse files
authored
Add process tags as list to remote config payload (#8705)
1 parent 65f56ae commit 74633af

File tree

4 files changed

+88
-11
lines changed

4 files changed

+88
-11
lines changed

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

+30-9
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
import datadog.trace.util.TraceUtils;
66
import java.nio.file.Path;
77
import java.nio.file.Paths;
8+
import java.util.Collections;
89
import java.util.LinkedHashMap;
10+
import java.util.List;
911
import java.util.Map;
1012
import java.util.stream.Collectors;
13+
import java.util.stream.Stream;
1114
import org.slf4j.Logger;
1215
import org.slf4j.LoggerFactory;
1316

@@ -18,6 +21,7 @@ public class ProcessTags {
1821
private static class Lazy {
1922
static final Map<String, String> TAGS = loadTags();
2023
static volatile UTF8BytesString serializedForm;
24+
static volatile List<String> listForm;
2125

2226
private static Map<String, String> loadTags() {
2327
Map<String, String> tags = new LinkedHashMap<>();
@@ -81,15 +85,17 @@ private static void fillJbossTags(Map<String, String> tags) {
8185
}
8286
}
8387

84-
static synchronized UTF8BytesString calculateSerializedForm() {
85-
if (serializedForm == null && !TAGS.isEmpty()) {
86-
serializedForm =
87-
UTF8BytesString.create(
88-
TAGS.entrySet().stream()
89-
.map(entry -> entry.getKey() + ":" + TraceUtils.normalizeTag(entry.getValue()))
90-
.collect(Collectors.joining(",")));
88+
static void calculate() {
89+
if (listForm != null || TAGS.isEmpty()) {
90+
return;
91+
}
92+
synchronized (Lazy.TAGS) {
93+
final Stream<String> tagStream =
94+
TAGS.entrySet().stream()
95+
.map(entry -> entry.getKey() + ":" + TraceUtils.normalizeTag(entry.getValue()));
96+
listForm = Collections.unmodifiableList(tagStream.collect(Collectors.toList()));
97+
serializedForm = UTF8BytesString.create(String.join(",", listForm));
9198
}
92-
return serializedForm;
9399
}
94100
}
95101

@@ -100,7 +106,20 @@ public static synchronized void addTag(String key, String value) {
100106
if (enabled) {
101107
Lazy.TAGS.put(key, value);
102108
Lazy.serializedForm = null;
109+
Lazy.listForm = null;
110+
}
111+
}
112+
113+
public static List<String> getTagsAsList() {
114+
if (!enabled) {
115+
return null;
116+
}
117+
final List<String> listForm = Lazy.listForm;
118+
if (listForm != null) {
119+
return listForm;
103120
}
121+
Lazy.calculate();
122+
return Lazy.listForm;
104123
}
105124

106125
public static UTF8BytesString getTagsForSerialization() {
@@ -111,13 +130,15 @@ public static UTF8BytesString getTagsForSerialization() {
111130
if (serializedForm != null) {
112131
return serializedForm;
113132
}
114-
return Lazy.calculateSerializedForm();
133+
Lazy.calculate();
134+
return Lazy.serializedForm;
115135
}
116136

117137
/** Visible for testing. */
118138
static void empty() {
119139
Lazy.TAGS.clear();
120140
Lazy.serializedForm = null;
141+
Lazy.listForm = null;
121142
}
122143

123144
/** Visible for testing. */

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

+7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class ProcessTagsForkedTest extends DDSpecification {
7272
ProcessTags.addTag("test", "value")
7373
then:
7474
assert ProcessTags.tagsForSerialization == null
75+
assert ProcessTags.tagsAsList == null
7576
}
7677

7778
def 'should lazily recalculate when a tag is added'() {
@@ -80,12 +81,18 @@ class ProcessTagsForkedTest extends DDSpecification {
8081
ProcessTags.reset()
8182
when:
8283
def processTags = ProcessTags.tagsForSerialization
84+
def tagsAsList = ProcessTags.tagsAsList
8385
then:
8486
assert ProcessTags.enabled
8587
assert processTags != null
88+
assert tagsAsList != null
89+
assert tagsAsList.size() > 0
8690
when:
8791
ProcessTags.addTag("test", "value")
8892
then:
8993
assert ProcessTags.tagsForSerialization.toString() == "$processTags,test:value"
94+
def size = ProcessTags.tagsAsList.size()
95+
assert size == tagsAsList.size() + 1
96+
assert ProcessTags.tagsAsList[size - 1] == "test:value"
9097
}
9198
}

remote-config/remote-config-core/src/main/java/datadog/remoteconfig/tuf/RemoteConfigRequest.java

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package datadog.remoteconfig.tuf;
22

33
import com.squareup.moshi.Json;
4+
import datadog.trace.api.ProcessTags;
45
import java.util.ArrayList;
56
import java.util.Collection;
67
import java.util.List;
@@ -26,7 +27,14 @@ public static RemoteConfigRequest newRequest(
2627

2728
ClientInfo.TracerInfo tracerInfo =
2829
new RemoteConfigRequest.ClientInfo.TracerInfo(
29-
runtimeId, tracerVersion, serviceName, extraServices, serviceEnv, serviceVersion, tags);
30+
runtimeId,
31+
tracerVersion,
32+
serviceName,
33+
extraServices,
34+
serviceEnv,
35+
serviceVersion,
36+
tags,
37+
ProcessTags.getTagsAsList());
3038

3139
ClientInfo clientInfo =
3240
new RemoteConfigRequest.ClientInfo(
@@ -174,21 +182,26 @@ public static class TracerInfo {
174182
@Json(name = "app_version")
175183
private final String serviceVersion;
176184

185+
@Json(name = "process_tags")
186+
private final List<String> processTags;
187+
177188
public TracerInfo(
178189
String runtimeId,
179190
String tracerVersion,
180191
String serviceName,
181192
List<String> extraServices,
182193
String serviceEnv,
183194
String serviceVersion,
184-
List<String> tags) {
195+
List<String> tags,
196+
List<String> processTags) {
185197
this.runtimeId = runtimeId;
186198
this.tracerVersion = tracerVersion;
187199
this.serviceName = serviceName;
188200
this.extraServices = extraServices;
189201
this.serviceEnv = serviceEnv;
190202
this.serviceVersion = serviceVersion;
191203
this.tags = tags;
204+
this.processTags = processTags;
192205
}
193206

194207
public String getServiceName() {
@@ -210,6 +223,10 @@ public String getServiceVersion() {
210223
public List<String> getTags() {
211224
return tags;
212225
}
226+
227+
public List<String> getProcessTags() {
228+
return processTags;
229+
}
213230
}
214231

215232
private static class AgentInfo {

remote-config/remote-config-core/src/test/groovy/datadog/remoteconfig/PollerRequestFactoryTest.groovy

+32
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package datadog.remoteconfig
22

3+
import com.squareup.moshi.Moshi
34
import datadog.remoteconfig.tuf.RemoteConfigRequest
5+
import datadog.trace.api.ProcessTags
46
import datadog.trace.bootstrap.instrumentation.api.Tags
57
import datadog.trace.test.util.DDSpecification
68
import datadog.trace.api.Config
79
import datadog.trace.api.remoteconfig.ServiceNameCollector
810

11+
import static datadog.trace.api.config.GeneralConfig.EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED
12+
913
class PollerRequestFactoryTest extends DDSpecification {
1014

1115
static final String TRACER_VERSION = "v1.2.3"
@@ -51,4 +55,32 @@ class PollerRequestFactoryTest extends DDSpecification {
5155
then:
5256
request.client.tracerInfo.extraServices.contains(extraService)
5357
}
58+
59+
void 'remote config provides process tags when enabled = #enabled'() {
60+
setup:
61+
if (enabled) {
62+
injectSysConfig(EXPERIMENTAL_PROPAGATE_PROCESS_TAGS_ENABLED, "true")
63+
}
64+
ProcessTags.reset()
65+
PollerRequestFactory factory = new PollerRequestFactory(Config.get(), TRACER_VERSION, CONTAINER_ID, ENTITY_ID, INVALID_REMOTE_CONFIG_URL, null)
66+
67+
when:
68+
def request = factory.buildRemoteConfigRequest( Collections.singletonList("ASM"), null, null, 0, ServiceNameCollector.get())
69+
def json = new Moshi.Builder().build().adapter(RemoteConfigRequest).toJson(request)
70+
then:
71+
def epName = request.client.tracerInfo.processTags.find {it =~ "entrypoint.name:.+"}
72+
def workingDir = request.client.tracerInfo.processTags.find {it =~ "entrypoint.workdir:.+"}
73+
74+
if (enabled) {
75+
assert workingDir != null
76+
assert epName != null
77+
assert json.contains('"process_tags":[')
78+
} else {
79+
assert workingDir == null
80+
assert epName == null
81+
assert !json.contains('"process_tags":[')
82+
}
83+
where:
84+
enabled << [true, false]
85+
}
5486
}

0 commit comments

Comments
 (0)