Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ba11240

Browse files
authoredNov 8, 2022
Enables test server to run in CloudFunctions (GoogleCloudPlatform#173)
* Enables Cloud Function resource detection * Refactor: add utility class to extract common attributes * Add build steps for cloud build trigger * Fix cloudbuild yaml file name * Update test runner image * Add missing javadoc * Removes unnecessary rethrow for exception
1 parent e494d5b commit ba11240

File tree

13 files changed

+433
-28
lines changed

13 files changed

+433
-28
lines changed
 

‎build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,22 @@ subprojects {
118118
springWebVersion = '2.4.5'
119119
springOpenFeignVersion = '3.0.0'
120120
springOtelVersion = '1.0.0-M8'
121+
cloudEventsCoreVersion = '2.4.0'
122+
cloudFunctionsFrameworkApiVersion = '1.0.4'
121123

122124
libraries = [
123125
auto_service_annotations : "com.google.auto.service:auto-service-annotations:${autoServiceVersion}",
124126
auto_service : "com.google.auto.service:auto-service:${autoServiceVersion}",
125127
auto_value_annotations : "com.google.auto.value:auto-value-annotations:${autoValueVersion}",
126128
auto_value : "com.google.auto.value:auto-value:${autoValueVersion}",
129+
cloudevents_core : "io.cloudevents:cloudevents-core:${cloudEventsCoreVersion}",
127130
google_cloud_core : "com.google.cloud:google-cloud-core:${googleCloudVersion}",
128131
google_cloud_trace : "com.google.cloud:google-cloud-trace:${googleTraceVersion}",
129132
google_cloud_trace_grpc : "com.google.api.grpc:grpc-google-cloud-trace-v2:2.0.1",
130133
google_cloud_monitoring : "com.google.cloud:google-cloud-monitoring:${cloudMonitoringVersion}",
131134
google_cloud_monitoring_grpc : "com.google.cloud:grpc-google-cloud-monitoring-v3:${cloudMonitoringVersion}",
132135
google_cloud_pubsub : "com.google.cloud:google-cloud-pubsub:${pubSubVersion}",
136+
google_cloud_functions_framework : "com.google.cloud.functions:functions-framework-api:${cloudFunctionsFrameworkApiVersion}",
133137
slf4j : "org.slf4j:slf4j-api:${slf4jVersion}",
134138
opentelemetry_api : "io.opentelemetry:opentelemetry-api:${openTelemetryVersion}",
135139
opentelemetry_context : "io.opentelemetry:opentelemetry-context:${openTelemetryVersion}",
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2022 Google
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
steps:
16+
# Generate shadowJar for the instrumented test server
17+
- name: "gradle:7.5.1-jdk11"
18+
id: generate-jar
19+
entrypoint: "gradle"
20+
timeout: 4m
21+
args: ["shadowJar"]
22+
23+
# Zip the generated JAR file
24+
- name: ubuntu
25+
id: zip-jar
26+
entrypoint: bash
27+
args:
28+
- '-c'
29+
- |
30+
apt-get update && \
31+
apt-get install zip --assume-yes && \
32+
cd e2e-test-server/build/libs && \
33+
zip function-source e2e-test-server-0.1.0-SNAPSHOT-all.jar
34+
35+
# Run the test
36+
- name: $_TEST_RUNNER_IMAGE
37+
id: run-tests-cloudfunction
38+
dir: /
39+
env: ["PROJECT_ID=$PROJECT_ID"]
40+
args:
41+
- cloud-functions-gen2
42+
- --functionsource=/workspace/e2e-test-server/build/libs/function-source.zip
43+
- --runtime=java11
44+
- --entrypoint=com.google.cloud.opentelemetry.endtoend.CloudFunctionHandler
45+
46+
logsBucket: gs://opentelemetry-ops-e2e-cloud-build-logs
47+
substitutions:
48+
_TEST_RUNNER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-e2e-testing:0.14.1
49+
_TEST_SERVER_IMAGE: gcr.io/${PROJECT_ID}/opentelemetry-operations-java-e2e-test-server:${SHORT_SHA}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2022 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.opentelemetry.detectors;
17+
18+
import io.opentelemetry.api.common.AttributesBuilder;
19+
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
20+
21+
/**
22+
* A utility class that contains method that facilitate extraction of attributes from environment
23+
* variables and metadata configurations.
24+
*
25+
* <p>This class only adds helper methods to extract {@link ResourceAttributes} that are common
26+
* across all the supported compute environments.
27+
*/
28+
public class AttributesExtractorUtil {
29+
30+
/**
31+
* Utility method to extract cloud availability zone from passed {@link GCPMetadataConfig}. The
32+
* method modifies the passed attributesBuilder by adding the extracted property to it. If the
33+
* zone cannot be found, calling this method has no effect.
34+
*
35+
* <ul>
36+
* <li>If the availability zone cannot be found, calling this method has no effect.
37+
* <li>Calling this method will update {@link ResourceAttributes#CLOUD_AVAILABILITY_ZONE}
38+
* attribute.
39+
* </ul>
40+
*
41+
* <p>Example zone: australia-southeast1-a
42+
*
43+
* @param attributesBuilder The {@link AttributesBuilder} to which the extracted property needs to
44+
* be added.
45+
* @param metadataConfig The {@link GCPMetadataConfig} from which the cloud availability zone
46+
* value is extracted.
47+
*/
48+
public static void addAvailabilityZoneFromMetadata(
49+
AttributesBuilder attributesBuilder, GCPMetadataConfig metadataConfig) {
50+
String zone = metadataConfig.getZone();
51+
if (zone != null) {
52+
attributesBuilder.put(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, zone);
53+
}
54+
}
55+
56+
/**
57+
* Utility method to extract the cloud region from passed {@link GCPMetadataConfig}. The method
58+
* modifies the passed attributesBuilder by adding the extracted property to it.
59+
*
60+
* <ul>
61+
* <li>If the cloud region cannot be found, calling this method has no effect.
62+
* <li>Calling this method will update {@link ResourceAttributes#CLOUD_REGION} attribute.
63+
* </ul>
64+
*
65+
* <p>Example region: australia-southeast1
66+
*
67+
* @param attributesBuilder The {@link AttributesBuilder} to which the extracted property needs to
68+
* be added.
69+
* @param metadataConfig The {@link GCPMetadataConfig} from which the cloud region value is
70+
* extracted.
71+
*/
72+
public static void addCloudRegionFromMetadata(
73+
AttributesBuilder attributesBuilder, GCPMetadataConfig metadataConfig) {
74+
String zone = metadataConfig.getZone();
75+
if (zone != null) {
76+
// Parsing required to scope up to a region
77+
String[] splitArr = zone.split("-");
78+
if (splitArr.length > 2) {
79+
attributesBuilder.put(ResourceAttributes.CLOUD_REGION, splitArr[0] + "-" + splitArr[1]);
80+
}
81+
}
82+
}
83+
84+
/**
85+
* Utility method to extract the current compute instance ID from the passed {@link
86+
* GCPMetadataConfig}. The method modifies the passed attributesBuilder by adding the extracted
87+
* property to it.
88+
*
89+
* <ul>
90+
* <li>If the instance ID cannot be found, calling this method has no effect.
91+
* <li>Calling this method will update {@link ResourceAttributes#FAAS_ID} attribute.
92+
* </ul>
93+
*
94+
* @param attributesBuilder The {@link AttributesBuilder} to which the extracted property needs to
95+
* be added.
96+
* @param metadataConfig The {@link GCPMetadataConfig} from which the instance ID value is
97+
* extracted.
98+
*/
99+
public static void addInstanceIdFromMetadata(
100+
AttributesBuilder attributesBuilder, GCPMetadataConfig metadataConfig) {
101+
String instanceId = metadataConfig.getInstanceId();
102+
if (instanceId != null) {
103+
attributesBuilder.put(ResourceAttributes.FAAS_ID, instanceId);
104+
}
105+
}
106+
}

‎detectors/resources/src/main/java/com/google/cloud/opentelemetry/detectors/CloudRunResource.java

+3-15
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,6 @@ public Attributes getAttributes() {
5050
attrBuilders.put(
5151
ResourceAttributes.CLOUD_PLATFORM, ResourceAttributes.CloudPlatformValues.GCP_CLOUD_RUN);
5252

53-
String zone = metadata.getZone();
54-
if (zone != null) {
55-
attrBuilders.put(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, zone);
56-
57-
// Parsing required to scope up to a region
58-
String[] splitArr = zone.split("-");
59-
if (splitArr.length > 2) {
60-
attrBuilders.put(ResourceAttributes.CLOUD_REGION, splitArr[0] + "-" + splitArr[1]);
61-
}
62-
}
63-
6453
String cloudRunService = envVars.get("K_SERVICE");
6554
if (cloudRunService != null) {
6655
attrBuilders.put(ResourceAttributes.FAAS_NAME, cloudRunService);
@@ -71,10 +60,9 @@ public Attributes getAttributes() {
7160
attrBuilders.put(ResourceAttributes.FAAS_VERSION, cloudRunServiceRevision);
7261
}
7362

74-
String instanceId = metadata.getInstanceId();
75-
if (instanceId != null) {
76-
attrBuilders.put(ResourceAttributes.FAAS_ID, instanceId);
77-
}
63+
AttributesExtractorUtil.addAvailabilityZoneFromMetadata(attrBuilders, metadata);
64+
AttributesExtractorUtil.addCloudRegionFromMetadata(attrBuilders, metadata);
65+
AttributesExtractorUtil.addInstanceIdFromMetadata(attrBuilders, metadata);
7866
}
7967

8068
return attrBuilders.build();

‎detectors/resources/src/main/java/com/google/cloud/opentelemetry/detectors/GCEResource.java

+2-11
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,8 @@ public Attributes getAttributes() {
5050
attrBuilders.put(ResourceAttributes.CLOUD_ACCOUNT_ID, projectId);
5151
}
5252

53-
// Example zone: australia-southeast1-a
54-
String zone = metadata.getZone();
55-
if (zone != null) {
56-
attrBuilders.put(ResourceAttributes.CLOUD_AVAILABILITY_ZONE, zone);
57-
58-
// Parsing required to scope up to a region
59-
String[] splitArr = zone.split("-");
60-
if (splitArr.length > 2) {
61-
attrBuilders.put(ResourceAttributes.CLOUD_REGION, splitArr[0] + "-" + splitArr[1]);
62-
}
63-
}
53+
AttributesExtractorUtil.addAvailabilityZoneFromMetadata(attrBuilders, metadata);
54+
AttributesExtractorUtil.addCloudRegionFromMetadata(attrBuilders, metadata);
6455

6556
String instanceId = metadata.getInstanceId();
6657
if (instanceId != null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2022 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.opentelemetry.detectors;
17+
18+
import io.opentelemetry.api.common.Attributes;
19+
import io.opentelemetry.api.common.AttributesBuilder;
20+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
21+
import io.opentelemetry.sdk.autoconfigure.spi.ResourceProvider;
22+
import io.opentelemetry.sdk.resources.Resource;
23+
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
24+
25+
public final class GCFResource implements ResourceProvider {
26+
private final GCPMetadataConfig metadata;
27+
private final EnvVars envVars;
28+
29+
public GCFResource() {
30+
this.metadata = GCPMetadataConfig.DEFAULT_INSTANCE;
31+
this.envVars = EnvVars.DEFAULT_INSTANCE;
32+
}
33+
34+
// For testing only
35+
GCFResource(GCPMetadataConfig metadata, EnvVars envVars) {
36+
this.metadata = metadata;
37+
this.envVars = envVars;
38+
}
39+
40+
public Attributes getAttributes() {
41+
if (!metadata.isRunningOnGcp()) {
42+
return Attributes.empty();
43+
}
44+
45+
AttributesBuilder attrBuilders = Attributes.builder();
46+
attrBuilders.put(ResourceAttributes.CLOUD_PROVIDER, ResourceAttributes.CloudProviderValues.GCP);
47+
48+
if (envVars.get("FUNCTION_TARGET") != null) {
49+
// add the resource attributes for Cloud Run
50+
attrBuilders.put(
51+
ResourceAttributes.CLOUD_PLATFORM,
52+
ResourceAttributes.CloudPlatformValues.GCP_CLOUD_FUNCTIONS);
53+
54+
String cloudFunctionName = envVars.get("K_SERVICE");
55+
if (cloudFunctionName != null) {
56+
attrBuilders.put(ResourceAttributes.FAAS_NAME, cloudFunctionName);
57+
}
58+
59+
String cloudFunctionVersion = envVars.get("K_REVISION");
60+
if (cloudFunctionVersion != null) {
61+
attrBuilders.put(ResourceAttributes.FAAS_VERSION, cloudFunctionVersion);
62+
}
63+
64+
AttributesExtractorUtil.addAvailabilityZoneFromMetadata(attrBuilders, metadata);
65+
AttributesExtractorUtil.addCloudRegionFromMetadata(attrBuilders, metadata);
66+
AttributesExtractorUtil.addInstanceIdFromMetadata(attrBuilders, metadata);
67+
}
68+
69+
return attrBuilders.build();
70+
}
71+
72+
@Override
73+
public Resource createResource(ConfigProperties config) {
74+
return Resource.create(getAttributes());
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
com.google.cloud.opentelemetry.detectors.GCEResource
22
com.google.cloud.opentelemetry.detectors.GKEResource
33
com.google.cloud.opentelemetry.detectors.CloudRunResource
4+
com.google.cloud.opentelemetry.detectors.GCFResource

0 commit comments

Comments
 (0)
Please sign in to comment.