Skip to content

Commit 827c28a

Browse files
authored
---
yaml --- r: 9699 b: refs/heads/master c: 5c41d41 h: refs/heads/master i: 9697: 2c6e38a 9695: 9698a91
1 parent 58c6621 commit 827c28a

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 3db8085fb0c4ada19d3fc438e52ecb667995ed92
2+
refs/heads/master: 5c41d41c1ce36456a3778146b2d2f695d61531fa
33
refs/heads/travis: 47e4fee4fd5af9b2a8ce46f23c72ec95f9b195b2
44
refs/heads/gh-pages: e04ec732e2dd43f0ebc9921f3a6291b50c820e3a
55
refs/tags/0.0.9: 22f1839238f66c39e67ed4dfdcd273b1ae2e8444

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public TraceLoggingEnhancer(String prefix) {
3535
/**
3636
* Set the Trace ID associated with any logging done by the current thread.
3737
*
38-
* @param id The traceID
38+
* @param id The traceID, in the form projects/[PROJECT_ID]/traces/[TRACE_ID]
3939
*/
4040
public static void setCurrentTraceId(String id) {
4141
if (id == null) {
@@ -58,7 +58,7 @@ public static String getCurrentTraceId() {
5858
public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder) {
5959
String traceId = getCurrentTraceId();
6060
if (traceId != null) {
61-
builder.addLabel(traceIdLabel, traceId);
61+
builder.setTrace(traceId);
6262
}
6363
}
6464
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ public class LoggingHandlerTest {
131131
.addLabel("levelValue", String.valueOf(LoggingLevel.EMERGENCY.intValue()))
132132
.setTimestamp(123456789L)
133133
.build();
134+
private static final LogEntry TRACE_ENTRY = LogEntry.newBuilder(StringPayload.of(MESSAGE))
135+
.setSeverity(Severity.DEBUG)
136+
.addLabel("levelName", "FINEST")
137+
.addLabel("levelValue", String.valueOf(Level.FINEST.intValue()))
138+
.setTrace("projects/projectId/traces/traceId")
139+
.setTimestamp(123456789L)
140+
.build();
134141
private static final String CONFIG_NAMESPACE = "com.google.cloud.logging.LoggingHandler";
135142
private static final ImmutableMap<String, String> CONFIG_MAP =
136143
ImmutableMap.<String, String>builder()
@@ -314,6 +321,31 @@ public void enhanceLogEntry(Builder builder) {
314321
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
315322
}
316323

324+
@Test
325+
public void testTraceEnhancedLogEntry() {
326+
expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
327+
expect(options.getService()).andReturn(logging);
328+
MonitoredResource resource = MonitoredResource.of("custom", ImmutableMap.<String, String>of());
329+
logging.setFlushSeverity(Severity.ERROR);
330+
expectLastCall().once();
331+
logging.setWriteSynchronicity(Synchronicity.ASYNC);
332+
expectLastCall().once();
333+
logging.write(
334+
ImmutableList.of(TRACE_ENTRY),
335+
WriteOption.logName(LOG_NAME),
336+
WriteOption.resource(resource),
337+
WriteOption.labels(BASE_SEVERITY_MAP));
338+
expectLastCall().once();
339+
replay(options, logging);
340+
LoggingEnhancer enhancer = new TraceLoggingEnhancer();
341+
TraceLoggingEnhancer.setCurrentTraceId("projects/projectId/traces/traceId");
342+
Handler handler =
343+
new LoggingHandler(LOG_NAME, options, resource, Collections.singletonList(enhancer));
344+
handler.setLevel(Level.ALL);
345+
handler.setFormatter(new TestFormatter());
346+
handler.publish(newLogRecord(Level.FINEST, MESSAGE));
347+
}
348+
317349
@Test
318350
public void testReportWriteError() {
319351
expect(options.getProjectId()).andReturn(PROJECT).anyTimes();

0 commit comments

Comments
 (0)