Skip to content

Commit 7ff03cf

Browse files
authored
fix(logging): allow X-Cloud-Trace-Context fields to be optional (#3062)
* docs(logging): update doc link explaining x-cloud-trace-context * fix(logging): X-Cloud-Trace-Context fields can be optional (traceSample defaults to false) * style: go fmt logging.go * style(logging): updated inline comments to be clearer * refactor(logging): reCloudTraceContext regex is more readable
1 parent 49f497e commit 7ff03cf

2 files changed

Lines changed: 51 additions & 17 deletions

File tree

logging/logging.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -873,33 +873,31 @@ func (l *Logger) writeLogEntries(entries []*logpb.LogEntry) {
873873
// (for example by calling SetFlags or SetPrefix).
874874
func (l *Logger) StandardLogger(s Severity) *log.Logger { return l.stdLoggers[s] }
875875

876-
var reCloudTraceContext = regexp.MustCompile(`([a-f\d]+)/([a-f\d]+);o=(\d)`)
876+
var reCloudTraceContext = regexp.MustCompile(
877+
// Matches on "TRACE_ID"
878+
`([a-f\d]+)?` +
879+
// Matches on "/SPAN_ID"
880+
`(?:/([a-f\d]+))?` +
881+
// Matches on ";0=TRACE_TRUE"
882+
`(?:;o=(\d))?`)
877883

878884
func deconstructXCloudTraceContext(s string) (traceID, spanID string, traceSampled bool) {
879-
// As per the format described at https://cloud.google.com/trace/docs/troubleshooting#force-trace
885+
// As per the format described at https://cloud.google.com/trace/docs/setup#force-trace
880886
// "X-Cloud-Trace-Context: TRACE_ID/SPAN_ID;o=TRACE_TRUE"
881887
// for example:
882-
// "X-Cloud-Trace-Context: 105445aa7843bc8bf206b120001000/0;o=1"
888+
// "X-Cloud-Trace-Context: 105445aa7843bc8bf206b120001000/1;o=1"
883889
//
884890
// We expect:
885-
// * traceID: "105445aa7843bc8bf206b120001000"
886-
// * spanID: ""
887-
// * traceSampled: true
888-
matches := reCloudTraceContext.FindAllStringSubmatch(s, -1)
889-
if len(matches) != 1 {
890-
return
891-
}
891+
// * traceID (optional): "105445aa7843bc8bf206b120001000"
892+
// * spanID (optional): "1"
893+
// * traceSampled (optional): true
894+
matches := reCloudTraceContext.FindStringSubmatch(s)
892895

893-
sub := matches[0]
894-
if len(sub) != 4 {
895-
return
896-
}
896+
traceID, spanID, traceSampled = matches[1], matches[2], matches[3] == "1"
897897

898-
traceID, spanID = sub[1], sub[2]
899898
if spanID == "0" {
900899
spanID = ""
901900
}
902-
traceSampled = sub[3] == "1"
903901

904902
return
905903
}

logging/logging_unexported_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,18 +288,54 @@ func TestToLogEntryTrace(t *testing.T) {
288288
},
289289
logging.LogEntry{Trace: "projects/P/traces/105445aa7843bc8bf206b120001000", SpanId: "000000000000004a", TraceSampled: true},
290290
},
291+
{
292+
"X-Trace-Context header with blank trace",
293+
Entry{
294+
HTTPRequest: &HTTPRequest{
295+
Request: &http.Request{
296+
URL: u,
297+
Header: http.Header{"X-Cloud-Trace-Context": {"/0;o=1"}},
298+
},
299+
},
300+
},
301+
logging.LogEntry{TraceSampled: true},
302+
},
291303
{
292304
"X-Trace-Context header with blank span",
293305
Entry{
294306
HTTPRequest: &HTTPRequest{
295307
Request: &http.Request{
296308
URL: u,
297-
Header: http.Header{"X-Cloud-Trace-Context": {"105445aa7843bc8bf206b120001000/0;o=0"}},
309+
Header: http.Header{"X-Cloud-Trace-Context": {"105445aa7843bc8bf206b120001000/;o=0"}},
298310
},
299311
},
300312
},
301313
logging.LogEntry{Trace: "projects/P/traces/105445aa7843bc8bf206b120001000"},
302314
},
315+
{
316+
"X-Trace-Context header with missing traceSampled aka ?o=*",
317+
Entry{
318+
HTTPRequest: &HTTPRequest{
319+
Request: &http.Request{
320+
URL: u,
321+
Header: http.Header{"X-Cloud-Trace-Context": {"105445aa7843bc8bf206b120001000/0"}},
322+
},
323+
},
324+
},
325+
logging.LogEntry{Trace: "projects/P/traces/105445aa7843bc8bf206b120001000"},
326+
},
327+
{
328+
"X-Trace-Context header with all blank fields",
329+
Entry{
330+
HTTPRequest: &HTTPRequest{
331+
Request: &http.Request{
332+
URL: u,
333+
Header: http.Header{"X-Cloud-Trace-Context": {""}},
334+
},
335+
},
336+
},
337+
logging.LogEntry{},
338+
},
303339
{
304340
"Invalid X-Trace-Context header but already set TraceID",
305341
Entry{

0 commit comments

Comments
 (0)