diff --git a/sdk-extensions/resources/README.md b/sdk-extensions/resources/README.md index 406544bcdc..a2e7da01c6 100644 --- a/sdk-extensions/resources/README.md +++ b/sdk-extensions/resources/README.md @@ -19,13 +19,24 @@ Implemented attributes: Implementation: `io.opentelemetry.sdk.extension.resources.ProcessResource` -Specification: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/resource/semantic_conventions/process.md +Specification: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/resource/semantic_conventions/process.md#process Implemented attributes: - `process.pid` - `process.executable.path` (note, we assume the `java` binary is located in the `bin` subfolder of `JAVA_HOME`) - `process.command_line` (note this includes all system properties and arguments when running) +### Java Runtime + +Implementation: `io.opentelemetry.sdk.extension.resources.ProcessRuntimeResource` + +Specification: https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/resource/semantic_conventions/process.md#process-runtimes + +Implemented attributes: +- `process.runtime.name` +- `process.runtime.version` +- `process.runtime.description` + ## Platforms This package currently does not run on Android. It has been verified on OpenJDK and should work on diff --git a/sdk-extensions/resources/src/main/java/io/opentelemetry/sdk/extension/resources/ProcessRuntimeResource.java b/sdk-extensions/resources/src/main/java/io/opentelemetry/sdk/extension/resources/ProcessRuntimeResource.java new file mode 100644 index 0000000000..398cfb3103 --- /dev/null +++ b/sdk-extensions/resources/src/main/java/io/opentelemetry/sdk/extension/resources/ProcessRuntimeResource.java @@ -0,0 +1,40 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.extension.resources; + +import static io.opentelemetry.sdk.resources.ResourceAttributes.PROCESS_RUNTIME_DESCRIPTION; +import static io.opentelemetry.sdk.resources.ResourceAttributes.PROCESS_RUNTIME_NAME; +import static io.opentelemetry.sdk.resources.ResourceAttributes.PROCESS_RUNTIME_VERSION; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.sdk.resources.ResourceProvider; + +/** {@link ResourceProvider} which provides information about the Java runtime. */ +public final class ProcessRuntimeResource extends ResourceProvider { + @Override + protected Attributes getAttributes() { + try { + String name = System.getProperty("java.runtime.name"); + String version = System.getProperty("java.runtime.version"); + String description = + System.getProperty("java.vm.vendor") + + " " + + System.getProperty("java.vm.name") + + " " + + System.getProperty("java.vm.version"); + + return Attributes.of( + PROCESS_RUNTIME_NAME, + name, + PROCESS_RUNTIME_VERSION, + version, + PROCESS_RUNTIME_DESCRIPTION, + description); + } catch (SecurityException ignored) { + return Attributes.empty(); + } + } +} diff --git a/sdk-extensions/resources/src/main/resources/META-INF/services/io.opentelemetry.sdk.resources.ResourceProvider b/sdk-extensions/resources/src/main/resources/META-INF/services/io.opentelemetry.sdk.resources.ResourceProvider index 1c415cb8d5..7d953871a6 100644 --- a/sdk-extensions/resources/src/main/resources/META-INF/services/io.opentelemetry.sdk.resources.ResourceProvider +++ b/sdk-extensions/resources/src/main/resources/META-INF/services/io.opentelemetry.sdk.resources.ResourceProvider @@ -1,2 +1,3 @@ io.opentelemetry.sdk.extension.resources.OsResource io.opentelemetry.sdk.extension.resources.ProcessResource +io.opentelemetry.sdk.extension.resources.ProcessRuntimeResource diff --git a/sdk-extensions/resources/src/test/java/io/opentelemetry/sdk/extension/resources/ProcessRuntimeResourceTest.java b/sdk-extensions/resources/src/test/java/io/opentelemetry/sdk/extension/resources/ProcessRuntimeResourceTest.java new file mode 100644 index 0000000000..82a0a71df6 --- /dev/null +++ b/sdk-extensions/resources/src/test/java/io/opentelemetry/sdk/extension/resources/ProcessRuntimeResourceTest.java @@ -0,0 +1,37 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.extension.resources; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.common.ReadableAttributes; +import io.opentelemetry.sdk.resources.Resource; +import io.opentelemetry.sdk.resources.ResourceAttributes; +import org.junit.jupiter.api.Test; + +class ProcessRuntimeResourceTest { + @Test + void shouldCreateRuntimeAttributes() { + // when + ReadableAttributes attributes = new ProcessRuntimeResource().getAttributes(); + + // then + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_NAME)).isNotBlank(); + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_VERSION)).isNotBlank(); + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_DESCRIPTION)).isNotBlank(); + } + + @Test + void inDefault() { + // when + ReadableAttributes attributes = Resource.getDefault().getAttributes(); + + // then + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_NAME)).isNotBlank(); + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_VERSION)).isNotBlank(); + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_DESCRIPTION)).isNotBlank(); + } +} diff --git a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java index 277a6996c8..6ef1db889f 100644 --- a/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java +++ b/sdk/common/src/main/java/io/opentelemetry/sdk/resources/ResourceAttributes.java @@ -51,6 +51,24 @@ public final class ResourceAttributes { /** The username of the user that owns the process. */ public static final AttributeKey PROCESS_OWNER = stringKey("process.owner"); + // TODO: these should be removed once SemanticAttributes contain process.runtime.* + /** + * The name of the runtime of this process. For compiled native binaries, this SHOULD be the name + * of the compiler. + */ + public static final AttributeKey PROCESS_RUNTIME_NAME = stringKey("process.runtime.name"); + /** + * The version of the runtime of this process, as returned by the runtime without modification. + */ + public static final AttributeKey PROCESS_RUNTIME_VERSION = + stringKey("process.runtime.version"); + /** + * An additional description about the runtime of the process, for example a specific vendor + * customization of the runtime environment. + */ + public static final AttributeKey PROCESS_RUNTIME_DESCRIPTION = + stringKey("process.runtime.description"); + /** * Logical name of the service. MUST be the same for all instances of horizontally scaled * services.