Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds attributes to startSpan #9199

Merged
merged 9 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Refactor code
Signed-off-by: Gagan Juneja <gjjuneja@amazon.com>
  • Loading branch information
Gagan Juneja committed Aug 11, 2023
commit ea297f7841b16d4361df7b81b432ac2f7480df9b
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ final class OTelAttributesConverter {
/**
* Constructor.
*/
OTelAttributesConverter() {}
private OTelAttributesConverter() {}

/**
* Attribute converter.
* @param attributes attributes
* @return otel attributes.
*/
Attributes convert(org.opensearch.telemetry.tracing.attributes.Attributes attributes) {
static Attributes convert(org.opensearch.telemetry.tracing.attributes.Attributes attributes) {
AttributesBuilder attributesBuilder = Attributes.builder();
if (attributes != null) {
attributes.getAttributesMap().forEach((x, y) -> addSpanAttribute(x, y, attributesBuilder));
}
return attributesBuilder.build();
}

private void addSpanAttribute(String key, Object value, AttributesBuilder attributesBuilder) {
private static void addSpanAttribute(String key, Object value, AttributesBuilder attributesBuilder) {
if (value instanceof Boolean) {
attributesBuilder.put(key, (Boolean) value);
} else if (value instanceof Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
public class OTelTracingTelemetry implements TracingTelemetry {

private static final Logger logger = LogManager.getLogger(OTelTracingTelemetry.class);
private static final OTelAttributesConverter OTEL_ATTRIBUTES_CONVERTER = new OTelAttributesConverter();

private final OpenTelemetry openTelemetry;
private final io.opentelemetry.api.trace.Tracer otelTracer;

Expand Down Expand Up @@ -58,7 +56,7 @@ public TracingContextPropagator getContextPropagator() {
}

private Span createOtelSpan(String spanName, Span parentSpan, Attributes attributes) {
io.opentelemetry.api.trace.Span otelSpan = otelSpan(spanName, parentSpan, OTEL_ATTRIBUTES_CONVERTER.convert(attributes));
io.opentelemetry.api.trace.Span otelSpan = otelSpan(spanName, parentSpan, OTelAttributesConverter.convert(attributes));
return new OTelSpan(spanName, otelSpan, parentSpan);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@

public class OTelAttributesConverterTests extends OpenSearchTestCase {

private final OTelAttributesConverter converter = new OTelAttributesConverter();

public void testConverterNullAttributes() {
io.opentelemetry.api.common.Attributes otelAttributes = converter.convert(null);
io.opentelemetry.api.common.Attributes otelAttributes = OTelAttributesConverter.convert(null);
assertEquals(0, otelAttributes.size());
}

public void testConverterEmptyAttributes() {
Attributes attributes = Attributes.EMPTY;
io.opentelemetry.api.common.Attributes otelAttributes = converter.convert(null);
io.opentelemetry.api.common.Attributes otelAttributes = OTelAttributesConverter.convert(null);
assertEquals(0, otelAttributes.size());
}

public void testConverterSingleAttributes() {
Attributes attributes = Attributes.create().addAttribute("key1", "value");
io.opentelemetry.api.common.Attributes otelAttributes = converter.convert(attributes);
io.opentelemetry.api.common.Attributes otelAttributes = OTelAttributesConverter.convert(attributes);
assertEquals(1, otelAttributes.size());
assertEquals("value", otelAttributes.get(InternalAttributeKeyImpl.create("key1", AttributeType.STRING)));
}

public void testConverterMultipleAttributes() {
Attributes attributes = Attributes.create().addAttribute("key1", 1l).addAttribute("key2", 1.0).addAttribute("key3", true).addAttribute("key4", "value4");
Attributes attributes = Attributes.create()
.addAttribute("key1", 1l)
.addAttribute("key2", 1.0)
.addAttribute("key3", true)
.addAttribute("key4", "value4");
Map<String, ?> attributeMap = attributes.getAttributesMap();
io.opentelemetry.api.common.Attributes otelAttributes = converter.convert(attributes);
io.opentelemetry.api.common.Attributes otelAttributes = OTelAttributesConverter.convert(attributes);
assertEquals(4, otelAttributes.size());
otelAttributes.asMap().forEach(
(x,y) -> assertEquals(attributeMap.get(x.getKey()), y)
);
otelAttributes.asMap().forEach((x, y) -> assertEquals(attributeMap.get(x.getKey()), y));
}
}