diff options
| author | Bogdan Drutu <bdrutu@google.com> | 2019-01-30 15:47:48 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-30 15:47:48 -0800 |
| commit | 1cd62e42ea14e4313076f417eff4af6db1980d0f (patch) | |
| tree | c0ceedd8916460cae9ddc824191569be883efc89 /contrib | |
| parent | e8cba95228e9f104b325a99b3499d0da4be84e8d (diff) | |
| download | platform_external_opencensus-java-1cd62e42ea14e4313076f417eff4af6db1980d0f.tar.gz platform_external_opencensus-java-1cd62e42ea14e4313076f417eff4af6db1980d0f.tar.bz2 platform_external_opencensus-java-1cd62e42ea14e4313076f417eff4af6db1980d0f.zip | |
Deprecate MonitoredResource and change to use Resource. (#1718)
* Deprecate MonitoredResource and change to use Resource.
* Fix deprecated usages and imports.
* Resolve more deprecation usage warnings.
* Use full class names for deprecated classes
Diffstat (limited to 'contrib')
13 files changed, 540 insertions, 137 deletions
diff --git a/contrib/monitored_resource_util/build.gradle b/contrib/monitored_resource_util/build.gradle index 069f49c6..3c163a17 100644 --- a/contrib/monitored_resource_util/build.gradle +++ b/contrib/monitored_resource_util/build.gradle @@ -8,7 +8,8 @@ apply plugin: 'java' } dependencies { - compile project(':opencensus-api') + compile project(':opencensus-api'), + libraries.guava compileOnly libraries.auto_value signature "org.codehaus.mojo.signature:java17:1.0@signature" diff --git a/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/AwsEc2InstanceResource.java b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/AwsEc2InstanceResource.java new file mode 100644 index 00000000..66499e8f --- /dev/null +++ b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/AwsEc2InstanceResource.java @@ -0,0 +1,90 @@ +/* + * Copyright 2019, OpenCensus 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.opencensus.contrib.monitoredresource.util; + +import static com.google.common.base.MoreObjects.firstNonNull; +import static com.google.common.base.Preconditions.checkNotNull; + +import io.opencensus.resource.Resource; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Helper class for AWS EC2 instances {@code Resource}. + * + * @since 0.20 + */ +public final class AwsEc2InstanceResource { + private static final String ACCOUNT_ID = + firstNonNull(AwsIdentityDocUtils.getValueFromAwsIdentityDocument("accountId"), ""); + private static final String INSTANCE_ID = + firstNonNull(AwsIdentityDocUtils.getValueFromAwsIdentityDocument("instanceId"), ""); + private static final String REGION = + firstNonNull(AwsIdentityDocUtils.getValueFromAwsIdentityDocument("region"), ""); + + /** + * AWS key that represents a type of the resource. + * + * @since 0.20 + */ + public static final String TYPE = "aws.com/ec2/instance"; + + /** + * AWS key that represents the AWS account number for the VM. + * + * @since 0.20 + */ + public static final String ACCOUNT_ID_KEY = "aws.com/ec2/account_id"; + + /** + * AWS key that represents the VM instance identifier assigned by AWS. + * + * @since 0.20 + */ + public static final String INSTANCE_ID_KEY = "aws.com/ec2/instance_id"; + + /** + * AWS key that represents a region for the VM. + * + * @since 0.20 + */ + public static final String REGION_KEY = "aws.com/ec2/region"; + + /** + * Returns a {@link Resource} that describes a aws ec2 instance. + * + * @param accountId the AWS account ID. + * @param region the AWS region. + * @param instanceId the AWS EC2 instance ID. + * @return a {@link Resource} that describes a aws ec2 instance. + * @since 0.20 + */ + public static Resource create(String accountId, String region, String instanceId) { + Map<String, String> mutableLabels = new LinkedHashMap<String, String>(); + mutableLabels.put(ACCOUNT_ID_KEY, checkNotNull(accountId, "accountId")); + mutableLabels.put(REGION_KEY, checkNotNull(region, "region")); + mutableLabels.put(INSTANCE_ID_KEY, checkNotNull(instanceId, "instanceId")); + return Resource.create(TYPE, Collections.unmodifiableMap(mutableLabels)); + } + + static Resource detect() { + return create(ACCOUNT_ID, REGION, INSTANCE_ID); + } + + private AwsEc2InstanceResource() {} +} diff --git a/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/GcpGceInstanceResource.java b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/GcpGceInstanceResource.java new file mode 100644 index 00000000..a6731fdc --- /dev/null +++ b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/GcpGceInstanceResource.java @@ -0,0 +1,87 @@ +/* + * Copyright 2019, OpenCensus 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.opencensus.contrib.monitoredresource.util; + +import static com.google.common.base.MoreObjects.firstNonNull; +import static com.google.common.base.Preconditions.checkNotNull; + +import io.opencensus.resource.Resource; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Helper class for GCP GCE instance {@code Resource}. + * + * @since 0.20 + */ +public final class GcpGceInstanceResource { + private static final String PROJECT_ID = firstNonNull(GcpMetadataConfig.getProjectId(), ""); + private static final String INSTANCE_ID = firstNonNull(GcpMetadataConfig.getInstanceId(), ""); + private static final String ZONE = firstNonNull(GcpMetadataConfig.getZone(), ""); + + /** + * GCP GCE key that represents a type of the resource. + * + * @since 0.20 + */ + public static final String TYPE = "cloud.google.com/gce/instance"; + + /** + * GCP GCE key that represents the GCP account number for the instance. + * + * @since 0.20 + */ + public static final String PROJECT_ID_KEY = "cloud.google.com/gce/project_id"; + + /** + * GCP GCE key that represents the numeric VM instance identifier assigned by GCE. + * + * @since 0.20 + */ + public static final String INSTANCE_ID_KEY = "cloud.google.com/gce/instance_id"; + + /** + * GCP GCE key that represents the GCE zone in which the VM is running. + * + * @since 0.20 + */ + public static final String ZONE_KEY = "cloud.google.com/gce/zone"; + + /** + * Returns a {@link Resource} that describes a k8s container. + * + * @param projectId the GCP project number. + * @param zone the GCP zone. + * @param instanceId the GCP GCE instance ID. + * @return a {@link Resource} that describes a k8s container. + * @since 0.20 + */ + public static Resource create(String projectId, String zone, String instanceId) { + Map<String, String> mutableLabels = new LinkedHashMap<String, String>(); + mutableLabels.put(PROJECT_ID_KEY, checkNotNull(projectId, "projectId")); + mutableLabels.put(ZONE_KEY, checkNotNull(zone, "zone")); + mutableLabels.put(INSTANCE_ID_KEY, checkNotNull(instanceId, "instanceId")); + return Resource.create(TYPE, Collections.unmodifiableMap(mutableLabels)); + } + + static Resource detect() { + return create(PROJECT_ID, ZONE, INSTANCE_ID); + } + + private GcpGceInstanceResource() {} +} diff --git a/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/K8sContainerResource.java b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/K8sContainerResource.java new file mode 100644 index 00000000..b9abe1fb --- /dev/null +++ b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/K8sContainerResource.java @@ -0,0 +1,100 @@ +/* + * Copyright 2019, OpenCensus 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.opencensus.contrib.monitoredresource.util; + +import static com.google.common.base.MoreObjects.firstNonNull; +import static com.google.common.base.Preconditions.checkNotNull; + +import io.opencensus.resource.Resource; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Helper class for K8S container {@code Resource}. + * + * @since 0.20 + */ +public class K8sContainerResource { + private static final String CLUSTER_NAME = firstNonNull(GcpMetadataConfig.getClusterName(), ""); + private static final String CONTAINER_NAME = firstNonNull(System.getenv("CONTAINER_NAME"), ""); + private static final String NAMESPACE_NAME = firstNonNull(System.getenv("NAMESPACE"), ""); + private static final String POD_NAME = firstNonNull(System.getenv("HOSTNAME"), ""); + + /** + * Kubernetes resources key that represents a type of the resource. + * + * @since 0.20 + */ + public static final String TYPE = "k8s.io/container"; + + /** + * Kubernetes resources key that represents the name for the cluster the container is running in. + * + * @since 0.20 + */ + public static final String CLUSTER_NAME_KEY = "k8s.io/cluster/name"; + + /** + * Kubernetes resources key that represents the identifier for the GCE instance the container is + * running in. + * + * @since 0.20 + */ + public static final String NAMESPACE_NAME_KEY = "k8s.io/namespace/name"; + + /** + * Kubernetes resources key that represents the identifier for the pod the container is running + * in. + * + * @since 0.20 + */ + public static final String POD_NAME_KEY = "k8s.io/pod/name"; + + /** + * Kubernetes resources key that represents the name of the container. + * + * @since 0.20 + */ + public static final String CONTAINER_NAME_KEY = "k8s.io/container/name"; + + /** + * Returns a {@link Resource} that describes a k8s container. + * + * @param clusterName the k8s cluster name. + * @param namespace the k8s namespace. + * @param podName the k8s pod name. + * @param containerName the k8s container name. + * @return a {@link Resource} that describes a k8s container. + * @since 0.20 + */ + public static Resource create( + String clusterName, String namespace, String podName, String containerName) { + Map<String, String> mutableLabels = new LinkedHashMap<String, String>(); + mutableLabels.put(CLUSTER_NAME_KEY, checkNotNull(clusterName, "clusterName")); + mutableLabels.put(NAMESPACE_NAME_KEY, checkNotNull(namespace, "namespace")); + mutableLabels.put(POD_NAME_KEY, checkNotNull(podName, "podName")); + mutableLabels.put(CONTAINER_NAME_KEY, checkNotNull(containerName, "containerName")); + return Resource.create(TYPE, Collections.unmodifiableMap(mutableLabels)); + } + + static Resource detect() { + return create(CLUSTER_NAME, NAMESPACE_NAME, POD_NAME, CONTAINER_NAME); + } + + private K8sContainerResource() {} +} diff --git a/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/MonitoredResource.java b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/MonitoredResource.java index 70b64c0b..da8f2101 100644 --- a/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/MonitoredResource.java +++ b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/MonitoredResource.java @@ -16,11 +16,10 @@ package io.opencensus.contrib.monitoredresource.util; -import com.google.auto.value.AutoValue; +import static com.google.common.base.MoreObjects.firstNonNull; + import io.opencensus.resource.Resource; -import java.util.LinkedHashMap; import java.util.Map; -import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; /** @@ -29,8 +28,10 @@ import javax.annotation.concurrent.Immutable; * values. * * @since 0.13 + * @deprecated use {@link Resource}. */ @Immutable +@Deprecated public abstract class MonitoredResource { MonitoredResource() {} @@ -43,20 +44,6 @@ public abstract class MonitoredResource { */ public abstract ResourceType getResourceType(); - /* - * Returns the first of two given parameters that is not null, if either is, or otherwise - * throws a NullPointerException. - */ - private static <T> T firstNonNull(@Nullable T first, @Nullable T second) { - if (first != null) { - return first; - } - if (second != null) { - return second; - } - throw new NullPointerException("Both parameters are null"); - } - // TODO(songya): consider using a tagged union match() approach (that will introduce // dependency on opencensus-api). @@ -64,17 +51,12 @@ public abstract class MonitoredResource { * {@link MonitoredResource} for AWS EC2 instance. * * @since 0.13 + * @deprecated use {@link Resource}. */ @Immutable - @AutoValue - public abstract static class AwsEc2InstanceMonitoredResource extends MonitoredResource { - - private static final String AWS_ACCOUNT = - firstNonNull(AwsIdentityDocUtils.getValueFromAwsIdentityDocument("accountId"), ""); - private static final String AWS_INSTANCE_ID = - firstNonNull(AwsIdentityDocUtils.getValueFromAwsIdentityDocument("instanceId"), ""); - private static final String AWS_REGION = - firstNonNull(AwsIdentityDocUtils.getValueFromAwsIdentityDocument("region"), ""); + @Deprecated + public static final class AwsEc2InstanceMonitoredResource extends MonitoredResource { + private final Map<String, String> awsInstanceLabels; @Override public ResourceType getResourceType() { @@ -87,7 +69,9 @@ public abstract class MonitoredResource { * @return the AWS account ID. * @since 0.13 */ - public abstract String getAccount(); + public String getAccount() { + return firstNonNull(awsInstanceLabels.get(AwsEc2InstanceResource.ACCOUNT_ID_KEY), ""); + } /** * Returns the AWS EC2 instance ID. @@ -95,7 +79,9 @@ public abstract class MonitoredResource { * @return the AWS EC2 instance ID. * @since 0.13 */ - public abstract String getInstanceId(); + public String getInstanceId() { + return firstNonNull(awsInstanceLabels.get(AwsEc2InstanceResource.INSTANCE_ID_KEY), ""); + } /** * Returns the AWS region. @@ -103,7 +89,9 @@ public abstract class MonitoredResource { * @return the AWS region. * @since 0.13 */ - public abstract String getRegion(); + public String getRegion() { + return firstNonNull(awsInstanceLabels.get(AwsEc2InstanceResource.REGION_KEY), ""); + } /** * Returns an {@link AwsEc2InstanceMonitoredResource}. @@ -116,20 +104,16 @@ public abstract class MonitoredResource { */ public static AwsEc2InstanceMonitoredResource create( String account, String instanceId, String region) { - return new AutoValue_MonitoredResource_AwsEc2InstanceMonitoredResource( - account, instanceId, region); + return new AwsEc2InstanceMonitoredResource( + AwsEc2InstanceResource.create(account, region, instanceId).getLabels()); } - static AwsEc2InstanceMonitoredResource create() { - return create(AWS_ACCOUNT, AWS_INSTANCE_ID, AWS_REGION); + static AwsEc2InstanceMonitoredResource autoDetectResource() { + return new AwsEc2InstanceMonitoredResource(AwsEc2InstanceResource.detect().getLabels()); } - static Resource createResource() { - Map<String, String> labels = new LinkedHashMap<String, String>(); - labels.put(ResourceKeyConstants.AWS_REGION_KEY, AWS_REGION); - labels.put(ResourceKeyConstants.AWS_ACCOUNT_KEY, AWS_ACCOUNT); - labels.put(ResourceKeyConstants.AWS_INSTANCE_ID_KEY, AWS_INSTANCE_ID); - return Resource.create(ResourceKeyConstants.AWS_EC2_INSTANCE_TYPE, labels); + private AwsEc2InstanceMonitoredResource(Map<String, String> awsInstanceLabels) { + this.awsInstanceLabels = awsInstanceLabels; } } @@ -137,15 +121,12 @@ public abstract class MonitoredResource { * {@link MonitoredResource} for GCP GCE instance. * * @since 0.13 + * @deprecated use {@link Resource}. */ @Immutable - @AutoValue - public abstract static class GcpGceInstanceMonitoredResource extends MonitoredResource { - - private static final String GCP_ACCOUNT_ID = firstNonNull(GcpMetadataConfig.getProjectId(), ""); - private static final String GCP_INSTANCE_ID = - firstNonNull(GcpMetadataConfig.getInstanceId(), ""); - private static final String GCP_ZONE = firstNonNull(GcpMetadataConfig.getZone(), ""); + @Deprecated + public static final class GcpGceInstanceMonitoredResource extends MonitoredResource { + private final Map<String, String> gcpInstanceLabels; @Override public ResourceType getResourceType() { @@ -158,7 +139,9 @@ public abstract class MonitoredResource { * @return the GCP account number for the instance. * @since 0.13 */ - public abstract String getAccount(); + public String getAccount() { + return firstNonNull(gcpInstanceLabels.get(GcpGceInstanceResource.PROJECT_ID_KEY), ""); + } /** * Returns the GCP GCE instance ID. @@ -166,7 +149,9 @@ public abstract class MonitoredResource { * @return the GCP GCE instance ID. * @since 0.13 */ - public abstract String getInstanceId(); + public String getInstanceId() { + return firstNonNull(gcpInstanceLabels.get(GcpGceInstanceResource.INSTANCE_ID_KEY), ""); + } /** * Returns the GCP zone. @@ -174,7 +159,9 @@ public abstract class MonitoredResource { * @return the GCP zone. * @since 0.13 */ - public abstract String getZone(); + public String getZone() { + return firstNonNull(gcpInstanceLabels.get(GcpGceInstanceResource.ZONE_KEY), ""); + } /** * Returns a {@link GcpGceInstanceMonitoredResource}. @@ -187,20 +174,16 @@ public abstract class MonitoredResource { */ public static GcpGceInstanceMonitoredResource create( String account, String instanceId, String zone) { - return new AutoValue_MonitoredResource_GcpGceInstanceMonitoredResource( - account, instanceId, zone); + return new GcpGceInstanceMonitoredResource( + GcpGceInstanceResource.create(account, zone, instanceId).getLabels()); } - static GcpGceInstanceMonitoredResource create() { - return create(GCP_ACCOUNT_ID, GCP_INSTANCE_ID, GCP_ZONE); + static GcpGceInstanceMonitoredResource autoDetectResource() { + return new GcpGceInstanceMonitoredResource(GcpGceInstanceResource.detect().getLabels()); } - static Resource createResource() { - Map<String, String> labels = new LinkedHashMap<String, String>(); - labels.put(ResourceKeyConstants.GCP_ACCOUNT_ID_KEY, GCP_ACCOUNT_ID); - labels.put(ResourceKeyConstants.GCP_INSTANCE_ID_KEY, GCP_INSTANCE_ID); - labels.put(ResourceKeyConstants.GCP_ZONE_KEY, GCP_ZONE); - return Resource.create(ResourceKeyConstants.GCP_GCE_INSTANCE_TYPE, labels); + private GcpGceInstanceMonitoredResource(Map<String, String> gcpInstanceLabels) { + this.gcpInstanceLabels = gcpInstanceLabels; } } @@ -208,21 +191,13 @@ public abstract class MonitoredResource { * {@link MonitoredResource} for GCP GKE container. * * @since 0.13 + * @deprecated use {@link Resource}. */ @Immutable - @AutoValue - public abstract static class GcpGkeContainerMonitoredResource extends MonitoredResource { - - private static final String GCP_ACCOUNT_ID = firstNonNull(GcpMetadataConfig.getProjectId(), ""); - private static final String GCP_CLUSTER_NAME = - firstNonNull(GcpMetadataConfig.getClusterName(), ""); - private static final String GCP_CONTAINER_NAME = - firstNonNull(System.getenv("CONTAINER_NAME"), ""); - private static final String GCP_NAMESPACE_ID = firstNonNull(System.getenv("NAMESPACE"), ""); - private static final String GCP_INSTANCE_ID = - firstNonNull(GcpMetadataConfig.getInstanceId(), ""); - private static final String GCP_POD_ID = firstNonNull(System.getenv("HOSTNAME"), ""); - private static final String GCP_ZONE = firstNonNull(GcpMetadataConfig.getZone(), ""); + @Deprecated + public static final class GcpGkeContainerMonitoredResource extends MonitoredResource { + private final Map<String, String> k8sContainerLabels; + private final Map<String, String> gcpInstanceLabels; @Override public ResourceType getResourceType() { @@ -235,7 +210,9 @@ public abstract class MonitoredResource { * @return the GCP account number for the instance. * @since 0.13 */ - public abstract String getAccount(); + public String getAccount() { + return firstNonNull(gcpInstanceLabels.get(GcpGceInstanceResource.PROJECT_ID_KEY), ""); + } /** * Returns the GCP GKE cluster name. @@ -243,7 +220,9 @@ public abstract class MonitoredResource { * @return the GCP GKE cluster name. * @since 0.13 */ - public abstract String getClusterName(); + public String getClusterName() { + return firstNonNull(k8sContainerLabels.get(K8sContainerResource.CLUSTER_NAME_KEY), ""); + } /** * Returns the GCP GKE container name. @@ -251,7 +230,9 @@ public abstract class MonitoredResource { * @return the GCP GKE container name. * @since 0.13 */ - public abstract String getContainerName(); + public String getContainerName() { + return firstNonNull(k8sContainerLabels.get(K8sContainerResource.CONTAINER_NAME_KEY), ""); + } /** * Returns the GCP GKE namespace ID. @@ -259,7 +240,9 @@ public abstract class MonitoredResource { * @return the GCP GKE namespace ID. * @since 0.13 */ - public abstract String getNamespaceId(); + public String getNamespaceId() { + return firstNonNull(k8sContainerLabels.get(K8sContainerResource.NAMESPACE_NAME_KEY), ""); + } /** * Returns the GCP GKE instance ID. @@ -267,7 +250,9 @@ public abstract class MonitoredResource { * @return the GCP GKE instance ID. * @since 0.13 */ - public abstract String getInstanceId(); + public String getInstanceId() { + return firstNonNull(gcpInstanceLabels.get(GcpGceInstanceResource.INSTANCE_ID_KEY), ""); + } /** * Returns the GCP GKE Pod ID. @@ -275,7 +260,9 @@ public abstract class MonitoredResource { * @return the GCP GKE Pod ID. * @since 0.13 */ - public abstract String getPodId(); + public String getPodId() { + return firstNonNull(k8sContainerLabels.get(K8sContainerResource.POD_NAME_KEY), ""); + } /** * Returns the GCP zone. @@ -283,7 +270,9 @@ public abstract class MonitoredResource { * @return the GCP zone. * @since 0.13 */ - public abstract String getZone(); + public String getZone() { + return firstNonNull(gcpInstanceLabels.get(GcpGceInstanceResource.ZONE_KEY), ""); + } /** * Returns a {@link GcpGkeContainerMonitoredResource}. @@ -306,30 +295,21 @@ public abstract class MonitoredResource { String instanceId, String podId, String zone) { - return new AutoValue_MonitoredResource_GcpGkeContainerMonitoredResource( - account, clusterName, containerName, namespaceId, instanceId, podId, zone); + return new GcpGkeContainerMonitoredResource( + K8sContainerResource.create(clusterName, namespaceId, podId, containerName).getLabels(), + GcpGceInstanceResource.create(account, zone, instanceId).getLabels()); } - static GcpGkeContainerMonitoredResource create() { - return create( - GCP_ACCOUNT_ID, - GCP_CLUSTER_NAME, - GCP_CONTAINER_NAME, - GCP_NAMESPACE_ID, - GCP_INSTANCE_ID, - GCP_POD_ID, - GCP_ZONE); + static GcpGkeContainerMonitoredResource autoDetectResource() { + return new GcpGkeContainerMonitoredResource( + K8sContainerResource.detect().getLabels(), GcpGceInstanceResource.detect().getLabels()); } - static Resource createResource() { - Map<String, String> labels = new LinkedHashMap<String, String>(); - labels.put(ResourceKeyConstants.GCP_ACCOUNT_ID_KEY, GCP_ACCOUNT_ID); - labels.put(ResourceKeyConstants.GCP_ZONE_KEY, GCP_ZONE); - labels.put(ResourceKeyConstants.K8S_CLUSTER_NAME_KEY, GCP_CLUSTER_NAME); - labels.put(ResourceKeyConstants.K8S_CONTAINER_NAME_KEY, GCP_CONTAINER_NAME); - labels.put(ResourceKeyConstants.K8S_NAMESPACE_NAME_KEY, GCP_NAMESPACE_ID); - labels.put(ResourceKeyConstants.K8S_POD_NAME_KEY, GCP_POD_ID); - return Resource.create(ResourceKeyConstants.K8S_CONTAINER_TYPE, labels); + private GcpGkeContainerMonitoredResource( + Map<String, String> k8sContainerLabels, Map<String, String> gcpInstanceLabels) { + + this.k8sContainerLabels = k8sContainerLabels; + this.gcpInstanceLabels = gcpInstanceLabels; } } } diff --git a/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceUtils.java b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceUtils.java index b6b00ca6..b617fdf6 100644 --- a/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceUtils.java +++ b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceUtils.java @@ -16,9 +16,6 @@ package io.opencensus.contrib.monitoredresource.util; -import io.opencensus.contrib.monitoredresource.util.MonitoredResource.AwsEc2InstanceMonitoredResource; -import io.opencensus.contrib.monitoredresource.util.MonitoredResource.GcpGceInstanceMonitoredResource; -import io.opencensus.contrib.monitoredresource.util.MonitoredResource.GcpGkeContainerMonitoredResource; import io.opencensus.resource.Resource; import java.util.ArrayList; import java.util.List; @@ -44,13 +41,13 @@ public final class MonitoredResourceUtils { @Nullable public static MonitoredResource getDefaultResource() { if (System.getenv("KUBERNETES_SERVICE_HOST") != null) { - return GcpGkeContainerMonitoredResource.create(); + return MonitoredResource.GcpGkeContainerMonitoredResource.autoDetectResource(); } if (GcpMetadataConfig.getInstanceId() != null) { - return GcpGceInstanceMonitoredResource.create(); + return MonitoredResource.GcpGceInstanceMonitoredResource.autoDetectResource(); } if (AwsIdentityDocUtils.isRunningOnAwsEc2()) { - return AwsEc2InstanceMonitoredResource.create(); + return MonitoredResource.AwsEc2InstanceMonitoredResource.autoDetectResource(); } return null; } @@ -66,15 +63,16 @@ public final class MonitoredResourceUtils { public static Resource detectResource() { List<Resource> resourceList = new ArrayList<Resource>(); resourceList.add(Resource.createFromEnvironmentVariables()); - if (System.getenv("KUBERNETES_SERVICE_HOST") != null) { - resourceList.add(GcpGkeContainerMonitoredResource.createResource()); - } else if (GcpMetadataConfig.getInstanceId() != null) { - resourceList.add(GcpGceInstanceMonitoredResource.createResource()); + resourceList.add(K8sContainerResource.detect()); + } + // This can be true even if this is k8s container in case of GKE and we want to merge these + // resources. + if (GcpMetadataConfig.getInstanceId() != null) { + resourceList.add(GcpGceInstanceResource.detect()); } - if (AwsIdentityDocUtils.isRunningOnAwsEc2()) { - resourceList.add(AwsEc2InstanceMonitoredResource.createResource()); + resourceList.add(AwsEc2InstanceResource.detect()); } return Resource.mergeResources(resourceList); } diff --git a/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/ResourceKeyConstants.java b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/ResourceKeyConstants.java index 4dd72044..16db5452 100644 --- a/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/ResourceKeyConstants.java +++ b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/ResourceKeyConstants.java @@ -20,7 +20,10 @@ package io.opencensus.contrib.monitoredresource.util; * Constants for collecting resource information. * * @since 0.18 + * @deprecated use constant values from resource helper {@link AwsEc2InstanceResource}, {@link + * GcpGceInstanceResource} and {@link K8sContainerResource}. */ +@Deprecated public final class ResourceKeyConstants { /** @@ -28,28 +31,28 @@ public final class ResourceKeyConstants { * * @since 0.18 */ - public static final String AWS_EC2_INSTANCE_TYPE = "aws.com/ec2/instance"; + public static final String AWS_EC2_INSTANCE_TYPE = AwsEc2InstanceResource.TYPE; /** * AWS key that represents a region for the VM. * * @since 0.18 */ - public static final String AWS_REGION_KEY = "aws.com/ec2/region"; + public static final String AWS_REGION_KEY = AwsEc2InstanceResource.REGION_KEY; /** * AWS key that represents the AWS account number for the VM. * * @since 0.18 */ - public static final String AWS_ACCOUNT_KEY = "aws.com/ec2/account_id"; + public static final String AWS_ACCOUNT_KEY = AwsEc2InstanceResource.ACCOUNT_ID_KEY; /** * AWS key that represents the VM instance identifier assigned by AWS. * * @since 0.18 */ - public static final String AWS_INSTANCE_ID_KEY = "aws.com/ec2/instance_id"; + public static final String AWS_INSTANCE_ID_KEY = AwsEc2InstanceResource.INSTANCE_ID_KEY; /** * AWS key that represents a prefix for region value. @@ -63,49 +66,49 @@ public final class ResourceKeyConstants { * * @since 0.18 */ - public static final String GCP_GCE_INSTANCE_TYPE = "cloud.google.com/gce/instance"; + public static final String GCP_GCE_INSTANCE_TYPE = GcpGceInstanceResource.TYPE; /** * GCP GCE key that represents the GCP account number for the instance. * * @since 0.18 */ - public static final String GCP_ACCOUNT_ID_KEY = "cloud.google.com/gce/project_id"; + public static final String GCP_ACCOUNT_ID_KEY = GcpGceInstanceResource.PROJECT_ID_KEY; /** * GCP GCE key that represents the numeric VM instance identifier assigned by GCE. * * @since 0.18 */ - public static final String GCP_INSTANCE_ID_KEY = "cloud.google.com/gce/instance_id"; + public static final String GCP_INSTANCE_ID_KEY = GcpGceInstanceResource.INSTANCE_ID_KEY; /** * GCP GCE key that represents the GCE zone in which the VM is running. * * @since 0.18 */ - public static final String GCP_ZONE_KEY = "cloud.google.com/gce/zone"; + public static final String GCP_ZONE_KEY = GcpGceInstanceResource.ZONE_KEY; /** * Kubernetes resources key that represents a type of the resource. * * @since 0.18 */ - public static final String K8S_CONTAINER_TYPE = "k8s.io/container"; + public static final String K8S_CONTAINER_TYPE = K8sContainerResource.TYPE; /** * Kubernetes resources key that represents the name for the cluster the container is running in. * * @since 0.18 */ - public static final String K8S_CLUSTER_NAME_KEY = "k8s.io/cluster/name"; + public static final String K8S_CLUSTER_NAME_KEY = K8sContainerResource.CLUSTER_NAME_KEY; /** * Kubernetes resources key that represents the name of the container. * * @since 0.18 */ - public static final String K8S_CONTAINER_NAME_KEY = "k8s.io/container/name"; + public static final String K8S_CONTAINER_NAME_KEY = K8sContainerResource.CONTAINER_NAME_KEY; /** * Kubernetes resources key that represents the identifier for the GCE instance the container is @@ -113,7 +116,7 @@ public final class ResourceKeyConstants { * * @since 0.18 */ - public static final String K8S_NAMESPACE_NAME_KEY = "k8s.io/namespace/name"; + public static final String K8S_NAMESPACE_NAME_KEY = K8sContainerResource.NAMESPACE_NAME_KEY; /** * Kubernetes resources key that represents the identifier for the pod the container is running @@ -121,7 +124,7 @@ public final class ResourceKeyConstants { * * @since 0.18 */ - public static final String K8S_POD_NAME_KEY = "k8s.io/pod/name"; + public static final String K8S_POD_NAME_KEY = K8sContainerResource.POD_NAME_KEY; private ResourceKeyConstants() {} } diff --git a/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/ResourceType.java b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/ResourceType.java index f2816676..83869e7f 100644 --- a/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/ResourceType.java +++ b/contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/ResourceType.java @@ -21,7 +21,9 @@ package io.opencensus.contrib.monitoredresource.util; * automatically detected by OpenCensus. * * @since 0.13 + * @deprecated Use {@link ResourceKeyConstants}. */ +@Deprecated public enum ResourceType { /** diff --git a/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/AwsEc2InstanceResourceTest.java b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/AwsEc2InstanceResourceTest.java new file mode 100644 index 00000000..b36cdf67 --- /dev/null +++ b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/AwsEc2InstanceResourceTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2019, OpenCensus 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.opencensus.contrib.monitoredresource.util; + +import static com.google.common.truth.Truth.assertThat; + +import io.opencensus.resource.Resource; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link AwsEc2InstanceResource}. */ +@RunWith(JUnit4.class) +public class AwsEc2InstanceResourceTest { + private static final String AWS_ACCOUNT_ID = "aws-account"; + private static final String AWS_INSTANCE_ID = "instance"; + private static final String AWS_REGION = "us-west-2"; + + @Test + public void create_AwsEc2InstanceResource() { + Resource resource = AwsEc2InstanceResource.create(AWS_ACCOUNT_ID, AWS_REGION, AWS_INSTANCE_ID); + assertThat(resource.getType()).isEqualTo(AwsEc2InstanceResource.TYPE); + assertThat(resource.getLabels()) + .containsExactly( + AwsEc2InstanceResource.ACCOUNT_ID_KEY, + AWS_ACCOUNT_ID, + AwsEc2InstanceResource.REGION_KEY, + AWS_REGION, + AwsEc2InstanceResource.INSTANCE_ID_KEY, + AWS_INSTANCE_ID); + } +} diff --git a/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/GcpGceInstanceResourceTest.java b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/GcpGceInstanceResourceTest.java new file mode 100644 index 00000000..c1c80b7c --- /dev/null +++ b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/GcpGceInstanceResourceTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2019, OpenCensus 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.opencensus.contrib.monitoredresource.util; + +import static com.google.common.truth.Truth.assertThat; + +import io.opencensus.resource.Resource; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link GcpGceInstanceResource}. */ +@RunWith(JUnit4.class) +public class GcpGceInstanceResourceTest { + private static final String GCP_PROJECT_ID = "gcp-project"; + private static final String GCP_INSTANCE_ID = "instance"; + private static final String GCP_ZONE = "us-east1"; + + @Test + public void create_GcpGceInstanceResource() { + Resource resource = GcpGceInstanceResource.create(GCP_PROJECT_ID, GCP_ZONE, GCP_INSTANCE_ID); + assertThat(resource.getType()).isEqualTo(GcpGceInstanceResource.TYPE); + assertThat(resource.getLabels()) + .containsExactly( + GcpGceInstanceResource.PROJECT_ID_KEY, + GCP_PROJECT_ID, + GcpGceInstanceResource.ZONE_KEY, + GCP_ZONE, + GcpGceInstanceResource.INSTANCE_ID_KEY, + GCP_INSTANCE_ID); + } +} diff --git a/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/K8sContainerResourceTest.java b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/K8sContainerResourceTest.java new file mode 100644 index 00000000..dfcb2728 --- /dev/null +++ b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/K8sContainerResourceTest.java @@ -0,0 +1,51 @@ +/* + * Copyright 2019, OpenCensus 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.opencensus.contrib.monitoredresource.util; + +import static com.google.common.truth.Truth.assertThat; + +import io.opencensus.resource.Resource; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link K8sContainerResource}. */ +@RunWith(JUnit4.class) +public class K8sContainerResourceTest { + private static final String K8S_CLUSTER_NAME = "cluster"; + private static final String K8S_NAMESPACE_NAME = "namespace"; + private static final String K8S_POD_NAME = "pod-id"; + private static final String K8S_CONTAINER_NAME = "container"; + + @Test + public void create_K8sContainerResourceTest() { + Resource resource = + K8sContainerResource.create( + K8S_CLUSTER_NAME, K8S_NAMESPACE_NAME, K8S_POD_NAME, K8S_CONTAINER_NAME); + assertThat(resource.getType()).isEqualTo(K8sContainerResource.TYPE); + assertThat(resource.getLabels()) + .containsExactly( + K8sContainerResource.CLUSTER_NAME_KEY, + K8S_CLUSTER_NAME, + K8sContainerResource.NAMESPACE_NAME_KEY, + K8S_NAMESPACE_NAME, + K8sContainerResource.POD_NAME_KEY, + K8S_POD_NAME, + K8sContainerResource.CONTAINER_NAME_KEY, + K8S_CONTAINER_NAME); + } +} diff --git a/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceTest.java b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceTest.java index 0defcbd7..4fa341cb 100644 --- a/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceTest.java +++ b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceTest.java @@ -18,9 +18,6 @@ package io.opencensus.contrib.monitoredresource.util; import static com.google.common.truth.Truth.assertThat; -import io.opencensus.contrib.monitoredresource.util.MonitoredResource.AwsEc2InstanceMonitoredResource; -import io.opencensus.contrib.monitoredresource.util.MonitoredResource.GcpGceInstanceMonitoredResource; -import io.opencensus.contrib.monitoredresource.util.MonitoredResource.GcpGkeContainerMonitoredResource; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -42,8 +39,9 @@ public class MonitoredResourceTest { @Test public void testAwsEc2InstanceMonitoredResource() { - AwsEc2InstanceMonitoredResource resource = - AwsEc2InstanceMonitoredResource.create(AWS_ACCOUNT, AWS_INSTANCE, AWS_REGION); + MonitoredResource.AwsEc2InstanceMonitoredResource resource = + MonitoredResource.AwsEc2InstanceMonitoredResource.create( + AWS_ACCOUNT, AWS_INSTANCE, AWS_REGION); assertThat(resource.getResourceType()).isEqualTo(ResourceType.AWS_EC2_INSTANCE); assertThat(resource.getAccount()).isEqualTo(AWS_ACCOUNT); assertThat(resource.getInstanceId()).isEqualTo(AWS_INSTANCE); @@ -52,8 +50,9 @@ public class MonitoredResourceTest { @Test public void testGcpGceInstanceMonitoredResource() { - GcpGceInstanceMonitoredResource resource = - GcpGceInstanceMonitoredResource.create(GCP_PROJECT, GCP_INSTANCE, GCP_ZONE); + MonitoredResource.GcpGceInstanceMonitoredResource resource = + MonitoredResource.GcpGceInstanceMonitoredResource.create( + GCP_PROJECT, GCP_INSTANCE, GCP_ZONE); assertThat(resource.getResourceType()).isEqualTo(ResourceType.GCP_GCE_INSTANCE); assertThat(resource.getAccount()).isEqualTo(GCP_PROJECT); assertThat(resource.getInstanceId()).isEqualTo(GCP_INSTANCE); @@ -62,8 +61,8 @@ public class MonitoredResourceTest { @Test public void testGcpGkeContainerMonitoredResource() { - GcpGkeContainerMonitoredResource resource = - GcpGkeContainerMonitoredResource.create( + MonitoredResource.GcpGkeContainerMonitoredResource resource = + MonitoredResource.GcpGkeContainerMonitoredResource.create( GCP_PROJECT, GCP_GKE_CLUSTER_NAME, GCP_GKE_CONTAINER_NAME, diff --git a/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceUtilsTest.java b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceUtilsTest.java index 6bc0efc9..877631a3 100644 --- a/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceUtilsTest.java +++ b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceUtilsTest.java @@ -45,11 +45,11 @@ public class MonitoredResourceUtilsTest { public void testDetectResource() { Resource resource = MonitoredResourceUtils.detectResource(); if (System.getenv("KUBERNETES_SERVICE_HOST") != null) { - assertThat(resource.getType()).isEqualTo(ResourceKeyConstants.K8S_CONTAINER_TYPE); + assertThat(resource.getType()).isEqualTo(K8sContainerResource.TYPE); } else if (GcpMetadataConfig.getInstanceId() != null) { - assertThat(resource.getType()).isEqualTo(ResourceKeyConstants.GCP_GCE_INSTANCE_TYPE); + assertThat(resource.getType()).isEqualTo(GcpGceInstanceResource.TYPE); } else if (AwsIdentityDocUtils.isRunningOnAwsEc2()) { - assertThat(resource.getType()).isEqualTo(ResourceKeyConstants.AWS_EC2_INSTANCE_TYPE); + assertThat(resource.getType()).isEqualTo(AwsEc2InstanceResource.TYPE); } else { assertThat(resource).isNotNull(); assertThat(resource.getType()).isNull(); |
