Skip to content

Commit d37fbd5

Browse files
authored
Merge pull request #7399 from DataDog/mtoff/otel-sdk-attributes
Move span Attributes implementation outside of SpanLink class
2 parents b4db634 + 96dcbbe commit d37fbd5

File tree

12 files changed

+105
-97
lines changed

12 files changed

+105
-97
lines changed

dd-java-agent/agent-otel/otel-shim/src/main/java/datadog/opentelemetry/shim/trace/OtelConventions.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
import static java.util.Locale.ROOT;
1616

1717
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
18+
import datadog.trace.bootstrap.instrumentation.api.SpanAttributes;
1819
import datadog.trace.bootstrap.instrumentation.api.Tags;
1920
import io.opentelemetry.api.common.AttributeKey;
21+
import io.opentelemetry.api.common.Attributes;
2022
import io.opentelemetry.api.trace.SpanKind;
23+
import java.util.List;
2124
import javax.annotation.Nullable;
2225
import org.slf4j.Logger;
2326
import org.slf4j.LoggerFactory;
@@ -224,4 +227,46 @@ private static String getStringAttribute(AgentSpan span, String key) {
224227
}
225228
return (String) tag;
226229
}
230+
231+
public static AgentSpan.Attributes convertAttributes(Attributes attributes) {
232+
if (attributes.isEmpty()) {
233+
return SpanAttributes.EMPTY;
234+
}
235+
SpanAttributes.Builder builder = SpanAttributes.builder();
236+
attributes.forEach(
237+
(attributeKey, value) -> {
238+
String key = attributeKey.getKey();
239+
switch (attributeKey.getType()) {
240+
case STRING:
241+
builder.put(key, (String) value);
242+
break;
243+
case BOOLEAN:
244+
builder.put(key, (boolean) value);
245+
break;
246+
case LONG:
247+
builder.put(key, (long) value);
248+
break;
249+
case DOUBLE:
250+
builder.put(key, (double) value);
251+
break;
252+
case STRING_ARRAY:
253+
//noinspection unchecked
254+
builder.putStringArray(key, (List<String>) value);
255+
break;
256+
case BOOLEAN_ARRAY:
257+
//noinspection unchecked
258+
builder.putBooleanArray(key, (List<Boolean>) value);
259+
break;
260+
case LONG_ARRAY:
261+
//noinspection unchecked
262+
builder.putLongArray(key, (List<Long>) value);
263+
break;
264+
case DOUBLE_ARRAY:
265+
//noinspection unchecked
266+
builder.putDoubleArray(key, (List<Double>) value);
267+
break;
268+
}
269+
});
270+
return builder.build();
271+
}
227272
}
Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package datadog.opentelemetry.shim.trace;
22

3+
import static datadog.opentelemetry.shim.trace.OtelConventions.convertAttributes;
4+
35
import datadog.opentelemetry.shim.context.propagation.TraceStateHelper;
46
import datadog.trace.api.DDSpanId;
57
import datadog.trace.api.DDTraceId;
68
import datadog.trace.bootstrap.instrumentation.api.SpanLink;
7-
import datadog.trace.bootstrap.instrumentation.api.SpanLinkAttributes;
89
import io.opentelemetry.api.trace.SpanContext;
9-
import java.util.List;
1010

1111
public class OtelSpanLink extends SpanLink {
1212
public OtelSpanLink(SpanContext spanContext) {
@@ -21,46 +21,4 @@ public OtelSpanLink(SpanContext spanContext, io.opentelemetry.api.common.Attribu
2121
TraceStateHelper.encodeHeader(spanContext.getTraceState()),
2222
convertAttributes(attributes));
2323
}
24-
25-
private static Attributes convertAttributes(io.opentelemetry.api.common.Attributes attributes) {
26-
if (attributes.isEmpty()) {
27-
return SpanLinkAttributes.EMPTY;
28-
}
29-
SpanLinkAttributes.Builder builder = SpanLinkAttributes.builder();
30-
attributes.forEach(
31-
(attributeKey, value) -> {
32-
String key = attributeKey.getKey();
33-
switch (attributeKey.getType()) {
34-
case STRING:
35-
builder.put(key, (String) value);
36-
break;
37-
case BOOLEAN:
38-
builder.put(key, (boolean) value);
39-
break;
40-
case LONG:
41-
builder.put(key, (long) value);
42-
break;
43-
case DOUBLE:
44-
builder.put(key, (double) value);
45-
break;
46-
case STRING_ARRAY:
47-
//noinspection unchecked
48-
builder.putStringArray(key, (List<String>) value);
49-
break;
50-
case BOOLEAN_ARRAY:
51-
//noinspection unchecked
52-
builder.putBooleanArray(key, (List<Boolean>) value);
53-
break;
54-
case LONG_ARRAY:
55-
//noinspection unchecked
56-
builder.putLongArray(key, (List<Long>) value);
57-
break;
58-
case DOUBLE_ARRAY:
59-
//noinspection unchecked
60-
builder.putDoubleArray(key, (List<Double>) value);
61-
break;
62-
}
63-
});
64-
return builder.build();
65-
}
6624
}

dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/OpenTelemetryInstrumentation.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public String[] helperClassNames() {
7474
"datadog.opentelemetry.shim.trace.OtelSpanBuilder$1",
7575
"datadog.opentelemetry.shim.trace.OtelSpanContext",
7676
"datadog.opentelemetry.shim.trace.OtelSpanLink",
77-
"datadog.opentelemetry.shim.trace.OtelSpanLink$1",
7877
"datadog.opentelemetry.shim.trace.OtelTracer",
7978
"datadog.opentelemetry.shim.trace.OtelTracerBuilder",
8079
"datadog.opentelemetry.shim.trace.OtelTracerProvider",

dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextInstrumentation.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public String[] helperClassNames() {
6868
"datadog.opentelemetry.shim.trace.OtelSpanBuilder$1",
6969
"datadog.opentelemetry.shim.trace.OtelSpanContext",
7070
"datadog.opentelemetry.shim.trace.OtelSpanLink",
71-
"datadog.opentelemetry.shim.trace.OtelSpanLink$1",
7271
"datadog.opentelemetry.shim.trace.OtelTracer",
7372
"datadog.opentelemetry.shim.trace.OtelTracerBuilder",
7473
"datadog.opentelemetry.shim.trace.OtelTracerProvider",

dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextStorageInstrumentation.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public String[] helperClassNames() {
6969
"datadog.opentelemetry.shim.trace.OtelSpanBuilder$1",
7070
"datadog.opentelemetry.shim.trace.OtelSpanContext",
7171
"datadog.opentelemetry.shim.trace.OtelSpanLink",
72-
"datadog.opentelemetry.shim.trace.OtelSpanLink$1",
7372
"datadog.opentelemetry.shim.trace.OtelTracer",
7473
"datadog.opentelemetry.shim.trace.OtelTracerBuilder",
7574
"datadog.opentelemetry.shim.trace.OtelTracerProvider",

dd-trace-core/src/main/java/datadog/trace/core/DDSpanLink.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package datadog.trace.core;
22

3-
import static datadog.trace.bootstrap.instrumentation.api.SpanLinkAttributes.EMPTY;
3+
import static datadog.trace.bootstrap.instrumentation.api.SpanAttributes.EMPTY;
44

55
import com.squareup.moshi.FromJson;
66
import com.squareup.moshi.JsonAdapter;
77
import com.squareup.moshi.Moshi;
88
import com.squareup.moshi.ToJson;
99
import datadog.trace.api.DDSpanId;
1010
import datadog.trace.api.DDTraceId;
11+
import datadog.trace.bootstrap.instrumentation.api.AgentSpan.Attributes;
1112
import datadog.trace.bootstrap.instrumentation.api.AgentSpanLink;
13+
import datadog.trace.bootstrap.instrumentation.api.SpanAttributes;
1214
import datadog.trace.bootstrap.instrumentation.api.SpanLink;
13-
import datadog.trace.bootstrap.instrumentation.api.SpanLinkAttributes;
1415
import datadog.trace.core.propagation.ExtractedContext;
1516
import datadog.trace.core.propagation.PropagationTags;
1617
import java.util.List;
@@ -130,7 +131,7 @@ AgentSpanLink fromSpanLinkJson(SpanLinkJson json) {
130131
DDSpanId.fromHex(json.span_id),
131132
json.flags,
132133
json.tracestate,
133-
SpanLinkAttributes.fromMap(json.attributes));
134+
SpanAttributes.fromMap(json.attributes));
134135
}
135136
}
136137

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import datadog.trace.api.DDSpanId
55
import datadog.trace.api.DDTraceId
66
import datadog.trace.api.DynamicConfig
77
import datadog.trace.bootstrap.instrumentation.api.ContextVisitors
8+
import datadog.trace.bootstrap.instrumentation.api.SpanAttributes
89
import datadog.trace.bootstrap.instrumentation.api.SpanLink
9-
import datadog.trace.bootstrap.instrumentation.api.SpanLinkAttributes
1010
import datadog.trace.common.writer.ListWriter
1111
import datadog.trace.core.propagation.ExtractedContext
1212
import datadog.trace.core.propagation.W3CHttpCodec
@@ -19,7 +19,7 @@ import java.util.stream.IntStream
1919
import static datadog.trace.api.DDTags.SPAN_LINKS
2020
import static datadog.trace.bootstrap.instrumentation.api.AgentSpanLink.DEFAULT_FLAGS
2121
import static datadog.trace.bootstrap.instrumentation.api.AgentSpanLink.SAMPLED_FLAG
22-
import static datadog.trace.bootstrap.instrumentation.api.SpanLinkAttributes.EMPTY
22+
import static datadog.trace.bootstrap.instrumentation.api.SpanAttributes.EMPTY
2323
import static datadog.trace.core.propagation.W3CHttpCodec.TRACE_PARENT_KEY
2424
import static datadog.trace.core.propagation.W3CHttpCodec.TRACE_STATE_KEY
2525
import static java.util.stream.Collectors.toList
@@ -172,7 +172,7 @@ class DDSpanLinkTest extends DDCoreSpecification {
172172
DDSpanId.fromHex(String.format("123456789abc%04d", index)),
173173
index % 2 == 0 ? SAMPLED_FLAG : DEFAULT_FLAGS,
174174
"",
175-
SpanLinkAttributes.fromMap(attributes))
175+
SpanAttributes.fromMap(attributes))
176176
}
177177

