@@ -40,10 +40,13 @@ private enum Label {
40
40
ContainerName ("container_name" ),
41
41
InstanceId ("instance_id" ),
42
42
InstanceName ("instance_name" ),
43
+ Location ("location" ),
43
44
ModuleId ("module_id" ),
44
45
NamespaceId ("namespace_id" ),
45
46
PodId ("pod_id" ),
46
47
ProjectId ("project_id" ),
48
+ RevisionName ("revision_name" ),
49
+ ServiceName ("service_name" ),
47
50
VersionId ("version_id" ),
48
51
Zone ("zone" );
49
52
@@ -59,6 +62,7 @@ String getKey() {
59
62
}
60
63
61
64
private enum Resource {
65
+ CloudRun ("cloud_run_revision" ),
62
66
Container ("container" ),
63
67
GaeAppFlex ("gae_app_flex" ),
64
68
GaeAppStandard ("gae_app_standard" ),
@@ -80,8 +84,6 @@ String getKey() {
80
84
81
85
private static ImmutableMultimap <String , Label > resourceTypeWithLabels =
82
86
ImmutableMultimap .<String , Label >builder ()
83
- .putAll (Resource .GaeAppFlex .getKey (), Label .ModuleId , Label .VersionId , Label .Zone )
84
- .putAll (Resource .GaeAppStandard .getKey (), Label .ModuleId , Label .VersionId )
85
87
.putAll (
86
88
Resource .Container .getKey (),
87
89
Label .ClusterName ,
@@ -90,6 +92,9 @@ String getKey() {
90
92
Label .NamespaceId ,
91
93
Label .PodId ,
92
94
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 )
93
98
.putAll (Resource .GceInstance .getKey (), Label .InstanceId , Label .Zone )
94
99
.build ();
95
100
@@ -147,6 +152,9 @@ private static String getValue(Label label) {
147
152
case InstanceName :
148
153
value = getAppEngineInstanceName ();
149
154
break ;
155
+ case Location :
156
+ value = getCloudRunLocation ();
157
+ break ;
150
158
case ModuleId :
151
159
value = getAppEngineModuleId ();
152
160
break ;
@@ -156,6 +164,12 @@ private static String getValue(Label label) {
156
164
case PodId :
157
165
value = System .getenv ("HOSTNAME" );
158
166
break ;
167
+ case RevisionName :
168
+ value = System .getenv ("K_REVISION" );
169
+ break ;
170
+ case ServiceName :
171
+ value = System .getenv ("K_SERVICE" );
172
+ break ;
159
173
case VersionId :
160
174
value = getAppEngineVersionId ();
161
175
break ;
@@ -171,6 +185,12 @@ private static String getValue(Label label) {
171
185
172
186
/* Detect monitored Resource type using environment variables, else return global as default. */
173
187
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
+ }
174
194
if (System .getenv ("GAE_INSTANCE" ) != null ) {
175
195
return Resource .GaeAppFlex ;
176
196
}
@@ -199,6 +219,14 @@ private static String getAppEngineInstanceName() {
199
219
return System .getenv ("GAE_INSTANCE" );
200
220
}
201
221
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
+
202
230
private static List <LoggingEnhancer > createEnhancers (Resource resourceType ) {
203
231
List <LoggingEnhancer > enhancers = new ArrayList <>(2 );
204
232
switch (resourceType ) {
0 commit comments