Skip to content

Commit ebf9557

Browse files
committed
Further simplify the API and SDK for entities
- Remove `Entity` and only expose `EntityBuilder` similar to how we do `LogRecord`s. - Have methods on `Resource` which determine if you're attaching the entity, simplify "remove" or just ignore for now.
1 parent 0711350 commit ebf9557

File tree

11 files changed

+67
-255
lines changed

11 files changed

+67
-255
lines changed

api/incubator/src/main/java/io/opentelemetry/api/incubator/entities/Entity.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

api/incubator/src/main/java/io/opentelemetry/api/incubator/entities/EntityBuilder.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,20 @@
1010
import java.util.function.Consumer;
1111

1212
/**
13-
* A builder of {@link Entity} that allows to add identifying or descriptive {@link Attributes}, as
14-
* well as type and schema_url.
13+
* A builder of an Entity that allows to add identifying or descriptive {@link Attributes}, as well
14+
* as type and schema_url.
15+
*
16+
* <p>Entity represents an object of interest associated with produced telemetry: traces, metrics or
17+
* logs.
18+
*
19+
* <p>For example, telemetry produced using OpenTelemetry SDK is normally associated with a Service
20+
* entity. Similarly, OpenTelemetry defines system metrics for a host. The Host is the entity we
21+
* want to associate metrics with in this case.
22+
*
23+
* <p>Entities may be also associated with produced telemetry indirectly. For example a service that
24+
* produces telemetry is also related with a process in which the service runs, so we say that the
25+
* Service entity is related to the Process entity. The process normally also runs on a host, so we
26+
* say that the Process entity is related to the Host entity.
1527
*/
1628
public interface EntityBuilder {
1729
/**
@@ -38,6 +50,6 @@ public interface EntityBuilder {
3850
*/
3951
EntityBuilder withId(Consumer<AttributesBuilder> f);
4052

41-
/** Create the {@link Entity} from this. */
42-
Entity build();
53+
/** Emits the current entity. */
54+
void emit();
4355
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/entities/NoopEntity.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

api/incubator/src/main/java/io/opentelemetry/api/incubator/entities/NoopEntityBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,5 @@ public EntityBuilder withId(Consumer<AttributesBuilder> f) {
2525
}
2626

2727
@Override
28-
public Entity build() {
29-
return new NoopEntity();
30-
}
28+
public void emit() {}
3129
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/entities/NoopResource.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,12 @@
88
final class NoopResource implements Resource {
99

1010
@Override
11-
public boolean addOrUpdate(Entity e) {
11+
public boolean removeEntity(String entityType) {
1212
return false;
1313
}
1414

1515
@Override
16-
public boolean removeEntity(Entity e) {
17-
return false;
18-
}
19-
20-
@Override
21-
public EntityBuilder createEntity(String entityType) {
16+
public EntityBuilder attachEntity(String entityType) {
2217
return new NoopEntityBuilder();
2318
}
2419
}

api/incubator/src/main/java/io/opentelemetry/api/incubator/entities/Resource.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,19 @@
77

88
/** The active resource for which Telemetry is being generated. */
99
public interface Resource {
10-
/**
11-
* Adds an {@link Entity} to this resource.
12-
*
13-
* <p>If the entity already exists, this updates the description.
14-
*
15-
* @param e The entity
16-
* @return true if the entity was added or updated, false if there was a conflict.
17-
*/
18-
public boolean addOrUpdate(Entity e);
19-
2010
/**
2111
* Removes an {@link Entity} from this resource.
2212
*
23-
* @param e The entity
13+
* @param entityType the type of entity to remove.
2414
* @return true if entity was found and removed.
2515
*/
26-
public boolean removeEntity(Entity e);
16+
public boolean removeEntity(String entityType);
2717

2818
/**
29-
* Returns a builder that can construct an {@link Entity}.
19+
* Attaches an entity to the current {@link Resource}.
3020
*
3121
* @param entityType The type of the entity.
32-
* @return A builder that can construct an entity.
22+
* @return A builder that can construct an {@link Entity}.
3323
*/
34-
public EntityBuilder createEntity(String entityType);
24+
public EntityBuilder attachEntity(String entityType);
3525
}

sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/entities/PassthroughEntity.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/entities/PassthroughEntityBuilder.java renamed to sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/entities/SdkEntityBuilder.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
package io.opentelemetry.sdk.extension.incubator.entities;
77

88
import io.opentelemetry.api.common.AttributesBuilder;
9-
import io.opentelemetry.api.incubator.entities.Entity;
109
import io.opentelemetry.api.incubator.entities.EntityBuilder;
10+
import io.opentelemetry.sdk.resources.internal.Entity;
1111
import java.util.function.Consumer;
1212

13-
final class PassthroughEntityBuilder implements EntityBuilder {
13+
final class SdkEntityBuilder implements EntityBuilder {
1414
private final io.opentelemetry.sdk.resources.internal.EntityBuilder builder;
15+
private final Consumer<Entity> emitter;
1516

16-
PassthroughEntityBuilder(io.opentelemetry.sdk.resources.internal.EntityBuilder builder) {
17-
this.builder = builder;
17+
SdkEntityBuilder(String entityType, Consumer<Entity> emitter) {
18+
this.builder = Entity.builder(entityType);
19+
this.emitter = emitter;
1820
}
1921

2022
@Override
@@ -36,7 +38,7 @@ public EntityBuilder withId(Consumer<AttributesBuilder> f) {
3638
}
3739

3840
@Override
39-
public Entity build() {
40-
return new PassthroughEntity(builder.build());
41+
public void emit() {
42+
emitter.accept(builder.build());
4143
}
4244
}

sdk-extensions/incubator/src/main/java/io/opentelemetry/sdk/extension/incubator/entities/SdkResource.java

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@
55

66
package io.opentelemetry.sdk.extension.incubator.entities;
77

8-
import io.opentelemetry.api.incubator.entities.Entity;
98
import io.opentelemetry.api.incubator.entities.EntityBuilder;
109
import io.opentelemetry.sdk.resources.Resource;
11-
import io.opentelemetry.sdk.resources.ResourceBuilder;
10+
import io.opentelemetry.sdk.resources.internal.Entity;
1211
import io.opentelemetry.sdk.resources.internal.EntityUtil;
13-
import java.util.ArrayList;
14-
import java.util.Collection;
1512
import java.util.concurrent.atomic.AtomicReference;
1613

1714
final class SdkResource implements io.opentelemetry.api.incubator.entities.Resource {
1815

1916
private final AtomicReference<Resource> resource = new AtomicReference<>(Resource.empty());
20-
2117
private final Object writeLock = new Object();
2218

2319
/** Returns the currently active resource. */
@@ -31,48 +27,21 @@ public Resource getResource() {
3127
}
3228

3329
@Override
34-
public boolean addOrUpdate(Entity e) {
35-
synchronized (writeLock) {
36-
Resource current = getResource();
37-
Resource next =
38-
EntityUtil.addEntity(Resource.builder(), ((PassthroughEntity) e).getPassthrough())
39-
.build();
40-
Resource result = current.merge(next);
41-
// We rely on a volatile read to force this to be written.
42-
resource.lazySet(result);
43-
return EntityUtil.getEntities(result).stream()
44-
.anyMatch(r -> r.getType().equals(e.getType()) && r.getId().equals(e.getId()));
45-
}
30+
public boolean removeEntity(String entityType) {
31+
// TODO Auto-generated method stub
32+
throw new UnsupportedOperationException("Unimplemented method 'removeEntity'");
4633
}
4734

48-
@Override
49-
public boolean removeEntity(Entity e) {
35+
void attachEntityOnEmit(Entity e) {
5036
synchronized (writeLock) {
5137
Resource current = getResource();
52-
Collection<io.opentelemetry.sdk.resources.internal.Entity> previousEntities =
53-
EntityUtil.getEntities(current);
54-
Collection<io.opentelemetry.sdk.resources.internal.Entity> currentEntities =
55-
new ArrayList<>(previousEntities);
56-
boolean result = currentEntities.removeIf(c -> c.getType().equals(e.getType()));
57-
ResourceBuilder rb = Resource.builder();
58-
EntityUtil.addAllEntity(rb, currentEntities);
59-
rb.putAll(
60-
current.getAttributes().toBuilder()
61-
.removeIf(
62-
key ->
63-
previousEntities.stream()
64-
.anyMatch(
65-
pe ->
66-
pe.getId().asMap().containsKey(key)
67-
|| pe.getDescription().asMap().containsKey(key)))
68-
.build());
69-
resource.lazySet(rb.build());
70-
return result;
38+
Resource next = EntityUtil.addEntity(Resource.builder(), e).build();
39+
resource.lazySet(current.merge(next));
7140
}
7241
}
7342

7443
@Override
75-
public EntityBuilder createEntity(String entityType) {
76-
return PassthroughEntity.builder(entityType);
44+
public EntityBuilder attachEntity(String entityType) {
45+
return new SdkEntityBuilder(entityType, this::attachEntityOnEmit);
7746
}
7847
}

0 commit comments

Comments
 (0)