Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: common/eventlog/internal/logservice/logservice.go

Issue 2517503002: luci-go: Basic support for event logging in Go. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "golang.org/x/net/context/ctxhttp"
18 )
19
20 // Client sends event logs to the eventlog service.
21 type Client struct {
22 HTTPClient *http.Client
23 serverAddr string
24 logSource string
25 }
26
27 // NewClient constructs a new Client.
28 // Users must call Close when the Client is no longer needed.
29 func NewClient(serverAddr, logSourceName string) *Client {
30 return &Client{
31 serverAddr: serverAddr,
32 logSource: logSourceName,
33 }
34 }
35
36 // TODO(mcgreevy): support bundling log requests.
37
38 // LogSync synchronously logs events to the eventlog service.
39 // The EventTime in each event must have been obtained from time.Now.
40 func (c *Client) LogSync(ctx context.Context, events []*logpb.LogRequestLite_Log EventLite) error {
41 // TODO(mcgreevy): consider supporting custom clocks.
42 log := &logpb.LogRequestLite{
43 RequestTimeMs: proto.Int64(time.Now().UnixNano() / 1e6),
44 LogSourceName: &c.logSource,
45 LogEvent: events,
46 }
47
48 buf, err := proto.Marshal(log)
49 if err != nil {
50 return err
51 }
52
53 req, err := http.NewRequest("POST", c.serverAddr, bytes.NewBuffer(buf))
djd-OOO-Apr2017 2016/11/21 04:02:24 NewBuffer -> NewReader
mcgreevy 2016/11/21 04:51:06 Done.
54 if err != nil {
55 return err
56 }
57
58 req.Header = make(http.Header)
djd-OOO-Apr2017 2016/11/21 04:02:24 You can drop this line.
mcgreevy 2016/11/21 04:51:06 Done.
59 req.Header.Set("Content-Type", "application/octet-stream")
60 resp, err := ctxhttp.Do(ctx, c.HTTPClient, req)
djd-OOO-Apr2017 2016/11/21 04:02:24 We're targetting 1.7+ IIUC So req = req.WithCont
mcgreevy 2016/11/21 04:51:06 Yes, it looks like we moved to 1.7 since I last sy
61 if err != nil {
62 return err
63 }
64 resp.Body.Close()
65
66 return nil
67 }
68
69 // Close flushes any pending logs and releases any resources held by the client.
70 // Close should be called when the client is no longer needed.
71 func (c *Client) Close() error {
72 // We will need this later, but for now it's a no-op.
73 return nil
74 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698