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

Side by Side Diff: common/eventlog/eventlog.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 eventlog provides a client which can be used to to collect and send b atches of ChromeInfraEvent logs to the eventlog service.
6 package eventlog
7
8 import (
9 "net/http"
10 "time"
11
12 "golang.org/x/net/context"
13
14 "github.com/golang/protobuf/proto"
15 "github.com/luci/luci-go/common/eventlog/internal/logservice"
16 logpb "github.com/luci/luci-go/common/eventlog/proto"
17 )
18
19 // TestEndpoint is the server address for test logs.
20 const TestEndpoint = "https://jmt17.google.com/log"
21
22 // ProdEndpoint is the server address for production logs.
23 const ProdEndpoint = "https://play.googleapis.com/log"
24
25 // Client may be used to send ChromeInfraEvent logs to the eventlog service.
26 type Client struct {
27 c *logservice.Client
28
29 // EventSource identifies the log producer. It may be configured before sending logs.
30 EventSource *logpb.InfraEventSource
31 }
32
33 // NewClient constructs a Client which can be used to send ChromeInfraEvent logs to the eventlog service.
34 // Users must call Close when the Client is no longer needed.
35 func NewClient(serverAddr string, opts ...ClientOption) *Client {
36 // TODO(mcgreevy): help users to set EventSource?
37
38 settings := clientSettings{
39 HTTPClient: http.DefaultClient,
40 }
41 settings.Populate(opts)
42
43 serviceClient := logservice.NewClient(serverAddr, "CHROME_INFRA")
44 serviceClient.HTTPClient = settings.HTTPClient
45 return &Client{c: serviceClient}
46 }
47
48 // LogSync synchronously logs events to the eventlog service.
49 // Use NewLogEvent to assist with constructing a well-formed log event.
50 // LogSync takes ownership of events.
51 func (c *Client) LogSync(ctx context.Context, events []*ChromeInfraLogEvent) err or {
djd-OOO-Apr2017 2016/11/21 04:02:24 I'd use the ... form for events here (for the comm
mcgreevy 2016/11/21 04:51:06 Done.
52 var logEvents []*logpb.LogRequestLite_LogEventLite
53
54 for _, event := range events {
55 sourceExt, err := proto.Marshal(event.InfraEvent)
56 if err != nil {
57 return err
58 }
59 event.LogEvent.SourceExtension = sourceExt
60 logEvents = append(logEvents, event.LogEvent)
61 }
62
63 return c.c.LogSync(ctx, logEvents)
64 }
65
66 // NewLogEvent constructs a well-formed log event.
67 func (c *Client) NewLogEvent(ctx context.Context, eventTime TypedTime) *ChromeIn fraLogEvent {
68 // TODO(mcgreevy): look into whether we can populate TraceID, SpanId, Pa rentId from the context.
69 // TODO(mcgreevy): support event_code, event_flow_id.
70 return &ChromeInfraLogEvent{
71 LogEvent: &logpb.LogRequestLite_LogEventLite{
72 EventTimeMs: proto.Int64(eventTime.Time.UnixNano() / 1e6 ),
73 },
74 InfraEvent: &logpb.ChromeInfraEvent{
75 TimestampKind: &eventTime.Kind,
76 EventSource: c.EventSource,
77 },
78 }
79 }
80
81 // Close flushes any pending logs and releases any resources held by the client.
82 // Close should be called when the client is no longer needed.
83 func (c *Client) Close() error {
84 return c.c.Close()
85 }
86
87 // ChromeInfraLogEvent stores a pending LogEvent, and the proto used to populate its SourceExtension field.
88 type ChromeInfraLogEvent struct {
89 // LogEvent is the event to be sent to the server. The LogEvent's Source Extension
90 // field will be overwritten with the serialized contents of InfraEvent
91 // when the log event is sent to the server.
92 LogEvent *logpb.LogRequestLite_LogEventLite
93 InfraEvent *logpb.ChromeInfraEvent
94 }
95
96 // TypedTime is a time, accompanied by an indication of what the time represents .
97 type TypedTime struct {
98 // The time at which an event occurrred. Must have been obtained from Ti me.Now.
djd-OOO-Apr2017 2016/11/21 04:02:24 sp. occurred The "must have been obtained" is the
mcgreevy 2016/11/21 04:51:06 Yeah, it could, but I'm worried about making this
djd-OOO-Apr2017 2016/11/21 05:51:26 I like the first of those options (Point/Begin/End
99 Time time.Time
100 Kind logpb.ChromeInfraEvent_TimestampKind
101 }
OLDNEW
« no previous file with comments | « no previous file | common/eventlog/internal/logservice/logservice.go » ('j') | common/eventlog/internal/logservice/logservice.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698