Skip to content

Commit e6c6a2b

Browse files
authored
Remove MR mapping for cloud run and cloud functions (#250)
* 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
1 parent db7d9c0 commit e6c6a2b

3 files changed

Lines changed: 113 additions & 22 deletions

File tree

examples/autoinstrument/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,59 @@ Or, if you'd like to synthesize a parent trace:
5353
curl -H "traceparent: 00-ff000000000000000000000000000041-ff00000000000041-01" ${cluster_ip}
5454
```
5555

56+
## Running in Google Cloud Run
57+
58+
To run this example in Google Cloud Run, you need to run the convenience script provided. After following the prerequisites,
59+
60+
First, export the Google Cloud Region for cloud run to `GOOGLE_CLOUD_RUN_REGION` environment variable:
61+
62+
```shell
63+
# This can be any supported Google cloud region
64+
export GOOGLE_CLOUD_RUN_REGION=us-central1
65+
```
66+
67+
Then, from the root of the repository,
68+
```shell
69+
cd examples/autoinstrument && ./run_in_cloud-run.sh
70+
```
71+
This will deploy the containerized application to Cloud Run and you will be presented with a service URL which would look something like -
72+
73+
```text
74+
Service URL: https://hello-autoinstrument-cloud-run-m43qtxry5q-uc.a.run.app
75+
```
76+
77+
#### Calling the service from browser
78+
79+
Once the Cloud Run service is deployed, run:
80+
81+
```shell
82+
gcloud beta run services proxy hello-autoinstrument-cloud-run --port=8080
83+
```
84+
85+
This will allow you to call the service from your browser via localhost -
86+
87+
```text
88+
http://localhost:8080/
89+
http://localhost:8080/greeting
90+
```
91+
92+
#### Calling the service from command line
93+
94+
You can also make **authenticated** requests to the service from command line via cURL using the service URL to the application.
95+
96+
```shell
97+
# Make sure to replace the SERVICE_URL with the one that was generated for your deployment
98+
99+
# Making a request to /
100+
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" ${SERVICE_URL}/
101+
102+
# Making a request to /greeting
103+
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" ${SERVICE_URL}/greeting
104+
```
105+
106+
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).
107+
With public access enabled, you would no longer need to provide the auth token within your requests.
108+
56109
## Running locally in a docker container
57110

58111
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.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2023 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
CONTAINER_REGISTRY=cloud-run-applications
18+
REGISTRY_LOCATION=us-central1
19+
IMAGE_NAME="${REGISTRY_LOCATION}-docker.pkg.dev/${GOOGLE_CLOUD_PROJECT}/${CONTAINER_REGISTRY}/hello-autoinstrument-java"
20+
21+
if [[ -z "${GOOGLE_CLOUD_PROJECT}" ]]; then
22+
echo "GOOGLE_CLOUD_PROJECT environment variable not set"
23+
exit 1
24+
fi
25+
26+
if [[ -z "${GOOGLE_APPLICATION_CREDENTIALS}" ]]; then
27+
echo "GOOGLE_APPLICATION_CREDENTIALS environment variable not set"
28+
exit 1
29+
fi
30+
31+
if [[ -z "${GOOGLE_CLOUD_RUN_REGION}" ]]; then
32+
echo "GOOGLE_CLOUD_RUN_REGION environment variable not set"
33+
exit 1
34+
fi
35+
36+
echo "ENVIRONMENT VARIABLES VERIFIED"
37+
38+
echo "CREATING CLOUD ARTIFACT REPOSITORY"
39+
gcloud artifacts repositories create ${CONTAINER_REGISTRY} --repository-format=docker --location=${REGISTRY_LOCATION} --description="Sample applications to run on Google Cloud Run"
40+
echo "CREATED ${CONTAINER_REGISTRY} in ${REGISTRY_LOCATION}"
41+
42+
echo "BUILDING SAMPLE APP IMAGE"
43+
gradle clean jib --image "${IMAGE_NAME}"
44+
45+
echo "RUNNING SAMPLE APP ON PORT 8080"
46+
gcloud run deploy hello-autoinstrument-cloud-run \
47+
--image="${IMAGE_NAME}" \
48+
--region="${GOOGLE_CLOUD_RUN_REGION}"

shared/resourcemapping/src/main/java/com/google/cloud/opentelemetry/resource/ResourceTranslator.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.opentelemetry.api.common.AttributeKey;
1919
import io.opentelemetry.sdk.resources.Resource;
2020
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
21+
import java.util.Arrays;
2122
import java.util.List;
2223
import java.util.Optional;
2324

@@ -70,50 +71,43 @@ public static AttributeMapping create(
7071
}
7172

7273
private static List<AttributeMapping> GCE_INSTANCE_LABELS =
73-
java.util.Arrays.asList(
74+
Arrays.asList(
7475
AttributeMapping.create("zone", ResourceAttributes.CLOUD_AVAILABILITY_ZONE),
7576
AttributeMapping.create("instance_id", ResourceAttributes.HOST_ID));
7677
private static List<AttributeMapping> K8S_CONTAINER_LABELS =
77-
java.util.Arrays.asList(
78+
Arrays.asList(
7879
AttributeMapping.create(
7980
"location",
80-
java.util.Arrays.asList(
81+
Arrays.asList(
8182
ResourceAttributes.CLOUD_AVAILABILITY_ZONE, ResourceAttributes.CLOUD_REGION)),
8283
AttributeMapping.create("cluster_name", ResourceAttributes.K8S_CLUSTER_NAME),
8384
AttributeMapping.create("namespace_name", ResourceAttributes.K8S_NAMESPACE_NAME),
8485
AttributeMapping.create("container_name", ResourceAttributes.K8S_CONTAINER_NAME),
8586
AttributeMapping.create("pod_name", ResourceAttributes.K8S_POD_NAME));
8687
private static List<AttributeMapping> AWS_EC2_INSTANCE_LABELS =
87-
java.util.Arrays.asList(
88+
Arrays.asList(
8889
AttributeMapping.create("instance_id", ResourceAttributes.HOST_ID),
8990
AttributeMapping.create("region", ResourceAttributes.CLOUD_AVAILABILITY_ZONE),
9091
AttributeMapping.create("aws_account", ResourceAttributes.CLOUD_ACCOUNT_ID));
91-
private static List<AttributeMapping> GOOGLE_CLOUD_RUN_INSTANCE_LABELS =
92-
java.util.Arrays.asList(
93-
AttributeMapping.create("location", ResourceAttributes.CLOUD_REGION),
94-
AttributeMapping.create("service_name", ResourceAttributes.FAAS_NAME),
95-
AttributeMapping.create("configuration_name", ResourceAttributes.FAAS_NAME),
96-
AttributeMapping.create("revision_name", ResourceAttributes.FAAS_VERSION));
97-
private static List<AttributeMapping> GOOGLE_CLOUD_FUNCTION_INSTANCE_LABELS =
98-
java.util.Arrays.asList(
99-
AttributeMapping.create("region", ResourceAttributes.CLOUD_REGION),
100-
AttributeMapping.create("function_name", ResourceAttributes.FAAS_NAME));
10192
private static List<AttributeMapping> GOOGLE_CLOUD_APP_ENGINE_INSTANCE_LABELS =
102-
java.util.Arrays.asList(
93+
Arrays.asList(
10394
AttributeMapping.create("module_id", ResourceAttributes.FAAS_NAME),
10495
AttributeMapping.create("version_id", ResourceAttributes.FAAS_VERSION),
10596
AttributeMapping.create("instance_id", ResourceAttributes.FAAS_ID),
10697
AttributeMapping.create("location", ResourceAttributes.CLOUD_REGION));
10798
private static List<AttributeMapping> GENERIC_TASK_LABELS =
108-
java.util.Arrays.asList(
99+
Arrays.asList(
109100
AttributeMapping.create(
110101
"location",
111-
java.util.Arrays.asList(
102+
Arrays.asList(
112103
ResourceAttributes.CLOUD_AVAILABILITY_ZONE, ResourceAttributes.CLOUD_REGION),
113104
"global"),
114105
AttributeMapping.create("namespace", ResourceAttributes.SERVICE_NAMESPACE, ""),
115106
AttributeMapping.create("job", ResourceAttributes.SERVICE_NAME, ""),
116-
AttributeMapping.create("task_id", ResourceAttributes.SERVICE_INSTANCE_ID, ""));
107+
AttributeMapping.create(
108+
"task_id",
109+
Arrays.asList(ResourceAttributes.SERVICE_INSTANCE_ID, ResourceAttributes.FAAS_ID),
110+
""));
117111

118112
/** Converts a Java OpenTelemetry SDK resource into a GCP resource. */
119113
public static GcpResource mapResource(Resource resource) {
@@ -128,10 +122,6 @@ public static GcpResource mapResource(Resource resource) {
128122
return mapBase(resource, "k8s_container", K8S_CONTAINER_LABELS);
129123
case ResourceAttributes.CloudPlatformValues.AWS_EC2:
130124
return mapBase(resource, "aws_ec2_instance", AWS_EC2_INSTANCE_LABELS);
131-
case ResourceAttributes.CloudPlatformValues.GCP_CLOUD_RUN:
132-
return mapBase(resource, "cloud_run_revision", GOOGLE_CLOUD_RUN_INSTANCE_LABELS);
133-
case ResourceAttributes.CloudPlatformValues.GCP_CLOUD_FUNCTIONS:
134-
return mapBase(resource, "cloud_function", GOOGLE_CLOUD_FUNCTION_INSTANCE_LABELS);
135125
case ResourceAttributes.CloudPlatformValues.GCP_APP_ENGINE:
136126
return mapBase(resource, "gae_instance", GOOGLE_CLOUD_APP_ENGINE_INSTANCE_LABELS);
137127
default:

0 commit comments

Comments
 (0)