Skip to content

Commit dda1d0a

Browse files
authored
fix: resource detection for 2nd gen of Cloud Functions works incorrectly (#1045)
* fix: resource detection for 2nd gen of Cloud Functions works incorrectly * Add a test
1 parent 9b82109 commit dda1d0a

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

google-cloud-logging/src/main/java/com/google/cloud/logging/MonitoredResourceUtil.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ private static Resource detectResourceType() {
169169
return Resource.Global;
170170
}
171171

172+
if (getter.getEnv("FUNCTION_SIGNATURE_TYPE") != null
173+
&& getter.getEnv("FUNCTION_TARGET") != null) {
174+
return Resource.CloudFunction;
175+
}
172176
if (getter.getEnv("K_SERVICE") != null
173177
&& getter.getEnv("K_REVISION") != null
174178
&& getter.getEnv("K_CONFIGURATION") != null) {
@@ -179,10 +183,6 @@ private static Resource detectResourceType() {
179183
&& getter.getEnv("GAE_VERSION") != null) {
180184
return Resource.AppEngine;
181185
}
182-
if (getter.getEnv("FUNCTION_SIGNATURE_TYPE") != null
183-
&& getter.getEnv("FUNCTION_TARGET") != null) {
184-
return Resource.CloudFunction;
185-
}
186186
if (getter.getAttribute("instance/attributes/cluster-name") != null) {
187187
return Resource.K8sContainer;
188188
}

google-cloud-logging/src/test/java/com/google/cloud/logging/MonitoredResourceUtilTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ public void testResourceTypeCloudFunction() {
243243
expect(getterMock.getEnv("FUNCTION_SIGNATURE_TYPE")).andReturn(MOCKED_NON_EMPTY).once();
244244
expect(getterMock.getEnv("FUNCTION_TARGET")).andReturn(MOCKED_NON_EMPTY).once();
245245
expect(getterMock.getEnv("K_SERVICE")).andReturn(MockedFunctionName).anyTimes();
246+
expect(getterMock.getEnv("K_REVISION")).andReturn(MockedFunctionName + ".1").anyTimes();
247+
expect(getterMock.getEnv("K_CONFIGURATION"))
248+
.andReturn(MockedFunctionName + "-config")
249+
.anyTimes();
246250
expect(getterMock.getEnv(anyString())).andReturn(null).anyTimes();
247251
replay(getterMock);
248252
// exercise

0 commit comments

Comments
 (0)