generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
routing/http: feat: add client latency measure
Note that this measure includes some labels that also record: - HTTP status codes - HTTP method and path - Errors returned by the HTTP client - Call counts (included as part of histogram measure)
- Loading branch information
Showing
4 changed files
with
212 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"strconv" | ||
"time" | ||
|
||
"go.opencensus.io/stats" | ||
"go.opencensus.io/stats/view" | ||
"go.opencensus.io/tag" | ||
) | ||
|
||
var ( | ||
distMS = view.Distribution(0, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000) | ||
distLength = view.Distribution(0, 1, 2, 5, 10, 11, 12, 15, 20, 50, 100, 200, 500) | ||
|
||
measureLatency = stats.Int64("routing_http_client_latency", "the latency of operations by the routing HTTP client", stats.UnitMilliseconds) | ||
measureLength = stats.Int64("routing_http_client_length", "the number of elements in a response collection", stats.UnitDimensionless) | ||
|
||
keyOperation = tag.MustNewKey("operation") | ||
keyHost = tag.MustNewKey("host") | ||
keyStatusCode = tag.MustNewKey("code") | ||
keyError = tag.MustNewKey("error") | ||
|
||
ViewLatency = &view.View{ | ||
Measure: measureLatency, | ||
Aggregation: distMS, | ||
TagKeys: []tag.Key{keyOperation, keyHost, keyStatusCode, keyError}, | ||
} | ||
ViewLength = &view.View{ | ||
Measure: measureLength, | ||
Aggregation: distLength, | ||
TagKeys: []tag.Key{keyOperation, keyHost}, | ||
} | ||
|
||
OpenCensusViews = []*view.View{ | ||
ViewLatency, | ||
ViewLength, | ||
} | ||
) | ||
|
||
type measurement struct { | ||
operation string | ||
err error | ||
latency time.Duration | ||
statusCode int | ||
host string | ||
length int | ||
} | ||
|
||
func (m measurement) record(ctx context.Context) { | ||
stats.RecordWithTags( | ||
ctx, | ||
[]tag.Mutator{ | ||
tag.Upsert(keyHost, m.host), | ||
tag.Upsert(keyOperation, m.operation), | ||
tag.Upsert(keyStatusCode, strconv.Itoa(m.statusCode)), | ||
tag.Upsert(keyError, metricsErrStr(m.err)), | ||
}, | ||
measureLatency.M(int64(m.latency)), | ||
) | ||
if m.err == nil { | ||
stats.RecordWithTags( | ||
ctx, | ||
[]tag.Mutator{ | ||
tag.Upsert(keyHost, m.host), | ||
tag.Upsert(keyOperation, m.operation), | ||
}, | ||
measureLength.M(int64(m.length)), | ||
) | ||
} | ||
if m.err != nil { | ||
fmt.Printf("err: %s\n", m.err) | ||
} | ||
} | ||
|
||
func newMeasurement(operation string) measurement { | ||
return measurement{ | ||
operation: operation, | ||
host: "None", | ||
} | ||
} | ||
|
||
// metricsErrStr converts an error into a string that can be used as a metric label. | ||
// Errs are mapped to strings explicitly to avoid accidental high dimensionality. | ||
func metricsErrStr(err error) string { | ||
if err == nil { | ||
return "None" | ||
} | ||
var httpErr *HTTPError | ||
if errors.As(err, &httpErr) { | ||
return "HTTP" | ||
} | ||
if errors.Is(err, context.DeadlineExceeded) { | ||
return "DeadlineExceeded" | ||
} | ||
if errors.Is(err, context.Canceled) { | ||
return "Canceled" | ||
} | ||
var dnsErr *net.DNSError | ||
if errors.As(err, &dnsErr) { | ||
if dnsErr.IsNotFound { | ||
return "DNSNotFound" | ||
} | ||
if dnsErr.IsTimeout { | ||
return "DNSTimeout" | ||
} | ||
return "DNS" | ||
} | ||
|
||
var netErr net.Error | ||
if errors.As(err, &netErr) { | ||
if netErr.Timeout() { | ||
return "NetTimeout" | ||
} | ||
return "Net" | ||
} | ||
|
||
return "Other" | ||
} |