Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
Signed-off-by: Gagan Juneja <gjjuneja@amazon.com>
  • Loading branch information
Gagan Juneja committed Aug 11, 2023
1 parent 407079a commit 707ac18
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* compatible open source license.
*/

package org.opensearch.telemetry.tracing.exporter;
package org.opensearch.telemetry.tracing;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
Expand All @@ -15,19 +15,19 @@
/**
* Converts {@link org.opensearch.telemetry.tracing.attributes.Attributes} to OTel {@link Attributes}
*/
public class OTelAttributesConverter {
final class OTelAttributesConverter {

/**
* Constructor.
*/
public OTelAttributesConverter() {}
OTelAttributesConverter() {}

/**
* Attribute converter.
* @param attributes attributes
* @return otel attributes.
*/
public Attributes convert(org.opensearch.telemetry.tracing.attributes.Attributes attributes) {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.io.Closeable;
import java.io.IOException;
import org.opensearch.telemetry.tracing.attributes.Attributes;
import org.opensearch.telemetry.tracing.exporter.OTelAttributesConverter;

/**
* OTel based Telemetry provider
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.tracing;

import io.opentelemetry.api.common.AttributeType;
import io.opentelemetry.api.internal.InternalAttributeKeyImpl;
import java.util.Map;
import org.opensearch.telemetry.tracing.attributes.Attributes;
import org.opensearch.test.OpenSearchTestCase;

public class OTelAttributesConverterTests extends OpenSearchTestCase {

private final OTelAttributesConverter converter = new OTelAttributesConverter();

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

public void testConverterEmptyAttributes() {
Attributes attributes = Attributes.EMPTY;
io.opentelemetry.api.common.Attributes otelAttributes = converter.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);
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");
Map<String, ?> attributeMap = attributes.getAttributesMap();
io.opentelemetry.api.common.Attributes otelAttributes = converter.convert(attributes);
assertEquals(4, otelAttributes.size());
otelAttributes.asMap().forEach(
(x,y) -> assertEquals(attributeMap.get(x.getKey()), y)
);
}
}

0 comments on commit 707ac18

Please sign in to comment.