178178
def assertLink(SpanLink expected, DDSpanLink.SpanLinkJson actual) {

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentSpan.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,4 +228,20 @@ interface Extracted extends Context {
228228
String getCustomIpHeader();
229229
}
230230
}
231+
232+
interface Attributes {
233+
/**
234+
* Gets the attributes as an immutable map.
235+
*
236+
* @return The attributes as an immutable map.
237+
*/
238+
Map<String, String> asMap();
239+
240+
/**
241+
* Checks whether the attributes are empty.
242+
*
243+
* @return {@code true} if the attributes are empty, {@code false} otherwise.
244+
*/
245+
boolean isEmpty();
246+
}
231247
}

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/AgentSpanLink.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package datadog.trace.bootstrap.instrumentation.api;
22

33
import datadog.trace.api.DDTraceId;
4-
import java.util.Map;
4+
import datadog.trace.bootstrap.instrumentation.api.AgentSpan.Attributes;
55

66
/**
77
* This interface describes a link to another span. The linked span could be part of the same trace
@@ -51,20 +51,4 @@ public interface AgentSpanLink {
5151
* @return The link attributes.
5252
*/
5353
Attributes attributes();
54-
55-
interface Attributes {
56-
/**
57-
* Gets the attributes as an immutable map.
58-
*
59-
* @return The attributes as an immutable map.
60-
*/
61-
Map<String, String> asMap();
62-
63-
/**
64-
* Checks whether the attributes are empty.
65-
*
66-
* @return {@code true} if the attributes are empty, {@code false} otherwise.
67-
*/
68-
boolean isEmpty();
69-
}
7054
}

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/SpanLinkAttributes.java renamed to internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/SpanAttributes.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@
22

33
import static java.util.Objects.requireNonNull;
44

5+
import datadog.trace.bootstrap.instrumentation.api.AgentSpan.Attributes;
56
import java.util.Collections;
67
import java.util.HashMap;
78
import java.util.List;
89
import java.util.Map;
910

10-
/** This class is a base implementation of {@link AgentSpanLink.Attributes}. */
11-
public class SpanLinkAttributes implements AgentSpanLink.Attributes {
12-
/** An empty span links attributes. */
13-
public static final AgentSpanLink.Attributes EMPTY =
14-
new SpanLinkAttributes(Collections.emptyMap());
11+
/** This class is a base implementation of {@link Attributes}. */
12+
public class SpanAttributes implements Attributes {
13+
/** Represent an empty attributes. */
14+
public static final Attributes EMPTY = new SpanAttributes(Collections.emptyMap());
1515

1616
private final Map<String, String> attributes;
1717

18-
protected SpanLinkAttributes(Map<String, String> attributes) {
18+
protected SpanAttributes(Map<String, String> attributes) {
1919
this.attributes = attributes;
2020
}
2121

2222
/**
23-
* Gets a builder to create span link attributes.
23+
* Gets a builder to create attributes.
2424
*
25-
* @return A builder to create span link attributes.
25+
* @return A builder to create attributes.
2626
*/
2727
public static Builder builder() {
2828
return new Builder();
2929
}
3030

3131
/**
32-
* Create span link attributes from its map representation.
32+
* Create attributes from its map representation.
3333
*
34-
* @param map A map representing the span link attributes.
35-
* @return The related span link attributes.
34+
* @param map A map representing the attributes.
35+
* @return The related attributes.
3636
*/
37-
public static SpanLinkAttributes fromMap(Map<String, String> map) {
38-
return new SpanLinkAttributes(new HashMap<>(map));
37+
public static SpanAttributes fromMap(Map<String, String> map) {
38+
return new SpanAttributes(new HashMap<>(map));
3939
}
4040

4141
@Override
@@ -50,7 +50,7 @@ public boolean isEmpty() {
5050

5151
@Override
5252
public String toString() {
53-
return "SpanLinkAttributes{" + this.attributes + '}';
53+
return "SpanAttributes{" + this.attributes + '}';
5454
}
5555

5656
public static class Builder {
@@ -115,8 +115,8 @@ protected <T> Builder putArray(String key, List<T> array) {
115115
return this;
116116
}
117117

118-
public AgentSpanLink.Attributes build() {
119-
return new SpanLinkAttributes(this.attributes);
118+
public Attributes build() {
119+
return new SpanAttributes(this.attributes);
120120
}
121121
}
122122
}

0 commit comments

Comments
 (0)