(t *testing.T)
| 31 | ) |
| 32 | |
| 33 | func TestStackdriverLog(t *testing.T) { |
| 34 | const ( |
| 35 | startTime = 1507914000 |
| 36 | startTimeNanos = 512 |
| 37 | |
| 38 | latencySec = 5 |
| 39 | latencyNanos = 123456789 |
| 40 | |
| 41 | endTime = startTime + latencySec |
| 42 | endTimeNanos = startTimeNanos + latencyNanos |
| 43 | ) |
| 44 | tracer := otel.Tracer("test") |
| 45 | ctx, span := tracer.Start(context.Background(), "test") |
| 46 | defer span.End() |
| 47 | sc := trace.SpanContextFromContext(ctx) |
| 48 | buf := new(bytes.Buffer) |
| 49 | var logErr error |
| 50 | l := NewStackdriverLogger(buf, func(e error) { logErr = e }) |
| 51 | want := &Entry{ |
| 52 | ReceivedTime: time.Unix(startTime, startTimeNanos), |
| 53 | RequestMethod: "POST", |
| 54 | RequestURL: "/foo/bar", |
| 55 | RequestHeaderSize: 456, |
| 56 | RequestBodySize: 123000, |
| 57 | UserAgent: "Chrome proxied through Firefox and Edge", |
| 58 | Referer: "http://www.example.com/", |
| 59 | Proto: "HTTP/1.1", |
| 60 | RemoteIP: "12.34.56.78", |
| 61 | ServerIP: "127.0.0.1", |
| 62 | Status: 404, |
| 63 | ResponseHeaderSize: 555, |
| 64 | ResponseBodySize: 789000, |
| 65 | Latency: latencySec*time.Second + latencyNanos*time.Nanosecond, |
| 66 | TraceID: sc.TraceID(), |
| 67 | SpanID: sc.SpanID(), |
| 68 | } |
| 69 | ent := *want // copy in case Log accidentally mutates |
| 70 | l.Log(&ent) |
| 71 | if logErr != nil { |
| 72 | t.Error("Logger called error callback:", logErr) |
| 73 | } |
| 74 | |
| 75 | var got json.RawMessage |
| 76 | if err := json.Unmarshal(buf.Bytes(), &got); err != nil { |
| 77 | t.Fatal("Unmarshal:", err) |
| 78 | } |
| 79 | |
| 80 | var r map[string]any |
| 81 | if err := json.Unmarshal(got, &r); err != nil { |
| 82 | t.Error("Unmarshal record:", err) |
| 83 | } else { |
| 84 | rr, _ := r["httpRequest"].(map[string]any) |
| 85 | if rr == nil { |
| 86 | t.Error("httpRequest does not exist in record or is not a JSON object") |
| 87 | } |
| 88 | if got, want := jsonString(rr, "requestMethod"), ent.RequestMethod; got != want { |
| 89 | t.Errorf("httpRequest.requestMethod = %q; want %q", got, want) |
| 90 | } |
nothing calls this directly
no test coverage detected