Skip to content

Commit 9c222dd

Browse files
committed
Add incubating EntityProvider API for specification work
1 parent ddf096d commit 9c222dd

File tree

12 files changed

+520
-0
lines changed

12 files changed

+520
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.extension.incubator.entities;
7+
8+
import io.opentelemetry.api.common.Attributes;
9+
import javax.annotation.Nullable;
10+
import javax.annotation.concurrent.Immutable;
11+
12+
/**
13+
* Entity represents an object of interest associated with produced telemetry: traces, metrics or
14+
* logs.
15+
*
16+
* <p>For example, telemetry produced using OpenTelemetry SDK is normally associated with a Service
17+
* entity. Similarly, OpenTelemetry defines system metrics for a host. The Host is the entity we
18+
* want to associate metrics with in this case.
19+
*
20+
* <p>Entities may be also associated with produced telemetry indirectly. For example a service that
21+
* produces telemetry is also related with a process in which the service runs, so we say that the
22+
* Service entity is related to the Process entity. The process normally also runs on a host, so we
23+
* say that the Process entity is related to the Host entity.
24+
*/
25+
@Immutable
26+
public interface Entity {
27+
/**
28+
* Returns the entity type string of this entity. Must not be null.
29+
*
30+
* @return the entity type.
31+
*/
32+
String getType();
33+
34+
/**
35+
* Returns a map of attributes that identify the entity.
36+
*
37+
* @return a map of attributes.
38+
*/
39+
Attributes getId();
40+
41+
/**
42+
* Returns a map of attributes that describe the entity.
43+
*
44+
* @return a map of attributes.
45+
*/
46+
Attributes getDescription();
47+
48+
/**
49+
* Returns the URL of the OpenTelemetry schema used by this resource. May be null if this entity
50+
* does not abide by schema conventions (i.e. is custom).
51+
*
52+
* @return An OpenTelemetry schema URL.
53+
* @since 1.4.0
54+
*/
55+
@Nullable
56+
String getSchemaUrl();
57+
58+
/**
59+
* Returns a new {@link EntityBuilder} instance populated with the data of this {@link Entity}.
60+
*/
61+
EntityBuilder toBuilder();
62+
63+
/**
64+
* Returns a new {@link EntityBuilder} instance for creating arbitrary {@link Entity}.
65+
*
66+
* @param entityType the entity type string of this entity.
67+
*/
68+
public static EntityBuilder builder(String entityType) {
69+
return PassthroughEntity.builder(entityType);
70+
}
71+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.extension.incubator.entities;
7+
8+
import io.opentelemetry.api.common.Attributes;
9+
import io.opentelemetry.api.common.AttributesBuilder;
10+
import java.util.function.Consumer;
11+
12+
/**
13+
* A builder of {@link Entity} that allows to add identifying or descriptive {@link Attributes}, as
14+
* well as type and schema_url.
15+
*/
16+
public interface EntityBuilder {
17+
/**
18+
* Assign an OpenTelemetry schema URL to the resulting Entity.
19+
*
20+
* @param schemaUrl The URL of the OpenTelemetry schema being used to create this Entity.
21+
* @return this
22+
*/
23+
EntityBuilder setSchemaUrl(String schemaUrl);
24+
25+
/**
26+
* Modify the descriptive attributes of this Entity.
27+
*
28+
* @param f A thunk which manipulates descriptive attributes.
29+
* @return this
30+
*/
31+
EntityBuilder withDescription(Consumer<AttributesBuilder> f);
32+
33+
/**
34+
* Modify the identifying attributes of this Entity.
35+
*
36+
* @param f A thunk which manipulates identifying attributes.
37+
* @return this
38+
*/
39+
EntityBuilder withId(Consumer<AttributesBuilder> f);
40+
41+
/** Create the {@link Entity} from this. */
42+
Entity build();
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.extension.incubator.entities;
7+
8+
import java.util.Collection;
9+
10+
/**
11+
* The Entity detector in the SDK is responsible for detecting possible entities that could identify
12+
* the SDK (called "associated entities"). For Example, if the SDK is running in a kubernetes pod,
13+
* it may provide an Entity for that pod.
14+
*/
15+
public interface EntityDetector {
16+
/**
17+
* Discovers {@link Entity} and their current attributes.
18+
*
19+
* @return a list of discovered entities.
20+
*/
21+
public Collection<Entity> detect();
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.extension.incubator.entities;
7+
8+
import io.opentelemetry.sdk.resources.Resource;
9+
10+
/**
11+
* A provider of {@link Resource} for this SDK.
12+
*
13+
* <p>{@code EntityProvider} is responsible for:
14+
*
15+
* <p>- Detecting Entities using registered detectors. - Providing thread-safe access to the current
16+
* resource.
17+
*
18+
* <p>The future of this class may include the ability to add/remove {@link Entity} objects or
19+
* re-run detectors.
20+
*/
21+
public interface EntityProvider {
22+
/** the current {@link Resource} detected. */
23+
public Resource getResource();
24+
25+
/** A builder of {@link EntityProvider}. */
26+
public static EntityProviderBuilder builder() {
27+
return new SdkEntityProviderBuilder();
28+
}
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.extension.incubator.entities;
7+
8+
import io.opentelemetry.sdk.resources.Resource;
9+
10+
/** Builder of {@link EntityProvider}. */
11+
public interface EntityProviderBuilder {
12+
/** Adds an entity detector, which will detect {@link Entity}s to place in the resource. */
13+
EntityProviderBuilder addDetector(EntityDetector detector);
14+
15+
/**
16+
* Adds a discovered resource to include in resolving the SDK's resource.
17+
*
18+
* @deprecated Use {@link #addDetector(EntityDetector)}.
19+
*/
20+
@Deprecated
21+
EntityProviderBuilder addDetectedResource(Resource resource);
22+
23+
/** Sets whether or not default entity detectors will be included. */
24+
EntityProviderBuilder includeDefaults(boolean include);
25+
26+
/**
27+
* Returns the SDK entity provider which uses these detectors.
28+
*
29+
* @return the EntityProvider.
30+
*/
31+
EntityProvider build();
32+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.extension.incubator.entities;
7+
8+
import io.opentelemetry.api.common.Attributes;
9+
import javax.annotation.Nullable;
10+
11+
final class PassthroughEntity implements Entity {
12+
private final io.opentelemetry.sdk.resources.internal.Entity entity;
13+
14+
PassthroughEntity(io.opentelemetry.sdk.resources.internal.Entity entity) {
15+
this.entity = entity;
16+
}
17+
18+
io.opentelemetry.sdk.resources.internal.Entity getPassthrough() {
19+
return entity;
20+
}
21+
22+
@Override
23+
public String getType() {
24+
return entity.getType();
25+
}
26+
27+
@Override
28+
public Attributes getId() {
29+
return entity.getId();
30+
}
31+
32+
@Override
33+
public Attributes getDescription() {
34+
return entity.getDescription();
35+
}
36+
37+
@Override
38+
@Nullable
39+
public String getSchemaUrl() {
40+
return entity.getSchemaUrl();
41+
}
42+
43+
@Override
44+
public EntityBuilder toBuilder() {
45+
return new PassthroughEntityBuilder(entity.toBuilder());
46+
}
47+
48+
static EntityBuilder builder(String entityType) {
49+
return new PassthroughEntityBuilder(
50+
io.opentelemetry.sdk.resources.internal.Entity.builder(entityType));
51+
}
52+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.extension.incubator.entities;
7+
8+
import io.opentelemetry.api.common.AttributesBuilder;
9+
import java.util.function.Consumer;
10+
11+
final class PassthroughEntityBuilder implements EntityBuilder {
12+
private final io.opentelemetry.sdk.resources.internal.EntityBuilder builder;
13+
14+
PassthroughEntityBuilder(io.opentelemetry.sdk.resources.internal.EntityBuilder builder) {
15+
this.builder = builder;
16+
}
17+
18+
@Override
19+
public EntityBuilder setSchemaUrl(String schemaUrl) {
20+
builder.setSchemaUrl(schemaUrl);
21+
return this;
22+
}
23+
24+
@Override
25+
public EntityBuilder withDescription(Consumer<AttributesBuilder> f) {
26+
builder.withDescription(f);
27+
return this;
28+
}
29+
30+
@Override
31+
public EntityBuilder withId(Consumer<AttributesBuilder> f) {
32+
builder.withId(f);
33+
return this;
34+
}
35+
36+
@Override
37+
public Entity build() {
38+
return new PassthroughEntity(builder.build());
39+
}
40+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.extension.incubator.entities;
7+
8+
import io.opentelemetry.sdk.resources.Resource;
9+
10+
/**
11+
* Instance of {@link EntityProvider}.
12+
*
13+
* <p>This class doesn't do much now, but will expand in responsibilities.
14+
*/
15+
class SdkEntityProvider implements EntityProvider {
16+
private final Resource resource;
17+
18+
SdkEntityProvider(Resource resource) {
19+
this.resource = resource;
20+
}
21+
22+
@Override
23+
public Resource getResource() {
24+
return resource;
25+
}
26+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.sdk.extension.incubator.entities;
7+
8+
import io.opentelemetry.sdk.extension.incubator.entities.detectors.ServiceDetector;
9+
import io.opentelemetry.sdk.extension.incubator.entities.detectors.TelemetrySdkDetector;
10+
import io.opentelemetry.sdk.resources.Resource;
11+
import io.opentelemetry.sdk.resources.internal.EntityUtil;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import java.util.stream.Collectors;
15+
16+
class SdkEntityProviderBuilder implements EntityProviderBuilder {
17+
private final List<EntityDetector> entityDetectors = new ArrayList<>();
18+
private final List<Resource> detectedResources = new ArrayList<>();
19+
private boolean includeDefaults = true;
20+
21+
@Override
22+
public EntityProviderBuilder addDetector(EntityDetector detector) {
23+
this.entityDetectors.add(detector);
24+
return this;
25+
}
26+
27+
@Override
28+
@Deprecated
29+
public EntityProviderBuilder addDetectedResource(Resource resource) {
30+
this.detectedResources.add(resource);
31+
return this;
32+
}
33+
34+
private final Resource mergeDetectedAndRaw() {
35+
if (includeDefaults) {
36+
entityDetectors.add(new ServiceDetector());
37+
entityDetectors.add(new TelemetrySdkDetector());
38+
}
39+
Resource result = Resource.empty();
40+
for (EntityDetector detector : entityDetectors) {
41+
result =
42+
result.merge(
43+
EntityUtil.addAllEntity(
44+
Resource.builder(),
45+
detector.detect().stream()
46+
.map(e -> ((PassthroughEntity) e).getPassthrough())
47+
.collect(Collectors.toList()))
48+
.build());
49+
}
50+
for (Resource next : detectedResources) {
51+
result = result.merge(next);
52+
}
53+
return result;
54+
}
55+
56+
@Override
57+
public EntityProvider build() {
58+
return new SdkEntityProvider(mergeDetectedAndRaw());
59+
}
60+
61+
@Override
62+
public EntityProviderBuilder includeDefaults(boolean include) {
63+
this.includeDefaults = include;
64+
return this;
65+
}
66+
}

0 commit comments

Comments
 (0)