Skip to content

Commit 6d4ad77

Browse files
authored
---
yaml --- r: 8063 b: refs/heads/tswast-patch-1 c: ae51932 h: refs/heads/master i: 8061: 13b06d9 8059: b8f509d 8055: 97475bc 8047: d7ca53c 8031: b5b9ef2 7999: df4b61b 7935: 8edb7c2
1 parent 8f65de2 commit 6d4ad77

4 files changed

Lines changed: 62 additions & 17 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: 776d39f03201329c32497e97a04cb0c64c7139c9
60+
refs/heads/tswast-patch-1: ae51932dbabc3d4e06061347fd091b3137ebf48b
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/google-cloud-logging/src/main/java/com/google/cloud/logging/LoggingHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public LoggingHandler(
206206

207207
this.enhancers.addAll(enhancersParam);
208208

209-
List<LoggingEnhancer> loggingEnhancers = MonitoredResourceUtil.getResourceEnhancers();
209+
List<LoggingEnhancer> loggingEnhancers = MonitoredResourceUtil.createResourceEnhancers();
210210
if (loggingEnhancers != null) {
211211
this.enhancers.addAll(loggingEnhancers);
212212
}

branches/tswast-patch-1/google-cloud-logging/src/main/java/com/google/cloud/logging/MonitoredResourceUtil.java

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
import com.google.cloud.MetadataConfig;
2020
import com.google.cloud.MonitoredResource;
2121
import com.google.cloud.ServiceOptions;
22+
import com.google.cloud.logging.LogEntry.Builder;
2223
import com.google.common.base.Strings;
2324
import com.google.common.collect.ImmutableMap;
25+
2426
import java.util.ArrayList;
2527
import java.util.Collections;
28+
import java.util.HashMap;
2629
import java.util.List;
2730
import java.util.Map;
2831

@@ -72,6 +75,8 @@ String getKey() {
7275
}
7376
}
7477

78+
private static final String APPENGINE_LABEL_PREFIX = "appengine.googleapis.com/";
79+
7580
private static Map<String, Label[]> resourceTypeWithLabels;
7681

7782
static {
@@ -80,15 +85,13 @@ String getKey() {
8085
.put(
8186
Resource.GaeAppFlex.getKey(),
8287
new Label[] {
83-
Label.InstanceName,
84-
Label.ModuleId,
85-
Label.VersionId,
86-
Label.InstanceId,
87-
Label.Zone
88+
Label.ModuleId,
89+
Label.VersionId,
90+
Label.Zone
8891
})
8992
.put(
9093
Resource.GaeAppStandard.getKey(),
91-
new Label[] {Label.AppId, Label.ModuleId, Label.VersionId})
94+
new Label[] {Label.ModuleId, Label.VersionId})
9295
.put(Resource.Container.getKey(), new Label[] {Label.ClusterName, Label.Zone})
9396
.put(Resource.GceInstance.getKey(), new Label[] {Label.InstanceId, Label.Zone})
9497
.build();
@@ -124,11 +127,12 @@ public static MonitoredResource getResource(String projectId, String resourceTyp
124127

125128
/**
126129
* Returns custom log entry enhancers (if available) for resource type.
130+
*
127131
* @return custom long entry enhancers
128132
*/
129-
public static List<LoggingEnhancer> getResourceEnhancers() {
133+
public static List<LoggingEnhancer> createResourceEnhancers() {
130134
Resource resourceType = getAutoDetectedResourceType();
131-
return getEnhancers(resourceType);
135+
return createEnhancers(resourceType);
132136
}
133137

134138
private static String getValue(Label label) {
@@ -192,19 +196,54 @@ private static String getAppEngineInstanceName() {
192196
return System.getenv("GAE_INSTANCE");
193197
}
194198

195-
private static List<LoggingEnhancer> getEnhancers(Resource resourceType) {
196-
List<LoggingEnhancer> enhancers;
199+
private static List<LoggingEnhancer> createEnhancers(Resource resourceType) {
200+
List<LoggingEnhancer> enhancers = new ArrayList<>(2);
197201
switch (resourceType) {
198202
// Trace logging enhancer is supported on GAE Flex and Standard.
199-
case GaeAppStandard:
200203
case GaeAppFlex:
201-
enhancers = new ArrayList<>();
202-
enhancers.add(new TraceLoggingEnhancer());
204+
enhancers.add(new LabelLoggingEnhancer(
205+
APPENGINE_LABEL_PREFIX, Collections.singletonList(Label.InstanceName)));
206+
enhancers.add(new TraceLoggingEnhancer(APPENGINE_LABEL_PREFIX));
207+
break;
208+
case GaeAppStandard:
209+
enhancers.add(new TraceLoggingEnhancer(APPENGINE_LABEL_PREFIX));
203210
break;
204211
default:
205-
enhancers = Collections.emptyList();
206212
break;
207213
}
208214
return enhancers;
209215
}
216+
217+
/**
218+
* Adds additional resource-based labels to log entries.
219+
* Labels that can be provided with {@link MonitoredResource.Builder#addLabel(String, String)}
220+
* are restricted to a supported set per resource.
221+
*
222+
* @see <a href="https://cloud.google.com/logging/docs/api/v2/resource-list">Logging Labels</a>
223+
*/
224+
private static class LabelLoggingEnhancer implements LoggingEnhancer {
225+
226+
private final Map<String, String> labels;
227+
228+
LabelLoggingEnhancer(String prefix, List<Label> labelNames) {
229+
labels = new HashMap<>();
230+
if (labelNames != null) {
231+
for (Label labelName : labelNames) {
232+
String labelValue = MonitoredResourceUtil.getValue(labelName);
233+
if (labelValue != null) {
234+
String fullLabelName = (prefix != null) ?
235+
prefix + labelName.getKey() : labelName.getKey();
236+
labels.put(fullLabelName, labelValue);
237+
}
238+
}
239+
}
240+
}
241+
242+
@Override
243+
public void enhanceLogEntry(Builder logEntry) {
244+
for (Map.Entry<String, String> label : labels.entrySet()) {
245+
logEntry.addLabel(label.getKey(), label.getValue());
246+
}
247+
}
248+
}
210249
}

branches/tswast-patch-1/google-cloud-logging/src/main/java/com/google/cloud/logging/TraceLoggingEnhancer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
/* Adds tracing support for logging with thread-local trace ID tracking. */
2020
public class TraceLoggingEnhancer implements LoggingEnhancer {
2121

22+
private final String traceIdLabel;
23+
24+
public TraceLoggingEnhancer(String prefix) {
25+
this.traceIdLabel = (prefix != null) ? prefix + "trace_id" : "";
26+
}
27+
2228
private static final ThreadLocal<String> traceId = new ThreadLocal<>();
2329

2430
/**
@@ -43,7 +49,7 @@ public static String getCurrentTraceId() {
4349
public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder) {
4450
String traceId = getCurrentTraceId();
4551
if (traceId != null) {
46-
builder.addLabel("trace_id", traceId);
52+
builder.addLabel(traceIdLabel, traceId);
4753
}
4854
}
4955
}

0 commit comments

Comments
 (0)