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
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void serviceInstanceIdFallback_defaultBehavior() {
assertThat(result.getAttribute(stringKey("service.name"))).isEqualTo("test-service");
assertThat(EntityUtil.getEntities(result))
.containsExactlyInAnyOrder(
telemetrySdkEntity(),
serviceEntity("test-service"),
Entity.builder(
"service.instance",
Expand All @@ -110,7 +111,8 @@ void serviceInstanceIdExplicitValuePreserved() {
(r, c) -> r);

assertThat(result.getAttribute(stringKey("service.instance.id"))).isEqualTo("my-custom-id-123");
assertThat(EntityUtil.getEntities(result)).containsExactly(serviceEntity("test-service"));
assertThat(EntityUtil.getEntities(result))
.containsExactlyInAnyOrder(telemetrySdkEntity(), serviceEntity("test-service"));
assertThat(EntityUtil.getUnassociatedAttributes(result))
.containsEntry(stringKey("service.instance.id"), "my-custom-id-123");
}
Expand All @@ -128,7 +130,8 @@ void serviceInstanceEntityOverridesFallback() {
(r, c) -> r);

assertThat(EntityUtil.getEntities(result))
.containsExactly(
.containsExactlyInAnyOrder(
telemetrySdkEntity(),
Entity.builder(
"service.instance", Attributes.of(stringKey("service.instance.id"), "custom"))
.build());
Expand Down Expand Up @@ -305,6 +308,13 @@ static Stream<Arguments> legacyEntityOverrides() {
configuredService.toBuilder().put("service.version", "1.0").build()));
}

private static Entity telemetrySdkEntity() {
return EntityUtil.getEntities(Resource.getDefault()).stream()
.filter(entity -> entity.getType().equals("telemetry.sdk"))
.findFirst()
.get();
}

private static Entity serviceEntity(String name) {
return Entity.builder("service", Attributes.of(stringKey("service.name"), name))
.setSchemaUrl("https://opentelemetry.io/schemas/1.40.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ void configureResource_ServiceNameFromEnvironment() {
(r, c) -> r);

assertThat(EntityUtil.getEntities(resource))
.containsExactly(
.containsExactlyInAnyOrder(
EntityUtil.getEntities(Resource.getDefault()).stream()
.filter(entity -> entity.getType().equals("telemetry.sdk"))
.findFirst()
.get(),
Entity.builder("service", Attributes.of(AttributeKey.stringKey("service.name"), "test"))
.setSchemaUrl("https://opentelemetry.io/schemas/1.40.0")
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,12 @@ void entityFiltering(
entityDetectorContext(EntityUtil.createResource(Collections.singletonList(host))));

if (!expectedId.equals(host.getId())) {
assertThat(EntityUtil.getEntities(resource)).isEmpty();
assertThat(EntityUtil.getEntities(resource))
.containsExactlyElementsOf(EntityUtil.getEntities(Resource.getDefault()));
} else {
assertThat(EntityUtil.getEntities(resource))
.containsAll(EntityUtil.getEntities(Resource.getDefault()))
.filteredOn(entity -> entity.getType().equals("host"))
.singleElement()
.satisfies(filtered -> {
assertThat(filtered.getId()).isEqualTo(expectedId);
Expand Down Expand Up @@ -258,7 +261,8 @@ void explicitAttributesOverrideEntities(boolean useAttributeList) {
model,
entityDetectorContext(
EntityUtil.createResource(Collections.singletonList(service))));
assertThat(EntityUtil.getEntities(resource)).isEmpty();
assertThat(EntityUtil.getEntities(resource))
.containsExactlyElementsOf(EntityUtil.getEntities(Resource.getDefault()));
assertThat(resource)
.isEqualTo(
Resource.getDefault().toBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ private SemConvConstants() {}

public static final String SERVICE_TYPE = "service";
public static final String SERVICE_INSTANCE_TYPE = "service.instance";
public static final String TELEMETRY_SDK_TYPE = "telemetry.sdk";

// Attributes

public static final AttributeKey<String> SERVICE_NAME = AttributeKey.stringKey("service.name");
public static final AttributeKey<String> SERVICE_INSTANCE_ID =
AttributeKey.stringKey("service.instance.id");
public static final AttributeKey<String> TELEMETRY_SDK_LANGUAGE =
AttributeKey.stringKey("telemetry.sdk.language");
public static final AttributeKey<String> TELEMETRY_SDK_NAME =
AttributeKey.stringKey("telemetry.sdk.name");
public static final AttributeKey<String> TELEMETRY_SDK_VERSION =
AttributeKey.stringKey("telemetry.sdk.version");

public static final AttributeKey<String> OTEL_COMPONENT_TYPE =
AttributeKey.stringKey("otel.component.type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.sdk.common.internal.OtelVersion;
import io.opentelemetry.sdk.common.internal.SemConvConstants;
import io.opentelemetry.sdk.resources.internal.AttributeCheckUtil;
import io.opentelemetry.sdk.resources.internal.Entity;
import io.opentelemetry.sdk.resources.internal.EntityUtil;
Expand All @@ -32,38 +33,37 @@
public abstract class Resource {
private static final Logger logger = Logger.getLogger(Resource.class.getName());

private static final AttributeKey<String> SERVICE_NAME = AttributeKey.stringKey("service.name");
private static final AttributeKey<String> TELEMETRY_SDK_LANGUAGE =
AttributeKey.stringKey("telemetry.sdk.language");
private static final AttributeKey<String> TELEMETRY_SDK_NAME =
AttributeKey.stringKey("telemetry.sdk.name");
private static final AttributeKey<String> TELEMETRY_SDK_VERSION =
AttributeKey.stringKey("telemetry.sdk.version");
private static final Resource EMPTY = create(Attributes.empty());
private static final Resource TELEMETRY_SDK;

/**
* The MANDATORY Resource instance contains the mandatory attributes that must be used if they are
* not provided by the Resource that is given to an SDK signal provider.
*/
private static final Resource MANDATORY =
create(Attributes.of(SERVICE_NAME, "unknown_service:java"));
create(Attributes.of(SemConvConstants.SERVICE_NAME, "unknown_service:java"));

static {
TELEMETRY_SDK =
create(
Attributes.builder()
.put(TELEMETRY_SDK_NAME, "opentelemetry")
.put(TELEMETRY_SDK_LANGUAGE, "java")
.put(TELEMETRY_SDK_VERSION, OtelVersion.VERSION)
.build());
}
private static final Resource TELEMETRY_SDK =
Comment thread
jack-berg marked this conversation as resolved.
Resolved
Comment thread
jack-berg marked this conversation as resolved.
Resolved
builder()
.addEntity(
Entity.builder(
SemConvConstants.TELEMETRY_SDK_TYPE,
Attributes.of(
SemConvConstants.TELEMETRY_SDK_NAME,
"opentelemetry",
SemConvConstants.TELEMETRY_SDK_LANGUAGE,
"java"))
.setDescription(
Attributes.of(SemConvConstants.TELEMETRY_SDK_VERSION, OtelVersion.VERSION))
.setSchemaUrl(SemConvConstants.SCHEMA_URL_V1_40_0)
.build())
.build();

private static final Resource DEFAULT = MANDATORY.merge(TELEMETRY_SDK);

/**
* Returns the default {@link Resource}. This resource contains the default attributes provided by
* the SDK.
* the SDK, with the telemetry SDK attributes associated with an experimental {@code
* telemetry.sdk} entity.
*
* @return a {@code Resource}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ void testMergeResources_entitySchemas() {

assertThat(pinned.getSchemaUrl()).isEqualTo("S1");
assertThat(Resource.empty().merge(pinned)).isEqualTo(pinned);
assertThat(Resource.getDefault().merge(explicit).getSchemaUrl()).isEqualTo("explicit");
assertThat(Resource.empty().merge(explicit)).isEqualTo(explicit);
assertThat(withoutEntitySchema.getSchemaUrl()).isNull();
assertThat(
Resource.create(Attributes.of(stringKey("a"), "1"), "S1")
Expand Down Expand Up @@ -855,6 +855,78 @@ void testDefaultResources() {
assertThat(resource.getAttribute(stringKey("telemetry.sdk.language"))).isEqualTo("java");
assertThat(resource.getAttribute(stringKey("telemetry.sdk.version")))
.isEqualTo(System.getProperty("otel.test.project-version"));
assertThat(resource.getEntities())
.containsExactly(
Entity.builder(
"telemetry.sdk",
Attributes.of(
stringKey("telemetry.sdk.name"),
"opentelemetry",
stringKey("telemetry.sdk.language"),
"java"))
.setDescription(
Attributes.of(
stringKey("telemetry.sdk.version"),
System.getProperty("otel.test.project-version")))
.setSchemaUrl("https://opentelemetry.io/schemas/1.40.0")
.build());
assertThat(resource.getUnassociatedAttributes())
.isEqualTo(Attributes.of(stringKey("service.name"), "unknown_service:java"));
assertThat(resource.getSchemaUrl()).isEqualTo("https://opentelemetry.io/schemas/1.40.0");
assertThat(resource.toBuilder().build()).isEqualTo(resource);
assertThat(Resource.empty().merge(resource)).isEqualTo(resource);
}

@ParameterizedTest
@MethodSource("defaultTelemetrySdkOverrides")
void testDefaultResources_telemetrySdkOverride(String key, String value) {
Resource resource =
Resource.getDefault().merge(Resource.create(Attributes.of(stringKey(key), value)));

assertThat(resource.getEntities()).isEmpty();
assertThat(resource.getAttributes())
.isEqualTo(Resource.getDefault().getAttributes().toBuilder().put(key, value).build());
assertThat(resource.getUnassociatedAttributes()).isEqualTo(resource.getAttributes());
}

static Stream<Arguments> defaultTelemetrySdkOverrides() {
return Stream.of(
Arguments.argumentSet("name", "telemetry.sdk.name", "custom-sdk"),
Arguments.argumentSet("language", "telemetry.sdk.language", "kotlin"),
Arguments.argumentSet("version", "telemetry.sdk.version", "custom-version"));
}

@ParameterizedTest
@MethodSource("defaultServiceOverrides")
void testDefaultResources_serviceOverride(Resource override, Resource expected) {
assertThat(Resource.getDefault().merge(override)).isEqualTo(expected);
assertThat(Resource.getDefault().toBuilder().putAll(override).build())
.isEqualTo(expected.toBuilder().build());
assertThat(logs.getEvents()).filteredOn(event -> event.getLevel().equals(WARN)).isEmpty();
}

static Stream<Arguments> defaultServiceOverrides() {
Resource defaults = Resource.getDefault();
Entity service =
Entity.builder("service", Attributes.of(stringKey("service.name"), "configured"))
.setSchemaUrl("https://opentelemetry.io/schemas/1.40.0")
.build();
Resource configuredService = Resource.builder().addEntity(service).build();
Resource withoutService =
defaults.toBuilder().removeIf(key -> key.getKey().equals("service.name")).build();
return Stream.of(
Arguments.argumentSet(
"unchanged name preserves fallback",
Resource.create(Attributes.of(stringKey("service.name"), "unknown_service:java")),
defaults),
Arguments.argumentSet(
"ordinary name overrides fallback",
Resource.create(Attributes.of(stringKey("service.name"), "configured")),
withoutService.toBuilder().put("service.name", "configured").build()),
Arguments.argumentSet(
"entity replaces fallback",
configuredService,
withoutService.toBuilder().addEntity(service).build()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import io.opentelemetry.context.Scope;
import io.opentelemetry.internal.testing.slf4j.SuppressLogger;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.data.LinkData;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.samplers.Sampler;
Expand All @@ -47,6 +48,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -1261,11 +1263,9 @@ void spanDataToString() {
+ "spanId=0000000000000000, "
+ "traceFlags=00, "
+ "traceState=ArrayBasedTraceState\\{entries=\\[]}, remote=false, valid=false}, "
+ "resource=Resource\\{schemaUrl=null, "
+ "entities=\\[\\], "
+ "attributes=\\{service.name=\"unknown_service:java\", "
+ "telemetry.sdk.language=\"java\", telemetry.sdk.name=\"opentelemetry\", "
+ "telemetry.sdk.version=\"\\d+.\\d+.\\d+(-rc.\\d+)?(-SNAPSHOT)?\"\\}\\}, "
+ "resource="
+ Pattern.quote(Resource.getDefault().toString())
+ ", "
+ "instrumentationScopeInfo=InstrumentationScopeInfo\\{"
+ "name=SpanBuilderSdkTest, version=null, schemaUrl=null, attributes=\\{}}, "
+ "name=span_name, "
Expand Down
Loading