internal/dcensus: customize our http views

Our usage of opencensus views is updated so that we only export 'custom
metrics' to StackDriver that start with the custom.googleapis.com
prefix. Additionally views are updated to always contain the server
route tag or client host tag.

Fixes b/141183559

Change-Id: Ie17db025bd61a3cc1ea969d214947d09cdacf96a
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/553907
CI-Result: Cloud Build <devtools-proctor-result-processor@system.gserviceaccount.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
This commit is contained in:
Rob Findley 2019-09-18 13:09:23 -04:00 коммит произвёл Julie Qiu
Родитель 0ae71ff38d
Коммит ef1f08bb21
5 изменённых файлов: 78 добавлений и 35 удалений

Просмотреть файл

@ -26,7 +26,6 @@ import (
"golang.org/x/discovery/internal/proxy"
"contrib.go.opencensus.io/integrations/ocsql"
"go.opencensus.io/plugin/ochttp"
)
var (
@ -84,15 +83,14 @@ func main() {
router := dcensus.NewRouter(nil)
server.Install(router.Handle)
views := append(ochttp.DefaultServerViews, ochttp.DefaultClientViews...)
views = append(views, dcensus.ViewByCodeRouteMethod, dcensus.ViewByCodeRouteMethodLatencyDistribution)
views := append(dcensus.ClientViews, dcensus.ServerViews...)
if err := dcensus.Init(views...); err != nil {
log.Fatal(err)
}
// We are not currently forwarding any ports on AppEngine, so serving debug
// information is broken.
if !config.OnAppEngine() {
dcensusServer, err := dcensus.NewServer(views...)
dcensusServer, err := dcensus.NewServer()
if err != nil {
log.Fatal(err)
}

Просмотреть файл

@ -14,7 +14,6 @@ import (
"cloud.google.com/go/logging"
"contrib.go.opencensus.io/integrations/ocsql"
"go.opencensus.io/plugin/ochttp"
"golang.org/x/discovery/internal/config"
"golang.org/x/discovery/internal/dcensus"
"golang.org/x/discovery/internal/frontend"
@ -68,14 +67,13 @@ func main() {
router := dcensus.NewRouter(frontend.TagRoute)
server.Install(router.Handle)
views := append(ochttp.DefaultServerViews, dcensus.ViewByCodeRouteMethod, dcensus.ViewByCodeRouteMethodLatencyDistribution)
if err := dcensus.Init(views...); err != nil {
if err := dcensus.Init(dcensus.ServerViews...); err != nil {
log.Fatal(err)
}
// We are not currently forwarding any ports on AppEngine, so serving debug
// information is broken.
if !config.OnAppEngine() {
dcensusServer, err := dcensus.NewServer(views...)
dcensusServer, err := dcensus.NewServer()
if err != nil {
log.Fatal(err)
}

Просмотреть файл

@ -102,7 +102,7 @@ var (
)
firstByteLatencyDistribution = &view.View{
Name: "go-discovery/first_byte_latency",
Name: "custom.googleapis.com/go-discovery/prober/first_byte_latency",
Measure: firstByteLatency,
Aggregation: ochttp.DefaultLatencyDistribution,
Description: "first-byte latency, by probe name and response status",

Просмотреть файл

@ -87,7 +87,7 @@ func Init(views ...*view.View) error {
}
// NewServer creates a new http.Handler for serving debug information.
func NewServer(views ...*view.View) (http.Handler, error) {
func NewServer() (http.Handler, error) {
pe, err := prometheus.NewExporter(prometheus.Options{})
if err != nil {
return nil, fmt.Errorf("dcensus.NewServer: prometheus.NewExporter: %v", err)
@ -170,27 +170,74 @@ func NewViewExporter() (_ *stackdriver.Exporter, err error) {
})
}
const (
codeRouteMethodCount = "opencensus.io/http/server/response_count_by_status_code_route_method"
codeRouteMethodLatency = "opencensus.io/http/server/response_latency_distribution_by_status_code_route_method"
// Customizations of ochttp views. Views are updated as follows:
// + names are changed to use the custom.googleapis.com prefix, in accordance
// with https://cloud.google.com/monitoring/api/v3/metrics-details#label_names
// + ClientHost and ServerRoute are added to resp. client and server metrics.
// Since these are bounded cardinality in our metrics, they are useful to
// add additional context.
// + Method tags are removed. We don't have any routes that accept more than
// one HTTP method.
var (
ClientCompletedCount = &view.View{
Name: "custom.googleapis.com/go-discovery/http/client/completed_count",
Measure: ochttp.ClientRoundtripLatency,
Aggregation: view.Count(),
Description: "Count of completed requests, by HTTP method and response status",
TagKeys: []tag.Key{ochttp.KeyClientHost, ochttp.KeyClientStatus},
}
ClientRoundtripLatencyDistribution = &view.View{
Name: "custom.googleapis.com/go-discovery/http/client/roundtrip_latency",
Measure: ochttp.ClientRoundtripLatency,
Aggregation: ochttp.DefaultLatencyDistribution,
Description: "End-to-end latency, by ClientHost and ClientStatus",
TagKeys: []tag.Key{ochttp.KeyClientHost, ochttp.KeyClientStatus},
}
ClientReceivedBytesDistribution = &view.View{
Name: "custom.googleapis.com/go-discovery/http/client/received_bytes",
Measure: ochttp.ClientReceivedBytes,
Aggregation: ochttp.DefaultSizeDistribution,
Description: "Total bytes received in response bodies (not including headers but including " +
"error responses with bodies), by ClientHost and ClientStatus",
TagKeys: []tag.Key{ochttp.KeyClientHost, ochttp.KeyClientStatus},
}
ServerRequestCount = &view.View{
Name: "custom.googleapis.com/go-discovery/http/server/request_count",
Description: "Count of HTTP requests started by ServerRoute",
TagKeys: []tag.Key{ochttp.KeyServerRoute},
Measure: ochttp.ServerRequestCount,
Aggregation: view.Count(),
}
ServerResponseCount = &view.View{
Name: "custom.googleapis.com/go-discovery/http/server/response_count",
Description: "Server response count by status code and route",
TagKeys: []tag.Key{ochttp.StatusCode, ochttp.KeyServerRoute},
Measure: ochttp.ServerLatency,
Aggregation: view.Count(),
}
ServerLatency = &view.View{
Name: "custom.googleapis.com/go-discovery/http/server/response_latency",
Description: "Server response distribution by status code and route",
TagKeys: []tag.Key{ochttp.KeyServerRoute},
Measure: ochttp.ServerLatency,
Aggregation: ochttp.DefaultLatencyDistribution,
}
ServerResponseBytes = &view.View{
Name: "custom.googleapis.com/go-discovery/http/server/response_bytes",
Description: "Size distribution of HTTP response body",
TagKeys: []tag.Key{ochttp.KeyServerRoute},
Measure: ochttp.ServerResponseBytes,
Aggregation: ochttp.DefaultSizeDistribution,
}
ClientViews = []*view.View{
ClientCompletedCount,
ClientRoundtripLatencyDistribution,
ClientReceivedBytesDistribution,
}
ServerViews = []*view.View{
ServerRequestCount,
ServerResponseCount,
ServerLatency,
ServerResponseBytes,
}
)
// ViewByCodeRouteMethod is a view of HTTP server requests parameterized
// by StatusCode, Route, and HTTP method.
var ViewByCodeRouteMethod = &view.View{
Name: codeRouteMethodCount,
Description: "Server response count by status code",
TagKeys: []tag.Key{ochttp.StatusCode, ochttp.KeyServerRoute, ochttp.Method},
Measure: ochttp.ServerLatency,
Aggregation: view.Count(),
}
// ViewByCodeRouteMethodLatencyDistribution is a view of HTTP server requests
// parameterized by StatusCode, Route, and HTTP method.
var ViewByCodeRouteMethodLatencyDistribution = &view.View{
Name: codeRouteMethodLatency,
Description: "Server response distribution by status code",
TagKeys: []tag.Key{ochttp.StatusCode, ochttp.KeyServerRoute, ochttp.Method},
Measure: ochttp.ServerLatency,
Aggregation: ochttp.DefaultLatencyDistribution,
}

Просмотреть файл

@ -16,7 +16,7 @@ import (
)
func TestRouter(t *testing.T) {
view.Register(ViewByCodeRouteMethod)
view.Register(ServerResponseCount)
handler := func(w http.ResponseWriter, r *http.Request) {}
tagger := func(route string, r *http.Request) string {
tag := strings.Trim(route, "/")
@ -40,7 +40,7 @@ func TestRouter(t *testing.T) {
}
resp.Body.Close()
}
rows, err := view.RetrieveData(codeRouteMethodCount)
rows, err := view.RetrieveData(ServerResponseCount.Name)
if err != nil {
t.Fatal(err)
}