Source File
client_stats.go
Belonging Package
go.opencensus.io/plugin/ochttp
package ochttp
import (
)
type statsTransport struct {
base http.RoundTripper
}
func ( statsTransport) ( *http.Request) (*http.Response, error) {
, := tag.New(.Context(),
tag.Upsert(KeyClientHost, .Host),
tag.Upsert(Host, .Host),
tag.Upsert(KeyClientPath, .URL.Path),
tag.Upsert(Path, .URL.Path),
tag.Upsert(KeyClientMethod, .Method),
tag.Upsert(Method, .Method))
= .WithContext()
:= &tracker{
start: time.Now(),
ctx: ,
}
.reqSize = -1
} else if .ContentLength > 0 {
.reqSize = .ContentLength
}
stats.Record(, ClientRequestCount.M(1))
, := .base.RoundTrip()
if != nil {
.statusCode = http.StatusInternalServerError
.end()
} else {
.statusCode = .StatusCode
if .Method != "HEAD" {
.respContentLength = .ContentLength
}
if .Body == nil {
.end()
} else {
.body = .Body
.Body = wrappedBody(, .Body)
}
}
return ,
}
func ( statsTransport) ( *http.Request) {
type interface {
(*http.Request)
}
if , := .base.(); {
.()
}
}
type tracker struct {
ctx context.Context
respSize int64
respContentLength int64
reqSize int64
start time.Time
body io.ReadCloser
statusCode int
endOnce sync.Once
}
var _ io.ReadCloser = (*tracker)(nil)
func ( *tracker) () {
.endOnce.Do(func() {
:= float64(time.Since(.start)) / float64(time.Millisecond)
:= .respSize
if .respSize == 0 && .respContentLength > 0 {
= .respContentLength
}
:= []stats.Measurement{
ClientSentBytes.M(.reqSize),
ClientReceivedBytes.M(),
ClientRoundtripLatency.M(),
ClientLatency.M(),
ClientResponseBytes.M(.respSize),
}
if .reqSize >= 0 {
= append(, ClientRequestBytes.M(.reqSize))
}
stats.RecordWithTags(.ctx, []tag.Mutator{
tag.Upsert(StatusCode, strconv.Itoa(.statusCode)),
tag.Upsert(KeyClientStatus, strconv.Itoa(.statusCode)),
}, ...)
})
}
func ( *tracker) ( []byte) (int, error) {
, := .body.Read()
.respSize += int64()
switch {
case nil:
return , nil
case io.EOF:
.end()
}
return ,
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |