From 890f1414bfac1419190f2de025252195264b38b6 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Tue, 29 Jan 2019 17:33:00 -0800 Subject: [PATCH 1/4] Deprecate MonitoredResource and change to use Resource. --- contrib/monitored_resource_util/build.gradle | 3 +- .../util/AwsEc2InstanceResource.java | 90 +++++++++ .../util/GcpGceInstanceResource.java | 87 +++++++++ .../util/K8sContainerResource.java | 100 ++++++++++ .../util/MonitoredResource.java | 178 ++++++++---------- .../util/MonitoredResourceUtils.java | 19 +- .../util/ResourceKeyConstants.java | 29 +-- .../monitoredresource/util/ResourceType.java | 2 + .../util/AwsEc2InstanceResourceTest.java | 46 +++++ .../util/GcpGceInstanceResourceTest.java | 46 +++++ .../util/K8sContainerResourceTest.java | 51 +++++ .../util/MonitoredResourceUtilsTest.java | 6 +- .../StackdriverV2ExporterHandler.java | 2 +- 13 files changed, 533 insertions(+), 126 deletions(-) create mode 100644 contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/AwsEc2InstanceResource.java create mode 100644 contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/GcpGceInstanceResource.java create mode 100644 contrib/monitored_resource_util/src/main/java/io/opencensus/contrib/monitoredresource/util/K8sContainerResource.java create mode 100644 contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/AwsEc2InstanceResourceTest.java create mode 100644 contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/GcpGceInstanceResourceTest.java create mode 100644 contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/K8sContainerResourceTest.java diff --git a/contrib/monitored_resource_util/build.gradle b/contrib/monitored_resource_util/build.gradle index 069f49c6a7..3c163a17f7 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 0000000000..66499e8f08 --- /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 mutableLabels = new LinkedHashMap(); + 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 0000000000..a6731fdca4 --- /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 mutableLabels = new LinkedHashMap(); + 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 0000000000..b9abe1fb19 --- /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 mutableLabels = new LinkedHashMap(); + 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 70b64c0b84..da8f21019f 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 @@ * 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 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 @@ private static T firstNonNull(@Nullable T first, @Nullable T second) { * {@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 awsInstanceLabels; @Override public ResourceType getResourceType() { @@ -87,7 +69,9 @@ public ResourceType getResourceType() { * @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 ResourceType getResourceType() { * @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 ResourceType getResourceType() { * @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 ResourceType getResourceType() { */ 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 labels = new LinkedHashMap(); - 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 awsInstanceLabels) { + this.awsInstanceLabels = awsInstanceLabels; } } @@ -137,15 +121,12 @@ static Resource createResource() { * {@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 gcpInstanceLabels; @Override public ResourceType getResourceType() { @@ -158,7 +139,9 @@ public ResourceType getResourceType() { * @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 ResourceType getResourceType() { * @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 ResourceType getResourceType() { * @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 ResourceType getResourceType() { */ 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 labels = new LinkedHashMap(); - 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 gcpInstanceLabels) { + this.gcpInstanceLabels = gcpInstanceLabels; } } @@ -208,21 +191,13 @@ static Resource createResource() { * {@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 k8sContainerLabels; + private final Map gcpInstanceLabels; @Override public ResourceType getResourceType() { @@ -235,7 +210,9 @@ public ResourceType getResourceType() { * @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 ResourceType getResourceType() { * @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 ResourceType getResourceType() { * @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 ResourceType getResourceType() { * @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 ResourceType getResourceType() { * @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 ResourceType getResourceType() { * @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 ResourceType getResourceType() { * @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 static GcpGkeContainerMonitoredResource create( 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 labels = new LinkedHashMap(); - 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 k8sContainerLabels, Map 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 b6b00ca645..da758f25f4 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 @@ -44,13 +44,13 @@ public final class MonitoredResourceUtils { @Nullable public static MonitoredResource getDefaultResource() { if (System.getenv("KUBERNETES_SERVICE_HOST") != null) { - return GcpGkeContainerMonitoredResource.create(); + return GcpGkeContainerMonitoredResource.autoDetectResource(); } if (GcpMetadataConfig.getInstanceId() != null) { - return GcpGceInstanceMonitoredResource.create(); + return GcpGceInstanceMonitoredResource.autoDetectResource(); } if (AwsIdentityDocUtils.isRunningOnAwsEc2()) { - return AwsEc2InstanceMonitoredResource.create(); + return AwsEc2InstanceMonitoredResource.autoDetectResource(); } return null; } @@ -66,15 +66,16 @@ public static MonitoredResource getDefaultResource() { public static Resource detectResource() { List resourceList = new ArrayList(); 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 4dd7204409..16db54529d 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 @@ * 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 f281667631..83869e7fa5 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 @@ * 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 0000000000..b36cdf6787 --- /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 0000000000..c1c80b7c4f --- /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 0000000000..dfcb272847 --- /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/MonitoredResourceUtilsTest.java b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceUtilsTest.java index 6bc0efc9d3..877631a31b 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 void testGetDefaultResource() { 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(); diff --git a/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java index b677dab943..364388dd70 100644 --- a/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java +++ b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java @@ -75,6 +75,7 @@ */ /** Exporter to Stackdriver Trace API v2. */ +@SuppressWarnings("deprecation") final class StackdriverV2ExporterHandler extends SpanExporter.Handler { private static final Tracer tracer = Tracing.getTracer(); @@ -100,7 +101,6 @@ final class StackdriverV2ExporterHandler extends SpanExporter.Handler { .build(); @javax.annotation.Nullable - @SuppressWarnings("deprecation") private static final MonitoredResource RESOURCE = MonitoredResourceUtils.getDefaultResource(); // Only initialize once. From a53aa5b64c35d9c37f4d53523beb69e571429d88 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Tue, 29 Jan 2019 21:06:47 -0800 Subject: [PATCH 2/4] Fix deprecated usages and imports. --- .../util/MonitoredResourceUtils.java | 9 ++---- .../util/MonitoredResourceTest.java | 15 ++++----- .../stackdriver/StackdriverExportUtils.java | 31 ++++++++++--------- 3 files changed, 26 insertions(+), 29 deletions(-) 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 da758f25f4..b617fdf695 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.autoDetectResource(); + return MonitoredResource.GcpGkeContainerMonitoredResource.autoDetectResource(); } if (GcpMetadataConfig.getInstanceId() != null) { - return GcpGceInstanceMonitoredResource.autoDetectResource(); + return MonitoredResource.GcpGceInstanceMonitoredResource.autoDetectResource(); } if (AwsIdentityDocUtils.isRunningOnAwsEc2()) { - return AwsEc2InstanceMonitoredResource.autoDetectResource(); + return MonitoredResource.AwsEc2InstanceMonitoredResource.autoDetectResource(); } return null; } 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 0defcbd7c4..10255a7ae0 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 @@ 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,8 @@ 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 +49,8 @@ public void testAwsEc2InstanceMonitoredResource() { @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 +59,8 @@ public void testGcpGceInstanceMonitoredResource() { @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/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtils.java b/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtils.java index 0e841f8a26..85c9ea4ccc 100644 --- a/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtils.java +++ b/exporters/stats/stackdriver/src/main/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtils.java @@ -37,8 +37,10 @@ import com.google.protobuf.Timestamp; import io.opencensus.common.Function; import io.opencensus.common.Functions; +import io.opencensus.contrib.monitoredresource.util.AwsEc2InstanceResource; +import io.opencensus.contrib.monitoredresource.util.GcpGceInstanceResource; +import io.opencensus.contrib.monitoredresource.util.K8sContainerResource; import io.opencensus.contrib.monitoredresource.util.MonitoredResourceUtils; -import io.opencensus.contrib.monitoredresource.util.ResourceKeyConstants; import io.opencensus.metrics.LabelKey; import io.opencensus.metrics.LabelValue; import io.opencensus.metrics.export.Distribution.Bucket; @@ -412,15 +414,15 @@ static void setResourceForBuilder( Map mappings; switch (type) { - case ResourceKeyConstants.GCP_GCE_INSTANCE_TYPE: + case GcpGceInstanceResource.TYPE: builder.setType(GCP_GCE_INSTANCE); mappings = GCP_RESOURCE_MAPPING; break; - case ResourceKeyConstants.K8S_CONTAINER_TYPE: + case K8sContainerResource.TYPE: builder.setType(GCP_GKE_CONTAINER); mappings = K8S_RESOURCE_MAPPING; break; - case ResourceKeyConstants.AWS_EC2_INSTANCE_TYPE: + case AwsEc2InstanceResource.TYPE: builder.setType(AWS_EC2_INSTANCE); mappings = AWS_RESOURCE_MAPPING; break; @@ -561,28 +563,29 @@ private static void createTimeSeries( private static Map getGcpResourceLabelsMappings() { Map resourceLabels = new LinkedHashMap(); resourceLabels.put("project_id", STACKDRIVER_PROJECT_ID_KEY); - resourceLabels.put("instance_id", ResourceKeyConstants.GCP_INSTANCE_ID_KEY); - resourceLabels.put("zone", ResourceKeyConstants.GCP_ZONE_KEY); + resourceLabels.put("instance_id", GcpGceInstanceResource.INSTANCE_ID_KEY); + resourceLabels.put("zone", GcpGceInstanceResource.ZONE_KEY); return Collections.unmodifiableMap(resourceLabels); } private static Map getK8sResourceLabelsMappings() { Map resourceLabels = new LinkedHashMap(); resourceLabels.put("project_id", STACKDRIVER_PROJECT_ID_KEY); - resourceLabels.put("location", ResourceKeyConstants.GCP_ZONE_KEY); - resourceLabels.put("cluster_name", ResourceKeyConstants.K8S_CLUSTER_NAME_KEY); - resourceLabels.put("namespace_name", ResourceKeyConstants.K8S_NAMESPACE_NAME_KEY); - resourceLabels.put("pod_name", ResourceKeyConstants.K8S_POD_NAME_KEY); - resourceLabels.put("container_name", ResourceKeyConstants.K8S_CONTAINER_NAME_KEY); + resourceLabels.put("location", GcpGceInstanceResource.ZONE_KEY); + resourceLabels.put("instance_id", GcpGceInstanceResource.INSTANCE_ID_KEY); + resourceLabels.put("cluster_name", K8sContainerResource.CLUSTER_NAME_KEY); + resourceLabels.put("namespace_name", K8sContainerResource.NAMESPACE_NAME_KEY); + resourceLabels.put("pod_name", K8sContainerResource.POD_NAME_KEY); + resourceLabels.put("container_name", K8sContainerResource.CONTAINER_NAME_KEY); return Collections.unmodifiableMap(resourceLabels); } private static Map getAwsResourceLabelsMappings() { Map resourceLabels = new LinkedHashMap(); resourceLabels.put("project_id", STACKDRIVER_PROJECT_ID_KEY); - resourceLabels.put("instance_id", ResourceKeyConstants.AWS_INSTANCE_ID_KEY); - resourceLabels.put("region", ResourceKeyConstants.AWS_REGION_KEY); - resourceLabels.put("aws_account", ResourceKeyConstants.AWS_ACCOUNT_KEY); + resourceLabels.put("instance_id", AwsEc2InstanceResource.INSTANCE_ID_KEY); + resourceLabels.put("region", AwsEc2InstanceResource.REGION_KEY); + resourceLabels.put("aws_account", AwsEc2InstanceResource.ACCOUNT_ID_KEY); return Collections.unmodifiableMap(resourceLabels); } From 91f8e459b7348c4bbab22d91bcb28873ff50d56f Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Tue, 29 Jan 2019 21:22:44 -0800 Subject: [PATCH 3/4] Resolve more deprecation usage warnings. --- .../util/MonitoredResourceTest.java | 6 ++-- .../StackdriverExportUtilsTest.java | 36 ++++++++++--------- .../StackdriverV2ExporterHandler.java | 23 ++++++------ 3 files changed, 35 insertions(+), 30 deletions(-) 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 10255a7ae0..4fa341cb38 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 @@ -40,7 +40,8 @@ public class MonitoredResourceTest { @Test public void testAwsEc2InstanceMonitoredResource() { MonitoredResource.AwsEc2InstanceMonitoredResource resource = - MonitoredResource.AwsEc2InstanceMonitoredResource.create(AWS_ACCOUNT, AWS_INSTANCE, AWS_REGION); + 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); @@ -50,7 +51,8 @@ public void testAwsEc2InstanceMonitoredResource() { @Test public void testGcpGceInstanceMonitoredResource() { MonitoredResource.GcpGceInstanceMonitoredResource resource = - MonitoredResource.GcpGceInstanceMonitoredResource.create(GCP_PROJECT, GCP_INSTANCE, GCP_ZONE); + 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); diff --git a/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtilsTest.java b/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtilsTest.java index ede8a0b544..b68ea8b601 100644 --- a/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtilsTest.java +++ b/exporters/stats/stackdriver/src/test/java/io/opencensus/exporter/stats/stackdriver/StackdriverExportUtilsTest.java @@ -37,7 +37,9 @@ import com.google.monitoring.v3.TimeSeries; import com.google.monitoring.v3.TypedValue; import io.opencensus.common.Timestamp; -import io.opencensus.contrib.monitoredresource.util.ResourceKeyConstants; +import io.opencensus.contrib.monitoredresource.util.AwsEc2InstanceResource; +import io.opencensus.contrib.monitoredresource.util.GcpGceInstanceResource; +import io.opencensus.contrib.monitoredresource.util.K8sContainerResource; import io.opencensus.metrics.LabelKey; import io.opencensus.metrics.LabelValue; import io.opencensus.metrics.export.Distribution.Bucket; @@ -638,15 +640,15 @@ public void convertSummaryMetricWithNullSum() { public void setResourceForBuilder_GcpInstanceType() { MonitoredResource.Builder monitoredResourceBuilder = DEFAULT_RESOURCE_WITH_PROJECT_ID.clone(); Map resourceLabels = new HashMap(); - resourceLabels.put(ResourceKeyConstants.GCP_ACCOUNT_ID_KEY, "proj1"); - resourceLabels.put(ResourceKeyConstants.GCP_INSTANCE_ID_KEY, "inst1"); - resourceLabels.put(ResourceKeyConstants.GCP_ZONE_KEY, "zone1"); + resourceLabels.put(GcpGceInstanceResource.PROJECT_ID_KEY, "proj1"); + resourceLabels.put(GcpGceInstanceResource.INSTANCE_ID_KEY, "inst1"); + resourceLabels.put(GcpGceInstanceResource.ZONE_KEY, "zone1"); resourceLabels.put("extra_key", "must be ignored"); Map expectedResourceLabels = new HashMap(); expectedResourceLabels.put("project_id", "proj1"); expectedResourceLabels.put("instance_id", "inst1"); expectedResourceLabels.put("zone", "zone1"); - Resource resource = Resource.create(ResourceKeyConstants.GCP_GCE_INSTANCE_TYPE, resourceLabels); + Resource resource = Resource.create(GcpGceInstanceResource.TYPE, resourceLabels); StackdriverExportUtils.setResourceForBuilder(monitoredResourceBuilder, resource); @@ -662,27 +664,29 @@ public void setResourceForBuilder_GcpInstanceType() { public void setResourceForBuilder_K8sInstanceType() { MonitoredResource.Builder monitoredResourceBuilder = DEFAULT_RESOURCE_WITH_PROJECT_ID.clone(); Map resourceLabels = new HashMap(); - resourceLabels.put(ResourceKeyConstants.GCP_ZONE_KEY, "zone1"); - resourceLabels.put(ResourceKeyConstants.K8S_CLUSTER_NAME_KEY, "cluster1"); - resourceLabels.put(ResourceKeyConstants.K8S_CONTAINER_NAME_KEY, "container1"); - resourceLabels.put(ResourceKeyConstants.K8S_NAMESPACE_NAME_KEY, "namespace1"); - resourceLabels.put(ResourceKeyConstants.K8S_POD_NAME_KEY, "pod1"); + resourceLabels.put(GcpGceInstanceResource.ZONE_KEY, "zone1"); + resourceLabels.put(GcpGceInstanceResource.INSTANCE_ID_KEY, "instance1"); + resourceLabels.put(K8sContainerResource.CLUSTER_NAME_KEY, "cluster1"); + resourceLabels.put(K8sContainerResource.CONTAINER_NAME_KEY, "container1"); + resourceLabels.put(K8sContainerResource.NAMESPACE_NAME_KEY, "namespace1"); + resourceLabels.put(K8sContainerResource.POD_NAME_KEY, "pod1"); resourceLabels.put("extra_key", "must be ignored"); Map expectedResourceLabels = new HashMap(); expectedResourceLabels.put("project_id", "proj1"); expectedResourceLabels.put("location", "zone1"); + expectedResourceLabels.put("instance_id", "instance1"); expectedResourceLabels.put("cluster_name", "cluster1"); expectedResourceLabels.put("namespace_name", "namespace1"); expectedResourceLabels.put("pod_name", "pod1"); expectedResourceLabels.put("container_name", "container1"); - Resource resource = Resource.create(ResourceKeyConstants.K8S_CONTAINER_TYPE, resourceLabels); + Resource resource = Resource.create(K8sContainerResource.TYPE, resourceLabels); StackdriverExportUtils.setResourceForBuilder(monitoredResourceBuilder, resource); assertThat(monitoredResourceBuilder.getType()).isNotNull(); assertThat(monitoredResourceBuilder.getLabelsMap()).isNotEmpty(); assertThat(monitoredResourceBuilder.getType()).isEqualTo("k8s_container"); - assertThat(monitoredResourceBuilder.getLabelsMap().size()).isEqualTo(6); + assertThat(monitoredResourceBuilder.getLabelsMap().size()).isEqualTo(7); assertThat(monitoredResourceBuilder.getLabelsMap()) .containsExactlyEntriesIn(expectedResourceLabels); } @@ -691,9 +695,9 @@ public void setResourceForBuilder_K8sInstanceType() { public void setResourceForBuilder_AwsInstanceType() { MonitoredResource.Builder monitoredResourceBuilder = DEFAULT_RESOURCE_WITH_PROJECT_ID.clone(); Map resourceLabels = new HashMap(); - resourceLabels.put(ResourceKeyConstants.AWS_REGION_KEY, "region1"); - resourceLabels.put(ResourceKeyConstants.AWS_ACCOUNT_KEY, "account1"); - resourceLabels.put(ResourceKeyConstants.AWS_INSTANCE_ID_KEY, "instance1"); + resourceLabels.put(AwsEc2InstanceResource.REGION_KEY, "region1"); + resourceLabels.put(AwsEc2InstanceResource.ACCOUNT_ID_KEY, "account1"); + resourceLabels.put(AwsEc2InstanceResource.INSTANCE_ID_KEY, "instance1"); resourceLabels.put("extra_key", "must be ignored"); Map expectedResourceLabels = new HashMap(); expectedResourceLabels.put("project_id", "proj1"); @@ -701,7 +705,7 @@ public void setResourceForBuilder_AwsInstanceType() { expectedResourceLabels.put("region", "region1"); expectedResourceLabels.put("aws_account", "account1"); - Resource resource = Resource.create(ResourceKeyConstants.AWS_EC2_INSTANCE_TYPE, resourceLabels); + Resource resource = Resource.create(AwsEc2InstanceResource.TYPE, resourceLabels); StackdriverExportUtils.setResourceForBuilder(monitoredResourceBuilder, resource); diff --git a/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java index 364388dd70..33a66c4bef 100644 --- a/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java +++ b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java @@ -42,12 +42,7 @@ import io.opencensus.common.OpenCensusLibraryInformation; import io.opencensus.common.Scope; import io.opencensus.common.Timestamp; -import io.opencensus.contrib.monitoredresource.util.MonitoredResource; -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.contrib.monitoredresource.util.MonitoredResourceUtils; -import io.opencensus.contrib.monitoredresource.util.ResourceType; +import io.opencensus.contrib.monitoredresource.util.*; import io.opencensus.trace.Annotation; import io.opencensus.trace.EndSpanOptions; import io.opencensus.trace.MessageEvent.Type; @@ -74,6 +69,10 @@ import org.checkerframework.checker.nullness.qual.Nullable; */ +// Import all monitoredresource.util package to avoid deprecated import warning until this class is +// changed to the new Resource API. +// TODO: Remove this when change the class to use Resource instead of MonitoredResource. + /** Exporter to Stackdriver Trace API v2. */ @SuppressWarnings("deprecation") final class StackdriverV2ExporterHandler extends SpanExporter.Handler { @@ -333,8 +332,8 @@ static Map getResourceLabels( ResourceType resourceType = resource.getResourceType(); switch (resourceType) { case AWS_EC2_INSTANCE: - AwsEc2InstanceMonitoredResource awsEc2InstanceMonitoredResource = - (AwsEc2InstanceMonitoredResource) resource; + MonitoredResource.AwsEc2InstanceMonitoredResource awsEc2InstanceMonitoredResource = + (MonitoredResource.AwsEc2InstanceMonitoredResource) resource; putToResourceAttributeMap( resourceLabels, resourceType, @@ -352,8 +351,8 @@ static Map getResourceLabels( "aws:" + awsEc2InstanceMonitoredResource.getRegion()); return Collections.unmodifiableMap(resourceLabels); case GCP_GCE_INSTANCE: - GcpGceInstanceMonitoredResource gcpGceInstanceMonitoredResource = - (GcpGceInstanceMonitoredResource) resource; + MonitoredResource.GcpGceInstanceMonitoredResource gcpGceInstanceMonitoredResource = + (MonitoredResource.GcpGceInstanceMonitoredResource) resource; putToResourceAttributeMap( resourceLabels, resourceType, @@ -368,8 +367,8 @@ static Map getResourceLabels( resourceLabels, resourceType, "zone", gcpGceInstanceMonitoredResource.getZone()); return Collections.unmodifiableMap(resourceLabels); case GCP_GKE_CONTAINER: - GcpGkeContainerMonitoredResource gcpGkeContainerMonitoredResource = - (GcpGkeContainerMonitoredResource) resource; + MonitoredResource.GcpGkeContainerMonitoredResource gcpGkeContainerMonitoredResource = + (MonitoredResource.GcpGkeContainerMonitoredResource) resource; putToResourceAttributeMap( resourceLabels, resourceType, From 53cf3794b567a7e923bc294371f6b35b11850c9e Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Wed, 30 Jan 2019 14:28:09 -0800 Subject: [PATCH 4/4] Use full class names for deprecated classes --- .../StackdriverV2ExporterHandler.java | 47 ++++++++++++------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java index 33a66c4bef..f3d249e94b 100644 --- a/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java +++ b/exporters/trace/stackdriver/src/main/java/io/opencensus/exporter/trace/stackdriver/StackdriverV2ExporterHandler.java @@ -42,7 +42,6 @@ import io.opencensus.common.OpenCensusLibraryInformation; import io.opencensus.common.Scope; import io.opencensus.common.Timestamp; -import io.opencensus.contrib.monitoredresource.util.*; import io.opencensus.trace.Annotation; import io.opencensus.trace.EndSpanOptions; import io.opencensus.trace.MessageEvent.Type; @@ -69,10 +68,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; */ -// Import all monitoredresource.util package to avoid deprecated import warning until this class is -// changed to the new Resource API. -// TODO: Remove this when change the class to use Resource instead of MonitoredResource. - /** Exporter to Stackdriver Trace API v2. */ @SuppressWarnings("deprecation") final class StackdriverV2ExporterHandler extends SpanExporter.Handler { @@ -100,7 +95,8 @@ final class StackdriverV2ExporterHandler extends SpanExporter.Handler { .build(); @javax.annotation.Nullable - private static final MonitoredResource RESOURCE = MonitoredResourceUtils.getDefaultResource(); + private static final io.opencensus.contrib.monitoredresource.util.MonitoredResource RESOURCE = + io.opencensus.contrib.monitoredresource.util.MonitoredResourceUtils.getDefaultResource(); // Only initialize once. private static final Map RESOURCE_LABELS = getResourceLabels(RESOURCE); @@ -324,16 +320,22 @@ private static Attributes.Builder toAttributesBuilderProto( @VisibleForTesting static Map getResourceLabels( - @javax.annotation.Nullable MonitoredResource resource) { + @javax.annotation.Nullable + io.opencensus.contrib.monitoredresource.util.MonitoredResource resource) { if (resource == null) { return Collections.emptyMap(); } Map resourceLabels = new HashMap(); - ResourceType resourceType = resource.getResourceType(); + io.opencensus.contrib.monitoredresource.util.ResourceType resourceType = + resource.getResourceType(); switch (resourceType) { case AWS_EC2_INSTANCE: - MonitoredResource.AwsEc2InstanceMonitoredResource awsEc2InstanceMonitoredResource = - (MonitoredResource.AwsEc2InstanceMonitoredResource) resource; + io.opencensus.contrib.monitoredresource.util.MonitoredResource + .AwsEc2InstanceMonitoredResource + awsEc2InstanceMonitoredResource = + (io.opencensus.contrib.monitoredresource.util.MonitoredResource + .AwsEc2InstanceMonitoredResource) + resource; putToResourceAttributeMap( resourceLabels, resourceType, @@ -351,8 +353,12 @@ static Map getResourceLabels( "aws:" + awsEc2InstanceMonitoredResource.getRegion()); return Collections.unmodifiableMap(resourceLabels); case GCP_GCE_INSTANCE: - MonitoredResource.GcpGceInstanceMonitoredResource gcpGceInstanceMonitoredResource = - (MonitoredResource.GcpGceInstanceMonitoredResource) resource; + io.opencensus.contrib.monitoredresource.util.MonitoredResource + .GcpGceInstanceMonitoredResource + gcpGceInstanceMonitoredResource = + (io.opencensus.contrib.monitoredresource.util.MonitoredResource + .GcpGceInstanceMonitoredResource) + resource; putToResourceAttributeMap( resourceLabels, resourceType, @@ -367,8 +373,12 @@ static Map getResourceLabels( resourceLabels, resourceType, "zone", gcpGceInstanceMonitoredResource.getZone()); return Collections.unmodifiableMap(resourceLabels); case GCP_GKE_CONTAINER: - MonitoredResource.GcpGkeContainerMonitoredResource gcpGkeContainerMonitoredResource = - (MonitoredResource.GcpGkeContainerMonitoredResource) resource; + io.opencensus.contrib.monitoredresource.util.MonitoredResource + .GcpGkeContainerMonitoredResource + gcpGkeContainerMonitoredResource = + (io.opencensus.contrib.monitoredresource.util.MonitoredResource + .GcpGkeContainerMonitoredResource) + resource; putToResourceAttributeMap( resourceLabels, resourceType, @@ -400,7 +410,7 @@ static Map getResourceLabels( private static void putToResourceAttributeMap( Map map, - ResourceType resourceType, + io.opencensus.contrib.monitoredresource.util.ResourceType resourceType, String attributeName, String attributeValue) { map.put( @@ -409,11 +419,14 @@ private static void putToResourceAttributeMap( } @VisibleForTesting - static String createResourceLabelKey(ResourceType resourceType, String resourceAttribute) { + static String createResourceLabelKey( + io.opencensus.contrib.monitoredresource.util.ResourceType resourceType, + String resourceAttribute) { return String.format("g.co/r/%s/%s", mapToStringResourceType(resourceType), resourceAttribute); } - private static String mapToStringResourceType(ResourceType resourceType) { + private static String mapToStringResourceType( + io.opencensus.contrib.monitoredresource.util.ResourceType resourceType) { switch (resourceType) { case GCP_GCE_INSTANCE: return "gce_instance";