22
22
import com .google .cloud .logging .LogEntry .Builder ;
23
23
import com .google .common .base .Strings ;
24
24
import com .google .common .collect .ImmutableMultimap ;
25
+ import java .io .IOException ;
26
+ import java .nio .charset .StandardCharsets ;
27
+ import java .nio .file .Files ;
28
+ import java .nio .file .Paths ;
25
29
import java .util .ArrayList ;
26
30
import java .util .Collections ;
27
31
import java .util .HashMap ;
@@ -43,7 +47,9 @@ private enum Label {
43
47
Location ("location" ),
44
48
ModuleId ("module_id" ),
45
49
NamespaceId ("namespace_id" ),
50
+ NamespaceName ("namespace_name" ),
46
51
PodId ("pod_id" ),
52
+ PodName ("pod_name" ),
47
53
ProjectId ("project_id" ),
48
54
RevisionName ("revision_name" ),
49
55
ServiceName ("service_name" ),
@@ -67,6 +73,7 @@ private enum Resource {
67
73
GaeAppFlex ("gae_app_flex" ),
68
74
GaeAppStandard ("gae_app_standard" ),
69
75
GceInstance ("gce_instance" ),
76
+ K8sContainer ("k8s_container" ),
70
77
Global ("global" );
71
78
72
79
private final String key ;
@@ -96,6 +103,13 @@ String getKey() {
96
103
.putAll (Resource .GaeAppFlex .getKey (), Label .ModuleId , Label .VersionId , Label .Zone )
97
104
.putAll (Resource .GaeAppStandard .getKey (), Label .ModuleId , Label .VersionId )
98
105
.putAll (Resource .GceInstance .getKey (), Label .InstanceId , Label .Zone )
106
+ .putAll (
107
+ Resource .K8sContainer .getKey (),
108
+ Label .Location ,
109
+ Label .ClusterName ,
110
+ Label .NamespaceName ,
111
+ Label .PodName ,
112
+ Label .ContainerName )
99
113
.build ();
100
114
101
115
private MonitoredResourceUtil () {}
@@ -116,7 +130,7 @@ public static MonitoredResource getResource(String projectId, String resourceTyp
116
130
MonitoredResource .newBuilder (resourceName ).addLabel (Label .ProjectId .getKey (), projectId );
117
131
118
132
for (Label label : resourceTypeWithLabels .get (resourceType )) {
119
- String value = getValue (label );
133
+ String value = getValue (label , resourceType );
120
134
if (value != null ) {
121
135
builder .addLabel (label .getKey (), value );
122
136
}
@@ -134,7 +148,7 @@ public static List<LoggingEnhancer> getResourceEnhancers() {
134
148
return createEnhancers (resourceType );
135
149
}
136
150
137
- private static String getValue (Label label ) {
151
+ private static String getValue (Label label , String resourceType ) {
138
152
String value ;
139
153
switch (label ) {
140
154
case AppId :
@@ -144,7 +158,12 @@ private static String getValue(Label label) {
144
158
value = MetadataConfig .getClusterName ();
145
159
break ;
146
160
case ContainerName :
147
- value = MetadataConfig .getContainerName ();
161
+ if (resourceType .equals ("k8s_container" )) {
162
+ String hostName = System .getenv ("HOSTNAME" );
163
+ value = hostName .substring (0 , hostName .indexOf ("-" ));
164
+ } else {
165
+ value = MetadataConfig .getContainerName ();
166
+ }
148
167
break ;
149
168
case InstanceId :
150
169
value = MetadataConfig .getInstanceId ();
@@ -161,6 +180,18 @@ private static String getValue(Label label) {
161
180
case NamespaceId :
162
181
value = MetadataConfig .getNamespaceId ();
163
182
break ;
183
+ case NamespaceName :
184
+ String filePath = System .getenv ("KUBERNETES_NAMESPACE_FILE" );
185
+ if (filePath == null ) {
186
+ filePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace" ;
187
+ }
188
+ try {
189
+ value = new String (Files .readAllBytes (Paths .get (filePath )), StandardCharsets .UTF_8 );
190
+ } catch (IOException e ) {
191
+ throw new LoggingException (e , true );
192
+ }
193
+ break ;
194
+ case PodName :
164
195
case PodId :
165
196
value = System .getenv ("HOSTNAME" );
166
197
break ;
@@ -261,10 +292,10 @@ private static class LabelLoggingEnhancer implements LoggingEnhancer {
261
292
labels = new HashMap <>();
262
293
if (labelNames != null ) {
263
294
for (Label labelName : labelNames ) {
264
- String labelValue = MonitoredResourceUtil .getValue (labelName );
295
+ String fullLabelName =
296
+ (prefix != null ) ? prefix + labelName .getKey () : labelName .getKey ();
297
+ String labelValue = MonitoredResourceUtil .getValue (labelName , fullLabelName );
265
298
if (labelValue != null ) {
266
- String fullLabelName =
267
- (prefix != null ) ? prefix + labelName .getKey () : labelName .getKey ();
268
299
labels .put (fullLabelName , labelValue );
269
300
}
270
301
}
0 commit comments