Skip to content

Commit f9f862c

Browse files
committed
logging: reduce message size
Fixes #1712.
1 parent eea697f commit f9f862c

3 files changed

Lines changed: 47 additions & 95 deletions

File tree

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

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.google.cloud.MonitoredResource;
2222
import com.google.cloud.logging.Logging.WriteOption;
2323
import com.google.common.collect.ImmutableList;
24-
24+
import com.google.common.collect.ImmutableMap;
2525
import java.util.ArrayList;
2626
import java.util.Collections;
2727
import java.util.LinkedList;
@@ -97,6 +97,8 @@ public class LoggingHandler extends Handler {
9797
private static final String HANDLERS_PROPERTY = "handlers";
9898
private static final String ROOT_LOGGER_NAME = "";
9999
private static final String[] NO_HANDLERS = new String[0];
100+
private static final String LEVEL_NAME_KEY = "levelName";
101+
private static final String LEVEL_VALUE_KEY = "levelValue";
100102

101103
private static final ThreadLocal<Boolean> inPublishCall = new ThreadLocal<>();
102104

@@ -106,6 +108,7 @@ public class LoggingHandler extends Handler {
106108
private volatile Logging logging;
107109
private Level flushLevel;
108110
private long flushSize;
111+
private final Level nativeLevel;
109112
private final List<Enhancer> enhancers;
110113

111114
/**
@@ -169,14 +172,29 @@ public LoggingHandler(String log, LoggingOptions options, MonitoredResource moni
169172
this.options = options != null ? options : LoggingOptions.getDefaultInstance();
170173
this.flushLevel = helper.getLevelProperty(className + ".flushLevel", LoggingLevel.ERROR);
171174
this.flushSize = helper.getLongProperty(className + ".flushSize", 1L);
172-
setLevel(helper.getLevelProperty(className + ".level", Level.INFO));
175+
176+
Level level = helper.getLevelProperty(className + ".level", Level.INFO);
177+
setLevel(level);
178+
nativeLevel = level;
179+
173180
setFilter(helper.getFilterProperty(className + ".filter", null));
174181
setFormatter(helper.getFormatterProperty(className + ".formatter", new SimpleFormatter()));
175182
String logName = firstNonNull(log, helper.getProperty(className + ".log", "java.log"));
176183
this.enhancers = enhancers != null ? enhancers : helper.getEnhancerProperty(className + ".enhancers");
177184
String resourceType = helper.getProperty(className + ".resourceType", "global");
178185
MonitoredResource resource = monitoredResource != null ? monitoredResource : getDefaultResource(resourceType);
179-
writeOptions = new WriteOption[]{WriteOption.logName(logName), WriteOption.resource(resource)};
186+
187+
writeOptions =
188+
new WriteOption[] {
189+
WriteOption.logName(logName),
190+
WriteOption.resource(resource),
191+
WriteOption.labels(
192+
ImmutableMap.of(
193+
LEVEL_NAME_KEY,
194+
nativeLevel.getName(),
195+
LEVEL_VALUE_KEY,
196+
nativeLevel.intValue() + ""))
197+
};
180198
} catch (Exception ex) {
181199
reportError(null, ex, ErrorManager.OPEN_FAILURE);
182200
throw ex;
@@ -223,7 +241,7 @@ private MonitoredResource getDefaultResource(String resourceType) {
223241
return builder.build();
224242
}
225243

226-
private static class LogConfigHelper {
244+
static class LogConfigHelper {
227245

228246
private final LogManager manager = LogManager.getLogManager();
229247

@@ -356,16 +374,19 @@ public void publish(LogRecord record) {
356374
}
357375

358376
private LogEntry entryFor(LogRecord record) {
359-
String payload;
360377
try {
361-
payload = getFormatter().format(record);
378+
String payload = getFormatter().format(record);
362379
Level level = record.getLevel();
363380
LogEntry.Builder builder = LogEntry.newBuilder(Payload.StringPayload.of(payload))
364-
.addLabel("levelName", level.getName())
365-
.addLabel("levelValue", String.valueOf(level.intValue()))
366381
.setTimestamp(record.getMillis())
367382
.setSeverity(severityFor(level));
368383

384+
if (!nativeLevel.equals(level)) {
385+
builder
386+
.addLabel("levelName", level.getName())
387+
.addLabel("levelValue", String.valueOf(level.intValue()));
388+
}
389+
369390
for (Enhancer enhancer : enhancers) {
370391
enhancer.enhanceLogEntry(builder, record);
371392
}

google-cloud-logging/src/test/java/com/google/cloud/logging/AsyncLoggingHandlerTest.java

Lines changed: 0 additions & 78 deletions
This file was deleted.

google-cloud-logging/src/test/java/com/google/cloud/logging/LoggingHandlerTest.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ public class LoggingHandlerTest {
7575
.build();
7676
private static final LogEntry INFO_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
7777
.setSeverity(Severity.INFO)
78-
.addLabel("levelName", "INFO")
79-
.addLabel("levelValue", String.valueOf(Level.INFO.intValue()))
8078
.setTimestamp(123456789L)
8179
.build();
8280
private static final LogEntry WARNING_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
@@ -127,8 +125,14 @@ public class LoggingHandlerTest {
127125
.addLabel("levelValue", String.valueOf(LoggingLevel.EMERGENCY.intValue()))
128126
.setTimestamp(123456789L)
129127
.build();
128+
private static final ImmutableMap<String, String> NATIVE_SEVERITY_MAP =
129+
ImmutableMap.of("levelName", Level.INFO.getName(), "levelValue", Level.INFO.intValue() + "");
130130
private static final WriteOption[] DEFAULT_OPTION =
131-
new WriteOption[] {WriteOption.logName(LOG_NAME), WriteOption.resource(DEFAULT_RESOURCE)};
131+
new WriteOption[] {
132+
WriteOption.logName(LOG_NAME),
133+
WriteOption.resource(DEFAULT_RESOURCE),
134+
WriteOption.labels(NATIVE_SEVERITY_MAP)
135+
};
132136

133137
private Logging logging;
134138
private LoggingOptions options;
@@ -148,11 +152,10 @@ public void setUp() {
148152
}
149153

150154
@After
151-
public void afterClass() {
155+
public void after() {
152156
EasyMock.verify(logging, options);
153157
}
154158

155-
156159
private static LogRecord newLogRecord(Level level, String message) {
157160
LogRecord record = new LogRecord(level, message);
158161
record.setMillis(123456789L);
@@ -215,8 +218,11 @@ public void testPublishCustomResource() {
215218
EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
216219
EasyMock.expect(options.getService()).andReturn(logging);
217220
MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of());
218-
logging.writeAsync(ImmutableList.of(FINEST_ENTRY), WriteOption.logName(LOG_NAME),
219-
WriteOption.resource(resource));
221+
logging.writeAsync(
222+
ImmutableList.of(FINEST_ENTRY),
223+
WriteOption.logName(LOG_NAME),
224+
WriteOption.resource(resource),
225+
WriteOption.labels(NATIVE_SEVERITY_MAP));
220226
EasyMock.expectLastCall().andReturn(ApiFutures.immediateFuture(null));
221227
EasyMock.replay(options, logging);
222228
Handler handler = new LoggingHandler(LOG_NAME, options, resource);
@@ -230,8 +236,11 @@ public void testEnhancedLogEntry() {
230236
EasyMock.expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
231237
EasyMock.expect(options.getService()).andReturn(logging);
232238
MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of());
233-
logging.writeAsync(ImmutableList.of(FINEST_ENHANCED_ENTRY), WriteOption.logName(LOG_NAME),
234-
WriteOption.resource(resource));
239+
logging.writeAsync(
240+
ImmutableList.of(FINEST_ENHANCED_ENTRY),
241+
WriteOption.logName(LOG_NAME),
242+
WriteOption.resource(resource),
243+
WriteOption.labels(NATIVE_SEVERITY_MAP));
235244
EasyMock.expectLastCall().andReturn(ApiFutures.immediateFuture(null));
236245
EasyMock.replay(options, logging);
237246
LoggingHandler.Enhancer enhancer = new LoggingHandler.Enhancer() {

0 commit comments

Comments
 (0)