Skip to content

Commit 8936d80

Browse files
committed
fixing perf, concurrent update issues
1 parent 33bccbd commit 8936d80

3 files changed

Lines changed: 30 additions & 22 deletions

File tree

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
}

google-cloud-logging/src/main/java/com/google/cloud/logging/MonitoredResourceUtil.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.ArrayList;
2727
import java.util.Collections;
28+
import java.util.HashMap;
2829
import java.util.List;
2930
import java.util.Map;
3031

@@ -83,7 +84,7 @@ String getKey() {
8384
new ImmutableMap.Builder<String, Label[]>()
8485
.put(
8586
Resource.GaeAppFlex.getKey(),
86-
new Label[]{
87+
new Label[] {
8788
Label.ModuleId,
8889
Label.VersionId,
8990
Label.Zone
@@ -126,9 +127,10 @@ public static MonitoredResource getResource(String projectId, String resourceTyp
126127

127128
/**
128129
* Returns custom log entry enhancers (if available) for resource type.
130+
*
129131
* @return custom long entry enhancers
130132
*/
131-
public static List<LoggingEnhancer> getResourceEnhancers() {
133+
public static List<LoggingEnhancer> createResourceEnhancers() {
132134
Resource resourceType = getAutoDetectedResourceType();
133135
return getEnhancers(resourceType);
134136
}
@@ -195,7 +197,7 @@ private static String getAppEngineInstanceName() {
195197
}
196198

197199
private static List<LoggingEnhancer> getEnhancers(Resource resourceType) {
198-
List<LoggingEnhancer> enhancers = new ArrayList<>();
200+
List<LoggingEnhancer> enhancers = new ArrayList<>(2);
199201
switch (resourceType) {
200202
// Trace logging enhancer is supported on GAE Flex and Standard.
201203
case GaeAppFlex:
@@ -207,34 +209,40 @@ private static List<LoggingEnhancer> getEnhancers(Resource resourceType) {
207209
enhancers.add(new TraceLoggingEnhancer(APPENGINE_LABEL_PREFIX));
208210
break;
209211
default:
210-
enhancers = Collections.emptyList();
211212
break;
212213
}
213214
return enhancers;
214215
}
215216

216217
/**
217-
* Adds additional resource-based labels to log entries.
218-
* Labels that can be provided with {@link MonitoredResource.Builder#addLabel(String, String)}
219-
* are restricted to a supported set per resource.
220-
* @see <a href="https://cloud.google.com/logging/docs/api/v2/resource-list">Logging Labels</a>
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>
221223
*/
222224
private static class LabelLoggingEnhancer implements LoggingEnhancer {
223-
String prefix;
224-
List<Label> labels;
225225

226-
LabelLoggingEnhancer(String prefix, List<Label> labels) {
227-
this.prefix = prefix != null ? prefix : "";
228-
this.labels = labels;
226+
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+
}
229240
}
230241

231242
@Override
232243
public void enhanceLogEntry(Builder logEntry) {
233-
for (Label label : labels) {
234-
String labelValue = MonitoredResourceUtil.getValue(label);
235-
if (labelValue != null) {
236-
logEntry.addLabel(prefix + label.getKey(), labelValue);
237-
}
244+
for (Map.Entry<String, String> label : labels.entrySet()) {
245+
logEntry.addLabel(label.getKey(), label.getValue());
238246
}
239247
}
240248
}

google-cloud-logging/src/main/java/com/google/cloud/logging/TraceLoggingEnhancer.java

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

22-
private final String prefix;
22+
private final String traceIdLabel;
2323

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

2828
private static final ThreadLocal<String> traceId = new ThreadLocal<>();
@@ -49,7 +49,7 @@ public static String getCurrentTraceId() {
4949
public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder) {
5050
String traceId = getCurrentTraceId();
5151
if (traceId != null) {
52-
builder.addLabel(prefix + "trace_id", traceId);
52+
builder.addLabel(traceIdLabel, traceId);
5353
}
5454
}
5555
}

0 commit comments

Comments
 (0)