| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 main |
| 6 |
| 7 import ( |
| 8 "os" |
| 9 "time" |
| 10 |
| 11 "golang.org/x/net/context" |
| 12 |
| 13 "github.com/golang/protobuf/proto" |
| 14 "github.com/luci/luci-go/common/eventlog" |
| 15 logpb "github.com/luci/luci-go/common/eventlog/proto" |
| 16 ) |
| 17 |
| 18 type syncLogger interface { |
| 19 LogSync(ctx context.Context, events ...*eventlog.ChromeInfraLogEvent) er
ror |
| 20 NewLogEvent(ctx context.Context, eventTime eventlog.TypedTime) *eventlog
.ChromeInfraLogEvent |
| 21 } |
| 22 |
| 23 // An IsolateEventLogger logs eventlogs which contain stats about the data uploa
ded to the isolate server. |
| 24 type IsolateEventLogger struct { |
| 25 client syncLogger |
| 26 |
| 27 // getEnvironmentValue looks up the value of an environment variable. |
| 28 // Set this to override the default implementation for testing. |
| 29 getEnvironmentValue func(key string) *string |
| 30 } |
| 31 |
| 32 // NewLogger returns an IsolateEventLogger which logs to the specified endpoint. |
| 33 func NewLogger(ctx context.Context, endpoint string) *IsolateEventLogger { |
| 34 l := &IsolateEventLogger{} |
| 35 if host := eventlogEndpoint(endpoint); host != "" { |
| 36 l.client = eventlog.NewClient(ctx, host) |
| 37 } |
| 38 return l |
| 39 } |
| 40 |
| 41 // 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 { |
| 43 if l.client == nil { |
| 44 return nil |
| 45 } |
| 46 bi := l.getBuildbotInfo() |
| 47 event := l.client.NewLogEvent(ctx, eventlog.Point()) |
| 48 event.InfraEvent.IsolateClientEvent = &logpb.IsolateClientEvent{ |
| 49 Binary: &logpb.Binary{ |
| 50 Name: proto.String("isolate"), |
| 51 VersionNumber: proto.String(version), |
| 52 }, |
| 53 Operation: op, |
| 54 ArchiveDetails: archiveDetails, |
| 55 Master: bi.master, |
| 56 Builder: bi.builder, |
| 57 BuildId: bi.buildID, |
| 58 Slave: bi.slave, |
| 59 StartTsUsec: proto.Int64(int64(start.UnixNano() / 1e3)), |
| 60 EndTsUsec: proto.Int64(int64(end.UnixNano() / 1e3)), |
| 61 } |
| 62 return l.client.LogSync(ctx, event) |
| 63 } |
| 64 |
| 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. |
| 77 type buildbotInfo struct { |
| 78 // Variables which are not present in the environment are nil. |
| 79 master, builder, buildID, slave *string |
| 80 } |
| 81 |
| 82 // getBuildbotInfo poulates a buildbotInfo with information from the environment
. |
| 83 func (l *IsolateEventLogger) getBuildbotInfo() *buildbotInfo { |
| 84 return &buildbotInfo{ |
| 85 master: l.getEnvValue("BUILDBOT_MASTERNAME"), |
| 86 builder: l.getEnvValue("BUILDBOT_BUILDERNAME"), |
| 87 buildID: l.getEnvValue("BUILDBOT_BUILDNUMBER"), |
| 88 slave: l.getEnvValue("BUILDBOT_SLAVENAME"), |
| 89 } |
| 90 } |
| 91 |
| 92 func (l *IsolateEventLogger) getEnvValue(key string) *string { |
| 93 if l.getEnvironmentValue != nil { |
| 94 return l.getEnvironmentValue(key) |
| 95 } |
| 96 if val, ok := os.LookupEnv(key); ok { |
| 97 return &val |
| 98 } |
| 99 return nil |
| 100 } |
| OLD | NEW |