Skip to content

Commit

Permalink
Remove MR mapping for cloud run and cloud functions (#250)
Browse files Browse the repository at this point in the history
* Support metric exporter for cloud run

* Enable metrics-exporter example to run as cloud run job (#249)

* Add readme with steps to run the example

* Add script to run example as Cloud Run Job

* Replace GCR with Artifact Registry

* Rename GOOGLE_CLOUD_RUN_JOB_REGION to GOOGLE_CLOUD_RUN_REGION

* Remove resource mapping for cloud-run

As per the exporter spec, we do not support pushing metric to cloud-run.
This change will cause cloud_run_revision to be treated as a
generic_task.

* Update the autoinstrument example to run in cloud-run

* Remove mapping for cloud functions

Cloud functions is also not writable for user-defined metrics, therefore
removing its explicit mapping so that it defaults to generic_task.

* Refactor: extract image name to a variable

* Update README to streamline service invocation instructions

* Update readme with instructions for command line
  • Loading branch information
psx95 authored Jun 13, 2023
1 parent db7d9c0 commit e6c6a2b
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 22 deletions.
53 changes: 53 additions & 0 deletions examples/autoinstrument/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,59 @@ Or, if you'd like to synthesize a parent trace:
curl -H "traceparent: 00-ff000000000000000000000000000041-ff00000000000041-01" ${cluster_ip}
```

## Running in Google Cloud Run

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 -

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

#### Calling the service from browser

Once the Cloud Run service is deployed, run:

```shell
gcloud beta run services proxy hello-autoinstrument-cloud-run --port=8080
```

This will allow you to call the service from your browser via localhost -

```text
http://localhost:8080/
http://localhost:8080/greeting
```

#### Calling the service from command line

You can also make **authenticated** requests to the service from command line via cURL using the service URL to the application.

```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
48 changes: 48 additions & 0 deletions examples/autoinstrument/run_in_cloud-run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/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
IMAGE_NAME="${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/hello-autoinstrument-java"

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"
echo "CREATED ${CONTAINER_REGISTRY} in ${REGISTRY_LOCATION}"

echo "BUILDING SAMPLE APP IMAGE"
gradle clean jib --image "${IMAGE_NAME}"

echo "RUNNING SAMPLE APP ON PORT 8080"
gcloud run deploy hello-autoinstrument-cloud-run \
--image="${IMAGE_NAME}" \
--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(
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

0 comments on commit e6c6a2b

Please sign in to comment.