From 925582f4634f20edb82339ebbbdc99b7e5c3482d Mon Sep 17 00:00:00 2001 From: songy23 Date: Mon, 11 Jun 2018 19:06:16 -0700 Subject: [PATCH 1/3] Expose the constructor of MonitoredResource. --- CHANGELOG.md | 1 + .../util/MonitoredResource.java | 81 ++++++++++++++++-- .../util/MonitoredResourceTest.java | 83 +++++++++++++++++++ 3 files changed, 160 insertions(+), 5 deletions(-) create mode 100644 contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c6709d8d4..838b7c5d58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ## Unreleased +- Expose the constructor of MonitoredResource. ## 0.14.0 - 2018-06-04 - Adds Tracing.getExportComponent().shutdown() for use within application shutdown hooks. 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 c7e91117fb..829e3cc52a 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 @@ -102,9 +102,29 @@ public ResourceType getResourceType() { */ public abstract String getRegion(); - static AwsEc2InstanceMonitoredResource create() { + /** + * Returns an {@link AwsEc2InstanceMonitoredResource}. + * + * @param account the AWS account ID. + * @param instanceId the AWS EC2 instance ID. + * @param region the AWS region. + * @return an {@code AwsEc2InstanceMonitoredResource}. + * @since 0.15 + */ + public static AwsEc2InstanceMonitoredResource create( + String account, String instanceId, String region) { return new AutoValue_MonitoredResource_AwsEc2InstanceMonitoredResource( - AWS_ACCOUNT, AWS_INSTANCE_ID, AWS_REGION); + account, instanceId, region); + } + + /** + * Returns an {@link AwsEc2InstanceMonitoredResource} with the default parameters. + * + * @return an {@code AwsEc2InstanceMonitoredResource}. + * @since 0.15 + */ + public static AwsEc2InstanceMonitoredResource create() { + return create(AWS_ACCOUNT, AWS_INSTANCE_ID, AWS_REGION); } } @@ -151,9 +171,29 @@ public ResourceType getResourceType() { */ public abstract String getZone(); - static GcpGceInstanceMonitoredResource create() { + /** + * Returns a {@link GcpGceInstanceMonitoredResource}. + * + * @param account the GCP account number. + * @param instanceId the GCP GCE instance ID. + * @param zone the GCP zone. + * @return a {@code GcpGceInstanceMonitoredResource}. + * @since 0.15 + */ + public static GcpGceInstanceMonitoredResource create( + String account, String instanceId, String zone) { return new AutoValue_MonitoredResource_GcpGceInstanceMonitoredResource( - GCP_ACCOUNT_ID, GCP_INSTANCE_ID, GCP_ZONE); + account, instanceId, zone); + } + + /** + * Returns a {@link GcpGceInstanceMonitoredResource} with the default parameters. + * + * @return a {@code GcpGceInstanceMonitoredResource}. + * @since 0.15 + */ + public static GcpGceInstanceMonitoredResource create() { + return create(GCP_ACCOUNT_ID, GCP_INSTANCE_ID, GCP_ZONE); } } @@ -238,8 +278,39 @@ public ResourceType getResourceType() { */ public abstract String getZone(); - static GcpGkeContainerMonitoredResource create() { + /** + * Returns a {@link GcpGkeContainerMonitoredResource}. + * + * @param account the GCP account number. + * @param clusterName the GCP GKE cluster name. + * @param containerName the GCP GKE container name. + * @param namespaceId the GCP GKE namespace ID. + * @param instanceId the GCP GKE instance ID. + * @param podId the GCP GKE Pod ID. + * @param zone the GCP zone. + * @return a {@code GcpGkeContainerMonitoredResource}. + * @since 0.15 + */ + public static GcpGkeContainerMonitoredResource create( + String account, + String clusterName, + String containerName, + String namespaceId, + String instanceId, + String podId, + String zone) { return new AutoValue_MonitoredResource_GcpGkeContainerMonitoredResource( + account, clusterName, containerName, namespaceId, instanceId, podId, zone); + } + + /** + * Returns a {@link GcpGkeContainerMonitoredResource} with the default parameters. + * + * @return a {@code GcpGkeContainerMonitoredResource} + * @since 0.15 + */ + public static GcpGkeContainerMonitoredResource create() { + return create( GCP_ACCOUNT_ID, GCP_CLUSTER_NAME, GCP_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 new file mode 100644 index 0000000000..0defcbd7c4 --- /dev/null +++ b/contrib/monitored_resource_util/src/test/java/io/opencensus/contrib/monitoredresource/util/MonitoredResourceTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2018, 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.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; + +/** Unit tests for {@link MonitoredResource}. */ +@RunWith(JUnit4.class) +public class MonitoredResourceTest { + + private static final String AWS_ACCOUNT = "aws-account"; + private static final String AWS_INSTANCE = "instance"; + private static final String AWS_REGION = "us-west-2"; + private static final String GCP_PROJECT = "gcp-project"; + private static final String GCP_INSTANCE = "instance"; + private static final String GCP_ZONE = "us-east1"; + private static final String GCP_GKE_NAMESPACE = "namespace"; + private static final String GCP_GKE_POD_ID = "pod-id"; + private static final String GCP_GKE_CONTAINER_NAME = "container"; + private static final String GCP_GKE_CLUSTER_NAME = "cluster"; + + @Test + public void testAwsEc2InstanceMonitoredResource() { + AwsEc2InstanceMonitoredResource resource = + 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); + assertThat(resource.getRegion()).isEqualTo(AWS_REGION); + } + + @Test + public void testGcpGceInstanceMonitoredResource() { + GcpGceInstanceMonitoredResource resource = + 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); + assertThat(resource.getZone()).isEqualTo(GCP_ZONE); + } + + @Test + public void testGcpGkeContainerMonitoredResource() { + GcpGkeContainerMonitoredResource resource = + GcpGkeContainerMonitoredResource.create( + GCP_PROJECT, + GCP_GKE_CLUSTER_NAME, + GCP_GKE_CONTAINER_NAME, + GCP_GKE_NAMESPACE, + GCP_INSTANCE, + GCP_GKE_POD_ID, + GCP_ZONE); + assertThat(resource.getResourceType()).isEqualTo(ResourceType.GCP_GKE_CONTAINER); + assertThat(resource.getAccount()).isEqualTo(GCP_PROJECT); + assertThat(resource.getClusterName()).isEqualTo(GCP_GKE_CLUSTER_NAME); + assertThat(resource.getContainerName()).isEqualTo(GCP_GKE_CONTAINER_NAME); + assertThat(resource.getNamespaceId()).isEqualTo(GCP_GKE_NAMESPACE); + assertThat(resource.getInstanceId()).isEqualTo(GCP_INSTANCE); + assertThat(resource.getPodId()).isEqualTo(GCP_GKE_POD_ID); + assertThat(resource.getZone()).isEqualTo(GCP_ZONE); + } +} From ce40cf77e7a4cb1ff75fa142179e533db1231548 Mon Sep 17 00:00:00 2001 From: Yang Song Date: Mon, 11 Jun 2018 21:57:34 -0700 Subject: [PATCH 2/3] Update CHANGELOG to be more precise. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 838b7c5d58..56af1d6d4f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ ## Unreleased -- Expose the constructor of MonitoredResource. +- Expose the factory methods of MonitoredResource. ## 0.14.0 - 2018-06-04 - Adds Tracing.getExportComponent().shutdown() for use within application shutdown hooks. From 42b5fadab172d9b83c0ca279f7e0392f8806a19e Mon Sep 17 00:00:00 2001 From: songy23 Date: Tue, 12 Jun 2018 09:38:19 -0700 Subject: [PATCH 3/3] Hide the factory methods that use env vars. --- .../util/MonitoredResource.java | 24 +++---------------- 1 file changed, 3 insertions(+), 21 deletions(-) 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 829e3cc52a..c828906d1f 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 @@ -117,13 +117,7 @@ public static AwsEc2InstanceMonitoredResource create( account, instanceId, region); } - /** - * Returns an {@link AwsEc2InstanceMonitoredResource} with the default parameters. - * - * @return an {@code AwsEc2InstanceMonitoredResource}. - * @since 0.15 - */ - public static AwsEc2InstanceMonitoredResource create() { + static AwsEc2InstanceMonitoredResource create() { return create(AWS_ACCOUNT, AWS_INSTANCE_ID, AWS_REGION); } } @@ -186,13 +180,7 @@ public static GcpGceInstanceMonitoredResource create( account, instanceId, zone); } - /** - * Returns a {@link GcpGceInstanceMonitoredResource} with the default parameters. - * - * @return a {@code GcpGceInstanceMonitoredResource}. - * @since 0.15 - */ - public static GcpGceInstanceMonitoredResource create() { + static GcpGceInstanceMonitoredResource create() { return create(GCP_ACCOUNT_ID, GCP_INSTANCE_ID, GCP_ZONE); } } @@ -303,13 +291,7 @@ public static GcpGkeContainerMonitoredResource create( account, clusterName, containerName, namespaceId, instanceId, podId, zone); } - /** - * Returns a {@link GcpGkeContainerMonitoredResource} with the default parameters. - * - * @return a {@code GcpGkeContainerMonitoredResource} - * @since 0.15 - */ - public static GcpGkeContainerMonitoredResource create() { + static GcpGkeContainerMonitoredResource create() { return create( GCP_ACCOUNT_ID, GCP_CLUSTER_NAME,