| 55 | } |
| 56 | |
| 57 | func (l *StackdriverLogger) log(ent *Entry) error { |
| 58 | defer l.mu.Unlock() |
| 59 | l.mu.Lock() |
| 60 | |
| 61 | l.buf.Reset() |
| 62 | // r represents the fluent-plugin-google-cloud format |
| 63 | // See https://github.com/GoogleCloudPlatform/fluent-plugin-google-cloud/blob/f93046d92f7722db2794a042c3f2dde5df91a90b/lib/fluent/plugin/out_google_cloud.rb#L145 |
| 64 | // to check json tags |
| 65 | var r struct { |
| 66 | HTTPRequest struct { |
| 67 | RequestMethod string `json:"requestMethod"` |
| 68 | RequestURL string `json:"requestUrl"` |
| 69 | RequestSize int64 `json:"requestSize,string"` |
| 70 | Status int `json:"status"` |
| 71 | ResponseSize int64 `json:"responseSize,string"` |
| 72 | UserAgent string `json:"userAgent"` |
| 73 | RemoteIP string `json:"remoteIp"` |
| 74 | Referer string `json:"referer"` |
| 75 | Latency string `json:"latency"` |
| 76 | } `json:"httpRequest"` |
| 77 | Timestamp struct { |
| 78 | Seconds int64 `json:"seconds"` |
| 79 | Nanos int `json:"nanos"` |
| 80 | } `json:"timestamp"` |
| 81 | TraceID string `json:"logging.googleapis.com/trace"` |
| 82 | SpanID string `json:"logging.googleapis.com/spanId"` |
| 83 | } |
| 84 | r.HTTPRequest.RequestMethod = ent.RequestMethod |
| 85 | r.HTTPRequest.RequestURL = ent.RequestURL |
| 86 | // TODO(light): determine whether this is the formula LogEntry expects. |
| 87 | r.HTTPRequest.RequestSize = ent.RequestHeaderSize + ent.RequestBodySize |
| 88 | r.HTTPRequest.Status = ent.Status |
| 89 | // TODO(light): determine whether this is the formula LogEntry expects. |
| 90 | r.HTTPRequest.ResponseSize = ent.ResponseHeaderSize + ent.ResponseBodySize |
| 91 | r.HTTPRequest.UserAgent = ent.UserAgent |
| 92 | r.HTTPRequest.RemoteIP = ent.RemoteIP |
| 93 | r.HTTPRequest.Referer = ent.Referer |
| 94 | r.HTTPRequest.Latency = string(appendLatency(nil, ent.Latency)) |
| 95 | |
| 96 | t := ent.ReceivedTime.Add(ent.Latency) |
| 97 | r.Timestamp.Seconds = t.Unix() |
| 98 | r.Timestamp.Nanos = t.Nanosecond() |
| 99 | r.TraceID = ent.TraceID.String() |
| 100 | r.SpanID = ent.SpanID.String() |
| 101 | if err := l.enc.Encode(r); err != nil { |
| 102 | return err |
| 103 | } |
| 104 | _, err := l.w.Write(l.buf.Bytes()) |
| 105 | |
| 106 | return err |
| 107 | } |
| 108 | |
| 109 | func appendLatency(b []byte, d time.Duration) []byte { |
| 110 | // Parses format understood by google-fluentd (which is looser than the documented LogEntry format). |