Chromium Code Reviews| 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 // When --eventlog-endpoint=auto, we decide to send eventlogs based on whether - -isolate-server matches prodIsolateServer | |
|
M-A Ruel
2017/06/08 14:06:05
I don't understand why we don't want to log everyw
mcgreevy
2017/06/08 23:19:01
The reason is that luci-go is an open source proje
| |
| 20 const prodIsolateServer = "https://isolateserver.appspot.com" | |
| 21 | |
| 22 // eventlogEndpoint returns a destination URL for eventlogs, based on the conten ts of | |
| 23 // the endpointFlag and isolateServerFlag. | |
| 24 func eventlogEndpoint(ctx context.Context, endpointFlag string, isolateServerFla g string) string { | |
| 25 switch endpointFlag { | |
| 26 case "auto": | |
| 27 if isolateServerFlag == prodIsolateServer { | |
| 28 // If we are uploading to the prod server, we log this a ctivity. | |
| 29 msg := "Automatically logging usage statistics to %s. If this is not desired, then set --eventlog-endpoint=none" | |
| 30 logging.Infof(ctx, msg, eventlog.ProdEndpoint) | |
| 31 return eventlog.ProdEndpoint | |
| 32 } | |
| 33 return "" | |
| 34 case "test": | |
| 35 return eventlog.TestEndpoint | |
| 36 case "prod": | |
| 37 return eventlog.ProdEndpoint | |
| 38 case "none": | |
| 39 return "" | |
| 40 default: | |
| 41 return endpointFlag | |
| 42 } | |
| 43 } | |
| 44 | |
| 18 type syncLogger interface { | 45 type syncLogger interface { |
| 19 LogSync(ctx context.Context, events ...*eventlog.ChromeInfraLogEvent) er ror | 46 LogSync(ctx context.Context, events ...*eventlog.ChromeInfraLogEvent) er ror |
| 20 NewLogEvent(ctx context.Context, eventTime eventlog.TypedTime) *eventlog .ChromeInfraLogEvent | 47 NewLogEvent(ctx context.Context, eventTime eventlog.TypedTime) *eventlog .ChromeInfraLogEvent |
| 21 } | 48 } |
| 22 | 49 |
| 23 // An IsolateEventLogger logs eventlogs which contain stats about the data uploa ded to the isolate server. | 50 // An IsolateEventLogger logs eventlogs which contain stats about the data uploa ded to the isolate server. |
| 24 type IsolateEventLogger struct { | 51 type IsolateEventLogger struct { |
| 25 client syncLogger | 52 client syncLogger |
| 26 | 53 |
| 27 // getEnvironmentValue looks up the value of an environment variable. | 54 // getEnvironmentValue looks up the value of an environment variable. |
| 28 // Set this to override the default implementation for testing. | 55 // Set this to override the default implementation for testing. |
| 29 getEnvironmentValue func(key string) *string | 56 getEnvironmentValue func(key string) *string |
| 30 } | 57 } |
| 31 | 58 |
| 32 // NewLogger returns an IsolateEventLogger which logs to the specified endpoint. | 59 // NewLogger returns an IsolateEventLogger which logs to the specified endpoint. |
| 60 // endpoint is the URL to which logs will be sent. | |
| 33 func NewLogger(ctx context.Context, endpoint string) *IsolateEventLogger { | 61 func NewLogger(ctx context.Context, endpoint string) *IsolateEventLogger { |
| 34 l := &IsolateEventLogger{} | 62 l := &IsolateEventLogger{} |
| 35 » if host := eventlogEndpoint(endpoint); host != "" { | 63 » if endpoint != "" { |
| 36 » » l.client = eventlog.NewClient(ctx, host) | 64 » » l.client = eventlog.NewClient(ctx, endpoint) |
| 37 } | 65 } |
| 38 return l | 66 return l |
| 39 } | 67 } |
| 40 | 68 |
| 41 // logStats synchronously logs an eventlog which describes an isolate run. | 69 // 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 { | 70 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 { | 71 if l.client == nil { |
| 44 return nil | 72 return nil |
| 45 } | 73 } |
| 46 bi := l.getBuildbotInfo() | 74 bi := l.getBuildbotInfo() |
| 47 event := l.client.NewLogEvent(ctx, eventlog.Point()) | 75 event := l.client.NewLogEvent(ctx, eventlog.Point()) |
| 48 event.InfraEvent.IsolateClientEvent = &logpb.IsolateClientEvent{ | 76 event.InfraEvent.IsolateClientEvent = &logpb.IsolateClientEvent{ |
| 49 Binary: &logpb.Binary{ | 77 Binary: &logpb.Binary{ |
| 50 Name: proto.String("isolate"), | 78 Name: proto.String("isolate"), |
| 51 VersionNumber: proto.String(version), | 79 VersionNumber: proto.String(version), |
| 52 }, | 80 }, |
| 53 Operation: op, | 81 Operation: op, |
| 54 ArchiveDetails: archiveDetails, | 82 ArchiveDetails: archiveDetails, |
| 55 Master: bi.master, | 83 Master: bi.master, |
| 56 Builder: bi.builder, | 84 Builder: bi.builder, |
| 57 BuildId: bi.buildID, | 85 BuildId: bi.buildID, |
| 58 Slave: bi.slave, | 86 Slave: bi.slave, |
| 59 StartTsUsec: proto.Int64(int64(start.UnixNano() / 1e3)), | 87 StartTsUsec: proto.Int64(int64(start.UnixNano() / 1e3)), |
| 60 EndTsUsec: proto.Int64(int64(end.UnixNano() / 1e3)), | 88 EndTsUsec: proto.Int64(int64(end.UnixNano() / 1e3)), |
| 61 } | 89 } |
| 62 return l.client.LogSync(ctx, event) | 90 return l.client.LogSync(ctx, event) |
| 63 } | 91 } |
| 64 | 92 |
| 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. | 93 // buildbotInfo contains information about the build in which this command was r un. |
| 77 type buildbotInfo struct { | 94 type buildbotInfo struct { |
| 78 // Variables which are not present in the environment are nil. | 95 // Variables which are not present in the environment are nil. |
| 79 master, builder, buildID, slave *string | 96 master, builder, buildID, slave *string |
| 80 } | 97 } |
| 81 | 98 |
| 82 // getBuildbotInfo poulates a buildbotInfo with information from the environment . | 99 // getBuildbotInfo poulates a buildbotInfo with information from the environment . |
| 83 func (l *IsolateEventLogger) getBuildbotInfo() *buildbotInfo { | 100 func (l *IsolateEventLogger) getBuildbotInfo() *buildbotInfo { |
| 84 return &buildbotInfo{ | 101 return &buildbotInfo{ |
| 85 master: l.getEnvValue("BUILDBOT_MASTERNAME"), | 102 master: l.getEnvValue("BUILDBOT_MASTERNAME"), |
| 86 builder: l.getEnvValue("BUILDBOT_BUILDERNAME"), | 103 builder: l.getEnvValue("BUILDBOT_BUILDERNAME"), |
| 87 buildID: l.getEnvValue("BUILDBOT_BUILDNUMBER"), | 104 buildID: l.getEnvValue("BUILDBOT_BUILDNUMBER"), |
| 88 slave: l.getEnvValue("BUILDBOT_SLAVENAME"), | 105 slave: l.getEnvValue("BUILDBOT_SLAVENAME"), |
| 89 } | 106 } |
| 90 } | 107 } |
| 91 | 108 |
| 92 func (l *IsolateEventLogger) getEnvValue(key string) *string { | 109 func (l *IsolateEventLogger) getEnvValue(key string) *string { |
| 93 if l.getEnvironmentValue != nil { | 110 if l.getEnvironmentValue != nil { |
| 94 return l.getEnvironmentValue(key) | 111 return l.getEnvironmentValue(key) |
| 95 } | 112 } |
| 96 if val, ok := os.LookupEnv(key); ok { | 113 if val, ok := os.LookupEnv(key); ok { |
| 97 return &val | 114 return &val |
| 98 } | 115 } |
| 99 return nil | 116 return nil |
| 100 } | 117 } |
| OLD | NEW |