@@ -19,10 +19,13 @@ package logging_test
1919import (
2020 "context"
2121 "encoding/json"
22+ "errors"
2223 "flag"
2324 "fmt"
2425 "log"
2526 "math/rand"
27+ "net/http"
28+ "net/url"
2629 "os"
2730 "strings"
2831 "sync"
@@ -41,6 +44,7 @@ import (
4144 "google.golang.org/api/iterator"
4245 "google.golang.org/api/option"
4346 mrpb "google.golang.org/genproto/googleapis/api/monitoredres"
47+ logpb "google.golang.org/genproto/googleapis/logging/v2"
4448 "google.golang.org/grpc"
4549 "google.golang.org/grpc/codes"
4650 "google.golang.org/grpc/status"
@@ -247,6 +251,183 @@ func TestContextFunc(t *testing.T) {
247251 }
248252}
249253
254+ func TestToLogEntry (t * testing.T ) {
255+ u := & url.URL {Scheme : "http" }
256+ tests := []struct {
257+ name string
258+ in logging.Entry
259+ want logpb.LogEntry
260+ wantError error
261+ }{
262+ {
263+ name : "BlankLogEntry" ,
264+ in : logging.Entry {},
265+ want : logpb.LogEntry {},
266+ }, {
267+ name : "Already set Trace" ,
268+ in : logging.Entry {Trace : "t1" },
269+ want : logpb.LogEntry {Trace : "t1" },
270+ }, {
271+ name : "No X-Trace-Context header" ,
272+ in : logging.Entry {
273+ HTTPRequest : & logging.HTTPRequest {
274+ Request : & http.Request {URL : u , Header : http.Header {"foo" : {"bar" }}},
275+ },
276+ },
277+ want : logpb.LogEntry {},
278+ }, {
279+ name : "X-Trace-Context header with all fields" ,
280+ in : logging.Entry {
281+ TraceSampled : false ,
282+ HTTPRequest : & logging.HTTPRequest {
283+ Request : & http.Request {
284+ URL : u ,
285+ Header : http.Header {"X-Cloud-Trace-Context" : {"105445aa7843bc8bf206b120001000/000000000000004a;o=1" }},
286+ },
287+ },
288+ },
289+ want : logpb.LogEntry {
290+ Trace : "projects/P/traces/105445aa7843bc8bf206b120001000" ,
291+ SpanId : "000000000000004a" ,
292+ TraceSampled : true ,
293+ },
294+ }, {
295+ name : "X-Trace-Context header with all fields; TraceSampled explicitly set" ,
296+ in : logging.Entry {
297+ TraceSampled : true ,
298+ HTTPRequest : & logging.HTTPRequest {
299+ Request : & http.Request {
300+ URL : u ,
301+ Header : http.Header {"X-Cloud-Trace-Context" : {"105445aa7843bc8bf206b120001000/000000000000004a;o=0" }},
302+ },
303+ },
304+ },
305+ want : logpb.LogEntry {
306+ Trace : "projects/P/traces/105445aa7843bc8bf206b120001000" ,
307+ SpanId : "000000000000004a" ,
308+ TraceSampled : true ,
309+ },
310+ }, {
311+ name : "X-Trace-Context header with all fields; TraceSampled from Header" ,
312+ in : logging.Entry {
313+ HTTPRequest : & logging.HTTPRequest {
314+ Request : & http.Request {
315+ URL : u ,
316+ Header : http.Header {"X-Cloud-Trace-Context" : {"105445aa7843bc8bf206b120001000/000000000000004a;o=1" }},
317+ },
318+ },
319+ },
320+ want : logpb.LogEntry {
321+ Trace : "projects/P/traces/105445aa7843bc8bf206b120001000" ,
322+ SpanId : "000000000000004a" ,
323+ TraceSampled : true ,
324+ },
325+ }, {
326+ name : "X-Trace-Context header with blank trace" ,
327+ in : logging.Entry {
328+ HTTPRequest : & logging.HTTPRequest {
329+ Request : & http.Request {
330+ URL : u ,
331+ Header : http.Header {"X-Cloud-Trace-Context" : {"/0;o=1" }},
332+ },
333+ },
334+ },
335+ want : logpb.LogEntry {
336+ TraceSampled : true ,
337+ },
338+ }, {
339+ name : "X-Trace-Context header with blank span" ,
340+ in : logging.Entry {
341+ HTTPRequest : & logging.HTTPRequest {
342+ Request : & http.Request {
343+ URL : u ,
344+ Header : http.Header {"X-Cloud-Trace-Context" : {"105445aa7843bc8bf206b120001000/;o=0" }},
345+ },
346+ },
347+ },
348+ want : logpb.LogEntry {
349+ Trace : "projects/P/traces/105445aa7843bc8bf206b120001000" ,
350+ },
351+ }, {
352+ name : "X-Trace-Context header with missing traceSampled aka ?o=*" ,
353+ in : logging.Entry {
354+ HTTPRequest : & logging.HTTPRequest {
355+ Request : & http.Request {
356+ URL : u ,
357+ Header : http.Header {"X-Cloud-Trace-Context" : {"105445aa7843bc8bf206b120001000/0" }},
358+ },
359+ },
360+ },
361+ want : logpb.LogEntry {
362+ Trace : "projects/P/traces/105445aa7843bc8bf206b120001000" ,
363+ },
364+ }, {
365+ name : "X-Trace-Context header with all blank fields" ,
366+ in : logging.Entry {
367+ HTTPRequest : & logging.HTTPRequest {
368+ Request : & http.Request {
369+ URL : u ,
370+ Header : http.Header {"X-Cloud-Trace-Context" : {"" }},
371+ },
372+ },
373+ },
374+ want : logpb.LogEntry {},
375+ }, {
376+ name : "Invalid X-Trace-Context header but already set TraceID" ,
377+ in : logging.Entry {
378+ HTTPRequest : & logging.HTTPRequest {
379+ Request : & http.Request {
380+ URL : u ,
381+ Header : http.Header {"X-Cloud-Trace-Context" : {"t3" }},
382+ },
383+ },
384+ Trace : "t4" ,
385+ },
386+ want : logpb.LogEntry {
387+ Trace : "t4" ,
388+ },
389+ }, {
390+ name : "Already set TraceID and SpanID" ,
391+ in : logging.Entry {Trace : "t1" , SpanID : "007" },
392+ want : logpb.LogEntry {
393+ Trace : "t1" ,
394+ SpanId : "007" ,
395+ },
396+ }, {
397+ name : "Empty request produces an error" ,
398+ in : logging.Entry {
399+ HTTPRequest : & logging.HTTPRequest {
400+ RequestSize : 128 ,
401+ },
402+ },
403+ wantError : errors .New ("logging: HTTPRequest must have a non-nil Request" ),
404+ },
405+ }
406+ for _ , test := range tests {
407+ t .Run (test .name , func (t * testing.T ) {
408+ e , err := logging .ToLogEntry (test .in , "projects/P" )
409+ if err != nil && test .wantError == nil {
410+ t .Fatalf ("Unexpected error: %+v: %v" , test .in , err )
411+ }
412+ if err == nil && test .wantError != nil {
413+ t .Fatalf ("Error is expected: %+v: %v" , test .in , test .wantError )
414+ }
415+ if test .wantError != nil {
416+ return
417+ }
418+ if got := e .Trace ; got != test .want .Trace {
419+ t .Errorf ("TraceId: %+v: got %q, want %q" , test .in , got , test .want .Trace )
420+ }
421+ if got := e .SpanId ; got != test .want .SpanId {
422+ t .Errorf ("SpanId: %+v: got %q, want %q" , test .in , got , test .want .SpanId )
423+ }
424+ if got := e .TraceSampled ; got != test .want .TraceSampled {
425+ t .Errorf ("TraceSampled: %+v: got %t, want %t" , test .in , got , test .want .TraceSampled )
426+ }
427+ })
428+ }
429+ }
430+
250431// compareEntries compares most fields list of Entries against expected. compareEntries does not compare:
251432// - HTTPRequest
252433// - Operation
0 commit comments