| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 // Package logservice provides a client which can be used to to collect and send
batches of logs to the eventlog service. |
| 6 package logservice |
| 7 |
| 8 import ( |
| 9 "bytes" |
| 10 "net/http" |
| 11 "time" |
| 12 |
| 13 "github.com/golang/protobuf/proto" |
| 14 |
| 15 logpb "github.com/luci/luci-go/common/eventlog/proto" |
| 16 "golang.org/x/net/context" |
| 17 ) |
| 18 |
| 19 // Client sends event logs to the eventlog service. |
| 20 type Client struct { |
| 21 HTTPClient *http.Client |
| 22 serverAddr string |
| 23 logSource string |
| 24 } |
| 25 |
| 26 // NewClient constructs a new Client. |
| 27 // Users must call Close when the Client is no longer needed. |
| 28 func NewClient(serverAddr, logSourceName string) *Client { |
| 29 return &Client{ |
| 30 serverAddr: serverAddr, |
| 31 logSource: logSourceName, |
| 32 } |
| 33 } |
| 34 |
| 35 // TODO(mcgreevy): support bundling log requests. |
| 36 |
| 37 // LogSync synchronously logs events to the eventlog service. |
| 38 // The EventTime in each event must have been obtained from time.Now. |
| 39 func (c *Client) LogSync(ctx context.Context, events []*logpb.LogRequestLite_Log
EventLite) error { |
| 40 // TODO(mcgreevy): consider supporting custom clocks. |
| 41 log := &logpb.LogRequestLite{ |
| 42 RequestTimeMs: proto.Int64(time.Now().UnixNano() / 1e6), |
| 43 LogSourceName: &c.logSource, |
| 44 LogEvent: events, |
| 45 } |
| 46 |
| 47 buf, err := proto.Marshal(log) |
| 48 if err != nil { |
| 49 return err |
| 50 } |
| 51 |
| 52 req, err := http.NewRequest("POST", c.serverAddr, bytes.NewReader(buf)) |
| 53 if err != nil { |
| 54 return err |
| 55 } |
| 56 |
| 57 req.Header.Set("Content-Type", "application/octet-stream") |
| 58 req = req.WithContext(ctx) |
| 59 resp, err := c.HTTPClient.Do(req) |
| 60 if err != nil { |
| 61 return err |
| 62 } |
| 63 resp.Body.Close() |
| 64 |
| 65 return nil |
| 66 } |
| 67 |
| 68 // Close flushes any pending logs and releases any resources held by the client. |
| 69 // Close should be called when the client is no longer needed. |
| 70 func (c *Client) Close() error { |
| 71 // We will need this later, but for now it's a no-op. |
| 72 return nil |
| 73 } |
| OLD | NEW |