Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion contrib/monitored_resource_util/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
}
Original file line number Diff line number Diff line change
@@ -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() {}
}
Original file line number Diff line number Diff line change
@@ -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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So project, instance-id and zone do not apply to the k8s resource?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are not related to k8s resource, but when we do autodetection if the pod/container runs on a GCE instance they will be added in the resource merging.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, make sense.

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() {}
}
Loading