Skip to content
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
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 @@ -307,6 +310,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 -> {
Expand Down Expand Up @@ -259,7 +262,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,40 @@
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);
// TODO(jack-berg): Explicitly null out schema url to avoid avoid assigning schemaUrl to default

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude: possible problem: Schema URL nulling on DEFAULT

The nulling works on DEFAULT itself. The attributes and the telemetry.sdk entity stay intact, the resource schemaUrl is null, and the entity keeps 1.40.0. Resource.merge() also behaves as before, because it uses buildWithSchemaUrl and a null base takes the other side's URL. The nulling does not survive toBuilder().build(): ResourceBuilder derives 1.40.0 from the single entity. So the common Resource.getDefault().toBuilder()...build() pattern now yields a resource with schemaUrl 1.40.0, which was null before. If that resource is later merged with a resource that has a different schemaUrl, the result is null and an info log is written. The PR's own test asserts this behavior. The TODO says the goal is to avoid assigning a schemaUrl, so the reviewer should decide whether this is acceptable or whether toBuilder() should pin a null URL.

Evidence:

  • sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java:61-69 DEFAULT is built with buildWithSchemaUrl(null), so the resource schemaUrl is null while the entity keeps 1.40.0.
  • sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceBuilder.java:283 build() derives the schema URL from the entities when none is set explicitly, so a single 1.40.0 entity gives 1.40.0.
  • sdk/common/src/main/java/io/opentelemetry/sdk/resources/Resource.java:270-278 toBuilder() copies a schemaUrl only if it is non-null, so a null one gets re-derived on build().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the request to preserve a null resource schema URL through toBuilder().build()? The comment describes the behavior but leaves the proposed change unclear.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's more of a "is this intended or not" question, rather than a suggestion of a fix.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the special nulling. It didn't survive rebuilding and made schema handling depend on the construction path. We already associate a schema url elsewhere we construct entity aware resources (e.g. ServiceInstanceIdResourceProvider and others) and I couldn't think of a good reason why the default reason would be different, especially given the fragility of whether or not its included post rebuilding. The default now carries 1.40.0 consistently. We accept normal schema-conflict behavior here: merging with another schema clears the resource-level URL but preserves attributes and entity schema URLs.

// resource which previously had none. Revisit when entities stabilize.
private static final Resource DEFAULT =
builder().putAll(MANDATORY).putAll(TELEMETRY_SDK).buildWithSchemaUrl(null);

/**
* 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 @@ -857,6 +857,83 @@ 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()).isNull();
Resource rebuilt = resource.toBuilder().build();
assertThat(rebuilt.getSchemaUrl()).isEqualTo("https://opentelemetry.io/schemas/1.40.0");
assertThat(rebuilt.getAttributes()).isEqualTo(resource.getAttributes());
assertThat(rebuilt.getEntities()).containsExactlyElementsOf(resource.getEntities());
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"))
.buildWithSchemaUrl(null);
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").buildWithSchemaUrl(null)),
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 @@ -35,6 +35,7 @@
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.Scope;
import io.opentelemetry.internal.testing.slf4j.SuppressLogger;
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 @@ -44,6 +45,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 javax.annotation.Nullable;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -1135,11 +1137,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