Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove MR mapping for cloud run and cloud functions #250

Merged
merged 10 commits into from
Jun 13, 2023
36 changes: 36 additions & 0 deletions examples/autoinstrument/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@ Or, if you'd like to synthesize a parent trace:
curl -H "traceparent: 00-ff000000000000000000000000000041-ff00000000000041-01" ${cluster_ip}
```

## Running in Google Cloud Run
Copy link
Contributor

Choose a reason for hiding this comment

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

In future, please keep unrelated changes (such as examples) in separate PRs.


To run this example in Google Cloud Run, you need to run the convenience script provided. After following the prerequisites,

First, export the Google Cloud Region for cloud run to `GOOGLE_CLOUD_RUN_REGION` environment variable:

```shell
# This can be any supported Google cloud region
export GOOGLE_CLOUD_RUN_REGION=us-central1
```

Then, from the root of the repository,
```shell
cd examples/autoinstrument && ./run_in_cloud-run.sh
```
This will deploy the containerized application to Cloud Run and you will be presented with a service URL which would look something like -
psx95 marked this conversation as resolved.
Show resolved Hide resolved

```text
Service URL: https://hello-autoinstrument-cloud-run-m43qtxry5q-uc.a.run.app
```

Comment on lines +71 to +76
Copy link
Contributor

Choose a reason for hiding this comment

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

Now that we're using the proxy, the service URL should no longer matter right?

Copy link
Contributor Author

@psx95 psx95 Jun 13, 2023

Choose a reason for hiding this comment

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

Yes, this is just to clarify what to expect in case of a successful deployment. Also, I believe now we are adding the cURL instructions back, so this would be useful there.

Once you have the service URL to the application, you can make **authenticated** requests to it. Authenticated requests can be made from the command line by passing an auth token in a cURL request -

```shell
# Make sure to replace the SERVICE_URL with the one that was generated for your deployment

# Making a request to /
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" ${SERVICE_URL}/

