1556 change resourceconstants to resourceattributes (#1590)

* renamed ResourceConstants to ResourceAttributes, and change to use attributesetter pattern

* tidy up javadoc

* tidy up javadoc
This commit is contained in:
jarebudev 2020-08-26 05:08:44 +01:00 committed by GitHub
parent 1175997dd1
commit 20669f48b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 237 additions and 195 deletions

View File

@ -24,7 +24,7 @@ import io.opentelemetry.common.ReadableKeyValuePairs.KeyValueConsumer;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.common.export.CompletableResultCode;
import io.opentelemetry.sdk.common.export.ConfigBuilder;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.data.SpanData.Event;
import io.opentelemetry.sdk.trace.export.SpanExporter;
@ -187,7 +187,7 @@ public final class ZipkinSpanExporter implements SpanExporter {
ReadableAttributes resourceAttributes = spanData.getResource().getAttributes();
// use the service.name from the Resource, if it's been set.
AttributeValue serviceNameValue = resourceAttributes.get(ResourceConstants.SERVICE_NAME);
AttributeValue serviceNameValue = resourceAttributes.get(ResourceAttributes.SERVICE_NAME.key());
if (serviceNameValue == null) {
return localEndpoint;
}
@ -311,7 +311,7 @@ public final class ZipkinSpanExporter implements SpanExporter {
* consistent. Many use a name from service discovery.
*
* <p>Note: this value, will be superseded by the value of {@link
* io.opentelemetry.sdk.resources.ResourceConstants#SERVICE_NAME} if it has been set in the
* io.opentelemetry.sdk.resources.ResourceAttributes#SERVICE_NAME} if it has been set in the
* {@link io.opentelemetry.sdk.resources.Resource} associated with the Tracer that created the
* spans.
*
@ -320,7 +320,7 @@ public final class ZipkinSpanExporter implements SpanExporter {
* @param serviceName The service name. It defaults to "unknown".
* @return this.
* @see io.opentelemetry.sdk.resources.Resource
* @see io.opentelemetry.sdk.resources.ResourceConstants
* @see io.opentelemetry.sdk.resources.ResourceAttributes
* @since 0.4.0
*/
public Builder setServiceName(String serviceName) {

View File

@ -28,7 +28,7 @@ import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.common.export.CompletableResultCode;
import io.opentelemetry.sdk.common.export.ConfigBuilder;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import io.opentelemetry.sdk.trace.TestSpanData;
import io.opentelemetry.sdk.trace.data.EventImpl;
import io.opentelemetry.sdk.trace.data.SpanData;
@ -126,7 +126,8 @@ class ZipkinSpanExporterTest {
final Resource resource =
Resource.create(
Attributes.of(
ResourceConstants.SERVICE_NAME, stringAttributeValue("super-zipkin-service")));
ResourceAttributes.SERVICE_NAME.key(),
stringAttributeValue("super-zipkin-service")));
SpanData data = buildStandardSpan().setResource(resource).build();
Endpoint expectedEndpoint = Endpoint.newBuilder().serviceName("super-zipkin-service").build();

View File

@ -0,0 +1,154 @@
/*
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentelemetry.sdk.resources;
import io.opentelemetry.trace.attributes.LongAttributeSetter;
import io.opentelemetry.trace.attributes.StringAttributeSetter;
/**
* Provides constants for resource semantic conventions defined by the OpenTelemetry specification.
*
* @see <a
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/resource/semantic_conventions/README.md">Resource
* Conventions</a>
*/
public final class ResourceAttributes {
/** The operating system type, such as {@code "WINDOWS"}, {@code "DARWIN"}, {@code "LINUX"}. */
public static final StringAttributeSetter OS_NAME = StringAttributeSetter.create("os.name");
/**
* Human readable information about the OS version, e.g. {@code "Microsoft Windows [Version
* 10.0.18363.778]"}, {@code "Ubuntu 18.04.1 LTS"}.
*/
public static final StringAttributeSetter OS_DESCRIPTION =
StringAttributeSetter.create("os.description");
/** Process identifier (PID). */
public static final LongAttributeSetter PROCESS_PID = LongAttributeSetter.create("process.pid");
/** The name of the process executable. */
public static final StringAttributeSetter PROCESS_EXECUTABLE_NAME =
StringAttributeSetter.create("process.executable.name");
/** The full path to the process executable. */
public static final StringAttributeSetter PROCESS_EXECUTABLE_PATH =
StringAttributeSetter.create("process.executable.path");
/** The command used to launch the process (i.e. the command name). */
public static final StringAttributeSetter PROCESS_COMMAND =
StringAttributeSetter.create("process.command");
/**
* The full command used to launch the process. The value can be either a list of strings
* representing the ordered list of arguments, or a single string representing the full command.
*/
public static final StringAttributeSetter PROCESS_COMMAND_LINE =
StringAttributeSetter.create("process.command_line");
/** The username of the user that owns the process. */
public static final StringAttributeSetter PROCESS_OWNER =
StringAttributeSetter.create("process.owner");
/**
* Logical name of the service. MUST be the same for all instances of horizontally scaled
* services.
*/
public static final StringAttributeSetter SERVICE_NAME =
StringAttributeSetter.create("service.name");
/**
* A namespace for `service.name`. A string value having a meaning that helps to distinguish a
* group of services,
*/
public static final StringAttributeSetter SERVICE_NAMESPACE =
StringAttributeSetter.create("service.namespace");
/**
* The string ID of the service instance. MUST be unique for each instance of the same
* `service.namespace,service.name` pair.
*/
public static final StringAttributeSetter SERVICE_INSTANCE =
StringAttributeSetter.create("service.instance.id");
/** The version string of the service API or implementation. */
public static final StringAttributeSetter SERVICE_VERSION =
StringAttributeSetter.create("service.version");
/** The name of the telemetry library. */
public static final StringAttributeSetter LIBRARY_NAME =
StringAttributeSetter.create("library.name");
/** The language of telemetry library and of the code instrumented with it. */
public static final StringAttributeSetter LIBRARY_LANGUAGE =
StringAttributeSetter.create("library.language");
/** The version string of the library. */
public static final StringAttributeSetter LIBRARY_VERSION =
StringAttributeSetter.create("library.version");
/** Container name. */
public static final StringAttributeSetter CONTAINER_NAME =
StringAttributeSetter.create("container.name");
/** Container id. */
public static final StringAttributeSetter CONTAINER_ID =
StringAttributeSetter.create("container.id");
/** Name of the image the container was built on. */
public static final StringAttributeSetter CONTAINER_IMAGE_NAME =
StringAttributeSetter.create("container.image.name");
/** Container image tag. */
public static final StringAttributeSetter CONTAINER_IMAGE_TAG =
StringAttributeSetter.create("container.image.tag");
/** The name of the cluster that the pod is running in. */
public static final StringAttributeSetter K8S_CLUSTER =
StringAttributeSetter.create("k8s.cluster.name");
/** The name of the namespace that the pod is running in. */
public static final StringAttributeSetter K8S_NAMESPACE =
StringAttributeSetter.create("k8s.namespace.name");
/** The name of the pod. */
public static final StringAttributeSetter K8S_POD = StringAttributeSetter.create("k8s.pod.name");
/** The name of the deployment. */
public static final StringAttributeSetter K8S_DEPLOYMENT =
StringAttributeSetter.create("k8s.deployment.name");
/** Hostname of the host. It contains what the `hostname` command returns on the host machine. */
public static final StringAttributeSetter HOST_HOSTNAME =
StringAttributeSetter.create("host.hostname");
/** Unique host id. For Cloud this must be the instance_id assigned by the cloud provider. */
public static final StringAttributeSetter HOST_ID = StringAttributeSetter.create("host.id");
/**
* Name of the host. It may contain what `hostname` returns on Unix systems, the fully qualified,
* or a name specified by the user.
*/
public static final StringAttributeSetter HOST_NAME = StringAttributeSetter.create("host.name");
/** Type of host. For Cloud this must be the machine type. */
public static final StringAttributeSetter HOST_TYPE = StringAttributeSetter.create("host.type");
/** Name of the VM image or OS install the host was instantiated from. */
public static final StringAttributeSetter HOST_IMAGE_NAME =
StringAttributeSetter.create("host.image.name");
/** VM image id. For Cloud, this value is from the provider. */
public static final StringAttributeSetter HOST_IMAGE_ID =
StringAttributeSetter.create("host.image.id");
/** The version string of the VM image. */
public static final StringAttributeSetter HOST_IMAGE_VERSION =
StringAttributeSetter.create("host.image.version");
/** Name of the cloud provider. */
public static final StringAttributeSetter CLOUD_PROVIDER =
StringAttributeSetter.create("cloud.provider");
/** The cloud account id used to identify different entities. */
public static final StringAttributeSetter CLOUD_ACCOUNT =
StringAttributeSetter.create("cloud.account.id");
/** A specific geographical location where different entities can run. */
public static final StringAttributeSetter CLOUD_REGION =
StringAttributeSetter.create("cloud.region");
/** Zones are a sub set of the region connected through low-latency links. */
public static final StringAttributeSetter CLOUD_ZONE = StringAttributeSetter.create("cloud.zone");
private ResourceAttributes() {}
}

View File

@ -1,124 +0,0 @@
/*
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentelemetry.sdk.resources;
/**
* Provides constants for resource semantic conventions defined by the OpenTelemetry specification.
*
* @see <a
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/resource/semantic_conventions/README.md">Resource
* Conventions</a>
*/
public class ResourceConstants {
/** The operating system type, such as {@code "WINDOWS"}, {@code "DARWIN"}, {@code "LINUX"}. */
public static final String OS_NAME = "os.name";
/**
* Human readable information about the OS version, e.g. {@code "Microsoft Windows [Version
* 10.0.18363.778]"}, {@code "Ubuntu 18.04.1 LTS"}.
*/
public static final String OS_DESCRIPTION = "os.description";
/** Process identifier (PID). */
public static final String PROCESS_PID = "process.pid";
/** The name of the process executable. */
public static final String PROCESS_EXECUTABLE_NAME = "process.executable.name";
/** The full path to the process executable. */
public static final String PROCESS_EXECUTABLE_PATH = "process.executable.path";
/** The command used to launch the process (i.e. the command name). */
public static final String PROCESS_COMMAND = "process.command";
/**
* The full command used to launch the process. The value can be either a list of strings
* representing the ordered list of arguments, or a single string representing the full command.
*/
public static final String PROCESS_COMMAND_LINE = "process.command_line";
/** The username of the user that owns the process. */
public static final String PROCESS_OWNER = "process.owner";
/**
* Logical name of the service. MUST be the same for all instances of horizontally scaled
* services.
*/
public static final String SERVICE_NAME = "service.name";
/**
* A namespace for `service.name`. A string value having a meaning that helps to distinguish a
* group of services,
*/
public static final String SERVICE_NAMESPACE = "service.namespace";
/**
* The string ID of the service instance. MUST be unique for each instance of the same
* `service.namespace,service.name` pair.
*/
public static final String SERVICE_INSTANCE = "service.instance.id";
/** The version string of the service API or implementation. */
public static final String SERVICE_VERSION = "service.version";
/** The name of the telemetry library. */
public static final String LIBRARY_NAME = "library.name";
/** The language of telemetry library and of the code instrumented with it. */
public static final String LIBRARY_LANGUAGE = "library.language";
/** The version string of the library. */
public static final String LIBRARY_VERSION = "library.version";
/** Container name. */
public static final String CONTAINER_NAME = "container.name";
/** Container id. */
public static final String CONTAINER_ID = "container.id";
/** Name of the image the container was built on. */
public static final String CONTAINER_IMAGE_NAME = "container.image.name";
/** Container image tag. */
public static final String CONTAINER_IMAGE_TAG = "container.image.tag";
/** The name of the cluster that the pod is running in. */
public static final String K8S_CLUSTER = "k8s.cluster.name";
/** The name of the namespace that the pod is running in. */
public static final String K8S_NAMESPACE = "k8s.namespace.name";
/** The name of the pod. */
public static final String K8S_POD = "k8s.pod.name";
/** The name of the deployment. */
public static final String K8S_DEPLOYMENT = "k8s.deployment.name";
/** Hostname of the host. It contains what the `hostname` command returns on the host machine. */
public static final String HOST_HOSTNAME = "host.hostname";
/** Unique host id. For Cloud this must be the instance_id assigned by the cloud provider. */
public static final String HOST_ID = "host.id";
/**
* Name of the host. It may contain what `hostname` returns on Unix systems, the fully qualified,
* or a name specified by the user.
*/
public static final String HOST_NAME = "host.name";
/** Type of host. For Cloud this must be the machine type. */
public static final String HOST_TYPE = "host.type";
/** Name of the VM image or OS install the host was instantiated from. */
public static final String HOST_IMAGE_NAME = "host.image.name";
/** VM image id. For Cloud, this value is from the provider. */
public static final String HOST_IMAGE_ID = "host.image.id";
/** The version string of the VM image. */
public static final String HOST_IMAGE_VERSION = "host.image.version";
/** Name of the cloud provider. */
public static final String CLOUD_PROVIDER = "cloud.provider";
/** The cloud account id used to identify different entities. */
public static final String CLOUD_ACCOUNT = "cloud.account.id";
/** A specific geographical location where different entities can run. */
public static final String CLOUD_REGION = "cloud.region";
/** Zones are a sub set of the region connected through low-latency links. */
public static final String CLOUD_ZONE = "cloud.zone";
private ResourceConstants() {}
}

View File

@ -21,7 +21,7 @@ import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.google.common.annotations.VisibleForTesting;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.io.File;
import java.io.IOException;
@ -78,13 +78,13 @@ public class BeanstalkResource extends ResourceProvider {
String value = parser.getText();
switch (parser.getCurrentName()) {
case DEVELOPMENT_ID:
attrBuilders.setAttribute(ResourceConstants.SERVICE_INSTANCE, value);
ResourceAttributes.SERVICE_INSTANCE.set(attrBuilders, value);
break;
case VERSION_LABEL:
attrBuilders.setAttribute(ResourceConstants.SERVICE_VERSION, value);
ResourceAttributes.SERVICE_VERSION.set(attrBuilders, value);
break;
case ENVIRONMENT_NAME:
attrBuilders.setAttribute(ResourceConstants.SERVICE_NAMESPACE, value);
ResourceAttributes.SERVICE_NAMESPACE.set(attrBuilders, value);
break;
default:
parser.skipChildren();

View File

@ -22,7 +22,7 @@ import com.fasterxml.jackson.core.JsonToken;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.io.ByteStreams;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@ -172,22 +172,22 @@ public class Ec2Resource extends ResourceProvider {
String value = parser.nextTextValue();
switch (parser.getCurrentName()) {
case "instanceId":
attrBuilders.setAttribute(ResourceConstants.HOST_ID, value);
ResourceAttributes.HOST_ID.set(attrBuilders, value);
break;
case "availabilityZone":
attrBuilders.setAttribute(ResourceConstants.CLOUD_ZONE, value);
ResourceAttributes.CLOUD_ZONE.set(attrBuilders, value);
break;
case "instanceType":
attrBuilders.setAttribute(ResourceConstants.HOST_TYPE, value);
ResourceAttributes.HOST_TYPE.set(attrBuilders, value);
break;
case "imageId":
attrBuilders.setAttribute(ResourceConstants.HOST_IMAGE_ID, value);
ResourceAttributes.HOST_IMAGE_ID.set(attrBuilders, value);
break;
case "accountId":
attrBuilders.setAttribute(ResourceConstants.CLOUD_ACCOUNT, value);
ResourceAttributes.CLOUD_ACCOUNT.set(attrBuilders, value);
break;
case "region":
attrBuilders.setAttribute(ResourceConstants.CLOUD_REGION, value);
ResourceAttributes.CLOUD_REGION.set(attrBuilders, value);
break;
default:
parser.skipChildren();
@ -198,8 +198,8 @@ public class Ec2Resource extends ResourceProvider {
return Attributes.empty();
}
attrBuilders.setAttribute(ResourceConstants.HOST_HOSTNAME, hostname);
attrBuilders.setAttribute(ResourceConstants.HOST_NAME, hostname);
ResourceAttributes.HOST_HOSTNAME.set(attrBuilders, hostname);
ResourceAttributes.HOST_NAME.set(attrBuilders, hostname);
return attrBuilders.build();
}

View File

@ -19,7 +19,7 @@ package io.opentelemetry.sdk.extensions.trace.aws.resource;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.net.InetAddress;
import java.net.UnknownHostException;
@ -64,14 +64,14 @@ public class EcsResource extends ResourceProvider {
Attributes.Builder attrBuilders = Attributes.newBuilder();
try {
String hostName = InetAddress.getLocalHost().getHostName();
attrBuilders.setAttribute(ResourceConstants.CONTAINER_NAME, hostName);
ResourceAttributes.CONTAINER_NAME.set(attrBuilders, hostName);
} catch (UnknownHostException e) {
logger.log(Level.WARNING, "Could not get docker container name from hostname.", e);
}
String containerId = dockerHelper.getContainerId();
if (!Strings.isNullOrEmpty(containerId)) {
attrBuilders.setAttribute(ResourceConstants.CONTAINER_ID, containerId);
ResourceAttributes.CONTAINER_ID.set(attrBuilders, containerId);
}
return attrBuilders.build();

View File

@ -22,7 +22,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.io.File;
import java.io.IOException;
@ -44,9 +44,10 @@ class BeanstalkResourceTest {
assertThat(attributes)
.isEqualTo(
Attributes.of(
ResourceConstants.SERVICE_INSTANCE, stringAttributeValue("4"),
ResourceConstants.SERVICE_VERSION, stringAttributeValue("2"),
ResourceConstants.SERVICE_NAMESPACE, stringAttributeValue("HttpSubscriber-env")));
ResourceAttributes.SERVICE_INSTANCE.key(), stringAttributeValue("4"),
ResourceAttributes.SERVICE_VERSION.key(), stringAttributeValue("2"),
ResourceAttributes.SERVICE_NAMESPACE.key(),
stringAttributeValue("HttpSubscriber-env")));
}
@Test

View File

@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import com.github.tomakehurst.wiremock.junit.WireMockClassRule;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.util.ServiceLoader;
import org.junit.Before;
@ -81,14 +81,15 @@ public class Ec2ResourceTest {
Attributes attributes = populator.getAttributes();
Attributes.Builder expectedAttrBuilders = Attributes.newBuilder();
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_ID, "i-1234567890abcdef0");
expectedAttrBuilders.setAttribute(ResourceConstants.CLOUD_ZONE, "us-west-2b");
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_TYPE, "t2.micro");
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_IMAGE_ID, "ami-5fb8c835");
expectedAttrBuilders.setAttribute(ResourceConstants.CLOUD_ACCOUNT, "123456789012");
expectedAttrBuilders.setAttribute(ResourceConstants.CLOUD_REGION, "us-west-2");
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_HOSTNAME, "ec2-1-2-3-4");
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_NAME, "ec2-1-2-3-4");
ResourceAttributes.HOST_ID.set(expectedAttrBuilders, "i-1234567890abcdef0");
ResourceAttributes.CLOUD_ZONE.set(expectedAttrBuilders, "us-west-2b");
ResourceAttributes.HOST_TYPE.set(expectedAttrBuilders, "t2.micro");
ResourceAttributes.HOST_IMAGE_ID.set(expectedAttrBuilders, "ami-5fb8c835");
ResourceAttributes.CLOUD_ACCOUNT.set(expectedAttrBuilders, "123456789012");
ResourceAttributes.CLOUD_REGION.set(expectedAttrBuilders, "us-west-2");
ResourceAttributes.HOST_HOSTNAME.set(expectedAttrBuilders, "ec2-1-2-3-4");
ResourceAttributes.HOST_NAME.set(expectedAttrBuilders, "ec2-1-2-3-4");
assertThat(attributes).isEqualTo(expectedAttrBuilders.build());
verify(
@ -112,14 +113,15 @@ public class Ec2ResourceTest {
Attributes attributes = populator.getAttributes();
Attributes.Builder expectedAttrBuilders = Attributes.newBuilder();
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_ID, "i-1234567890abcdef0");
expectedAttrBuilders.setAttribute(ResourceConstants.CLOUD_ZONE, "us-west-2b");
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_TYPE, "t2.micro");
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_IMAGE_ID, "ami-5fb8c835");
expectedAttrBuilders.setAttribute(ResourceConstants.CLOUD_ACCOUNT, "123456789012");
expectedAttrBuilders.setAttribute(ResourceConstants.CLOUD_REGION, "us-west-2");
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_HOSTNAME, "ec2-1-2-3-4");
expectedAttrBuilders.setAttribute(ResourceConstants.HOST_NAME, "ec2-1-2-3-4");
ResourceAttributes.HOST_ID.set(expectedAttrBuilders, "i-1234567890abcdef0");
ResourceAttributes.CLOUD_ZONE.set(expectedAttrBuilders, "us-west-2b");
ResourceAttributes.HOST_TYPE.set(expectedAttrBuilders, "t2.micro");
ResourceAttributes.HOST_IMAGE_ID.set(expectedAttrBuilders, "ami-5fb8c835");
ResourceAttributes.CLOUD_ACCOUNT.set(expectedAttrBuilders, "123456789012");
ResourceAttributes.CLOUD_REGION.set(expectedAttrBuilders, "us-west-2");
ResourceAttributes.HOST_HOSTNAME.set(expectedAttrBuilders, "ec2-1-2-3-4");
ResourceAttributes.HOST_NAME.set(expectedAttrBuilders, "ec2-1-2-3-4");
assertThat(attributes).isEqualTo(expectedAttrBuilders.build());
verify(

View File

@ -21,7 +21,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.net.InetAddress;
import java.net.UnknownHostException;
@ -50,9 +50,9 @@ class EcsResourceTest {
assertThat(attributes)
.isEqualTo(
Attributes.of(
ResourceConstants.CONTAINER_NAME,
ResourceAttributes.CONTAINER_NAME.key(),
stringAttributeValue(InetAddress.getLocalHost().getHostName()),
ResourceConstants.CONTAINER_ID,
ResourceAttributes.CONTAINER_ID.key(),
stringAttributeValue("0123456789A")));
}
@ -76,7 +76,7 @@ class EcsResourceTest {
assertThat(attributes)
.isEqualTo(
Attributes.of(
ResourceConstants.CONTAINER_NAME,
ResourceAttributes.CONTAINER_NAME.key(),
stringAttributeValue(InetAddress.getLocalHost().getHostName())));
}

View File

@ -17,7 +17,7 @@
package io.opentelemetry.sdk.extensions.resources;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import io.opentelemetry.sdk.resources.ResourceProvider;
import javax.annotation.Nullable;
@ -42,7 +42,7 @@ public class OsResource extends ResourceProvider {
String osName = getOs(os);
if (osName != null) {
attributes.setAttribute(ResourceConstants.OS_NAME, osName);
attributes.setAttribute(ResourceAttributes.OS_NAME.key(), osName);
}
String version = null;
@ -52,7 +52,7 @@ public class OsResource extends ResourceProvider {
// Ignore
}
String osDescription = version != null ? os + ' ' + version : os;
attributes.setAttribute(ResourceConstants.OS_DESCRIPTION, osDescription);
attributes.setAttribute(ResourceAttributes.OS_DESCRIPTION.key(), osDescription);
return attributes.build();
}

View File

@ -17,7 +17,7 @@
package io.opentelemetry.sdk.extensions.resources;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import io.opentelemetry.sdk.resources.ResourceProvider;
import java.io.File;
import java.lang.management.ManagementFactory;
@ -46,7 +46,7 @@ public class ProcessResource extends ResourceProvider {
}
if (pid >= 0) {
attributes.setAttribute(ResourceConstants.PROCESS_PID, pid);
ResourceAttributes.PROCESS_PID.set(attributes, pid);
}
String javaHome = null;
@ -68,13 +68,13 @@ public class ProcessResource extends ResourceProvider {
executablePath.append(".exe");
}
attributes.setAttribute(ResourceConstants.PROCESS_EXECUTABLE_PATH, executablePath.toString());
ResourceAttributes.PROCESS_EXECUTABLE_PATH.set(attributes, executablePath.toString());
StringBuilder commandLine = new StringBuilder(executablePath);
for (String arg : runtime.getInputArguments()) {
commandLine.append(' ').append(arg);
}
attributes.setAttribute(ResourceConstants.PROCESS_COMMAND_LINE, commandLine.toString());
ResourceAttributes.PROCESS_COMMAND_LINE.set(attributes, commandLine.toString());
}
return attributes.build();

View File

@ -22,7 +22,7 @@ import static org.assertj.core.api.Assumptions.assumeThat;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.common.ReadableAttributes;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import org.junit.jupiter.api.Test;
class OsResourceTest {
@ -33,30 +33,36 @@ class OsResourceTest {
void linux() {
assumeThat(System.getProperty("os.name").toLowerCase()).startsWith("linux");
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceConstants.OS_NAME).getStringValue()).isEqualTo("LINUX");
assertThat(attributes.get(ResourceConstants.OS_DESCRIPTION).getStringValue()).isNotEmpty();
assertThat(attributes.get(ResourceAttributes.OS_NAME.key()).getStringValue())
.isEqualTo("LINUX");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION.key()).getStringValue())
.isNotEmpty();
}
@Test
void macos() {
assumeThat(System.getProperty("os.name").toLowerCase()).startsWith("mac");
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceConstants.OS_NAME).getStringValue()).isEqualTo("DARWIN");
assertThat(attributes.get(ResourceConstants.OS_DESCRIPTION).getStringValue()).isNotEmpty();
assertThat(attributes.get(ResourceAttributes.OS_NAME.key()).getStringValue())
.isEqualTo("DARWIN");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION.key()).getStringValue())
.isNotEmpty();
}
@Test
void windows() {
assumeThat(System.getProperty("os.name").toLowerCase()).startsWith("windows");
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceConstants.OS_NAME).getStringValue()).isEqualTo("WINDOWS");
assertThat(attributes.get(ResourceConstants.OS_DESCRIPTION).getStringValue()).isNotEmpty();
assertThat(attributes.get(ResourceAttributes.OS_NAME.key()).getStringValue())
.isEqualTo("WINDOWS");
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION.key()).getStringValue())
.isNotEmpty();
}
@Test
void inDefault() {
ReadableAttributes attributes = Resource.getDefault().getAttributes();
assertThat(attributes.get(ResourceConstants.OS_NAME)).isNotNull();
assertThat(attributes.get(ResourceConstants.OS_DESCRIPTION)).isNotNull();
assertThat(attributes.get(ResourceAttributes.OS_NAME.key())).isNotNull();
assertThat(attributes.get(ResourceAttributes.OS_DESCRIPTION.key())).isNotNull();
}
}

