| OLD | NEW |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "os" | 8 "os" |
| 9 "time" | 9 "time" |
| 10 | 10 |
| 11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 12 | 12 |
| 13 "github.com/golang/protobuf/proto" | 13 "github.com/golang/protobuf/proto" |
| 14 "github.com/luci/luci-go/common/eventlog" | 14 "github.com/luci/luci-go/common/eventlog" |
| 15 logpb "github.com/luci/luci-go/common/eventlog/proto" | 15 logpb "github.com/luci/luci-go/common/eventlog/proto" |
| 16 "github.com/luci/luci-go/common/logging" |
| 16 ) | 17 ) |
| 17 | 18 |
| 19 // eventlogEndpoint returns a destination URL for eventlogs, based on the conten
ts of |
| 20 // the endpointFlag. |
| 21 func eventlogEndpoint(ctx context.Context, endpointFlag string) string { |
| 22 switch endpointFlag { |
| 23 case "auto": |
| 24 if autoEventlog() { |
| 25 msg := "Automatically logging usage statistics to %s. If
this is not desired, then set --eventlog-endpoint=none" |
| 26 logging.Infof(ctx, msg, eventlog.ProdEndpoint) |
| 27 return eventlog.ProdEndpoint |
| 28 } |
| 29 return "" |
| 30 case "test": |
| 31 return eventlog.TestEndpoint |
| 32 case "prod": |
| 33 return eventlog.ProdEndpoint |
| 34 case "none": |
| 35 return "" |
| 36 default: |
| 37 return endpointFlag |
| 38 } |
| 39 } |
| 40 |
| 18 type syncLogger interface { | 41 type syncLogger interface { |
| 19 LogSync(ctx context.Context, events ...*eventlog.ChromeInfraLogEvent) er
ror | 42 LogSync(ctx context.Context, events ...*eventlog.ChromeInfraLogEvent) er
ror |
| 20 NewLogEvent(ctx context.Context, eventTime eventlog.TypedTime) *eventlog
.ChromeInfraLogEvent | 43 NewLogEvent(ctx context.Context, eventTime eventlog.TypedTime) *eventlog
.ChromeInfraLogEvent |
| 21 } | 44 } |
| 22 | 45 |
| 23 // An IsolateEventLogger logs eventlogs which contain stats about the data uploa
ded to the isolate server. | 46 // An IsolateEventLogger logs eventlogs which contain stats about the data uploa
ded to the isolate server. |
| 24 type IsolateEventLogger struct { | 47 type IsolateEventLogger struct { |
| 25 client syncLogger | 48 client syncLogger |
| 26 | 49 |
| 27 // getEnvironmentValue looks up the value of an environment variable. | 50 // getEnvironmentValue looks up the value of an environment variable. |
| 28 // Set this to override the default implementation for testing. | 51 // Set this to override the default implementation for testing. |
| 29 getEnvironmentValue func(key string) *string | 52 getEnvironmentValue func(key string) *string |
| 30 } | 53 } |
| 31 | 54 |
| 32 // NewLogger returns an IsolateEventLogger which logs to the specified endpoint. | 55 // NewLogger returns an IsolateEventLogger which logs to the specified endpoint. |
| 56 // endpoint is the URL to which logs will be sent. |
| 33 func NewLogger(ctx context.Context, endpoint string) *IsolateEventLogger { | 57 func NewLogger(ctx context.Context, endpoint string) *IsolateEventLogger { |
| 34 l := &IsolateEventLogger{} | 58 l := &IsolateEventLogger{} |
| 35 » if host := eventlogEndpoint(endpoint); host != "" { | 59 » if endpoint != "" { |
| 36 » » l.client = eventlog.NewClient(ctx, host) | 60 » » l.client = eventlog.NewClient(ctx, endpoint) |
| 37 } | 61 } |
| 38 return l | 62 return l |
| 39 } | 63 } |
| 40 | 64 |
| 41 // logStats synchronously logs an eventlog which describes an isolate run. | 65 // logStats synchronously logs an eventlog which describes an isolate run. |
| 42 func (l *IsolateEventLogger) logStats(ctx context.Context, op *logpb.IsolateClie
ntEvent_Operation, start, end time.Time, archiveDetails *logpb.IsolateClientEven
t_ArchiveDetails) error { | 66 func (l *IsolateEventLogger) logStats(ctx context.Context, op *logpb.IsolateClie
ntEvent_Operation, start, end time.Time, archiveDetails *logpb.IsolateClientEven
t_ArchiveDetails) error { |
| 43 if l.client == nil { | 67 if l.client == nil { |
| 44 return nil | 68 return nil |
| 45 } | 69 } |
| 46 bi := l.getBuildbotInfo() | 70 bi := l.getBuildbotInfo() |
| 47 event := l.client.NewLogEvent(ctx, eventlog.Point()) | 71 event := l.client.NewLogEvent(ctx, eventlog.Point()) |
| 48 event.InfraEvent.IsolateClientEvent = &logpb.IsolateClientEvent{ | 72 event.InfraEvent.IsolateClientEvent = &logpb.IsolateClientEvent{ |
| 49 Binary: &logpb.Binary{ | 73 Binary: &logpb.Binary{ |
| 50 Name: proto.String("isolate"), | 74 Name: proto.String("isolate"), |
| 51 VersionNumber: proto.String(version), | 75 VersionNumber: proto.String(version), |
| 52 }, | 76 }, |
| 53 Operation: op, | 77 Operation: op, |
| 54 ArchiveDetails: archiveDetails, | 78 ArchiveDetails: archiveDetails, |
| 55 Master: bi.master, | 79 Master: bi.master, |
| 56 Builder: bi.builder, | 80 Builder: bi.builder, |
| 57 BuildId: bi.buildID, | 81 BuildId: bi.buildID, |
| 58 Slave: bi.slave, | 82 Slave: bi.slave, |
| 59 StartTsUsec: proto.Int64(int64(start.UnixNano() / 1e3)), | 83 StartTsUsec: proto.Int64(int64(start.UnixNano() / 1e3)), |
| 60 EndTsUsec: proto.Int64(int64(end.UnixNano() / 1e3)), | 84 EndTsUsec: proto.Int64(int64(end.UnixNano() / 1e3)), |
| 61 } | 85 } |
| 62 return l.client.LogSync(ctx, event) | 86 return l.client.LogSync(ctx, event) |
| 63 } | 87 } |
| 64 | 88 |
| 65 func eventlogEndpoint(endpointFlag string) string { | |
| 66 switch endpointFlag { | |
| 67 case "test": | |
| 68 return eventlog.TestEndpoint | |
| 69 case "prod": | |
| 70 return eventlog.ProdEndpoint | |
| 71 default: | |
| 72 return endpointFlag | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 // buildbotInfo contains information about the build in which this command was r
un. | 89 // buildbotInfo contains information about the build in which this command was r
un. |
| 77 type buildbotInfo struct { | 90 type buildbotInfo struct { |
| 78 // Variables which are not present in the environment are nil. | 91 // Variables which are not present in the environment are nil. |
| 79 master, builder, buildID, slave *string | 92 master, builder, buildID, slave *string |
| 80 } | 93 } |
| 81 | 94 |
| 82 // getBuildbotInfo poulates a buildbotInfo with information from the environment
. | 95 // getBuildbotInfo poulates a buildbotInfo with information from the environment
. |
| 83 func (l *IsolateEventLogger) getBuildbotInfo() *buildbotInfo { | 96 func (l *IsolateEventLogger) getBuildbotInfo() *buildbotInfo { |
| 84 return &buildbotInfo{ | 97 return &buildbotInfo{ |
| 85 master: l.getEnvValue("BUILDBOT_MASTERNAME"), | 98 master: l.getEnvValue("BUILDBOT_MASTERNAME"), |
| 86 builder: l.getEnvValue("BUILDBOT_BUILDERNAME"), | 99 builder: l.getEnvValue("BUILDBOT_BUILDERNAME"), |
| 87 buildID: l.getEnvValue("BUILDBOT_BUILDNUMBER"), | 100 buildID: l.getEnvValue("BUILDBOT_BUILDNUMBER"), |
| 88 slave: l.getEnvValue("BUILDBOT_SLAVENAME"), | 101 slave: l.getEnvValue("BUILDBOT_SLAVENAME"), |
| 89 } | 102 } |
| 90 } | 103 } |
| 91 | 104 |
| 92 func (l *IsolateEventLogger) getEnvValue(key string) *string { | 105 func (l *IsolateEventLogger) getEnvValue(key string) *string { |
| 93 if l.getEnvironmentValue != nil { | 106 if l.getEnvironmentValue != nil { |
| 94 return l.getEnvironmentValue(key) | 107 return l.getEnvironmentValue(key) |
| 95 } | 108 } |
| 96 if val, ok := os.LookupEnv(key); ok { | 109 if val, ok := os.LookupEnv(key); ok { |
| 97 return &val | 110 return &val |
| 98 } | 111 } |
| 99 return nil | 112 return nil |
| 100 } | 113 } |
| OLD | NEW |