Skip to content

Commit ce0e66d

Browse files
committed
Move Entity and EntityBuilder to be pure interfaces
1 parent 5460d5e commit ce0e66d

4 files changed

Lines changed: 124 additions & 62 deletions

File tree

sdk/common/src/main/java/io/opentelemetry/sdk/resources/internal/Entity.java

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.opentelemetry.sdk.resources.internal;
77

8-
import com.google.auto.value.AutoValue;
98
import io.opentelemetry.api.common.Attributes;
109
import javax.annotation.Nullable;
1110
import javax.annotation.concurrent.Immutable;
@@ -27,8 +26,7 @@
2726
* at any time.
2827
*/
2928
@Immutable
30-
@AutoValue
31-
public abstract class Entity {
29+
public interface Entity {
3230
/**
3331
* Returns the entity type string of this entity. Must not be null.
3432
*
@@ -60,38 +58,17 @@ public abstract class Entity {
6058
@Nullable
6159
public abstract String getSchemaUrl();
6260

63-
/**
64-
* Returns a {@link Entity}.
65-
*
66-
* @param entityType the entity type string of this entity.
67-
* @param id a map of attributes that identify the entity.
68-
* @param description a map of attributes that describe the entity.
69-
* @return a {@code Entity}.
70-
* @throws NullPointerException if {@code id} or {@code description} is null.
71-
* @throws IllegalArgumentException if entityType string, attribute key or attribute value is not
72-
* a valid printable ASCII string or exceed {@link AttributeCheckUtil#MAX_LENGTH} characters.
73-
*/
74-
static final Entity create(
75-
String entityType, Attributes id, Attributes description, @Nullable String schemaUrl) {
76-
AttributeCheckUtil.isValid(entityType);
77-
AttributeCheckUtil.checkAttributes(id);
78-
AttributeCheckUtil.checkAttributes(description);
79-
return new AutoValue_Entity(entityType, id, description, schemaUrl);
80-
}
81-
8261
/**
8362
* Returns a new {@link EntityBuilder} instance populated with the data of this {@link Entity}.
8463
*/
85-
public final EntityBuilder toBuilder() {
86-
return new EntityBuilder(this);
87-
}
64+
EntityBuilder toBuilder();
8865

8966
/**
9067
* Returns a new {@link EntityBuilder} instance for creating arbitrary {@link Entity}.
9168
*
9269
* @param entityType the entity type string of this entity.
9370
*/
94-
public static final EntityBuilder builder(String entityType) {
95-
return new EntityBuilder(entityType);
71+
public static EntityBuilder builder(String entityType) {
72+
return SdkEntity.builder(entityType);
9673
}
9774
}

sdk/common/src/main/java/io/opentelemetry/sdk/resources/internal/EntityBuilder.java

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import io.opentelemetry.api.common.Attributes;
99
import io.opentelemetry.api.common.AttributesBuilder;
1010
import java.util.function.Consumer;
11-
import javax.annotation.Nullable;
1211

1312
/**
1413
* A builder of {@link Entity} that allows to add identifying or descriptive {@link Attributes}, as
@@ -17,60 +16,31 @@
1716
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
1817
* at any time.
1918
*/
20-
public final class EntityBuilder {
21-
private final String entityType;
22-
private final AttributesBuilder descriptionBuilder;
23-
private final AttributesBuilder idBuilder;
24-
@Nullable private String schemaUrl;
25-
26-
EntityBuilder(String entityType) {
27-
this.entityType = entityType;
28-
this.descriptionBuilder = Attributes.builder();
29-
this.idBuilder = Attributes.builder();
30-
}
31-
32-
EntityBuilder(Entity seed) {
33-
this.entityType = seed.getType();
34-
this.schemaUrl = seed.getSchemaUrl();
35-
this.idBuilder = seed.getId().toBuilder();
36-
this.descriptionBuilder = seed.getDescription().toBuilder();
37-
}
38-
19+
public interface EntityBuilder {
3920
/**
4021
* Assign an OpenTelemetry schema URL to the resulting Entity.
4122
*
4223
* @param schemaUrl The URL of the OpenTelemetry schema being used to create this Entity.
4324
* @return this
4425
*/
45-
public EntityBuilder setSchemaUrl(String schemaUrl) {
46-
this.schemaUrl = schemaUrl;
47-
return this;
48-
}
26+
EntityBuilder setSchemaUrl(String schemaUrl);
4927

5028
/**
5129
* Modify the descriptive attributes of this Entity.
5230
*
5331
* @param f A thunk which manipulates descriptive attributes.
5432
* @return this
5533
*/
56-
public EntityBuilder withDescription(Consumer<AttributesBuilder> f) {
57-
f.accept(this.descriptionBuilder);
58-
return this;
59-
}
34+
EntityBuilder withDescription(Consumer<AttributesBuilder> f);
6035

6136
/**
6237
* Modify the identifying attributes of this Entity.
6338
*
6439
* @param f A thunk which manipulates identifying attributes.
6540
* @return this
6641
*/
67-
public EntityBuilder withId(Consumer<AttributesBuilder> f) {
68-
f.accept(this.idBuilder);
69-
return this;
70-
}
42+
EntityBuilder withId(Consumer<AttributesBuilder> f);
7143

7244
/** Create the {@link Entity} from this. */
73-
public Entity build() {
74-
return Entity.create(entityType, idBuilder.build(), descriptionBuilder.build(), schemaUrl);
75-
}
45+
Entity build();
7646
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.resources.internal;
7+
8+
import com.google.auto.value.AutoValue;
9+
import io.opentelemetry.api.common.Attributes;
10+
import javax.annotation.Nullable;
11+
import javax.annotation.concurrent.Immutable;
12+
13+
/**
14+
* SDK implementation of Entity.
15+
*
16+
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
17+
* at any time.
18+
*/
19+
@Immutable
20+
@AutoValue
21+
public abstract class SdkEntity implements Entity {
22+
/**
23+
* Returns a {@link Entity}.
24+
*
25+
* @param entityType the entity type string of this entity.
26+
* @param id a map of attributes that identify the entity.
27+
* @param description a map of attributes that describe the entity.
28+
* @return a {@code Entity}.
29+
* @throws NullPointerException if {@code id} or {@code description} is null.
30+
* @throws IllegalArgumentException if entityType string, attribute key or attribute value is not
31+
* a valid printable ASCII string or exceed {@link AttributeCheckUtil#MAX_LENGTH} characters.
32+
*/
33+
static final Entity create(
34+
String entityType, Attributes id, Attributes description, @Nullable String schemaUrl) {
35+
AttributeCheckUtil.isValid(entityType);
36+
AttributeCheckUtil.checkAttributes(id);
37+
AttributeCheckUtil.checkAttributes(description);
38+
return new AutoValue_SdkEntity(entityType, id, description, schemaUrl);
39+
}
40+
41+
@Override
42+
public final EntityBuilder toBuilder() {
43+
return new SdkEntityBuilder(this);
44+
}
45+
46+
/**
47+
* Returns a new {@link EntityBuilder} instance for creating arbitrary {@link Entity}.
48+
*
49+
* @param entityType the entity type string of this entity.
50+
*/
51+
public static final EntityBuilder builder(String entityType) {
52+
return new SdkEntityBuilder(entityType);
53+
}
54+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.resources.internal;
7+
8+
import io.opentelemetry.api.common.Attributes;
9+
import io.opentelemetry.api.common.AttributesBuilder;
10+
import java.util.function.Consumer;
11+
import javax.annotation.Nullable;
12+
13+
/**
14+
* A builder of {@link Entity} that allows to add identifying or descriptive {@link Attributes}, as
15+
* well as type and schema_url.
16+
*
17+
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
18+
* at any time.
19+
*/
20+
public final class SdkEntityBuilder implements EntityBuilder {
21+
private final String entityType;
22+
private final AttributesBuilder descriptionBuilder;
23+
private final AttributesBuilder idBuilder;
24+
@Nullable private String schemaUrl;
25+
26+
SdkEntityBuilder(String entityType) {
27+
this.entityType = entityType;
28+
this.descriptionBuilder = Attributes.builder();
29+
this.idBuilder = Attributes.builder();
30+
}
31+
32+
SdkEntityBuilder(Entity seed) {
33+
this.entityType = seed.getType();
34+
this.schemaUrl = seed.getSchemaUrl();
35+
this.idBuilder = seed.getId().toBuilder();
36+
this.descriptionBuilder = seed.getDescription().toBuilder();
37+
}
38+
39+
@Override
40+
public EntityBuilder setSchemaUrl(String schemaUrl) {
41+
this.schemaUrl = schemaUrl;
42+
return this;
43+
}
44+
45+
@Override
46+
public EntityBuilder withDescription(Consumer<AttributesBuilder> f) {
47+
f.accept(this.descriptionBuilder);
48+
return this;
49+
}
50+
51+
@Override
52+
public EntityBuilder withId(Consumer<AttributesBuilder> f) {
53+
f.accept(this.idBuilder);
54+
return this;
55+
}
56+
57+
@Override
58+
public Entity build() {
59+
return SdkEntity.create(entityType, idBuilder.build(), descriptionBuilder.build(), schemaUrl);
60+
}
61+
}

0 commit comments

Comments
 (0)