# Making a request to /greeting
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" ${SERVICE_URL}/greeting
```

You can also allow public access to your cloud-run service, details for which can be found [here](https://cloud.google.com/run/docs/authenticating/public#console-ui).
With public access enabled, you would no longer need to provide the auth token within your requests.

## Running locally in a docker container

In case you do not want to spin up your own GKE cluster, but still want telemetry to be published to Google Cloud, you can run the example in a docker container.
Expand Down
47 changes: 47 additions & 0 deletions examples/autoinstrument/run_in_cloud-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash
#
# Copyright 2023 Google LLC
#
# 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.
#
CONTAINER_REGISTRY=cloud-run-applications
REGISTRY_LOCATION=us-central1

if [[ -z "${GOOGLE_CLOUD_PROJECT}" ]]; then
echo "GOOGLE_CLOUD_PROJECT environment variable not set"
exit 1
fi

if [[ -z "${GOOGLE_APPLICATION_CREDENTIALS}" ]]; then
echo "GOOGLE_APPLICATION_CREDENTIALS environment variable not set"
exit 1
fi

if [[ -z "${GOOGLE_CLOUD_RUN_REGION}" ]]; then
echo "GOOGLE_CLOUD_RUN_REGION environment variable not set"
exit 1
fi

echo "ENVIRONMENT VARIABLES VERIFIED"

echo "CREATING CLOUD ARTIFACT REPOSITORY"
gcloud artifacts repositories create ${CONTAINER_REGISTRY} --repository-format=docker --location=${REGISTRY_LOCATION} --description="Sample applications to run on Google Cloud Run"
psx95 marked this conversation as resolved.
Show resolved Hide resolved
echo "CREATED ${CONTAINER_REGISTRY} in ${REGISTRY_LOCATION}"

echo "BUILDING SAMPLE APP IMAGE"
gradle clean jib --image "${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/hello-autoinstrument-java"
psx95 marked this conversation as resolved.
Show resolved Hide resolved

echo "RUNNING SAMPLE APP ON PORT 8080"
gcloud run deploy hello-autoinstrument-cloud-run \
--image="${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/hello-autoinstrument-java" \
--region="${GOOGLE_CLOUD_RUN_REGION}"
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -70,50 +71,43 @@ public static AttributeMapping create(
}

private static List<AttributeMapping> GCE_INSTANCE_LABELS =
java.util.Arrays.asList(
Arrays.asList(
psx95 marked this conversation as resolved.
Show resolved Hide resolved
AttributeMapping.create("zone", ResourceAttributes.CLOUD_AVAILABILITY_ZONE),
AttributeMapping.create("instance_id", ResourceAttributes.HOST_ID));
private static List<AttributeMapping> K8S_CONTAINER_LABELS =
java.util.Arrays.asList(
Arrays.asList(
AttributeMapping.create(
"location",
java.util.Arrays.asList(
Arrays.asList(
ResourceAttributes.CLOUD_AVAILABILITY_ZONE, ResourceAttributes.CLOUD_REGION)),
AttributeMapping.create("cluster_name", ResourceAttributes.K8S_CLUSTER_NAME),
AttributeMapping.create("namespace_name", ResourceAttributes.K8S_NAMESPACE_NAME),
AttributeMapping.create("container_name", ResourceAttributes.K8S_CONTAINER_NAME),
AttributeMapping.create("pod_name", ResourceAttributes.K8S_POD_NAME));
private static List<AttributeMapping> AWS_EC2_INSTANCE_LABELS =
java.util.Arrays.asList(
Arrays.asList(
AttributeMapping.create("instance_id", ResourceAttributes.HOST_ID),
AttributeMapping.create("region", ResourceAttributes.CLOUD_AVAILABILITY_ZONE),
AttributeMapping.create("aws_account", ResourceAttributes.CLOUD_ACCOUNT_ID));
private static List<AttributeMapping> GOOGLE_CLOUD_RUN_INSTANCE_LABELS =
java.util.Arrays.asList(
AttributeMapping.create("location", ResourceAttributes.CLOUD_REGION),
AttributeMapping.create("service_name", ResourceAttributes.FAAS_NAME),
AttributeMapping.create("configuration_name", ResourceAttributes.FAAS_NAME),
AttributeMapping.create("revision_name", ResourceAttributes.FAAS_VERSION));
private static List<AttributeMapping> GOOGLE_CLOUD_FUNCTION_INSTANCE_LABELS =
java.util.Arrays.asList(
AttributeMapping.create("region", ResourceAttributes.CLOUD_REGION),
AttributeMapping.create("function_name", ResourceAttributes.FAAS_NAME));
private static List<AttributeMapping> GOOGLE_CLOUD_APP_ENGINE_INSTANCE_LABELS =
java.util.Arrays.asList(
Arrays.asList(
AttributeMapping.create("module_id", ResourceAttributes.FAAS_NAME),
AttributeMapping.create("version_id", ResourceAttributes.FAAS_VERSION),
AttributeMapping.create("instance_id", ResourceAttributes.FAAS_ID),
AttributeMapping.create("location", ResourceAttributes.CLOUD_REGION));
private static List<AttributeMapping> GENERIC_TASK_LABELS =
java.util.Arrays.asList(
Arrays.asList(
AttributeMapping.create(
"location",
java.util.Arrays.asList(
Arrays.asList(
ResourceAttributes.CLOUD_AVAILABILITY_ZONE, ResourceAttributes.CLOUD_REGION),
"global"),
AttributeMapping.create("namespace", ResourceAttributes.SERVICE_NAMESPACE, ""),
AttributeMapping.create("job", ResourceAttributes.SERVICE_NAME, ""),
AttributeMapping.create("task_id", ResourceAttributes.SERVICE_INSTANCE_ID, ""));
AttributeMapping.create(
"task_id",
Arrays.asList(ResourceAttributes.SERVICE_INSTANCE_ID, ResourceAttributes.FAAS_ID),
""));

/** Converts a Java OpenTelemetry SDK resource into a GCP resource. */
public static GcpResource mapResource(Resource resource) {
Expand All @@ -128,10 +122,6 @@ public static GcpResource mapResource(Resource resource) {
return mapBase(resource, "k8s_container", K8S_CONTAINER_LABELS);
case ResourceAttributes.CloudPlatformValues.AWS_EC2:
return mapBase(resource, "aws_ec2_instance", AWS_EC2_INSTANCE_LABELS);
case ResourceAttributes.CloudPlatformValues.GCP_CLOUD_RUN:
return mapBase(resource, "cloud_run_revision", GOOGLE_CLOUD_RUN_INSTANCE_LABELS);
case ResourceAttributes.CloudPlatformValues.GCP_CLOUD_FUNCTIONS:
return mapBase(resource, "cloud_function", GOOGLE_CLOUD_FUNCTION_INSTANCE_LABELS);
case ResourceAttributes.CloudPlatformValues.GCP_APP_ENGINE:
return mapBase(resource, "gae_instance", GOOGLE_CLOUD_APP_ENGINE_INSTANCE_LABELS);
default:
Expand Down