From 6e47fc3a83c3ff6b7448d90911eb92d537a7ba73 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Thu, 26 Nov 2020 13:37:28 +0100 Subject: [PATCH 1/3] Support process.runtime.* resource attributes --- .../resources/ProcessRuntimeResource.java | 40 +++++++++++++++++++ ...entelemetry.sdk.resources.ResourceProvider | 1 + .../resources/ProcessRuntimeResourceTest.java | 37 +++++++++++++++++ .../sdk/resources/ResourceAttributes.java | 18 +++++++++ 4 files changed, 96 insertions(+) create mode 100644 sdk-extensions/resources/src/main/java/io/opentelemetry/sdk/extension/resources/ProcessRuntimeResource.java create mode 100644 sdk-extensions/resources/src/test/java/io/opentelemetry/sdk/extension/resources/ProcessRuntimeResourceTest.java 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 00000000000..0132392ccae --- /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 = + String.join( + " ", + 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 1c415cb8d51..7d953871a69 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 00000000000..e4381df6ef6 --- /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)).isNotNull(); + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_VERSION)).isNotNull(); + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_DESCRIPTION)).isNotNull(); + } + + @Test + void inDefault() { + // when + ReadableAttributes attributes = Resource.getDefault().getAttributes(); + + // then + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_NAME)).isNotNull(); + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_VERSION)).isNotNull(); + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_DESCRIPTION)).isNotNull(); + } +} 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 277a6996c89..6ef1db889ff 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. From 06127999fb8dda150beaca321421ce5579a81a14 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Thu, 26 Nov 2020 16:17:19 +0100 Subject: [PATCH 2/3] Code review comments --- .../extension/resources/ProcessRuntimeResource.java | 10 +++++----- .../resources/ProcessRuntimeResourceTest.java | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) 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 index 0132392ccae..398cfb3103a 100644 --- 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 @@ -20,11 +20,11 @@ protected Attributes getAttributes() { String name = System.getProperty("java.runtime.name"); String version = System.getProperty("java.runtime.version"); String description = - String.join( - " ", - System.getProperty("java.vm.vendor"), - System.getProperty("java.vm.name"), - System.getProperty("java.vm.version")); + System.getProperty("java.vm.vendor") + + " " + + System.getProperty("java.vm.name") + + " " + + System.getProperty("java.vm.version"); return Attributes.of( PROCESS_RUNTIME_NAME, 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 index e4381df6ef6..82a0a71df60 100644 --- 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 @@ -19,9 +19,9 @@ void shouldCreateRuntimeAttributes() { ReadableAttributes attributes = new ProcessRuntimeResource().getAttributes(); // then - assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_NAME)).isNotNull(); - assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_VERSION)).isNotNull(); - assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_DESCRIPTION)).isNotNull(); + 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 @@ -30,8 +30,8 @@ void inDefault() { ReadableAttributes attributes = Resource.getDefault().getAttributes(); // then - assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_NAME)).isNotNull(); - assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_VERSION)).isNotNull(); - assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_DESCRIPTION)).isNotNull(); + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_NAME)).isNotBlank(); + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_VERSION)).isNotBlank(); + assertThat(attributes.get(ResourceAttributes.PROCESS_RUNTIME_DESCRIPTION)).isNotBlank(); } } From d9ded933693503e31a98b3eaab6218aea59b014c Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Mon, 30 Nov 2020 17:17:57 +0100 Subject: [PATCH 3/3] Add process.runtime.* attributes to README.md --- sdk-extensions/resources/README.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/sdk-extensions/resources/README.md b/sdk-extensions/resources/README.md index 406544bcdca..a2e7da01c66 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