OLD | NEW |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 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 "bytes" | 8 "bytes" |
9 "encoding/json" | 9 "encoding/json" |
10 "fmt" | 10 "fmt" |
11 "time" | 11 "time" |
12 | 12 |
13 "golang.org/x/net/context" | 13 "golang.org/x/net/context" |
14 "google.golang.org/grpc/codes" | 14 "google.golang.org/grpc/codes" |
15 | 15 |
16 "github.com/luci/luci-go/common/logging/memlogger" | 16 "github.com/luci/luci-go/common/logging/memlogger" |
17 "github.com/luci/luci-go/common/tsmon" | 17 "github.com/luci/luci-go/common/tsmon" |
18 "github.com/luci/luci-go/common/tsmon/metric" | 18 "github.com/luci/luci-go/common/tsmon/metric" |
| 19 "github.com/luci/luci-go/common/tsmon/types" |
19 | 20 |
20 "github.com/luci/luci-go/client/tokenclient" | 21 "github.com/luci/luci-go/client/tokenclient" |
21 "github.com/luci/luci-go/common/api/tokenserver" | 22 "github.com/luci/luci-go/common/api/tokenserver" |
22 ) | 23 ) |
23 | 24 |
24 // UpdateOutcome describes overall status of tokend token update process. | 25 // UpdateOutcome describes overall status of tokend token update process. |
25 type UpdateOutcome string | 26 type UpdateOutcome string |
26 | 27 |
27 // Some known outcomes. | 28 // Some known outcomes. |
28 // | 29 // |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 return AtomicWriteFile(ctx, path, blob, 0644) | 132 return AtomicWriteFile(ctx, path, blob, 0644) |
132 } | 133 } |
133 | 134 |
134 //////////////////////////////////////////////////////////////////////////////// | 135 //////////////////////////////////////////////////////////////////////////////// |
135 // All tsmon metrics. | 136 // All tsmon metrics. |
136 | 137 |
137 var ( | 138 var ( |
138 // E.g. "1.0". See Version const in main.go. | 139 // E.g. "1.0". See Version const in main.go. |
139 metricVersion = metric.NewString( | 140 metricVersion = metric.NewString( |
140 "luci/machine_tokend/version", | 141 "luci/machine_tokend/version", |
141 » » "Major version of luci_machine_tokend executable") | 142 » » "Major version of luci_machine_tokend executable", |
| 143 » » types.MetricMetadata{}) |
142 | 144 |
143 // E.g. "luci-token-server/2123-abcdef" (<appid>/<version>). | 145 // E.g. "luci-token-server/2123-abcdef" (<appid>/<version>). |
144 metricServiceVersion = metric.NewString( | 146 metricServiceVersion = metric.NewString( |
145 "luci/machine_tokend/service_version", | 147 "luci/machine_tokend/service_version", |
146 » » "Identifier of the server version that generated the token") | 148 » » "Identifier of the server version that generated the token", |
| 149 » » types.MetricMetadata{}) |
147 | 150 |
148 // This should be >=30 min in the future if everything is ok. If update | 151 // This should be >=30 min in the future if everything is ok. If update |
149 // process fails repeatedly, it will be in the past (and the token is un
usable | 152 // process fails repeatedly, it will be in the past (and the token is un
usable |
150 // at this point). | 153 // at this point). |
151 metricTokenExpiry = metric.NewInt( | 154 metricTokenExpiry = metric.NewInt( |
152 "luci/machine_tokend/token_expiry_ts", | 155 "luci/machine_tokend/token_expiry_ts", |
153 » » "Unix timestamp of when the token expires, in microsec") | 156 » » "Unix timestamp of when the token expires, in microsec", |
| 157 » » types.MetricMetadata{Units: types.Microseconds}) |
154 | 158 |
155 // This should be no longer than 30 min in the past if everything is ok. | 159 // This should be no longer than 30 min in the past if everything is ok. |
156 metricTokenLastUpdate = metric.NewInt( | 160 metricTokenLastUpdate = metric.NewInt( |
157 "luci/machine_tokend/last_update_ts", | 161 "luci/machine_tokend/last_update_ts", |
158 » » "Unix timestamp of when the token was successfully updated, in m
icrosec") | 162 » » "Unix timestamp of when the token was successfully updated, in m
icrosec", |
| 163 » » types.MetricMetadata{Units: types.Microseconds}) |
159 | 164 |
160 // This should be [0-30] min in the future if everything ok. If update p
rocess | 165 // This should be [0-30] min in the future if everything ok. If update p
rocess |
161 // fails (at least once), it will be in the past. It's not a fatal condi
tion | 166 // fails (at least once), it will be in the past. It's not a fatal condi
tion |
162 // yet. | 167 // yet. |
163 metricTokenNextUpdate = metric.NewInt( | 168 metricTokenNextUpdate = metric.NewInt( |
164 "luci/machine_tokend/next_update_ts", | 169 "luci/machine_tokend/next_update_ts", |
165 » » "Unix timestamp of when the token must be updated next time, in
microsec") | 170 » » "Unix timestamp of when the token must be updated next time, in
microsec", |
| 171 » » types.MetricMetadata{Units: types.Microseconds}) |
166 | 172 |
167 // See UpdateOutcome enum and OutcomeFromRPCError for possible values. | 173 // See UpdateOutcome enum and OutcomeFromRPCError for possible values. |
168 // | 174 // |
169 // Positive values are "TOKEN_IS_GOOD" and "UPDATE_SUCCESS". | 175 // Positive values are "TOKEN_IS_GOOD" and "UPDATE_SUCCESS". |
170 metricUpdateOutcome = metric.NewString( | 176 metricUpdateOutcome = metric.NewString( |
171 "luci/machine_tokend/update_outcome", | 177 "luci/machine_tokend/update_outcome", |
172 » » "Overall outcome of the luci_machine_tokend invocation") | 178 » » "Overall outcome of the luci_machine_tokend invocation", |
| 179 » » types.MetricMetadata{}) |
173 | 180 |
174 // See UpdateReason enum for possible values. | 181 // See UpdateReason enum for possible values. |
175 metricUpdateReason = metric.NewString( | 182 metricUpdateReason = metric.NewString( |
176 "luci/machine_tokend/update_reason", | 183 "luci/machine_tokend/update_reason", |
177 » » "Why the token was updated or 'TOKEN_IS_GOOD' if token is still
valid") | 184 » » "Why the token was updated or 'TOKEN_IS_GOOD' if token is still
valid", |
| 185 » » types.MetricMetadata{}) |
178 | 186 |
179 metricTotalDuration = metric.NewInt( | 187 metricTotalDuration = metric.NewInt( |
180 "luci/machine_tokend/duration_total_us", | 188 "luci/machine_tokend/duration_total_us", |
181 » » "For how long luci_machine_tokend ran (including all local IO) i
n microsec") | 189 » » "For how long luci_machine_tokend ran (including all local IO) i
n microsec", |
| 190 » » types.MetricMetadata{Units: types.Microseconds}) |
182 | 191 |
183 metricRPCDuration = metric.NewInt( | 192 metricRPCDuration = metric.NewInt( |
184 "luci/machine_tokend/duration_rpc_us", | 193 "luci/machine_tokend/duration_rpc_us", |
185 » » "For how long an RPC to backend ran in microsec") | 194 » » "For how long an RPC to backend ran in microsec", |
| 195 » » types.MetricMetadata{Units: types.Microseconds}) |
186 ) | 196 ) |
187 | 197 |
188 // SendMetrics is called at the end of the token update process. | 198 // SendMetrics is called at the end of the token update process. |
189 // | 199 // |
190 // It dumps all relevant metrics to tsmon. | 200 // It dumps all relevant metrics to tsmon. |
191 func (s *StatusReport) SendMetrics(c context.Context) error { | 201 func (s *StatusReport) SendMetrics(c context.Context) error { |
192 c, _ = context.WithTimeout(c, 10*time.Second) | 202 c, _ = context.WithTimeout(c, 10*time.Second) |
193 rep := s.Report() | 203 rep := s.Report() |
194 | 204 |
195 metricVersion.Set(c, rep.TokendVersion) | 205 metricVersion.Set(c, rep.TokendVersion) |
196 if rep.ServiceVersion != "" { | 206 if rep.ServiceVersion != "" { |
197 metricServiceVersion.Set(c, rep.ServiceVersion) | 207 metricServiceVersion.Set(c, rep.ServiceVersion) |
198 } | 208 } |
199 if rep.TokenExpiryTS != 0 { | 209 if rep.TokenExpiryTS != 0 { |
200 metricTokenExpiry.Set(c, rep.TokenExpiryTS*1000000) | 210 metricTokenExpiry.Set(c, rep.TokenExpiryTS*1000000) |
201 } | 211 } |
202 if rep.TokenLastUpdateTS != 0 { | 212 if rep.TokenLastUpdateTS != 0 { |
203 metricTokenLastUpdate.Set(c, rep.TokenLastUpdateTS*1000000) | 213 metricTokenLastUpdate.Set(c, rep.TokenLastUpdateTS*1000000) |
204 } | 214 } |
205 if rep.TokenNextUpdateTS != 0 { | 215 if rep.TokenNextUpdateTS != 0 { |
206 metricTokenNextUpdate.Set(c, rep.TokenNextUpdateTS*1000000) | 216 metricTokenNextUpdate.Set(c, rep.TokenNextUpdateTS*1000000) |
207 } | 217 } |
208 metricUpdateOutcome.Set(c, rep.UpdateOutcome) | 218 metricUpdateOutcome.Set(c, rep.UpdateOutcome) |
209 metricUpdateReason.Set(c, rep.UpdateReason) | 219 metricUpdateReason.Set(c, rep.UpdateReason) |
210 metricTotalDuration.Set(c, rep.TotalDuration) | 220 metricTotalDuration.Set(c, rep.TotalDuration) |
211 metricRPCDuration.Set(c, rep.RPCDuration) | 221 metricRPCDuration.Set(c, rep.RPCDuration) |
212 | 222 |
213 return tsmon.Flush(c) | 223 return tsmon.Flush(c) |
214 } | 224 } |
OLD | NEW |