OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 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 tsmon | 5 package tsmon |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "net/url" | 9 "net/url" |
10 "strings" | 10 "strings" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 } | 73 } |
74 | 74 |
75 // InitializeFromFlags configures the tsmon library from flag values. | 75 // InitializeFromFlags configures the tsmon library from flag values. |
76 // | 76 // |
77 // This will set a Target (information about what's reporting metrics) and a | 77 // This will set a Target (information about what's reporting metrics) and a |
78 // Monitor (where to send the metrics to). | 78 // Monitor (where to send the metrics to). |
79 func InitializeFromFlags(c context.Context, fl *Flags) error { | 79 func InitializeFromFlags(c context.Context, fl *Flags) error { |
80 // Load the config file, and override its values with flags. | 80 // Load the config file, and override its values with flags. |
81 config, err := loadConfig(fl.ConfigFile) | 81 config, err := loadConfig(fl.ConfigFile) |
82 if err != nil { | 82 if err != nil { |
83 » » return errors.Annotate(err).Reason("failed to load config file a
t [%(path)s]"). | 83 » » return errors.Annotate(err, "failed to load config file at [%s]"
, fl.ConfigFile).Err() |
84 » » » D("path", fl.ConfigFile).Err() | |
85 } | 84 } |
86 | 85 |
87 if fl.Endpoint != "" { | 86 if fl.Endpoint != "" { |
88 config.Endpoint = fl.Endpoint | 87 config.Endpoint = fl.Endpoint |
89 } | 88 } |
90 if fl.Credentials != "" { | 89 if fl.Credentials != "" { |
91 config.Credentials = fl.Credentials | 90 config.Credentials = fl.Credentials |
92 } | 91 } |
93 if fl.ActAs != "" { | 92 if fl.ActAs != "" { |
94 config.ActAs = fl.ActAs | 93 config.ActAs = fl.ActAs |
95 } | 94 } |
96 | 95 |
97 mon, err := initMonitor(c, config) | 96 mon, err := initMonitor(c, config) |
98 switch { | 97 switch { |
99 case err != nil: | 98 case err != nil: |
100 » » return errors.Annotate(err).Reason("failed to initialize monitor
").Err() | 99 » » return errors.Annotate(err, "failed to initialize monitor").Err(
) |
101 case mon == nil: | 100 case mon == nil: |
102 return nil // tsmon is disabled | 101 return nil // tsmon is disabled |
103 } | 102 } |
104 | 103 |
105 // Monitoring is enabled, so get the expensive default values for hostna
me, | 104 // Monitoring is enabled, so get the expensive default values for hostna
me, |
106 // etc. | 105 // etc. |
107 if config.AutoGenHostname { | 106 if config.AutoGenHostname { |
108 fl.Target.AutoGenHostname = true | 107 fl.Target.AutoGenHostname = true |
109 } | 108 } |
110 if config.Hostname != "" { | 109 if config.Hostname != "" { |
111 if fl.Target.DeviceHostname == "" { | 110 if fl.Target.DeviceHostname == "" { |
112 fl.Target.DeviceHostname = config.Hostname | 111 fl.Target.DeviceHostname = config.Hostname |
113 } | 112 } |
114 if fl.Target.TaskHostname == "" { | 113 if fl.Target.TaskHostname == "" { |
115 fl.Target.TaskHostname = config.Hostname | 114 fl.Target.TaskHostname = config.Hostname |
116 } | 115 } |
117 } | 116 } |
118 if config.Region != "" { | 117 if config.Region != "" { |
119 if fl.Target.DeviceRegion == "" { | 118 if fl.Target.DeviceRegion == "" { |
120 fl.Target.DeviceRegion = config.Region | 119 fl.Target.DeviceRegion = config.Region |
121 } | 120 } |
122 if fl.Target.TaskRegion == "" { | 121 if fl.Target.TaskRegion == "" { |
123 fl.Target.TaskRegion = config.Region | 122 fl.Target.TaskRegion = config.Region |
124 } | 123 } |
125 } | 124 } |
126 fl.Target.SetDefaultsFromHostname() | 125 fl.Target.SetDefaultsFromHostname() |
127 t, err := target.NewFromFlags(&fl.Target) | 126 t, err := target.NewFromFlags(&fl.Target) |
128 if err != nil { | 127 if err != nil { |
129 » » return errors.Annotate(err).Reason("failed to configure target f
rom flags").Err() | 128 » » return errors.Annotate(err, "failed to configure target from fla
gs").Err() |
130 } | 129 } |
131 | 130 |
132 Initialize(c, mon, store.NewInMemory(t)) | 131 Initialize(c, mon, store.NewInMemory(t)) |
133 | 132 |
134 state := GetState(c) | 133 state := GetState(c) |
135 if state.Flusher != nil { | 134 if state.Flusher != nil { |
136 logging.Infof(c, "Canceling previous tsmon auto flush") | 135 logging.Infof(c, "Canceling previous tsmon auto flush") |
137 state.Flusher.stop() | 136 state.Flusher.stop() |
138 state.Flusher = nil | 137 state.Flusher = nil |
139 } | 138 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 // newAuthenticator returns a new authenticator for HTTP requests. | 218 // newAuthenticator returns a new authenticator for HTTP requests. |
220 func newAuthenticator(ctx context.Context, credentials, actAs string, scopes []s
tring) *auth.Authenticator { | 219 func newAuthenticator(ctx context.Context, credentials, actAs string, scopes []s
tring) *auth.Authenticator { |
221 // TODO(vadimsh): Don't hardcode auth options here, pass them from outsi
de | 220 // TODO(vadimsh): Don't hardcode auth options here, pass them from outsi
de |
222 // somehow. | 221 // somehow. |
223 authOpts := chromeinfra.DefaultAuthOptions() | 222 authOpts := chromeinfra.DefaultAuthOptions() |
224 authOpts.ServiceAccountJSONPath = credentials | 223 authOpts.ServiceAccountJSONPath = credentials |
225 authOpts.Scopes = scopes | 224 authOpts.Scopes = scopes |
226 authOpts.ActAsServiceAccount = actAs | 225 authOpts.ActAsServiceAccount = actAs |
227 return auth.NewAuthenticator(ctx, auth.SilentLogin, authOpts) | 226 return auth.NewAuthenticator(ctx, auth.SilentLogin, authOpts) |
228 } | 227 } |
OLD | NEW |