| 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 metric | 5 package metric |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "strings" | 8 "strings" |
| 9 "time" | 9 "time" |
| 10 | 10 |
| 11 "golang.org/x/net/context" | 11 "golang.org/x/net/context" |
| 12 | 12 |
| 13 "github.com/luci/luci-go/common/tsmon" |
| 13 "github.com/luci/luci-go/common/tsmon/distribution" | 14 "github.com/luci/luci-go/common/tsmon/distribution" |
| 14 "github.com/luci/luci-go/common/tsmon/field" | 15 "github.com/luci/luci-go/common/tsmon/field" |
| 15 "github.com/luci/luci-go/common/tsmon/types" | 16 "github.com/luci/luci-go/common/tsmon/types" |
| 16 ) | 17 ) |
| 17 | 18 |
| 18 // Metrics common to all tasks and devices. | 19 // Metrics common to all tasks and devices. |
| 19 var ( | 20 var ( |
| 20 presenceMetric = NewBool( | 21 presenceMetric = NewBool( |
| 21 "presence/up", | 22 "presence/up", |
| 22 "Set to True when the program is running, missing otherwise.", | 23 "Set to True when the program is running, missing otherwise.", |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 serverResponseStatusMetric = NewCounter( | 95 serverResponseStatusMetric = NewCounter( |
| 95 "http/server_response_status", | 96 "http/server_response_status", |
| 96 "Number of responses sent by HTTP status code.", | 97 "Number of responses sent by HTTP status code.", |
| 97 types.MetricMetadata{}, | 98 types.MetricMetadata{}, |
| 98 field.Int("status"), // HTTP status code | 99 field.Int("status"), // HTTP status code |
| 99 field.String("name"), // URL template | 100 field.String("name"), // URL template |
| 100 field.Bool("is_robot")) // If request is made by a bot | 101 field.Bool("is_robot")) // If request is made by a bot |
| 101 | 102 |
| 102 ) | 103 ) |
| 103 | 104 |
| 104 // UpdatePresenceMetrics sets the presence/up metric. | 105 func init() { |
| 105 func UpdatePresenceMetrics(ctx context.Context) { | 106 » registerCallbacks(context.Background()) |
| 106 » presenceMetric.Set(ctx, true) | 107 } |
| 108 |
| 109 func registerCallbacks(ctx context.Context) { |
| 110 » tsmon.RegisterCallbackIn(ctx, func(ctx context.Context) { |
| 111 » » presenceMetric.Set(ctx, true) |
| 112 » }) |
| 107 } | 113 } |
| 108 | 114 |
| 109 const ( | 115 const ( |
| 110 millisecondsInASecond = 1000.0 | 116 millisecondsInASecond = 1000.0 |
| 111 ) | 117 ) |
| 112 | 118 |
| 113 // UpdateHTTPMetrics updates the metrics for a request to a remote server. | 119 // UpdateHTTPMetrics updates the metrics for a request to a remote server. |
| 114 func UpdateHTTPMetrics(ctx context.Context, name string, client string, | 120 func UpdateHTTPMetrics(ctx context.Context, name string, client string, |
| 115 code int, duration time.Duration, requestBytes int64, responseBytes int6
4) { | 121 code int, duration time.Duration, requestBytes int64, responseBytes int6
4) { |
| 116 requestBytesMetric.Add(ctx, float64(requestBytes), name, client) | 122 requestBytesMetric.Add(ctx, float64(requestBytes), name, client) |
| 117 responseBytesMetric.Add(ctx, float64(responseBytes), name, client) | 123 responseBytesMetric.Add(ctx, float64(responseBytes), name, client) |
| 118 requestDurationsMetric.Add(ctx, duration.Seconds()*millisecondsInASecond
, name, client) | 124 requestDurationsMetric.Add(ctx, duration.Seconds()*millisecondsInASecond
, name, client) |
| 119 responseStatusMetric.Add(ctx, 1, code, name, client) | 125 responseStatusMetric.Add(ctx, 1, code, name, client) |
| 120 } | 126 } |
| 121 | 127 |
| 122 // UpdateServerMetrics updates the metrics for a handled request. | 128 // UpdateServerMetrics updates the metrics for a handled request. |
| 123 func UpdateServerMetrics(ctx context.Context, name string, code int, | 129 func UpdateServerMetrics(ctx context.Context, name string, code int, |
| 124 duration time.Duration, requestBytes int64, responseBytes int64, | 130 duration time.Duration, requestBytes int64, responseBytes int64, |
| 125 userAgent string) { | 131 userAgent string) { |
| 126 isRobot := (strings.Contains(userAgent, "GoogleBot") || | 132 isRobot := (strings.Contains(userAgent, "GoogleBot") || |
| 127 strings.Contains(userAgent, "GoogleSecurityScanner") || | 133 strings.Contains(userAgent, "GoogleSecurityScanner") || |
| 128 userAgent == "B3M/prober") | 134 userAgent == "B3M/prober") |
| 129 serverDurationsMetric.Add(ctx, duration.Seconds()*millisecondsInASecond, | 135 serverDurationsMetric.Add(ctx, duration.Seconds()*millisecondsInASecond, |
| 130 code, name, isRobot) | 136 code, name, isRobot) |
| 131 serverResponseStatusMetric.Add(ctx, 1, code, name, isRobot) | 137 serverResponseStatusMetric.Add(ctx, 1, code, name, isRobot) |
| 132 serverRequestBytesMetric.Add(ctx, float64(requestBytes), code, name, isR
obot) | 138 serverRequestBytesMetric.Add(ctx, float64(requestBytes), code, name, isR
obot) |
| 133 serverResponseBytesMetric.Add(ctx, float64(responseBytes), code, name, i
sRobot) | 139 serverResponseBytesMetric.Add(ctx, float64(responseBytes), code, name, i
sRobot) |
| 134 } | 140 } |
| OLD | NEW |