Skip to content

Commit b3c1b68

Browse files
meltsufinchingor13
authored andcommitted
fix: support for Cloud Run monitored resource (#78)
* fix: support for Cloud Run monitored resource Fixes: #71. * add check for not k8s * format
1 parent 67ce459 commit b3c1b68

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ private enum Label {
4040
ContainerName("container_name"),
4141
InstanceId("instance_id"),
4242
InstanceName("instance_name"),
43+
Location("location"),
4344
ModuleId("module_id"),
4445
NamespaceId("namespace_id"),
4546
PodId("pod_id"),
4647
ProjectId("project_id"),
48+
RevisionName("revision_name"),
49+
ServiceName("service_name"),
4750
VersionId("version_id"),
4851
Zone("zone");
4952

@@ -59,6 +62,7 @@ String getKey() {
5962
}
6063

6164
private enum Resource {
65+
CloudRun("cloud_run_revision"),
6266
Container("container"),
6367
GaeAppFlex("gae_app_flex"),
6468
GaeAppStandard("gae_app_standard"),
@@ -80,8 +84,6 @@ String getKey() {
8084

8185
private static ImmutableMultimap<String, Label> resourceTypeWithLabels =
8286
ImmutableMultimap.<String, Label>builder()
83-
.putAll(Resource.GaeAppFlex.getKey(), Label.ModuleId, Label.VersionId, Label.Zone)
84-
.putAll(Resource.GaeAppStandard.getKey(), Label.ModuleId, Label.VersionId)
8587
.putAll(
8688
Resource.Container.getKey(),
8789
Label.ClusterName,
@@ -90,6 +92,9 @@ String getKey() {
9092
Label.NamespaceId,
9193
Label.PodId,
9294
Label.Zone)
95+
.putAll(Resource.CloudRun.getKey(), Label.RevisionName, Label.ServiceName, Label.Location)
96+
.putAll(Resource.GaeAppFlex.getKey(), Label.ModuleId, Label.VersionId, Label.Zone)
97+
.putAll(Resource.GaeAppStandard.getKey(), Label.ModuleId, Label.VersionId)
9398
.putAll(Resource.GceInstance.getKey(), Label.InstanceId, Label.Zone)
9499
.build();
95100

@@ -147,6 +152,9 @@ private static String getValue(Label label) {
147152
case InstanceName:
148153
value = getAppEngineInstanceName();
149154
break;
155+
case Location:
156+
value = getCloudRunLocation();
157+
break;
150158
case ModuleId:
151159
value = getAppEngineModuleId();
152160
break;
@@ -156,6 +164,12 @@ private static String getValue(Label label) {
156164
case PodId:
157165
value = System.getenv("HOSTNAME");
158166
break;
167+
case RevisionName:
168+
value = System.getenv("K_REVISION");
169+
break;
170+
case ServiceName:
171+
value = System.getenv("K_SERVICE");
172+
break;
159173
case VersionId:
160174
value = getAppEngineVersionId();
161175
break;
@@ -171,6 +185,12 @@ private static String getValue(Label label) {
171185

172186
/* Detect monitored Resource type using environment variables, else return global as default. */
173187
private static Resource getAutoDetectedResourceType() {
188+
if (System.getenv("K_SERVICE") != null
189+
&& System.getenv("K_REVISION") != null
190+
&& System.getenv("K_CONFIGURATION") != null
191+
&& System.getenv("KUBERNETES_SERVICE_HOST") == null) {
192+
return Resource.CloudRun;
193+
}
174194
if (System.getenv("GAE_INSTANCE") != null) {
175195
return Resource.GaeAppFlex;
176196
}
@@ -199,6 +219,14 @@ private static String getAppEngineInstanceName() {
199219
return System.getenv("GAE_INSTANCE");
200220
}
201221

222+
private static String getCloudRunLocation() {
223+
String zone = MetadataConfig.getZone();
224+
// for Cloud Run managed, the zone is "REGION-1"
225+
// So, we need to strip the "-1" to set location to just the region
226+
if (zone.endsWith("-1")) return zone.substring(0, zone.length() - 2);
227+
else return zone;
228+
}
229+
202230
private static List<LoggingEnhancer> createEnhancers(Resource resourceType) {
203231
List<LoggingEnhancer> enhancers = new ArrayList<>(2);
204232
switch (resourceType) {

0 commit comments

Comments
 (0)