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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: common/eventlog/internal/logservice/logservice.go
diff --git a/common/eventlog/internal/logservice/logservice.go b/common/eventlog/internal/logservice/logservice.go
new file mode 100644
index 0000000000000000000000000000000000000000..7734bb01404136994e6c8c717913d147bc403c57
--- /dev/null
+++ b/common/eventlog/internal/logservice/logservice.go
@@ -0,0 +1,74 @@
+// Copyright 2016 The LUCI Authors. All rights reserved.
+// Use of this source code is governed under the Apache License, Version 2.0
+// that can be found in the LICENSE file.
+
+// Package logservice provides a client which can be used to to collect and send batches of logs to the eventlog service.
+package logservice
+
+import (
+ "bytes"
+ "net/http"
+ "time"
+
+ "github.com/golang/protobuf/proto"
+
+ logpb "github.com/luci/luci-go/common/eventlog/proto"
+ "golang.org/x/net/context"
+ "golang.org/x/net/context/ctxhttp"
+)
+
+// Client sends event logs to the eventlog service.
+type Client struct {
+ HTTPClient *http.Client
+ serverAddr string
+ logSource string
+}
+
+// NewClient constructs a new Client.
+// Users must call Close when the Client is no longer needed.
+func NewClient(serverAddr, logSourceName string) *Client {
+ return &Client{
+ serverAddr: serverAddr,
+ logSource: logSourceName,
+ }
+}
+
+// TODO(mcgreevy): support bundling log requests.
+
+// LogSync synchronously logs events to the eventlog service.
+// The EventTime in each event must have been obtained from time.Now.
+func (c *Client) LogSync(ctx context.Context, events []*logpb.LogRequestLite_LogEventLite) error {
+ // TODO(mcgreevy): consider supporting custom clocks.
+ log := &logpb.LogRequestLite{
+ RequestTimeMs: proto.Int64(time.Now().UnixNano() / 1e6),
+ LogSourceName: &c.logSource,
+ LogEvent: events,
+ }
+
+ buf, err := proto.Marshal(log)
+ if err != nil {
+ return err
+ }
+
+ 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.
+ if err != nil {
+ return err
+ }
+
+ 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.
+ req.Header.Set("Content-Type", "application/octet-stream")
+ 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
+ if err != nil {
+ return err
+ }
+ resp.Body.Close()
+
+ return nil
+}
+
+// Close flushes any pending logs and releases any resources held by the client.
+// Close should be called when the client is no longer needed.
+func (c *Client) Close() error {
+ // We will need this later, but for now it's a no-op.
+ return nil
+}

Powered by Google App Engine
This is Rietveld 408576698