forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitoredResourceUtil.java
More file actions
210 lines (188 loc) · 6.17 KB
/
Copy pathMonitoredResourceUtil.java
File metadata and controls
210 lines (188 loc) · 6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright 2017 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.logging;
import com.google.cloud.MetadataConfig;
import com.google.cloud.MonitoredResource;
import com.google.cloud.ServiceOptions;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Monitored resource construction utilities to detect resource type and add labels.
* Used by logging framework adapters to configure default resource.
* See usage in {@link LoggingHandler}.
*/
public class MonitoredResourceUtil {
private enum Label {
AppId("app_id"),
ClusterName("cluster_name"),
InstanceId("instance_id"),
InstanceName("instance_name"),
ModuleId("module_id"),
ProjectId("project_id"),
VersionId("version_id"),
Zone("zone");
private final String key;
Label(String key) {
this.key = key;
}
String getKey() {
return key;
}
}
private enum Resource {
Container("container"),
GaeAppFlex("gae_app_flex"),
GaeAppStandard("gae_app_standard"),
GceInstance("gce_instance"),
Global("global");
private final String key;
Resource(String key) {
this.key = key;
}
String getKey() {
return key;
}
}
private static Map<String, Label[]> resourceTypeWithLabels;
static {
resourceTypeWithLabels =
new ImmutableMap.Builder<String, Label[]>()
.put(
Resource.GaeAppFlex.getKey(),
new Label[] {
Label.InstanceName,
Label.ModuleId,
Label.VersionId,
Label.InstanceId,
Label.Zone
})
.put(
Resource.GaeAppStandard.getKey(),
new Label[] {Label.AppId, Label.ModuleId, Label.VersionId})
.put(Resource.Container.getKey(), new Label[] {Label.ClusterName, Label.Zone})
.put(Resource.GceInstance.getKey(), new Label[] {Label.InstanceId, Label.Zone})
.build();
}
private MonitoredResourceUtil() {
}
/* Return a self-configured monitored Resource. */
public static MonitoredResource getResource(String projectId, String resourceTypeParam) {
String resourceType = resourceTypeParam;
if (Strings.isNullOrEmpty(resourceType)) {
Resource detectedResourceType = getAutoDetectedResourceType();
resourceType = detectedResourceType.getKey();
}
// Currently, "gae_app" is the supported logging Resource type, but we distinguish
// between "gae_app_flex", "gae_app_standard" to support zone id, instance name logging on flex VMs.
// Hence, "gae_app_flex", "gae_app_standard" are trimmed to "gae_app"
String resourceName = resourceType.startsWith("gae_app") ? "gae_app" : resourceType;
MonitoredResource.Builder builder =
MonitoredResource.newBuilder(resourceName).addLabel(Label.ProjectId.getKey(), projectId);
Label[] resourceLabels = resourceTypeWithLabels.get(resourceType);
if (resourceLabels != null) {
for (Label label : resourceLabels) {
String value = getValue(label);
if (value != null) {
builder.addLabel(label.getKey(), value);
}
}
}
return builder.build();
}
/**
* Returns custom log entry enhancers (if available) for resource type.
* @return custom long entry enhancers
*/
public static List<LoggingEnhancer> getResourceEnhancers() {
Resource resourceType = getAutoDetectedResourceType();
return getEnhancers(resourceType);
}
private static String getValue(Label label) {
String value;
switch (label) {
case AppId:
value = ServiceOptions.getAppEngineAppId();
break;
case ClusterName:
value = MetadataConfig.getClusterName();
break;
case InstanceId:
value = MetadataConfig.getInstanceId();
break;
case InstanceName:
value = getAppEngineInstanceName();
break;
case ModuleId:
value = getAppEngineModuleId();
break;
case VersionId:
value = getAppEngineVersionId();
break;
case Zone:
value = MetadataConfig.getZone();
break;
default:
value = null;
break;
}
return value;
}
/* Detect monitored Resource type using environment variables, else return global as default. */
private static Resource getAutoDetectedResourceType() {
if (System.getenv("GAE_INSTANCE") != null) {
return Resource.GaeAppFlex;
}
if (System.getenv("KUBERNETES_SERVICE_HOST") != null) {
return Resource.Container;
}
if (ServiceOptions.getAppEngineAppId() != null) {
return Resource.GaeAppStandard;
}
if (MetadataConfig.getInstanceId() != null) {
return Resource.GceInstance;
}
// default Resource type
return Resource.Global;
}
private static String getAppEngineModuleId() {
return System.getenv("GAE_SERVICE");
}
private static String getAppEngineVersionId() {
return System.getenv("GAE_VERSION");
}
private static String getAppEngineInstanceName() {
return System.getenv("GAE_INSTANCE");
}
private static List<LoggingEnhancer> getEnhancers(Resource resourceType) {
List<LoggingEnhancer> enhancers;
switch (resourceType) {
// Trace logging enhancer is supported on GAE Flex and Standard.
case GaeAppStandard:
case GaeAppFlex:
enhancers = new ArrayList<>();
enhancers.add(new TraceLoggingEnhancer());
break;
default:
enhancers = Collections.emptyList();
break;
}
return enhancers;
}
}