Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -19,8 +19,10 @@
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.List;
Expand Down Expand Up @@ -72,23 +74,23 @@ String getKey() {
}
}

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

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
new Label[]{
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 @@ -193,18 +195,47 @@ private static String getAppEngineInstanceName() {
}

private static List<LoggingEnhancer> getEnhancers(Resource resourceType) {

This comment was marked as spam.

This comment was marked as spam.

List<LoggingEnhancer> enhancers;
List<LoggingEnhancer> enhancers = new ArrayList<>();

This comment was marked as spam.

This comment was marked as spam.

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();

This comment was marked as spam.

This comment was marked as spam.

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 {

This comment was marked as spam.

This comment was marked as spam.

String prefix;
List<Label> labels;

LabelLoggingEnhancer(String prefix, List<Label> labels) {

This comment was marked as spam.

This comment was marked as spam.

this.prefix = prefix != null ? prefix : "";
this.labels = labels;
}

@Override
public void enhanceLogEntry(Builder logEntry) {
for (Label label : labels) {
String labelValue = MonitoredResourceUtil.getValue(label);
if (labelValue != null) {
logEntry.addLabel(prefix + label.getKey(), labelValue);
}
}
}
}
}
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 prefix;

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

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(prefix + "trace_id", traceId);
}
}
}