| 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 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 for _, o := range opts { |
| 42 o.apply(settings) |
| 43 } |
| 44 |
| 45 serviceClient := logservice.NewClient(serverAddr, "CHROME_INFRA") |
| 46 serviceClient.HTTPClient = settings.HTTPClient |
| 47 return &Client{c: serviceClient} |
| 48 } |
| 49 |
| 50 // LogSync synchronously logs events to the eventlog service. |
| 51 // Use NewLogEvent to assist with constructing a well-formed log event. |
| 52 // LogSync takes ownership of events. |
| 53 func (c *Client) LogSync(ctx context.Context, events ...*ChromeInfraLogEvent) er
ror { |
| 54 var logEvents []*logpb.LogRequestLite_LogEventLite |
| 55 |
| 56 for _, event := range events { |
| 57 sourceExt, err := proto.Marshal(event.InfraEvent) |
| 58 if err != nil { |
| 59 return err |
| 60 } |
| 61 event.LogEvent.SourceExtension = sourceExt |
| 62 logEvents = append(logEvents, event.LogEvent) |
| 63 } |
| 64 |
| 65 return c.c.LogSync(ctx, logEvents) |
| 66 } |
| 67 |
| 68 // NewLogEvent constructs a well-formed log event. |
| 69 // eventTime is the time that the event occurred, typically obtained by calling |
| 70 // one of Point, Begin or End. |
| 71 func (c *Client) NewLogEvent(ctx context.Context, eventTime TypedTime) *ChromeIn
fraLogEvent { |
| 72 // TODO(mcgreevy): look into whether we can populate TraceID, SpanId, Pa
rentId from the context. |
| 73 // TODO(mcgreevy): support event_code, event_flow_id. |
| 74 return &ChromeInfraLogEvent{ |
| 75 LogEvent: &logpb.LogRequestLite_LogEventLite{ |
| 76 EventTimeMs: proto.Int64(eventTime.Time.UnixNano() / 1e6
), |
| 77 }, |
| 78 InfraEvent: &logpb.ChromeInfraEvent{ |
| 79 TimestampKind: &eventTime.Kind, |
| 80 EventSource: c.EventSource, |
| 81 }, |
| 82 } |
| 83 } |
| 84 |
| 85 // Close flushes any pending logs and releases any resources held by the client. |
| 86 // Close should be called when the client is no longer needed. |
| 87 func (c *Client) Close() error { |
| 88 return c.c.Close() |
| 89 } |
| 90 |
| 91 // ChromeInfraLogEvent stores a pending LogEvent, and the proto used to populate
its SourceExtension field. |
| 92 type ChromeInfraLogEvent struct { |
| 93 // LogEvent is the event to be sent to the server. The LogEvent's Source
Extension |
| 94 // field will be overwritten with the serialized contents of InfraEvent |
| 95 // when the log event is sent to the server. |
| 96 LogEvent *logpb.LogRequestLite_LogEventLite |
| 97 InfraEvent *logpb.ChromeInfraEvent |
| 98 } |
| 99 |
| 100 // TypedTime is a time, accompanied by an indication of what the time represents
. |
| 101 type TypedTime struct { |
| 102 // The time at which an event occurred. Must have been obtained from Tim
e.Now. |
| 103 Time time.Time |
| 104 Kind logpb.ChromeInfraEvent_TimestampKind |
| 105 } |
| 106 |
| 107 // Point returns the current time for use when logging an event with no temporal
extent. |
| 108 func Point() TypedTime { |
| 109 return TypedTime{time.Now(), logpb.ChromeInfraEvent_POINT} |
| 110 } |
| 111 |
| 112 // Begin returns the current time for use when logging the beginning of an event
. |
| 113 func Begin() TypedTime { |
| 114 return TypedTime{time.Now(), logpb.ChromeInfraEvent_BEGIN} |
| 115 } |
| 116 |
| 117 // End returns the current time for use when logging the end of an event. |
| 118 func End() TypedTime { |
| 119 return TypedTime{time.Now(), logpb.ChromeInfraEvent_END} |
| 120 } |
| OLD | NEW |