Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public LoggingHandler(

this.enhancers.addAll(enhancersParam);

List<LoggingEnhancer> loggingEnhancers = MonitoredResourceUtil.getResourceEnhancers();
List<LoggingEnhancer> loggingEnhancers = MonitoredResourceUtil.createResourceEnhancers();
if (loggingEnhancers != null) {
this.enhancers.addAll(loggingEnhancers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import com.google.cloud.MetadataConfig;
import com.google.cloud.MonitoredResource;
import com.google.cloud.ServiceOptions;
import com.google.cloud.logging.LogEntry.Builder;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -72,6 +75,8 @@ String getKey() {
}
}

private static final String APPENGINE_LABEL_PREFIX = "appengine.googleapis.com/";

private static Map<String, Label[]> resourceTypeWithLabels;

static {
Expand All @@ -80,15 +85,13 @@ String getKey() {
.put(
Resource.GaeAppFlex.getKey(),
new Label[] {
Label.InstanceName,
Label.ModuleId,
Label.VersionId,
Label.InstanceId,
Label.Zone
Label.ModuleId,
Label.VersionId,
Label.Zone
})
.put(
Resource.GaeAppStandard.getKey(),
new Label[] {Label.AppId, Label.ModuleId, Label.VersionId})
new Label[] {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();
Expand Down Expand Up @@ -124,11 +127,12 @@ public static MonitoredResource getResource(String projectId, String resourceTyp

/**
* Returns custom log entry enhancers (if available) for resource type.
*
* @return custom long entry enhancers
*/
public static List<LoggingEnhancer> getResourceEnhancers() {
public static List<LoggingEnhancer> createResourceEnhancers() {
Resource resourceType = getAutoDetectedResourceType();
return getEnhancers(resourceType);
return createEnhancers(resourceType);
}

private static String getValue(Label label) {
Expand Down Expand Up @@ -192,19 +196,54 @@ private static String getAppEngineInstanceName() {
return System.getenv("GAE_INSTANCE");
}

private static List<LoggingEnhancer> getEnhancers(Resource resourceType) {
List<LoggingEnhancer> enhancers;
private static List<LoggingEnhancer> createEnhancers(Resource resourceType) {
List<LoggingEnhancer> enhancers = new ArrayList<>(2);
switch (resourceType) {
// Trace logging enhancer is supported on GAE Flex and Standard.
case GaeAppStandard:
case GaeAppFlex:
enhancers = new ArrayList<>();
enhancers.add(new TraceLoggingEnhancer());
enhancers.add(new LabelLoggingEnhancer(
APPENGINE_LABEL_PREFIX, Collections.singletonList(Label.InstanceName)));
enhancers.add(new TraceLoggingEnhancer(APPENGINE_LABEL_PREFIX));
break;
case GaeAppStandard:
enhancers.add(new TraceLoggingEnhancer(APPENGINE_LABEL_PREFIX));
break;
default:
enhancers = Collections.emptyList();
break;
}
return enhancers;
}

/**
* Adds additional resource-based labels to log entries.
* Labels that can be provided with {@link MonitoredResource.Builder#addLabel(String, String)}
* are restricted to a supported set per resource.
*
* @see <a href="https://cloud.google.com/logging/docs/api/v2/resource-list">Logging Labels</a>
*/
private static class LabelLoggingEnhancer implements LoggingEnhancer {

private final Map<String, String> labels;

LabelLoggingEnhancer(String prefix, List<Label> labelNames) {
labels = new HashMap<>();
if (labelNames != null) {
for (Label labelName : labelNames) {
String labelValue = MonitoredResourceUtil.getValue(labelName);
if (labelValue != null) {
String fullLabelName = (prefix != null) ?
prefix + labelName.getKey() : labelName.getKey();
labels.put(fullLabelName, labelValue);
}
}
}
}

@Override
public void enhanceLogEntry(Builder logEntry) {
for (Map.Entry<String, String> label : labels.entrySet()) {
logEntry.addLabel(label.getKey(), label.getValue());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
/* Adds tracing support for logging with thread-local trace ID tracking. */
public class TraceLoggingEnhancer implements LoggingEnhancer {

private final String traceIdLabel;

public TraceLoggingEnhancer(String prefix) {
this.traceIdLabel = (prefix != null) ? prefix + "trace_id" : "";
}

private static final ThreadLocal<String> traceId = new ThreadLocal<>();

/**
Expand All @@ -43,7 +49,7 @@ public static String getCurrentTraceId() {
public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder) {
String traceId = getCurrentTraceId();
if (traceId != null) {
builder.addLabel("trace_id", traceId);
builder.addLabel(traceIdLabel, traceId);
}
}
}