View File

@ -21,7 +21,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.common.Attributes;
import io.opentelemetry.common.ReadableAttributes;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.resources.ResourceConstants;
import io.opentelemetry.sdk.resources.ResourceAttributes;
import org.junit.jupiter.api.Test;
class ProcessResourceTest {
@ -32,18 +32,20 @@ class ProcessResourceTest {
void normal() {
Attributes attributes = RESOURCE.getAttributes();
assertThat(attributes.get(ResourceConstants.PROCESS_PID).getLongValue()).isGreaterThan(1);
assertThat(attributes.get(ResourceConstants.PROCESS_EXECUTABLE_PATH).getStringValue())
assertThat(attributes.get(ResourceAttributes.PROCESS_PID.key()).getLongValue())
.isGreaterThan(1);
assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH.key()).getStringValue())
.contains("java");
assertThat(attributes.get(ResourceConstants.PROCESS_COMMAND_LINE).getStringValue())
.contains(attributes.get(ResourceConstants.PROCESS_EXECUTABLE_PATH).getStringValue());
assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE.key()).getStringValue())
.contains(
attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH.key()).getStringValue());
}
@Test
void inDefault() {
ReadableAttributes attributes = Resource.getDefault().getAttributes();
assertThat(attributes.get(ResourceConstants.PROCESS_PID)).isNotNull();
assertThat(attributes.get(ResourceConstants.PROCESS_EXECUTABLE_PATH)).isNotNull();
assertThat(attributes.get(ResourceConstants.PROCESS_COMMAND_LINE)).isNotNull();
assertThat(attributes.get(ResourceAttributes.PROCESS_PID.key())).isNotNull();
assertThat(attributes.get(ResourceAttributes.PROCESS_EXECUTABLE_PATH.key())).isNotNull();
assertThat(attributes.get(ResourceAttributes.PROCESS_COMMAND_LINE.key())).isNotNull();
}
}